-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdlmain.cpp
463 lines (434 loc) · 15.2 KB
/
sdlmain.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
* VGS-Zero SDK for Steam/SDL with GPU mode
* License under GPLv3: https://github.com/suzukiplan/vgszero/blob/master/LICENSE-VGS0.txt
* (C)2024, SUZUKI PLAN
*/
#include "SDL.h"
#include "gamepkg.h"
#include "../vgszero/src/core/vgs0.hpp"
#include "steam.hpp"
#include "sdlconf.hpp"
#include <chrono>
#include <map>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <vector>
#include <sys/stat.h>
#ifdef DARWIN
#define WINDOW_TITLE "Battle Marine for macOS"
#else
#define WINDOW_TITLE "Battle Marine for Linux"
#endif
extern "C" {
extern const unsigned int img_err_joypad[17664];
};
static pthread_mutex_t soundMutex = PTHREAD_MUTEX_INITIALIZER;
static bool halt = false;
static CSteam* steam = nullptr;
void log(const char* format, ...)
{
FILE* fp = fopen("log.txt", "a");
if (!fp) return;
char buf[256];
auto now = time(nullptr);
auto t = localtime(&now);
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
fprintf(fp, "%04d.%02d.%02d %02d:%02d:%02d %s\n",
t->tm_year + 1900,
t->tm_mon + 1,
t->tm_mday,
t->tm_hour,
t->tm_min,
t->tm_sec,
buf);
fclose(fp);
}
static void audioCallback(void* userdata, Uint8* stream, int len)
{
VGS0* vgs0 = (VGS0*)userdata;
pthread_mutex_lock(&soundMutex);
if (halt) {
pthread_mutex_unlock(&soundMutex);
return;
}
void* buf = vgs0->tickSound(len);
memcpy(stream, buf, len);
pthread_mutex_unlock(&soundMutex);
}
static inline unsigned char bit5To8(unsigned char bit5)
{
bit5 <<= 3;
bit5 |= (bit5 & 0b11100000) >> 5;
return bit5;
}
int main(int argc, char* argv[])
{
unlink("log.txt");
bool cliError = false;
int gpuType = SDL_WINDOW_OPENGL;
for (int i = 1; !cliError && i < argc; i++) {
switch (tolower(argv[i][1])) {
case 'g':
i++;
if (argc <= i) {
cliError = true;
break;
}
if (0 == strcasecmp(argv[i], "OpenGL")) {
gpuType = SDL_WINDOW_OPENGL;
} else if (0 == strcasecmp(argv[i], "Vulkan")) {
gpuType = SDL_WINDOW_VULKAN;
#ifdef DARWIN
} else if (0 == strcasecmp(argv[i], "Metal")) {
gpuType = SDL_WINDOW_METAL;
#endif
} else if (0 == strcasecmp(argv[i], "None")) {
gpuType = 0;
}
break;
case 'h':
cliError = true;
break;
default:
cliError = true;
break;
}
}
#ifdef DARWIN
gpuType |= SDL_WINDOW_ALLOW_HIGHDPI;
#endif
if (cliError) {
puts("usage: bmarine [-g { None .............. GPU: Do not use");
puts(" | OpenGL ............ GPU: OpenGL <default>");
puts(" | Vulkan ............ GPU: Vulkan");
#ifdef DARWIN
puts(" | Metal ............. GPU: Metal");
#endif
puts(" }]");
return 1;
}
log("Booting %s", WINDOW_TITLE);
SDL_version sdlVersion;
SDL_GetVersion(&sdlVersion);
log("SDL version: %d.%d.%d", sdlVersion.major, sdlVersion.minor, sdlVersion.patch);
Config cfg;
steam = new CSteam(log);
steam->init();
log("Initializing SDL");
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS)) {
log("SDL_Init failed: %s", SDL_GetError());
exit(-1);
}
// create window & renderer
SDL_DisplayMode display;
SDL_GetCurrentDisplayMode(0, &display);
log("Screen Resolution: width=%d, height=%d", display.w, display.h);
SDL_Window* window;
SDL_Renderer* renderer;
if (cfg.graphic.isFullScreen) {
gpuType |= SDL_WINDOW_FULLSCREEN;
}
if (0 !=SDL_CreateWindowAndRenderer(cfg.graphic.isFullScreen ? display.w : cfg.graphic.windowWidth,
cfg.graphic.isFullScreen ? display.h : cfg.graphic.windowHeight,
gpuType,
&window,
&renderer)) {
log("SDL_CreateWindowAndRenderer failed: %s", SDL_GetError());
exit(-1);
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_ShowCursor(SDL_DISABLE);
SDL_RenderPresent(renderer);
double sw = (cfg.graphic.isFullScreen ? display.w : cfg.graphic.windowWidth) / 480.0;
double sh = (cfg.graphic.isFullScreen ? display.h : cfg.graphic.windowHeight) / 384.0;
double scale = sw < sh ? sw : sh;
int frameWidth = (int)((cfg.graphic.isFullScreen ? display.w : cfg.graphic.windowWidth) / scale);
int frameHeight = (int)((cfg.graphic.isFullScreen ? display.h : cfg.graphic.windowHeight) / scale);
int framePitch = frameWidth * 4;
int offsetX = (frameWidth - 480) / 2;
int offsetY = (frameHeight - 384) / 2;
int maskTR = cfg.graphic.isScanline ? 0xF0F0F0F0 : 0xFFFFFFFF;
int maskBL = cfg.graphic.isScanline ? 0x8F8F8F8F : 0xFFFFFFFF;
int maskBR = cfg.graphic.isScanline ? 0x80808080 : 0xFFFFFFFF;
log("Texture: width=%d, height=%d, offsetX=%d, offsetY=%d", frameWidth, frameHeight, offsetX, offsetY);
SDL_RenderSetLogicalSize(renderer, frameWidth, frameHeight);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
auto texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, frameWidth, frameHeight);
if (!texture) {
log("SDL_CreateTexture failed: %s", SDL_GetError());
exit(-1);
}
auto frameBuffer = (unsigned int*)malloc(framePitch * frameHeight);
if (!frameBuffer) {
log("No memory");
exit(-1);
}
memset(frameBuffer, 0, framePitch * frameHeight);
log("Initializing VGS-Zero");
int romSize;
const void* rom = nullptr;
int bgmSize;
const void* bgm = nullptr;
int seSize;
const void* se = nullptr;
const unsigned char* ptr = gamepkg;
if (0 != memcmp(ptr, "VGS0PKG", 8)) {
puts("Invalid package!");
return -1;
}
ptr += 8;
memcpy(&romSize, ptr, 4);
ptr += 4;
rom = ptr;
ptr += romSize;
printf("- game.rom size: %d\n", romSize);
if (romSize < 8 + 8192) {
puts("Invalid game.rom size");
exit(-1);
}
memcpy(&bgmSize, ptr, 4);
ptr += 4;
bgm = 0 < bgmSize ? ptr : nullptr;
ptr += bgmSize;
printf("- bgm.dat size: %d\n", bgmSize);
memcpy(&seSize, ptr, 4);
ptr += 4;
se = 0 < seSize ? ptr : nullptr;
printf("- se.dat size: %d\n", seSize);
VGS0 vgs0;
if (0 < bgmSize) {
vgs0.loadBgm(bgm, bgmSize);
}
if (0 < seSize) {
vgs0.loadSoundEffect(se, seSize);
}
vgs0.loadRom(rom, romSize);
vgs0.setBgmVolume(cfg.sound.volumeBgm);
vgs0.setSeVolume(cfg.sound.volumeSe);
mkdir("save", S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH | S_IXOTH);
vgs0.saveCallback = [](VGS0* vgs0, const void* data, size_t size) -> bool {
log("Saving save.dat (%lubytes)", size);
FILE* fp = fopen("save/save.dat", "wb");
if (!fp) {
log("File open error!");
return false;
}
if (size != fwrite(data, 1, size, fp)) {
log("File write error!");
fclose(fp);
return false;
}
fclose(fp);
return true;
};
vgs0.loadCallback = [](VGS0* vgs0, void* data, size_t size) -> bool {
log("Loading save.dat (%lubytes)", size);
FILE* fp = fopen("save/save.dat", "rb");
if (!fp) {
log("File open error!");
return false;
}
size_t readSize = fread(data, 1, size, fp);
if (readSize < size) {
log("warning: file size is smaller than expected");
memset(&((char*)data)[readSize], 0, size - readSize);
}
fclose(fp);
return true;
};
log("Initializing AudioDriver");
SDL_AudioSpec desired;
SDL_AudioSpec obtained;
desired.freq = 44100;
desired.format = AUDIO_S16LSB;
desired.channels = 1;
desired.samples = 735; // desired.freq * 20 / 1000;
desired.callback = audioCallback;
desired.userdata = &vgs0;
auto audioDeviceId = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, 0);
if (0 == audioDeviceId) {
log(" ... SDL_OpenAudioDevice failed: %s", SDL_GetError());
exit(-1);
}
log("- obtained.freq = %d", obtained.freq);
log("- obtained.format = %X", obtained.format);
log("- obtained.channels = %d", obtained.channels);
log("- obtained.samples = %d", obtained.samples);
SDL_PauseAudioDevice(audioDeviceId, 0);
log("Start main loop...");
SDL_Event event;
unsigned int loopCount = 0;
const int waitFps60[3] = {17000, 17000, 16000};
unsigned char msxKeyCodeMap[16];
memset(msxKeyCodeMap, 0, sizeof(msxKeyCodeMap));
bool joypadConnected = false;
bool joypadConnectedPrev = false;
bool detectJoypadDisconnected = false;
unsigned char key1 = 0;
while (!halt) {
auto start = std::chrono::system_clock::now();
loopCount++;
if (loopCount % 6 == 0) {
SteamAPI_RunCallbacks();
}
// Keyboard Input (SDL2)
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
halt = true;
} else if (event.type == SDL_KEYDOWN) {
if (cfg.keyboard.quit == event.key.keysym.sym) {
halt = true;
}
if (cfg.keyboard.up == event.key.keysym.sym) {
key1 |= VGS0_JOYPAD_UP;
}
if (cfg.keyboard.down == event.key.keysym.sym) {
key1 |= VGS0_JOYPAD_DW;
}
if (cfg.keyboard.left == event.key.keysym.sym) {
key1 |= VGS0_JOYPAD_LE;
}
if (cfg.keyboard.right == event.key.keysym.sym) {
key1 |= VGS0_JOYPAD_RI;
}
if (cfg.keyboard.a == event.key.keysym.sym) {
key1 |= VGS0_JOYPAD_T1;
}
if (cfg.keyboard.b == event.key.keysym.sym) {
key1 |= VGS0_JOYPAD_T2;
}
if (cfg.keyboard.start == event.key.keysym.sym) {
key1 |= VGS0_JOYPAD_ST;
}
if (cfg.keyboard.start == event.key.keysym.sym) {
key1 |= VGS0_JOYPAD_SE;
}
if (cfg.keyboard.reset == event.key.keysym.sym) {
log("Reset");
vgs0.reset();
}
} else if (event.type == SDL_KEYUP) {
if (cfg.keyboard.up == event.key.keysym.sym) {
key1 ^= VGS0_JOYPAD_UP;
}
if (cfg.keyboard.down == event.key.keysym.sym) {
key1 ^= VGS0_JOYPAD_DW;
}
if (cfg.keyboard.left == event.key.keysym.sym) {
key1 ^= VGS0_JOYPAD_LE;
}
if (cfg.keyboard.right == event.key.keysym.sym) {
key1 ^= VGS0_JOYPAD_RI;
}
if (cfg.keyboard.a == event.key.keysym.sym) {
key1 ^= VGS0_JOYPAD_T1;
}
if (cfg.keyboard.b == event.key.keysym.sym) {
key1 ^= VGS0_JOYPAD_T2;
}
if (cfg.keyboard.start == event.key.keysym.sym) {
key1 ^= VGS0_JOYPAD_ST;
}
if (cfg.keyboard.start == event.key.keysym.sym) {
key1 ^= VGS0_JOYPAD_SE;
}
}
}
if (halt) {
break;
}
// SteamInput
auto pad1 = steam->getJoypad(&joypadConnected);
if (joypadConnected) {
if (!joypadConnectedPrev) {
log("Joypad Connected!");
}
detectJoypadDisconnected = false;
} else if (joypadConnectedPrev) {
log("Joypad Disconnected! (waiting for resume...)");
detectJoypadDisconnected = true;
auto fb = frameBuffer;
int ox = (frameWidth - 368) / 2;
fb += (frameHeight - 48) / 2 * frameWidth;
int ptr = 0;
for (int y = 0; y < 48; y++, fb += frameWidth) {
for (int x = 0; x < 368; x++) {
fb[ox + x] = img_err_joypad[ptr++];
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xff);
SDL_UpdateTexture(texture, nullptr, frameBuffer, framePitch);
SDL_SetRenderTarget(renderer, nullptr);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
usleep(20000);
continue;
} else if (detectJoypadDisconnected) {
usleep(20000);
continue;
}
joypadConnectedPrev = joypadConnected;
// execute emulator 1 frame
if (!steam->isOverlay()) {
pthread_mutex_lock(&soundMutex);
vgs0.tick(key1 | pad1);
pthread_mutex_unlock(&soundMutex);
if (vgs0.cpu->reg.IFF & 0x80) {
if (0 == (vgs0.cpu->reg.IFF & 0x01)) {
log("Detected the HALT while DI");
break;
}
}
}
// render graphics
auto vgsDisplay = vgs0.getDisplay();
auto pcDisplay = (unsigned int*)frameBuffer;
pcDisplay += offsetY * frameWidth;
for (int y = 0; y < 192; y++) {
for (int x = 0; x < 240; x++) {
unsigned int rgb555 = vgsDisplay[x];
unsigned int rgb888 = 0;
rgb888 |= bit5To8((rgb555 & 0b0111110000000000) >> 10);
rgb888 <<= 8;
rgb888 |= bit5To8((rgb555 & 0b0000001111100000) >> 5);
rgb888 <<= 8;
rgb888 |= bit5To8(rgb555 & 0b0000000000011111);
rgb888 <<= 8;
auto offset = offsetX + x * 2;
pcDisplay[offset] = rgb888;
pcDisplay[offset + 1] = rgb888 & maskTR;
pcDisplay[offset + frameWidth] = rgb888 & maskBL;
pcDisplay[offset + frameWidth + 1] = rgb888 & maskBR;
}
vgsDisplay += 240;
pcDisplay += frameWidth * 2;
}
// render display
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xff);
SDL_UpdateTexture(texture, nullptr, frameBuffer, framePitch);
SDL_SetRenderTarget(renderer, nullptr);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
// sync 60fps
std::chrono::duration<double> diff = std::chrono::system_clock::now() - start;
int us = (int)(diff.count() * 1000000);
int wait = waitFps60[loopCount % 3];
if (us < wait) {
usleep(wait - us);
}
}
cfg.save();
log("Terminating");
delete steam;
SDL_Quit();
free(frameBuffer);
return 0;
}