-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
309 lines (290 loc) · 9.21 KB
/
test.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
process.env.sdl2_global_export = 'true';
const fs = require('fs');
const ref = require('ref-napi');
// Use require('minsdl2js') for your project
const sdl2 = require('./index');
function fatal() {
console.log('Error:', SDL_GetError());
process.exit(1);
}
function log(...data) {
return console.log(...data);
}
function random_bool() {
return Math.floor(Math.random() * 2);
}
function random_color() {
return Math.floor(Math.random() * 255) << 16 |
Math.floor(Math.random() * 255) << 8 |
Math.floor(Math.random() * 255);
}
function get_closest_renderer() {
var renderers = [];
for (var i = 0; i < SDL_GetNumRenderDrivers(); i++) {
const renderer_info = new SDL_RendererInfo;
SDL_GetRenderDriverInfo(i, renderer_info.ref());
renderers.push(renderer_info.name);
}
if ((renderer_index = renderers.indexOf('direct3d12')) >= 0 && !process.argv.includes('--no-d3d12'))
return renderer_index;
if ((renderer_index = renderers.indexOf('direct3d11')) >= 0)
return renderer_index;
if ((renderer_index = renderers.indexOf('direct3d')) >= 0)
return renderer_index;
if ((renderer_index = renderers.indexOf('opengl')) >= 0) {
can_async_flip = false;
return renderer_index;
}
return -1;
}
var prefix = process.platform == 'win32' ? '' : 'lib';
var postfix = '';
if (process.argv.includes('--cygwin')) {
prefix = 'cyg';
postfix = '-2-0-0';
}
sdl2.load_sdl2_library(prefix + 'SDL2' + postfix);
sdl2.load_sdl2_image_library(prefix + 'SDL2_image' + postfix);
sdl2.load_sdl2_ttf_library(prefix + 'SDL2_ttf' + postfix);
// sdl2.load_sdl2_rtf_library(prefix + 'SDL2_rtf' + postfix);
sdl2.load_sdl2_mixer_library(prefix + 'SDL2_mixer' + postfix);
sdl2.load_sdl2_net_library(prefix + 'SDL2_net' + postfix);
sdl2.load_sdl2_gfx_library(prefix + 'SDL2_gfx' + postfix);
// sdl2.load_sdl2_sound_library(prefix + 'SDL2_sound' + postfix);
// sdl2.load_sdl2_gpu_library(prefix + 'SDL2_gpu' + postfix);
sdl2.export_sdl2_library(global);
sdl2.export_sdl2_image_library(global);
sdl2.export_sdl2_ttf_library(global);
// sdl2.export_sdl2_rtf_library(global);
sdl2.export_sdl2_mixer_library(global);
sdl2.export_sdl2_net_library(global);
sdl2.export_sdl2_gfx_library(global);
// sdl2.export_sdl2_sound_library(global);
// sdl2.export_sdl2_gpu_library(global);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_AUDIO))
fatal();
if (!IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG))
fatal();
if (TTF_Init())
fatal();
if (!Mix_Init(MIX_INIT_MP3 | MIX_INIT_OGG | MIX_INIT_MID | MIX_INIT_OPUS))
fatal();
if (SDLNet_Init())
fatal();
// if (!Sound_Init())
// fatal();
// if (!GPU_Init())
// fatal();
log(`CPU: ${SDL_GetCPUCount()} CPUs, RAM: ${SDL_GetSystemRAM()}MB`);
log('Platform:', SDL_GetPlatform());
log('Video Driver:', SDL_GetCurrentVideoDriver());
log('Base Path:', SDL_GetBasePath());
log('Language:', SDL_GetPreferredLocales().deref().language);
if (display = SDL_GetDisplayName(0)) {
var dm = new SDL_DisplayMode();
var ub = new SDL_Rect();
SDL_GetDesktopDisplayMode(0, dm.ref());
SDL_GetDisplayBounds(0, ub.ref());
log('Display:', display);
log(`Current Mode: [${dm.w}x${dm.h}@${dm.refresh_rate}Hz]`);
log(`Usable Bounds: From [${ub.x}x${ub.y}] to [${ub.x + ub.w}x${ub.y + ub.h}]`);
} else
fatal();
// log('Data Path:', SDL_GetPrefPath(null, 'minsdl2'));
var w = 640;
var h = 480;
const window = SDL_CreateWindow(
'Hello, World!',
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
w,
h,
SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE
);
if (icon = SDL_CreateRGBSurface(0, 32, 32, 32, 0, 0, 0, 0)) {
SDL_FillRect(icon, new SDL_Rect({
x: 0,
y: 0,
w: 32,
h: 32
}).ref(), random_color());
SDL_SetWindowIcon(window, icon);
SDL_FreeSurface(icon);
} else fatal();
var can_async_flip = true;
const renderer = SDL_CreateRenderer(
window,
get_closest_renderer(),
(process.argv.includes('--software') ? SDL_RENDERER_SOFTWARE : 0) |
(process.argv.includes('--no-accel') ? 0 : SDL_RENDERER_ACCELERATED) |
(process.argv.includes('--no-vsync') ? 0 : SDL_RENDERER_PRESENTVSYNC)
);
if (renderer == null)
fatal();
SDL_SetAssertionHandler(log, null);
/*const wavSpec = new SDL_AudioSpec;
const wavLength = new Uint32Array(1);
const wavBuffer = ref.ref(new Uint8Array(19574784));
SDL_LoadWAV('e:/music/hatebit - track3.wav', wavSpec.ref(), wavBuffer, wavLength);
const deviceId = SDL_OpenAudioDevice(null, 0, wavSpec.ref(), null, 0);
const success = SDL_QueueAudio(deviceId, wavBuffer.deref(), wavLength);
SDL_PauseAudioDevice(deviceId, 0);*/
const music_path = 'E:/Music/Master Boot Record - XCOPY.EXE.mp3';
var music;
if (fs.existsSync(music_path)) {
Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 320);
music = Mix_LoadMUS(music_path);
Mix_PlayMusic(music, -1);
}
const timer_freq = SDL_GetPerformanceFrequency();
const bg_path = 'E:/other/win7.png';
const font_path = 'C:/Windows/Fonts/segoeuib.ttf';
const bg = fs.existsSync(bg_path) ? IMG_Load(bg_path) : null;
const bg_texture = bg ? SDL_CreateTextureFromSurface(renderer, bg) : null;
if (bg)
SDL_FreeSurface(bg);
const font = fs.existsSync(font_path) ? TTF_OpenFont(font_path, 32) : null;
var text = '';
var cube_rect = new SDL_FRect({
x: 0,
y: 0,
w: 50,
h: 50
});
var mouse_point = new SDL_Point;
var speed_x = 250 * (Math.random() + 0.5);
var speed_y = 250 * (Math.random() + 0.5);
var is_colliding = false;
var delta_array = new Array(50).fill(0);
var last_tick = SDL_GetPerformanceCounter();
log('Allocations:', SDL_GetNumAllocations());
// Do NOT create while(true) {...} - it will leak memory
async function tick() {
var event = new SDL_Event;
while (SDL_PollEvent(event.ref())) {
switch (event.type) {
case SDL_QUIT:
if (bg_texture)
SDL_DestroyTexture(bg_texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
// GPU_Quit();
// Sound_Quit();
SDLNet_Quit();
if (music) {
Mix_FreeMusic(music);
Mix_CloseAudio();
}
Mix_Quit();
if (font)
TTF_CloseFont(font);
TTF_Quit();
IMG_Quit();
SDL_Quit();
log('Allocations:', SDL_GetNumAllocations());
process.exit(0);
return;
case SDL_MOUSEMOTION:
mouse_point.x = event.motion.x;
mouse_point.y = event.motion.y;
// SDL_SetWindowTitle(window, `Mouse Pos: [${event.motion.x}x${event.motion.y}]`);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_BACKSPACE) {
if (!text) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, 'Warning!', 'Already Empty!', window);
continue;
}
text = text.substr(0, text.length - 1);
} else
text += SDL_GetKeyName(event.key.keysym.sym);
SDL_SetWindowTitle(window, `Typing: ${text}`);
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
w = event.window.data1;
h = event.window.data2;
break;
}
break;
}
}
const now = SDL_GetPerformanceCounter();
const delta = (now - last_tick) / timer_freq;
last_tick = now;
cube_rect.x += speed_x * delta;
cube_rect.y += speed_y * delta;
if (cube_rect.x + cube_rect.w >= w)
speed_x = Math.min(speed_x, -speed_x);
else if (cube_rect.x <= 0)
speed_x = Math.max(speed_x, -speed_x);
if (cube_rect.y + cube_rect.h >= h)
speed_y = Math.min(speed_y, -speed_y);
else if (cube_rect.y <= 0)
speed_y = Math.max(speed_y, -speed_y);
if (SDL_PointInFRect(mouse_point, cube_rect)) {
if (!is_colliding) {
is_colliding = true;
if (random_bool()) {
speed_x *= -1;
if (random_bool()) speed_y *= -1;
} else {
speed_y *= -1;
if (random_bool()) speed_x *= -1;
}
speed_x = (speed_x > 0 ? 1 : -1) * 250 * (Math.random() + 0.5);
speed_y = (speed_y > 0 ? 1 : -1) * 250 * (Math.random() + 0.5);
}
} else if (is_colliding)
is_colliding = false;
if (bg_texture) {
SDL_RenderCopy(renderer, bg_texture, null, null);
} else {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
}
filledCircleRGBA(renderer, w >> 1, h >> 1, (w + h) >> 3, 255, 255, 255, 50);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRectF(renderer, cube_rect.ref());
const fps = 1 / delta;
delta_array.shift();
delta_array.push(delta);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
for (var i = 0; i < delta_array.length; i++) {
SDL_RenderDrawLine(renderer, i, 125 - delta_array[i] * 125, i + 1, 125);
}
if (font) {
const text_surface = TTF_RenderText_Blended(
font,
'FPS: ' + Math.round(fps).toString(),
new SDL_Color({
r: 0,
g: 255,
b: 255
})
);
const text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
SDL_RenderCopy(
renderer,
text_texture,
null,
new SDL_Rect({
x: 0,
y: 0,
w: text_surface.deref().w,
h: text_surface.deref().h
}).ref()
);
SDL_DestroyTexture(text_texture);
SDL_FreeSurface(text_surface);
}
// TODO: Does it work?
if (can_async_flip) {
SDL_RenderPresent.async(renderer, tick);
} else {
SDL_RenderPresent(renderer);
setImmediate(tick);
}
}
tick();