main.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * meg4/platform/sdl/main.c
  3. *
  4. * Copyright (C) 2023 bzt
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * @brief SDL "platform" for the MEG-4
  21. *
  22. */
  23. #include "meg4.h"
  24. #define SDL_ENABLE_OLD_NAMES
  25. #include <SDL.h>
  26. #ifdef __EMSCRIPTEN__
  27. #include <emscripten.h>
  28. #endif
  29. #include "../../src/stb_image.h" /* for stbi_zlib_decompress */
  30. #include "data.h"
  31. #if SDL_VERSION_ATLEAST(3,0,0)
  32. #include <SDL_main.h>
  33. #define SDL_ENABLE 1
  34. #define SDL_DISABLE 0
  35. #define SDL_WINDOW_FULLSCREEN_DESKTOP 1
  36. #define cdevice jdevice
  37. #define caxis jaxis
  38. #define cbutton button
  39. #define meg4_showcursor() SDL_ShowCursor()
  40. #define meg4_hidecursor() SDL_HideCursor()
  41. SDL_Gamepad *controller[4] = { 0 };
  42. #else
  43. #define meg4_showcursor() SDL_ShowCursor(SDL_ENABLE)
  44. #define meg4_hidecursor() SDL_ShowCursor(SDL_DISABLE)
  45. SDL_GameController *controller[4] = { 0 };
  46. #endif
  47. SDL_Window *window = NULL;
  48. SDL_Renderer *renderer = NULL;
  49. SDL_Texture *screen = NULL;
  50. SDL_Event event;
  51. SDL_AudioSpec have;
  52. int controllerid[4] = { -1, -1, -1, -1 };
  53. int main_draw = 1, main_ret, main_w = 0, main_h = 0, win_w, win_h, win_f = 0, audio = 0, main_alt = 0;
  54. int main_keymap[SDL_NUM_SCANCODES];
  55. void main_delay(int msec);
  56. #include "../common.h"
  57. /**
  58. * Exit emulator
  59. */
  60. void main_quit(void)
  61. {
  62. main_log(1, "quitting... ");
  63. meg4_poweroff();
  64. meg4_showcursor();
  65. if(screen) { SDL_DestroyTexture(screen); screen = NULL; }
  66. /* this crashes sometimes... but only sometimes... We'll exit so should be freed anyway */
  67. /* if(renderer) { SDL_DestroyRenderer(renderer); renderer = NULL; }*/
  68. if(window) {
  69. SDL_DestroyWindow(window);
  70. #ifndef __EMSCRIPTEN__
  71. /* restore original screen resolution */
  72. if(win_f && (main_w != win_w || main_h != win_h)) {
  73. #if SDL_VERSION_ATLEAST(3,0,0)
  74. window = SDL_CreateWindow("MEG-4", main_w, main_h, 1);
  75. #else
  76. window = SDL_CreateWindow("MEG-4", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, main_w, main_h,
  77. SDL_WINDOW_FULLSCREEN);
  78. #endif
  79. if(window) SDL_DestroyWindow(window);
  80. }
  81. #endif
  82. window = NULL;
  83. }
  84. if(audio) {
  85. #if SDL_VERSION_ATLEAST(3,0,0)
  86. SDL_PauseAudioDevice(audio);
  87. #else
  88. SDL_PauseAudioDevice(audio, 1);
  89. #endif
  90. SDL_CloseAudioDevice(audio); audio = 0; }
  91. SDL_Quit();
  92. #ifdef __EMSCRIPTEN__
  93. /* don't let emscripten fool you, this won't cancel the loop. it will quit... but neither of these work with asyncify! */
  94. emscripten_cancel_main_loop();
  95. /*emscripten_force_exit(0);*/
  96. #else
  97. /* DO NOT call exit(), that crashes android... */
  98. /* exit(0); */
  99. #endif
  100. }
  101. /**
  102. * Create window
  103. */
  104. void main_win(int w, int h, int f)
  105. {
  106. int p;
  107. void *data;
  108. char *title =
  109. #ifndef NOEDITORS
  110. "MEG-4";
  111. #else
  112. (char*)binary_game;
  113. #endif
  114. SDL_Surface *srf;
  115. if(screen) { SDL_DestroyTexture(screen); screen = NULL; }
  116. if(renderer) { SDL_DestroyRenderer(renderer); renderer = NULL; }
  117. if(window) { SDL_DestroyWindow(window); window = NULL; }
  118. if(!f) { win_w = w; win_h = h; }
  119. win_f = f;
  120. #if SDL_VERSION_ATLEAST(3,0,0)
  121. window = SDL_CreateWindow(title, f ? main_w : w, f ? main_h : h, f);
  122. #else
  123. window = SDL_CreateWindow(title,
  124. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  125. f ? main_w : w, f ? main_h : h,
  126. f ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
  127. #endif
  128. if(!window) return;
  129. #if SDL_VERSION_ATLEAST(3,0,0)
  130. renderer = SDL_CreateRenderer(window, NULL, SDL_RENDERER_ACCELERATED);
  131. if(!renderer) {
  132. renderer = SDL_CreateRenderer(window, NULL, SDL_RENDERER_SOFTWARE);
  133. if(!renderer) return;
  134. }
  135. #else
  136. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  137. if(!renderer) {
  138. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
  139. if(!renderer) return;
  140. }
  141. #endif
  142. if(meg4_icons.buf) {
  143. #if SDL_VERSION_ATLEAST(3,0,0)
  144. srf = SDL_CreateSurfaceFrom((Uint32 *)meg4_icons.buf, meg4_icons.w, meg4_icons.h, meg4_icons.w * 4,
  145. SDL_PIXELFORMAT_RGBA8888);
  146. #else
  147. srf = SDL_CreateRGBSurfaceFrom((Uint32 *)meg4_icons.buf, meg4_icons.w, 64, 32, meg4_icons.w * 4,
  148. 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
  149. #endif
  150. if(srf) {
  151. SDL_SetWindowIcon(window, srf);
  152. #if SDL_VERSION_ATLEAST(3,0,0)
  153. SDL_DestroySurface(srf);
  154. #else
  155. SDL_FreeSurface(srf);
  156. #endif
  157. }
  158. }
  159. screen = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 640, 400);
  160. if(screen) {
  161. SDL_LockTexture(screen, NULL, &data, &p);
  162. memset(data, 0, p * 400);
  163. SDL_UnlockTexture(screen);
  164. }
  165. }
  166. /**
  167. * Toggle fullscreen
  168. */
  169. void main_fullscreen(void)
  170. {
  171. win_f ^= 1;
  172. SDL_SetWindowFullscreen(window, win_f ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
  173. }
  174. /**
  175. * Make window focused
  176. */
  177. void main_focus(void)
  178. {
  179. SDL_RaiseWindow(window);
  180. if(win_f) SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
  181. }
  182. /**
  183. * Calculate pointer coordinates
  184. */
  185. void main_pointer(SDL_Rect *dst, int x, int y)
  186. {
  187. meg4_setptr(x < dst->x || !dst->w ? 0 : (x >= dst->x + dst->w ? meg4.screen.w : (x - dst->x) * meg4.screen.w / dst->w),
  188. y < dst->y || !dst->h ? 0 : (y >= dst->y + dst->h ? meg4.screen.h : (y - dst->y) * meg4.screen.h / dst->h));
  189. }
  190. /**
  191. * Main SDL emulator loop
  192. */
  193. /* emscripten does not allow return value... */
  194. void main_loop(void) {
  195. #ifdef __EMSCRIPTEN__
  196. #define exit_loop() main_quit()
  197. #else
  198. #define exit_loop() do{ main_ret = 0; return; }while(0)
  199. #endif
  200. int i, p;
  201. void *data;
  202. #ifndef NOEDITORS
  203. char *fn;
  204. #endif
  205. #if SDL_VERSION_ATLEAST(3,0,0)
  206. SDL_FRect src, dst;
  207. SDL_Rect idst;
  208. float mx, my;
  209. #else
  210. SDL_Rect src, dst;
  211. #endif
  212. meg4_run();
  213. data = NULL; p = 0;
  214. if(main_draw) {
  215. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  216. SDL_RenderClear(renderer);
  217. if(screen) SDL_LockTexture(screen, NULL, &data, &p);
  218. }
  219. meg4_redraw((uint32_t*)data, 640, 400, p);
  220. if(data) SDL_UnlockTexture(screen);
  221. src.x = src.y = 0; src.w = meg4.screen.w; src.h = meg4.screen.h;
  222. if(src.w && src.h) {
  223. if(!win_f && nearest) {
  224. #if SDL_VERSION_ATLEAST(2,0,12)
  225. SDL_SetTextureScaleMode(screen,
  226. #if SDL_VERSION_ATLEAST(3,0,0)
  227. SDL_SCALEMODE_NEAREST
  228. #else
  229. SDL_ScaleModeNearest
  230. #endif
  231. );
  232. #endif
  233. i = win_w / 320; p = win_h / 200; if(i > p) i = p;
  234. dst.w = 320 * i; dst.h = 200 * i;
  235. } else {
  236. #if SDL_VERSION_ATLEAST(2,0,12)
  237. SDL_SetTextureScaleMode(screen, nearest || (!((win_f ? main_w : win_w) % 320) && !((win_f ? main_h : win_h) % 200)) ?
  238. #if SDL_VERSION_ATLEAST(3,0,0)
  239. SDL_SCALEMODE_NEAREST : SDL_SCALEMODE_LINEAR
  240. #else
  241. SDL_ScaleModeNearest : SDL_ScaleModeLinear
  242. #endif
  243. );
  244. #endif
  245. dst.w = win_w; dst.h = src.h * win_w / src.w;
  246. if(dst.h > win_h) { dst.h = win_h; dst.w = src.w * win_h / src.h; }
  247. }
  248. } else dst.w = dst.h = 0;
  249. dst.x = (win_w - dst.w) / 2; dst.y = (win_h - dst.h) / 2;
  250. if(main_draw) {
  251. if(screen) SDL_RenderCopy(renderer, screen, &src, &dst);
  252. SDL_RenderPresent(renderer);
  253. }
  254. #if SDL_VERSION_ATLEAST(3,0,0)
  255. SDL_GetMouseState(&mx, &my);
  256. idst.x = dst.x; idst.y = dst.y; idst.w = dst.w; idst.h = dst.h;
  257. main_pointer(&idst, (int)mx, (int)my);
  258. #else
  259. SDL_GetMouseState(&i, &p);
  260. main_pointer(&dst, i, p);
  261. #endif
  262. event.type = 0; main_ret = 1;
  263. while(SDL_PollEvent(&event)) {
  264. switch(event.type) {
  265. #if SDL_VERSION_ATLEAST(3,0,0)
  266. case SDL_EVENT_QUIT: exit_loop(); break;
  267. case SDL_EVENT_WINDOW_RESIZED: {
  268. #else
  269. case SDL_QUIT: exit_loop(); break;
  270. case SDL_WINDOWEVENT:
  271. switch(event.window.event) {
  272. case SDL_WINDOWEVENT_CLOSE: exit_loop(); break;
  273. case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_SIZE_CHANGED:
  274. #endif
  275. win_w = event.window.data1; win_h = event.window.data2;
  276. break;
  277. }
  278. break;
  279. case SDL_MOUSEBUTTONDOWN:
  280. switch(event.button.button) {
  281. case SDL_BUTTON_LEFT: meg4_setbtn(MEG4_BTN_L); break;
  282. case SDL_BUTTON_MIDDLE: meg4_setbtn(MEG4_BTN_M); break;
  283. case SDL_BUTTON_RIGHT: meg4_setbtn(MEG4_BTN_R); break;
  284. }
  285. break;
  286. case SDL_MOUSEBUTTONUP:
  287. switch(event.button.button) {
  288. case SDL_BUTTON_LEFT: meg4_clrbtn(MEG4_BTN_L); break;
  289. case SDL_BUTTON_MIDDLE: meg4_clrbtn(MEG4_BTN_M); break;
  290. case SDL_BUTTON_RIGHT: meg4_clrbtn(MEG4_BTN_R); break;
  291. }
  292. break;
  293. case SDL_MOUSEWHEEL:
  294. meg4_setscr(event.wheel.y > 0, event.wheel.y < 0, event.wheel.x < 0, event.wheel.x > 0);
  295. break;
  296. case SDL_KEYDOWN:
  297. switch(event.key.keysym.sym) {
  298. case SDLK_LALT: case SDLK_LCTRL: main_alt = 1; break;
  299. case SDLK_RALT: main_alt = 0; break;
  300. case SDLK_RETURN:
  301. if(main_alt) {
  302. main_fullscreen();
  303. return;
  304. }
  305. meg4_pushkey("\n\0\0");
  306. break;
  307. case SDLK_q: if(main_alt) { exit_loop(); } break;
  308. case SDLK_ESCAPE:
  309. #ifndef NOEDITORS
  310. if(main_alt) { exit_loop(); } else meg4_pushkey("\x1b\0\0");
  311. #else
  312. exit_loop();
  313. #endif
  314. break;
  315. /* only for special keys that aren't handled by SDL_TEXTINPUT events */
  316. case SDLK_F1: meg4_pushkey("F1\0"); break;
  317. case SDLK_F2: meg4_pushkey("F2\0"); break;
  318. case SDLK_F3: meg4_pushkey("F3\0"); break;
  319. case SDLK_F4: meg4_pushkey("F4\0"); break;
  320. case SDLK_F5: meg4_pushkey("F5\0"); break;
  321. case SDLK_F6: meg4_pushkey("F6\0"); break;
  322. case SDLK_F7: meg4_pushkey("F7\0"); break;
  323. case SDLK_F8: meg4_pushkey("F8\0"); break;
  324. case SDLK_F9: meg4_pushkey("F9\0"); break;
  325. case SDLK_F10: meg4_pushkey("F10"); break;
  326. case SDLK_F11: main_fullscreen(); break;
  327. case SDLK_F12: meg4_pushkey("F12"); break;
  328. case SDLK_PRINTSCREEN: meg4_pushkey("PSc"); break;
  329. case SDLK_SCROLLLOCK: meg4_pushkey("SLk"); break;
  330. case SDLK_NUMLOCKCLEAR: meg4_pushkey("NLk"); break;
  331. case SDLK_BACKSPACE: meg4_pushkey("\b\0\0"); break;
  332. case SDLK_TAB: meg4_pushkey("\t\0\0"); break;
  333. case SDLK_CAPSLOCK: meg4_pushkey("CLk"); break;
  334. case SDLK_UP: meg4_pushkey("Up\0"); break;
  335. case SDLK_DOWN: meg4_pushkey("Down"); break;
  336. case SDLK_LEFT: meg4_pushkey("Left"); break;
  337. case SDLK_RIGHT: meg4_pushkey("Rght"); break;
  338. case SDLK_HOME: meg4_pushkey("Home"); break;
  339. case SDLK_END: meg4_pushkey("End"); break;
  340. case SDLK_PAGEUP: meg4_pushkey("PgUp"); break;
  341. case SDLK_PAGEDOWN: meg4_pushkey("PgDn"); break;
  342. case SDLK_INSERT: meg4_pushkey("Ins"); break;
  343. case SDLK_DELETE: meg4_pushkey("Del"); break;
  344. }
  345. if(event.key.keysym.scancode < SDL_NUM_SCANCODES) meg4_setkey(main_keymap[event.key.keysym.scancode]);
  346. break;
  347. case SDL_KEYUP:
  348. switch(event.key.keysym.sym) {
  349. case SDLK_LALT: case SDLK_LCTRL: main_alt = 0; break;
  350. }
  351. if(event.key.keysym.scancode < SDL_NUM_SCANCODES) meg4_clrkey(main_keymap[event.key.keysym.scancode]);
  352. break;
  353. case SDL_TEXTINPUT:
  354. if(!main_alt && (uint8_t)event.text.text[0] >= 32)
  355. meg4_pushkey((char*)&event.text.text);
  356. break;
  357. case SDL_CONTROLLERDEVICEADDED:
  358. for(i = 0; i < 4 && controllerid[i] != (int)event.cdevice.which; i++);
  359. if(i >= 4) for(i = 0; i < 4 && controllerid[i] != -1; i++);
  360. if(i < 4) {
  361. if(controller[i]) SDL_GameControllerClose(controller[i]);
  362. controller[i] = SDL_GameControllerOpen(event.cdevice.which);
  363. controllerid[i] = event.cdevice.which;
  364. }
  365. break;
  366. case SDL_CONTROLLERDEVICEREMOVED:
  367. for(i = 0; i < 4 && controllerid[i] != (int)event.cdevice.which; i++);
  368. if(i < 4) {
  369. if(controller[i]) SDL_GameControllerClose(controller[i]);
  370. controller[i] = NULL;
  371. controllerid[i] = -1;
  372. }
  373. break;
  374. case SDL_CONTROLLERBUTTONDOWN:
  375. for(i = 0; i < 4 && controllerid[i] != (int)event.cbutton.which; i++);
  376. if(i < 4) {
  377. switch(event.cbutton.button) {
  378. case SDL_CONTROLLER_BUTTON_DPAD_LEFT: meg4_setpad(i, MEG4_BTN_L); break;
  379. case SDL_CONTROLLER_BUTTON_DPAD_UP: meg4_setpad(i, MEG4_BTN_U); break;
  380. case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: meg4_setpad(i, MEG4_BTN_R); break;
  381. case SDL_CONTROLLER_BUTTON_DPAD_DOWN: meg4_setpad(i, MEG4_BTN_D); break;
  382. case SDL_CONTROLLER_BUTTON_A: meg4_setpad(i, MEG4_BTN_A); break;
  383. case SDL_CONTROLLER_BUTTON_B: meg4_setpad(i, MEG4_BTN_B); break;
  384. case SDL_CONTROLLER_BUTTON_X: meg4_setpad(i, MEG4_BTN_X); break;
  385. case SDL_CONTROLLER_BUTTON_Y: meg4_setpad(i, MEG4_BTN_Y); break;
  386. }
  387. }
  388. break;
  389. case SDL_CONTROLLERBUTTONUP:
  390. for(i = 0; i < 4 && controllerid[i] != (int)event.cbutton.which; i++);
  391. if(i < 4) {
  392. switch(event.cbutton.button) {
  393. case SDL_CONTROLLER_BUTTON_DPAD_LEFT: meg4_clrpad(i, MEG4_BTN_L); break;
  394. case SDL_CONTROLLER_BUTTON_DPAD_UP: meg4_clrpad(i, MEG4_BTN_U); break;
  395. case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: meg4_clrpad(i, MEG4_BTN_R); break;
  396. case SDL_CONTROLLER_BUTTON_DPAD_DOWN: meg4_clrpad(i, MEG4_BTN_D); break;
  397. case SDL_CONTROLLER_BUTTON_A: meg4_clrpad(i, MEG4_BTN_A); break;
  398. case SDL_CONTROLLER_BUTTON_B: meg4_clrpad(i, MEG4_BTN_B); break;
  399. case SDL_CONTROLLER_BUTTON_X: meg4_clrpad(i, MEG4_BTN_X); break;
  400. case SDL_CONTROLLER_BUTTON_Y: meg4_clrpad(i, MEG4_BTN_Y); break;
  401. }
  402. }
  403. break;
  404. case SDL_CONTROLLERAXISMOTION:
  405. for(i = 0; i < 4 && controllerid[i] != (int)event.caxis.which; i++);
  406. if(i < 4) {
  407. if(event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY || event.caxis.axis == SDL_CONTROLLER_AXIS_RIGHTY) {
  408. meg4_clrpad(i, MEG4_BTN_L | MEG4_BTN_R);
  409. if(event.caxis.value < -le16toh(meg4.mmio.padtres)) meg4_setpad(i, MEG4_BTN_L);
  410. if(event.caxis.value > le16toh(meg4.mmio.padtres)) meg4_setpad(i, MEG4_BTN_R);
  411. }
  412. if(event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX || event.caxis.axis == SDL_CONTROLLER_AXIS_RIGHTX) {
  413. meg4_clrpad(i, MEG4_BTN_U | MEG4_BTN_D);
  414. if(event.caxis.value < -le16toh(meg4.mmio.padtres)) meg4_setpad(i, MEG4_BTN_U);
  415. if(event.caxis.value > le16toh(meg4.mmio.padtres)) meg4_setpad(i, MEG4_BTN_D);
  416. }
  417. }
  418. break;
  419. /* normally finger events are automatically converted to mouse events, but in case SDL is configured not to do so */
  420. #if FINGEREVENTS
  421. case SDL_FINGERUP: case SDL_FINGERDOWN: case SDL_FINGERMOTION:
  422. SDL_GetWindowPosition(window, &i, &p);
  423. main_pointer(&dst, event.tfinger.x * main_w - i, event.tfinger.y * main_h - p);
  424. switch(event.tfinger.fingerId) {
  425. case 0: i = MEG4_BTN_L; break;
  426. case 1: i = MEG4_BTN_M; break;
  427. case 2: i = MEG4_BTN_R; break;
  428. default: i = -1; break;
  429. }
  430. if(i != -1) {
  431. if(event.type == SDL_FINGERUP)
  432. meg4_clrbtn(i);
  433. else
  434. meg4_setbtn(i);
  435. }
  436. break;
  437. #endif
  438. #ifndef NOEDITORS
  439. case SDL_DROPFILE:
  440. if(event.drop.file) {
  441. if((data = main_readfile(!memcmp(event.drop.file, "file://", 7) ? event.drop.file + 7 : event.drop.file, &i))) {
  442. fn = strrchr(event.drop.file, SEP[0]); if(!fn) fn = event.drop.file; else fn++;
  443. meg4_insert(fn, (uint8_t*)data, i);
  444. free(data);
  445. }
  446. SDL_free(event.drop.file);
  447. }
  448. break;
  449. #endif
  450. }
  451. }
  452. }
  453. /**
  454. * Workaround a stupid iOS and Android bug
  455. */
  456. int main_stupidios(void *data, SDL_Event *event)
  457. {
  458. (void)data;
  459. switch(event->type) {
  460. case SDL_APP_WILLENTERBACKGROUND: main_draw = 0; break;
  461. case SDL_APP_WILLENTERFOREGROUND: main_draw = 1; break;
  462. }
  463. return 1;
  464. }
  465. /**
  466. * Get text from clipboard (must be freed by caller)
  467. */
  468. char *main_getclipboard(void)
  469. {
  470. return SDL_GetClipboardText();
  471. }
  472. /**
  473. * Set text to clipboard
  474. */
  475. void main_setclipboard(char *str)
  476. {
  477. SDL_SetClipboardText((const char*)str);
  478. }
  479. /**
  480. * Show on-screen keyboard
  481. */
  482. void main_osk_show(void)
  483. {
  484. #if defined(__ANDROID__) || defined(__IOS__)
  485. SDL_StartTextInput();
  486. #endif
  487. }
  488. /**
  489. * Hide on-screen keyboard
  490. */
  491. void main_osk_hide(void)
  492. {
  493. #if defined(__ANDROID__) || defined(__IOS__)
  494. SDL_StopTextInput();
  495. #endif
  496. }
  497. /**
  498. * SDL audio callback
  499. */
  500. void main_audio(void *ctx, Uint8 *buf, int len)
  501. {
  502. (void)ctx;
  503. meg4_audiofeed((float*)buf, len >> 2);
  504. }
  505. /**
  506. * Delay
  507. */
  508. void main_delay(int msec)
  509. {
  510. SDL_Delay(msec);
  511. }
  512. /**
  513. * Print program version and copyright
  514. */
  515. void main_hdr(void)
  516. {
  517. printf("\r\nMEG-4 v%s (SDL%d, build %u) by bzt Copyright (C) 2023 GPLv3+\r\n\r\n", meg4ver, SDL_MAJOR_VERSION, BUILD);
  518. }
  519. /**
  520. * The real main procedure
  521. */
  522. int main(int argc, char **argv)
  523. {
  524. int i, j, w;
  525. char **infile = NULL, *ptr2;
  526. uint8_t *ptr;
  527. SDL_RWops *ops = NULL;
  528. SDL_AudioSpec want;
  529. SDL_version ver;
  530. #ifdef __EMSCRIPTEN__
  531. char detlng[3] = { 0 }, *lng = detlng;
  532. (void)argc; (void)argv;
  533. i = EM_ASM_INT({
  534. var ln=document.location.href.split('?')[1];if(ln==undefined)ln=navigator.language.substr(0,2);
  535. return ln.charCodeAt(1) * 256 + ln.charCodeAt(0);
  536. });
  537. detlng[0] = i & 0xff; detlng[1] = (i >> 8) & 0xff; detlng[2] = 0;
  538. #else
  539. #if !defined(NOEDITORS) && !defined(__EMSCRIPTEN__)
  540. char *fn;
  541. #endif
  542. int32_t tickdiff;
  543. uint32_t ticks;
  544. #if SDL_VERSION_ATLEAST(3,0,0)
  545. const SDL_DisplayMode *dm;
  546. #else
  547. SDL_DisplayMode dm;
  548. #endif
  549. #ifdef __WIN32__
  550. SDL_SysWMinfo wmInfo = { 0 };
  551. char *lng = main_lng;
  552. #else
  553. char *lng = getenv("LANG");
  554. #endif
  555. main_parsecommandline(argc, argv, &lng, &infile);
  556. #endif
  557. #ifndef NOEDITORS
  558. main_hdr();
  559. for(i = 0; i < 3; i++) printf(" %s\r\n", copyright[i]);
  560. printf("\r\n");
  561. #else
  562. for(i = 0; i < 128; i++) binary_game[i] ^= i + 7;
  563. #endif
  564. fflush(stdout);
  565. SDL_GetVersion(&ver);
  566. sprintf(meg4plat, "SDL %u.%u.%u", ver.major, ver.minor, ver.patch);
  567. /* set up keymap */
  568. memset(main_keymap, 0, sizeof(main_keymap));
  569. main_keymap[SDL_SCANCODE_A] = MEG4_KEY_A;
  570. main_keymap[SDL_SCANCODE_B] = MEG4_KEY_B;
  571. main_keymap[SDL_SCANCODE_C] = MEG4_KEY_C;
  572. main_keymap[SDL_SCANCODE_D] = MEG4_KEY_D;
  573. main_keymap[SDL_SCANCODE_E] = MEG4_KEY_E;
  574. main_keymap[SDL_SCANCODE_F] = MEG4_KEY_F;
  575. main_keymap[SDL_SCANCODE_G] = MEG4_KEY_G;
  576. main_keymap[SDL_SCANCODE_H] = MEG4_KEY_H;
  577. main_keymap[SDL_SCANCODE_I] = MEG4_KEY_I;
  578. main_keymap[SDL_SCANCODE_J] = MEG4_KEY_J;
  579. main_keymap[SDL_SCANCODE_K] = MEG4_KEY_K;
  580. main_keymap[SDL_SCANCODE_L] = MEG4_KEY_L;
  581. main_keymap[SDL_SCANCODE_M] = MEG4_KEY_M;
  582. main_keymap[SDL_SCANCODE_N] = MEG4_KEY_N;
  583. main_keymap[SDL_SCANCODE_O] = MEG4_KEY_O;
  584. main_keymap[SDL_SCANCODE_P] = MEG4_KEY_P;
  585. main_keymap[SDL_SCANCODE_Q] = MEG4_KEY_Q;
  586. main_keymap[SDL_SCANCODE_R] = MEG4_KEY_R;
  587. main_keymap[SDL_SCANCODE_S] = MEG4_KEY_S;
  588. main_keymap[SDL_SCANCODE_T] = MEG4_KEY_T;
  589. main_keymap[SDL_SCANCODE_U] = MEG4_KEY_U;
  590. main_keymap[SDL_SCANCODE_V] = MEG4_KEY_V;
  591. main_keymap[SDL_SCANCODE_W] = MEG4_KEY_W;
  592. main_keymap[SDL_SCANCODE_X] = MEG4_KEY_X;
  593. main_keymap[SDL_SCANCODE_Y] = MEG4_KEY_Y;
  594. main_keymap[SDL_SCANCODE_Z] = MEG4_KEY_Z;
  595. main_keymap[SDL_SCANCODE_1] = MEG4_KEY_1;
  596. main_keymap[SDL_SCANCODE_2] = MEG4_KEY_2;
  597. main_keymap[SDL_SCANCODE_3] = MEG4_KEY_3;
  598. main_keymap[SDL_SCANCODE_4] = MEG4_KEY_4;
  599. main_keymap[SDL_SCANCODE_5] = MEG4_KEY_5;
  600. main_keymap[SDL_SCANCODE_6] = MEG4_KEY_6;
  601. main_keymap[SDL_SCANCODE_7] = MEG4_KEY_7;
  602. main_keymap[SDL_SCANCODE_8] = MEG4_KEY_8;
  603. main_keymap[SDL_SCANCODE_9] = MEG4_KEY_9;
  604. main_keymap[SDL_SCANCODE_0] = MEG4_KEY_0;
  605. main_keymap[SDL_SCANCODE_RETURN] = MEG4_KEY_ENTER;
  606. main_keymap[SDL_SCANCODE_BACKSPACE] = MEG4_KEY_BACKSPACE;
  607. main_keymap[SDL_SCANCODE_TAB] = MEG4_KEY_TAB;
  608. main_keymap[SDL_SCANCODE_SPACE] = MEG4_KEY_SPACE;
  609. main_keymap[SDL_SCANCODE_MINUS] = MEG4_KEY_MINUS;
  610. main_keymap[SDL_SCANCODE_EQUALS] = MEG4_KEY_EQUAL;
  611. main_keymap[SDL_SCANCODE_LEFTBRACKET] = MEG4_KEY_LBRACKET;
  612. main_keymap[SDL_SCANCODE_RIGHTBRACKET] = MEG4_KEY_RBRACKET;
  613. main_keymap[SDL_SCANCODE_BACKSLASH] = MEG4_KEY_BACKSLASH;
  614. main_keymap[SDL_SCANCODE_NONUSHASH] = MEG4_KEY_BACKSLASH;
  615. main_keymap[SDL_SCANCODE_SEMICOLON] = MEG4_KEY_SEMICOLON;
  616. main_keymap[SDL_SCANCODE_APOSTROPHE] = MEG4_KEY_APOSTROPHE;
  617. main_keymap[SDL_SCANCODE_GRAVE] = MEG4_KEY_BACKQUOTE;
  618. main_keymap[SDL_SCANCODE_COMMA] = MEG4_KEY_COMMA;
  619. main_keymap[SDL_SCANCODE_PERIOD] = MEG4_KEY_PERIOD;
  620. main_keymap[SDL_SCANCODE_SLASH] = MEG4_KEY_SLASH;
  621. main_keymap[SDL_SCANCODE_CAPSLOCK] = MEG4_KEY_CAPSLOCK;
  622. main_keymap[SDL_SCANCODE_F1] = MEG4_KEY_F1;
  623. main_keymap[SDL_SCANCODE_F2] = MEG4_KEY_F2;
  624. main_keymap[SDL_SCANCODE_F3] = MEG4_KEY_F3;
  625. main_keymap[SDL_SCANCODE_F4] = MEG4_KEY_F4;
  626. main_keymap[SDL_SCANCODE_F5] = MEG4_KEY_F5;
  627. main_keymap[SDL_SCANCODE_F6] = MEG4_KEY_F6;
  628. main_keymap[SDL_SCANCODE_F7] = MEG4_KEY_F7;
  629. main_keymap[SDL_SCANCODE_F8] = MEG4_KEY_F8;
  630. main_keymap[SDL_SCANCODE_F9] = MEG4_KEY_F9;
  631. main_keymap[SDL_SCANCODE_F10] = MEG4_KEY_F10;
  632. main_keymap[SDL_SCANCODE_F11] = MEG4_KEY_F11;
  633. main_keymap[SDL_SCANCODE_F12] = MEG4_KEY_F12;
  634. main_keymap[SDL_SCANCODE_PRINTSCREEN] = MEG4_KEY_PRSCR;
  635. main_keymap[SDL_SCANCODE_SCROLLLOCK] = MEG4_KEY_SCRLOCK;
  636. main_keymap[SDL_SCANCODE_PAUSE] = MEG4_KEY_PAUSE;
  637. main_keymap[SDL_SCANCODE_INSERT] = MEG4_KEY_INS;
  638. main_keymap[SDL_SCANCODE_HOME] = MEG4_KEY_HOME;
  639. main_keymap[SDL_SCANCODE_PAGEUP] = MEG4_KEY_PGUP;
  640. main_keymap[SDL_SCANCODE_DELETE] = MEG4_KEY_DEL;
  641. main_keymap[SDL_SCANCODE_END] = MEG4_KEY_END;
  642. main_keymap[SDL_SCANCODE_PAGEDOWN] = MEG4_KEY_PGDN;
  643. main_keymap[SDL_SCANCODE_RIGHT] = MEG4_KEY_RIGHT;
  644. main_keymap[SDL_SCANCODE_LEFT] = MEG4_KEY_LEFT;
  645. main_keymap[SDL_SCANCODE_DOWN] = MEG4_KEY_DOWN;
  646. main_keymap[SDL_SCANCODE_UP] = MEG4_KEY_UP;
  647. main_keymap[SDL_SCANCODE_NUMLOCKCLEAR] = MEG4_KEY_NUMLOCK;
  648. main_keymap[SDL_SCANCODE_KP_DIVIDE] = MEG4_KEY_KP_DIV;
  649. main_keymap[SDL_SCANCODE_KP_MULTIPLY] = MEG4_KEY_KP_MUL;
  650. main_keymap[SDL_SCANCODE_KP_MINUS] = MEG4_KEY_KP_SUB;
  651. main_keymap[SDL_SCANCODE_KP_PLUS] = MEG4_KEY_KP_ADD;
  652. main_keymap[SDL_SCANCODE_KP_ENTER] = MEG4_KEY_KP_ENTER;
  653. main_keymap[SDL_SCANCODE_KP_1] = MEG4_KEY_KP_1;
  654. main_keymap[SDL_SCANCODE_KP_2] = MEG4_KEY_KP_2;
  655. main_keymap[SDL_SCANCODE_KP_3] = MEG4_KEY_KP_3;
  656. main_keymap[SDL_SCANCODE_KP_4] = MEG4_KEY_KP_4;
  657. main_keymap[SDL_SCANCODE_KP_5] = MEG4_KEY_KP_5;
  658. main_keymap[SDL_SCANCODE_KP_6] = MEG4_KEY_KP_6;
  659. main_keymap[SDL_SCANCODE_KP_7] = MEG4_KEY_KP_7;
  660. main_keymap[SDL_SCANCODE_KP_8] = MEG4_KEY_KP_8;
  661. main_keymap[SDL_SCANCODE_KP_9] = MEG4_KEY_KP_9;
  662. main_keymap[SDL_SCANCODE_KP_0] = MEG4_KEY_KP_0;
  663. main_keymap[SDL_SCANCODE_KP_PERIOD] = MEG4_KEY_KP_DEC;
  664. main_keymap[SDL_SCANCODE_DECIMALSEPARATOR] = MEG4_KEY_KP_DEC;
  665. main_keymap[SDL_SCANCODE_NONUSBACKSLASH] = MEG4_KEY_LESS;
  666. main_keymap[SDL_SCANCODE_APPLICATION] = MEG4_KEY_APP;
  667. main_keymap[SDL_SCANCODE_POWER] = MEG4_KEY_POWER;
  668. main_keymap[SDL_SCANCODE_KP_EQUALS] = MEG4_KEY_EQUAL;
  669. main_keymap[SDL_SCANCODE_EXECUTE] = MEG4_KEY_EXEC;
  670. main_keymap[SDL_SCANCODE_HELP] = MEG4_KEY_HELP;
  671. main_keymap[SDL_SCANCODE_MENU] = MEG4_KEY_MENU;
  672. main_keymap[SDL_SCANCODE_SELECT] = MEG4_KEY_SELECT;
  673. main_keymap[SDL_SCANCODE_STOP] = MEG4_KEY_STOP;
  674. main_keymap[SDL_SCANCODE_AGAIN] = MEG4_KEY_AGAIN;
  675. main_keymap[SDL_SCANCODE_UNDO] = MEG4_KEY_UNDO;
  676. main_keymap[SDL_SCANCODE_CUT] = MEG4_KEY_CUT;
  677. main_keymap[SDL_SCANCODE_COPY] = MEG4_KEY_COPY;
  678. main_keymap[SDL_SCANCODE_PASTE] = MEG4_KEY_PASTE;
  679. main_keymap[SDL_SCANCODE_FIND] = MEG4_KEY_FIND;
  680. main_keymap[SDL_SCANCODE_MUTE] = MEG4_KEY_MUTE;
  681. main_keymap[SDL_SCANCODE_VOLUMEUP] = MEG4_KEY_VOLUP;
  682. main_keymap[SDL_SCANCODE_VOLUMEDOWN] = MEG4_KEY_VOLDN;
  683. main_keymap[SDL_SCANCODE_INTERNATIONAL1] = MEG4_KEY_INT1;
  684. main_keymap[SDL_SCANCODE_INTERNATIONAL2] = MEG4_KEY_INT2;
  685. main_keymap[SDL_SCANCODE_INTERNATIONAL3] = MEG4_KEY_INT3;
  686. main_keymap[SDL_SCANCODE_INTERNATIONAL4] = MEG4_KEY_INT4;
  687. main_keymap[SDL_SCANCODE_INTERNATIONAL5] = MEG4_KEY_INT5;
  688. main_keymap[SDL_SCANCODE_INTERNATIONAL6] = MEG4_KEY_INT6;
  689. main_keymap[SDL_SCANCODE_INTERNATIONAL7] = MEG4_KEY_INT7;
  690. main_keymap[SDL_SCANCODE_INTERNATIONAL8] = MEG4_KEY_INT8;
  691. main_keymap[SDL_SCANCODE_LANG1] = MEG4_KEY_LNG1;
  692. main_keymap[SDL_SCANCODE_LANG2] = MEG4_KEY_LNG2;
  693. main_keymap[SDL_SCANCODE_LANG3] = MEG4_KEY_LNG3;
  694. main_keymap[SDL_SCANCODE_LANG4] = MEG4_KEY_LNG4;
  695. main_keymap[SDL_SCANCODE_LANG5] = MEG4_KEY_LNG5;
  696. main_keymap[SDL_SCANCODE_LANG6] = MEG4_KEY_LNG6;
  697. main_keymap[SDL_SCANCODE_LANG7] = MEG4_KEY_LNG7;
  698. main_keymap[SDL_SCANCODE_LANG8] = MEG4_KEY_LNG8;
  699. main_keymap[SDL_SCANCODE_LCTRL] = MEG4_KEY_LCTRL;
  700. main_keymap[SDL_SCANCODE_LSHIFT] = MEG4_KEY_LSHIFT;
  701. main_keymap[SDL_SCANCODE_LALT] = MEG4_KEY_LALT;
  702. main_keymap[SDL_SCANCODE_LGUI] = MEG4_KEY_LSUPER;
  703. main_keymap[SDL_SCANCODE_RCTRL] = MEG4_KEY_RCTRL;
  704. main_keymap[SDL_SCANCODE_RSHIFT] = MEG4_KEY_RSHIFT;
  705. main_keymap[SDL_SCANCODE_RALT] = MEG4_KEY_RALT;
  706. main_keymap[SDL_SCANCODE_RGUI] = MEG4_KEY_RSUPER;
  707. /* initialize screen and other SDL stuff */
  708. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER|SDL_INIT_EVENTS|SDL_INIT_GAMECONTROLLER)) {
  709. main_log(0, "unable to initialize SDL");
  710. return 1;
  711. }
  712. /* initialize audio */
  713. memset(&want, 0, sizeof(want));
  714. memset(&have, 0, sizeof(have));
  715. want.freq = 44100;
  716. #if SDL_VERSION_ATLEAST(3,0,0)
  717. want.format = SDL_AUDIO_F32;
  718. #else
  719. want.format = AUDIO_F32;
  720. #endif
  721. want.channels = 1;
  722. want.samples = 4096;
  723. want.callback = main_audio;
  724. audio = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
  725. if(audio && (have.freq != 44100 || have.channels != 1 || have.format !=
  726. #if SDL_VERSION_ATLEAST(3,0,0)
  727. SDL_AUDIO_F32
  728. #else
  729. AUDIO_F32
  730. #endif
  731. )) {
  732. SDL_CloseAudioDevice(audio); audio = 0;
  733. }
  734. if(verbose && audio) main_log(1, "audio opened %uHz, %u bits", have.freq, 32);
  735. /* turn on the emulator */
  736. meg4_poweron(lng);
  737. #if !defined(NOEDITORS) && !defined(__EMSCRIPTEN__)
  738. for(; infile && *infile; infile++) {
  739. if((ptr = main_readfile(*infile, &i))) {
  740. fn = strrchr(*infile, SEP[0]); if(!fn) fn = *infile; else fn++;
  741. meg4_insert(fn, ptr, i);
  742. free(ptr);
  743. }
  744. }
  745. #else
  746. (void)infile;
  747. #endif
  748. #ifdef __EMSCRIPTEN__
  749. win_w = main_w = EM_ASM_INT({ return Module.canvas.width; });
  750. win_h = main_h = EM_ASM_INT({ return Module.canvas.height; });
  751. main_win(main_w, main_h, 0);
  752. #else
  753. #if SDL_VERSION_ATLEAST(3,0,0)
  754. dm = SDL_GetDesktopDisplayMode(SDL_GetPrimaryDisplay());
  755. main_w = dm->w; main_h = dm->h;
  756. #else
  757. SDL_GetDesktopDisplayMode(0, &dm);
  758. /*dm.w = 640; dm.h = 400;*/
  759. main_w = dm.w; main_h = dm.h;
  760. #endif
  761. #if DEBUG
  762. main_win(640, 400, 0);
  763. #else
  764. i = main_w / 320; j = main_h / 200; if(i > j) i = j;
  765. win_w = 320 * i; win_h = 200 * i;
  766. main_win(win_w/*main_w*/, win_h/*main_h*/, 0/*1*/);
  767. if(!windowed) main_fullscreen();
  768. #endif
  769. #endif
  770. if(!window || !renderer) {
  771. meg4_poweroff();
  772. SDL_Quit();
  773. main_log(0, "unable to get SDL window");
  774. return 1;
  775. }
  776. #ifdef __WIN32__
  777. SDL_VERSION(&wmInfo.version);
  778. SDL_GetWindowWMInfo(window, &wmInfo);
  779. hwnd = wmInfo.info.win.window;
  780. #endif
  781. /* uncompress built-in gamecontrollerdb */
  782. ptr = (uint8_t*)binary_gamecontrollerdb + 3;
  783. i = *ptr++; ptr += 6; if(i & 4) { w = *ptr++; w += (*ptr++ << 8); ptr += w; } if(i & 8) { while(*ptr++ != 0); }
  784. if(i & 16) { while(*ptr++ != 0); } j = sizeof(binary_gamecontrollerdb) - (size_t)(ptr - binary_gamecontrollerdb);
  785. w = 0; ptr2 = (char*)stbi_zlib_decode_malloc_guesssize_headerflag((const char*)ptr, j, 4096, (int*)&w, 0);
  786. if(ptr2) {
  787. ops = SDL_RWFromConstMem(ptr2, w);
  788. SDL_GameControllerAddMappingsFromRW(ops, 0);
  789. #if SDL_VERSION_ATLEAST(3,0,0)
  790. SDL_DestroyRW(ops);
  791. #else
  792. SDL_FreeRW(ops);
  793. #endif
  794. free(ptr2);
  795. }
  796. #if SDL_VERSION_ATLEAST(3,0,0)
  797. SDL_SetGamepadEventsEnabled(SDL_ENABLE);
  798. #else
  799. SDL_GameControllerEventState(SDL_ENABLE);
  800. #endif
  801. meg4_hidecursor();
  802. if(audio) {
  803. #if SDL_VERSION_ATLEAST(3,0,0)
  804. SDL_PlayAudioDevice(audio);
  805. #else
  806. SDL_PauseAudioDevice(audio, 0);
  807. #endif
  808. }
  809. #if !defined(__ANDROID__) && !defined(__IOS__)
  810. SDL_StartTextInput();
  811. #endif
  812. /* execute the main emulator loop */
  813. #ifdef __EMSCRIPTEN__
  814. emscripten_set_main_loop(main_loop, 60, 1);
  815. /* this never reached! cancel_main_loop does not cancel the loop, it actually quits the app! */
  816. #else
  817. SDL_AddEventWatch(main_stupidios, NULL);
  818. while(1) {
  819. ticks = SDL_GetTicks();
  820. /* emscripten does not allow return value... so we have to use a global */
  821. main_loop();
  822. if(!main_ret) break;
  823. tickdiff = (1000/60) - (SDL_GetTicks() - ticks);
  824. if(tickdiff > 0 && tickdiff < 1000) {
  825. if(verbose == 2) { printf("meg4: free time %d msec \r",tickdiff); fflush(stdout); }
  826. SDL_Delay(tickdiff);
  827. }
  828. }
  829. main_quit();
  830. #endif
  831. return 0;
  832. }