-
Notifications
You must be signed in to change notification settings - Fork 0
/
SUGE_v2_2_2.c
147 lines (144 loc) · 4.35 KB
/
SUGE_v2_2_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Starry Universe Gravity Engine v2.2.0
* Update record on 2021-04-23:
* - Multiply simulate_acc before running to optimize performance
*
* Todo:
* - Use SSE four by four to quadruple performance
* - Rename the variables
*
* Table: performance test
* time(s) (1) (2) (3)
* v2.1.1 35 28 31
* v2.1.2 24 26 33
* v2.1.3 15 21 24
* v2.1.4 21 21 20
* v2.2.0 9 7 7
* v2.2.1 6 6 7
*
* Open file SUGE.cfg2 as configure. Structure:
* - num: Numbers of stars
* - X position(a list with num elements)
* - Y position(a list with num elements)
* - X velocity(a list with num elements)
* - Y velocity(a list with num elements)
* - Gravity(a list with num elements)
* - Foreground color of the line(a list with num elements, given as BBGGRR in hexdecimal)
* - Background color
* - Simulate accuracy
* - Display frequent(steps per display cicle)
* - Width of the line(per pixel)
* - X/Y offset(per pixel)
*/
#include <stdio.h>
#include <math.h>
#include <windows.h>
#ifdef USE_SSE
#include <xmmintrin.h>
__m128 m, n;
#endif
int num, display_freq, width, offset[2], count, i, j;
COLORREF background_color;
double simulate_acc, distance, p, q, r, s;
DWORD WINAPI threadProc(LPVOID lpParamter) {
FILE *file = fopen("SUGE.cfg2", "r");
HWND hwnd = (HWND)lpParamter;
fscanf(file, "%d", &num);
HDC hdc[num];
HPEN hpen[num];
double position[num][2], velocity[num][2], gravity[num];
COLORREF color[num];
for(i=0; i<num; ++i) fscanf(file, "%lf", &position[i][0]);
for(i=0; i<num; ++i) fscanf(file, "%lf", &position[i][1]);
for(i=0; i<num; ++i) fscanf(file, "%lf", &velocity[i][0]);
for(i=0; i<num; ++i) fscanf(file, "%lf", &velocity[i][1]);
for(i=0; i<num; ++i) fscanf(file, "%lf", &gravity[i]);
for(i=0; i<num; ++i) fscanf(file, "%lx", &color[i]);
fscanf(file, "%lx %lf %d %d %d %d", &background_color, &simulate_acc, &display_freq, &width, &offset[0], &offset[1]);
fclose(file);
for(i=0; i<num; ++i) {
gravity[i] *= simulate_acc * simulate_acc;
velocity[i][0] *= simulate_acc;
velocity[i][1] *= simulate_acc;
}
for(i=0; i<num; ++i) {
hdc[i] = GetDC(hwnd);
hpen[i] = CreatePen(PS_SOLID, width, color[i]);
SelectObject(hdc[i], hpen[i]);
position[i][0] += offset[0];
position[i][1] += offset[1];
MoveToEx(hdc[i], position[i][0], position[i][1], 0);
}
while(1) {
for(i=0; i<num; ++i) {
for(j=0; j<i; ++j) {
p = position[j][0] - position[i][0];
q = position[j][1] - position[i][1];
#ifdef USE_SSE
m = _mm_set1_ps(p * p + q * q);
n = _mm_rsqrt_ps(m);
distance = n[0] * n[0] * n[0];
#else
distance = pow(p * p + q * q, -1.5);
#endif
r = gravity[j] * distance;
s = gravity[i] * distance;
velocity[i][0] += p * r;
velocity[i][1] += q * r;
velocity[j][0] -= p * s;
velocity[j][1] -= q * s;
}
}
if(count % display_freq == 0) {
for(i=0; i<num; ++i) {
LineTo(hdc[i], position[i][0], position[i][1]);
}
}
for(i=0; i<num; ++i) {
position[i][0] += velocity[i][0];
position[i][1] += velocity[i][1];
}
++count;
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR pCmdLine, int nCmdShow) {
const char *CLASS_NAME = "SUGEWND";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(0, CLASS_NAME, "SUGE",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if(hwnd == NULL) return 0;
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
MSG msg={};
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch(uMsg) {
case WM_DESTROY: {
PostQuitMessage(0);
return 0;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc2 = BeginPaint(hwnd, &ps);
FillRect(hdc2, &ps.rcPaint, CreateSolidBrush(background_color));
EndPaint(hwnd, &ps);
return 0;
}
case WM_CREATE: {
HANDLE hThread = CreateThread(NULL, 0, threadProc, (LPVOID)hwnd, 0, NULL);
CloseHandle(hThread);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}