Advertisement
Guest User

Untitled

a guest
May 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.73 KB | None | 0 0
  1. /* Resistance's Portable-Adventure-Game-Engine (R-PAGE), Copyright (C) 2019 François Gutherz, Resistance.no
  2. Released under GPLv3, see licence.txt for details.
  3. */
  4.  
  5. #ifdef LATTICE
  6. #include "frwk/frwk.h"
  7.  
  8. #include "frwk/amiga/includes.prl"
  9. #include <time.h>
  10. #include <intuition/intuition.h>
  11. #include <graphics/gfxbase.h>
  12. #include <hardware/dmabits.h>
  13. #include <hardware/intbits.h>
  14. #include <hardware/custom.h>
  15. #include <graphics/gfxmacros.h>
  16. #include <proto/exec.h>
  17. #include <proto/dos.h>
  18. #include <proto/timer.h>
  19. #include <math.h>
  20. #include <time.h>
  21.  
  22. /*
  23. Common
  24. */
  25. #include "frwk/amiga/board.h"
  26. #include "frwk/amiga/ptreplay.h"
  27. #include "frwk/amiga/ptreplay_protos.h"
  28. #include "frwk/amiga/ptreplay_pragmas.h"
  29.  
  30. /*
  31. Routines
  32. */
  33. #include "frwk/amiga/screen.h"
  34. #include "frwk/amiga/bitmap.h"
  35. #include "frwk/amiga/color.h"
  36. #include "frwk/amiga/helper.h"
  37. #include "frwk/amiga/protracker.h"
  38. #include "frwk/amiga/input.h"
  39. #include "frwk/amiga/time.h"
  40.  
  41. /*
  42. Graphic assets
  43. */
  44. #include "frwk/amiga/screen_size.h"
  45. #include "frwk/amiga/mouse_pointer_data.h"
  46. #include "frwk/amiga/debug.h"
  47.  
  48. struct IntuitionBase *IntuitionBase = NULL;
  49. struct GfxBase *GfxBase = NULL;
  50. extern struct ExecBase *SysBase;
  51. extern struct DosLibrary *DOSBase;
  52. extern struct DiskfontBase *DiskfontBase;
  53. extern struct Custom far custom;
  54.  
  55. struct Task *main_task = NULL;
  56. BYTE oldPri;
  57.  
  58. struct View my_view;
  59. struct View *my_old_view;
  60.  
  61. /* Main ViewPort */
  62. buffered_screen *main_screen = NULL;
  63. short scr_x_offset = 0, scr_y_offset = 0;
  64.  
  65. struct TextFont *main_font = NULL;
  66.  
  67. /* Global clock */
  68. struct timeval startTime;
  69.  
  70. /* Input System */
  71. short input_mouse_button;
  72. short prev_input_mouse_button;
  73.  
  74. vec2 input_mouse_position;
  75. vec2 prev_input_mouse_position;
  76.  
  77. short input_rawkey;
  78.  
  79. /* platform interface Amiga implementation */
  80.  
  81. void platform_init(void)
  82. {
  83. BYTE error_code;
  84. #ifdef DEBUG_MACROS
  85. printf("platform_init()\n");
  86. #endif
  87. /* Open the Intuition library: */
  88. if (IntuitionBase == NULL)
  89. {
  90. IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  91. if (!IntuitionBase)
  92. {
  93. platform_system_alert("Could NOT open the Intuition library!");
  94. platform_uninit();
  95. exit(0);
  96. }
  97. }
  98. else
  99. {
  100. platform_system_alert("Platform already initialized!");
  101. exit(0);
  102. }
  103.  
  104. /* Open the Graphics library: */
  105. GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  106. if (!GfxBase)
  107. {
  108. platform_system_alert("Could NOT open the Graphics library!");
  109. platform_uninit();
  110. exit(0);
  111. }
  112.  
  113. /* Open the DiskFont library: */
  114. DiskfontBase = (struct DiskfontBase *)OpenLibrary("diskfont.library", 0);
  115. if (!DiskfontBase)
  116. {
  117. platform_system_alert("Could NOT open the Diskfont library!");
  118. platform_uninit();
  119. exit(0);
  120. }
  121.  
  122. main_task = FindTask(NULL);
  123. // oldPri = SetTaskPri(main_task, 16);
  124.  
  125. /* Timestamp of the platform startup */
  126. init_timer_device();
  127. timer_device_get_system_time(&startTime);
  128.  
  129. main_screen = NULL;
  130. }
  131.  
  132. void platform_uninit(void)
  133. {
  134. #ifdef DEBUG_MACROS
  135. printf("platform_uninit()\n");
  136. #endif
  137. if (main_task != NULL)
  138. SetTaskPri(main_task, oldPri);
  139.  
  140. uninit_timer_device();
  141.  
  142. /* Close the Graphics library: */
  143. if (GfxBase)
  144. CloseLibrary((struct Library *)GfxBase);
  145.  
  146. /* C Close the Intuition library: */
  147. if (IntuitionBase)
  148. CloseLibrary((struct Library *)IntuitionBase);
  149.  
  150. IntuitionBase = NULL;
  151. GfxBase = NULL;
  152. }
  153.  
  154. /*
  155. SYSTEM (resources, memory, multitasking...)
  156. -------------------------------------------
  157. */
  158.  
  159. BYTE platform_set_process_priority(BYTE new_priority)
  160. {
  161. return SetTaskPri(main_task, new_priority);
  162. }
  163.  
  164. ULONG platform_get_avail_video_memory(void)
  165. {
  166. return AvailMem(MEMF_CHIP);
  167. }
  168.  
  169. void platform_free_memory_block(BYTE *block_ptr, UWORD block_size)
  170. {
  171. FreeMem(block_ptr, block_size);
  172. }
  173.  
  174. void platform_system_alert(char *alert_message)
  175. {
  176. char guru_format_message[128];
  177. short margin_x;
  178. #ifdef DEBUG_MACROS
  179. printf("/!\\%s\n", alert_message);
  180. #endif
  181. memset(guru_format_message, 0, 128);
  182. strcpy(guru_format_message, " ");
  183. if (strlen(alert_message) > 76)
  184. alert_message[76] = 0x0;
  185. strcat(guru_format_message, alert_message);
  186. margin_x = ((640 - strlen(alert_message) * 8) / 2);
  187. guru_format_message[0] = (margin_x & 0xFF00) >> 8;
  188. guru_format_message[1] = margin_x & 0xFF;
  189. guru_format_message[2] = 0xF;
  190. DisplayAlert(RECOVERY_ALERT, guru_format_message, 32);
  191. }
  192.  
  193. void platform_system_flash(void)
  194. {
  195. DisplayBeep(main_screen->screen);
  196. }
  197.  
  198. ULONG platform_get_clock(void)
  199. {
  200. struct timeval endTime;
  201.  
  202. timer_device_get_system_time(&endTime);
  203. SubTime(&endTime, &startTime);
  204.  
  205. return (endTime.tv_secs * 1000 + endTime.tv_micro / 1000);
  206. }
  207.  
  208. /*
  209. VIDEO (video framebuffer access)
  210. --------------------------------
  211. */
  212.  
  213. void platform_video_open(int platform_video_open)
  214. {
  215. #ifdef DEBUG_MACROS
  216. printf("platform_video_open()\n");
  217. #endif
  218. if (main_screen == NULL)
  219. {
  220. if (platform_video_open == mode_lowres)
  221. main_screen = openMainScreen();
  222. else
  223. main_screen = openMainScreenCustom(320, 512, 32, FALSE); /* Double buffer is DISABLED */
  224.  
  225. main_font = NULL;
  226. }
  227. else
  228. {
  229. platform_system_alert("A screen is already open!");
  230. exit(0);
  231. }
  232. }
  233.  
  234. void __inline platform_video_vsync(void)
  235. {
  236. WaitTOF();
  237. }
  238.  
  239. void __inline platform_video_flip_buffers(void)
  240. {
  241. flipBuffers(main_screen);
  242. }
  243.  
  244. void __inline platform_video_present_screen(void)
  245. {
  246. presentScreen(main_screen);
  247. }
  248.  
  249. void platform_video_sync_buffers(void)
  250. {
  251. synchronizeBuffers(main_screen);
  252. }
  253.  
  254. void platform_video_clear(void)
  255. {
  256. short i;
  257. // SetRast(&(main_screen->RastPort), 0);
  258. // Move(&(main_screen->RastPort), 0, 0);
  259. // ClearScreen(&(main_screen->RastPort));
  260. if (main_screen != NULL)
  261. {
  262. for (i = 0; i < main_screen->screen->RastPort.BitMap->Depth; i++)
  263. {
  264. // memset(main_screen->RastPort.BitMap->Planes[i], 0x0, RASSIZE(main_screen->Width, main_screen->Height));
  265. BltClear(main_screen->screen->RastPort.BitMap->Planes[i], RASSIZE(main_screen->screen->Width, main_screen->screen->Height), 0);
  266. WaitBlit();
  267. }
  268. }
  269. else
  270. {
  271. platform_system_alert("No screen was found open!");
  272. exit(0);
  273. }
  274. }
  275.  
  276. void platform_video_blit_bitmap(platform_bitmap *source_bitmap, short source_x, short source_y, short width, short height, short x, short y)
  277. {
  278. BltBitMap(source_bitmap, source_x, source_y, main_screen->bitmaps[main_screen->physical], x, y, width, height, 0xC0, 0xFF, NULL);
  279. WaitBlit();
  280. }
  281.  
  282. void platform_video_blit_bitmap_with_mask(platform_bitmap *source_bitmap, short source_x, short source_y, short width, short height, short x, short y, platform_bitmap *mask_bitmap)
  283. {
  284. BltMaskBitMapRastPort(source_bitmap, source_x, source_y, &(main_screen->screen->RastPort), x, y, width, height, (ABC|ABNC|ANBC), mask_bitmap->Planes[0]);
  285. WaitBlit();
  286. }
  287.  
  288. void platform_video_set_palette(PALETTEPTR palette, short palette_size)
  289. {
  290. set_palette(&(main_screen->screen->ViewPort), &palette, 0, palette_size - 1);
  291. }
  292.  
  293. void platform_video_draw_rect(rect *r, short color)
  294. {
  295. rect video_rect;
  296.  
  297. video_rect.sx = 0;
  298. video_rect.sy = 0;
  299. video_rect.ex = main_screen->screen->Width - 1;
  300. video_rect.ey = main_screen->screen->Height - 1;
  301.  
  302. if (color < 0)
  303. color = (1 << main_screen->screen->RastPort.BitMap->Depth) - 1;
  304. SetAPen(&(main_screen->screen->RastPort), color);
  305. Move(&(main_screen->screen->RastPort), max(r->sx, video_rect.sx), max(r->sy, video_rect.sy));
  306. Draw(&(main_screen->screen->RastPort), min(r->ex, video_rect.ex), max(r->sy, video_rect.sy));
  307. Draw(&(main_screen->screen->RastPort), min(r->ex, video_rect.ex), min(r->ey, video_rect.ey));
  308. Draw(&(main_screen->screen->RastPort), max(r->sx, video_rect.sx), min(r->ey, video_rect.ey));
  309. Draw(&(main_screen->screen->RastPort), max(r->sx, video_rect.sx), max(r->sy, video_rect.sy));
  310. }
  311.  
  312. void platform_video_set_pixel(short x, short y, short color)
  313. {
  314. if (color < 0)
  315. color = (1 << main_screen->screen->RastPort.BitMap->Depth) - 1;
  316. SetAPen(&(main_screen->screen->RastPort), color);
  317. WritePixel(&(main_screen->screen->RastPort), x, y);
  318. }
  319.  
  320. void platform_video_set_font(char *font_filename, short font_size)
  321. {
  322. struct TextAttr ta;
  323.  
  324. if (main_font != NULL)
  325. CloseFont(main_font);
  326.  
  327. ta.ta_Name = font_filename;
  328. ta.ta_YSize = font_size;
  329. ta.ta_Flags = FPB_DISKFONT | FPF_DESIGNED;
  330. ta.ta_Style = FS_NORMAL;
  331.  
  332. main_font = OpenDiskFont(&ta);
  333. if (main_font)
  334. SetFont(&(main_screen->screen->RastPort), main_font);
  335. else
  336. {
  337. printf("Cannot open font %s!", font_filename);
  338. main_font = NULL;
  339. }
  340. }
  341.  
  342. void platform_video_draw_text(char *str, short x, short y, short color)
  343. {
  344. if (color < 0)
  345. color = (1 << main_screen->screen->RastPort.BitMap->Depth) - 1;
  346.  
  347. if (x < 0)
  348. x = (main_screen->screen->Width - TextLength(&(main_screen->screen->RastPort), str, strlen(str))) >> 1;
  349. if (y < 0)
  350. y = (main_screen->screen->Height - 8) >> 1;
  351.  
  352. SetAPen(&(main_screen->screen->RastPort), color);
  353. SetBPen(&(main_screen->screen->RastPort), 0);
  354. Move(&(main_screen->screen->RastPort), x, y + 8);
  355. SetDrMd(&(main_screen->screen->RastPort), 0);
  356. Text(&(main_screen->screen->RastPort), str, strlen(str));
  357. }
  358.  
  359. void platform_video_close(void)
  360. {
  361. #ifdef DEBUG_MACROS
  362. printf("platform_video_close()\n");
  363. #endif
  364. if (main_font)
  365. {
  366. CloseFont(main_font);
  367. main_font = NULL;
  368. }
  369.  
  370. closeMainScreen(main_screen);
  371. main_screen = NULL;
  372. }
  373.  
  374. /*
  375. BITMAP (direct bitmap access)
  376. -----------------------------
  377. */
  378.  
  379. ULONG platform_calculate_bitmap_bytesize(short width, short height, short depth)
  380. {
  381. return (RASSIZE(width, height) * depth);
  382. }
  383.  
  384. platform_bitmap *platform_new_bitmap(short width, short height, short depth)
  385. {
  386. return (platform_bitmap *)allocate_new_bitmap(width, height, depth);
  387. }
  388.  
  389. BOOL platform_load_packed_file_to_bitmap(platform_bitmap **bitmap, platform_palette **palette, BYTE *packed_buffer, char *filename)
  390. {
  391. return load_pak_img_to_bitmap((struct BitMap **)bitmap, (UWORD **)palette, packed_buffer, filename);
  392. }
  393.  
  394.  
  395. short platform_bitmap_get_width(platform_bitmap *bitmap)
  396. {
  397. return(short)(bitmap->BytesPerRow << 3);
  398. }
  399.  
  400. short platform_bitmap_get_height(platform_bitmap *bitmap)
  401. {
  402. return(short)(bitmap->Rows);
  403. }
  404.  
  405. BOOL platform_load_packed_bitmap(platform_bitmap **new_bitmap, platform_palette **new_palette, char *bitmap_filename)
  406. {
  407. return load_pak_img_to_new_bitmap(new_bitmap, new_palette, bitmap_filename);
  408. }
  409.  
  410. platform_bitmap *platform_build_bitmap_mask(platform_bitmap *source_bitmap)
  411. {
  412. // TODO
  413. return NULL;
  414. }
  415.  
  416. void platform_free_bitmap(platform_bitmap *bitmap)
  417. {
  418. free_allocated_bitmap(bitmap);
  419. }
  420.  
  421. /*
  422. INPUT (mouse, keyboard...)
  423. --------------------------
  424. */
  425.  
  426. BOOL platform_input_init(void)
  427. {
  428. if (main_screen->screen == NULL)
  429. {
  430. platform_system_alert("Cannot init. input without a screen open !");
  431. return FALSE;
  432. }
  433.  
  434. if (main_screen->window == NULL)
  435. {
  436. platform_system_alert("Cannot init. input without a window open !");
  437. return FALSE;
  438. }
  439.  
  440. input_window_init(main_screen->window);
  441. }
  442.  
  443. void platform_input_update(void)
  444. {
  445. prev_input_mouse_button = input_mouse_button;
  446. prev_input_mouse_position = input_mouse_position;
  447. input_update(&input_mouse_button, &(input_mouse_position.x), &(input_mouse_position.y), &input_rawkey);
  448. }
  449.  
  450. void platform_mouse_button_flush(void)
  451. {
  452. ActivateWindow(main_screen->window); /* FIXME : this patch helps the main window to get the focus after game init */
  453. input_mouse_button = 0;
  454. }
  455.  
  456. BOOL platform_mouse_button_left_is_down(void)
  457. {
  458. if ((input_mouse_button & PLATFORM_MOUSE_LEFT_BUTTON))
  459. return TRUE;
  460.  
  461. return FALSE;
  462. }
  463.  
  464. BOOL platform_mouse_button_right_is_down(void)
  465. {
  466. if ((input_mouse_button & PLATFORM_MOUSE_RIGHT_BUTTON))
  467. return TRUE;
  468.  
  469. return FALSE;
  470. }
  471.  
  472. BOOL platform_mouse_button_left_was_down(void)
  473. {
  474. if ((input_mouse_button & PLATFORM_MOUSE_LEFT_BUTTON) && !(prev_input_mouse_button & PLATFORM_MOUSE_LEFT_BUTTON))
  475. return TRUE;
  476.  
  477. return FALSE;
  478. }
  479.  
  480. BOOL platform_mouse_button_right_was_down(void)
  481. {
  482. if ((input_mouse_button & PLATFORM_MOUSE_RIGHT_BUTTON) && !(prev_input_mouse_button & PLATFORM_MOUSE_RIGHT_BUTTON))
  483. return TRUE;
  484.  
  485. return FALSE;
  486. }
  487.  
  488. void platform_mouse_get_values(short *button, vec2 *mouse_coords)
  489. {
  490. // input_update(button, &(mouse_coords->x), &(mouse_coords->y));
  491. *button = input_mouse_button;
  492. *mouse_coords = input_mouse_position;
  493. }
  494.  
  495. void platform_mouse_show(void)
  496. {
  497. if (main_screen != NULL && main_screen->screen->FirstWindow != NULL)
  498. {
  499. #ifdef DEBUG_MACROS
  500. printf("platform_mouse_show()\n");
  501. #endif
  502. // ClearPointer(main_screen->FirstWindow);
  503. SetPointer(main_screen->screen->FirstWindow, pointer_normal_data, 16, 16, -1, -1);
  504. }
  505. else
  506. platform_system_alert("Cannot set cursor state without a window ownership!");
  507. }
  508.  
  509. void platform_mouse_wait(void)
  510. {
  511. if (main_screen != NULL && main_screen->screen->FirstWindow != NULL)
  512. {
  513. #ifdef DEBUG_MACROS
  514. printf("platform_mouse_show()\n");
  515. #endif
  516. // ClearPointer(main_screen->FirstWindow);
  517. SetPointer(main_screen->screen->FirstWindow, wait_pointer, WAIT_POINTER_HEIGHT, 16, -1, -1);
  518. }
  519. else
  520. platform_system_alert("Cannot set cursor state without a window ownership!");
  521. }
  522.  
  523. void platform_mouse_hide(void)
  524. {
  525. if (main_screen != NULL && main_screen->screen->FirstWindow != NULL)
  526. {
  527. #ifdef DEBUG_MACROS
  528. printf("platform_mouse_hide()\n");
  529. #endif
  530. SetPointer(main_screen->screen->FirstWindow, pointer_normal_data, 0, 0, 0, 0);
  531. }
  532. else
  533. platform_system_alert("Cannot set cursor state without a window ownership!");
  534. }
  535.  
  536. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement