]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg-output-raylib.c
9fc00fac3ccf2f79d324bce2af260dc6cd904c73
[pistorm] / platforms / amiga / rtg / rtg-output-raylib.c
1 // SPDX-License-Identifier: MIT
2
3 #include "config_file/config_file.h"
4 #include "emulator.h"
5 #include "rtg.h"
6
7 #include "raylib/raylib.h"
8
9 #include <pthread.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
17
18 //#define DEBUG_RAYLIB_RTG
19
20 #ifdef DEBUG_RAYLIB_RTG
21 #define DEBUG printf
22 #else
23 #define DEBUG(...)
24 #endif
25
26 uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0, emulator_exiting = 0, rtg_output_in_vblank = 0;
27 extern uint8_t *rtg_mem, display_enabled;
28 extern uint32_t framebuffer_addr;
29 extern uint32_t framebuffer_addr_adj;
30
31 extern uint16_t rtg_display_width, rtg_display_height;
32 extern uint16_t rtg_display_format;
33 extern uint16_t rtg_pitch, rtg_total_rows;
34 extern uint16_t rtg_offset_x, rtg_offset_y;
35
36 static pthread_t thread_id;
37 static uint8_t mouse_cursor_enabled = 0, cursor_image_updated = 0, updating_screen = 0, debug_palette = 0, show_fps = 0;
38 static uint8_t mouse_cursor_w = 16, mouse_cursor_h = 16;
39 static int16_t mouse_cursor_x = 0, mouse_cursor_y = 0;
40
41 struct rtg_shared_data {
42     uint16_t *width, *height;
43     uint16_t *format, *pitch;
44     uint16_t *offset_x, *offset_y;
45     uint8_t *memory;
46     uint32_t *addr;
47     uint8_t *running;
48 };
49
50 struct rtg_shared_data rtg_share_data;
51 static uint32_t palette[256];
52 static uint32_t cursor_palette[256];
53
54 uint32_t cursor_data[256 * 256];
55
56 void rtg_update_screen() {}
57
58 uint32_t rtg_to_raylib[RTGFMT_NUM] = {
59     PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
60     PIXELFORMAT_UNCOMPRESSED_R5G6B5,
61     PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
62     PIXELFORMAT_UNCOMPRESSED_R5G5B5A1,
63 };
64
65 uint32_t rtg_pixel_size[RTGFMT_NUM] = {
66     1,
67     2,
68     4,
69     2,
70 };
71
72 void *rtgThread(void *args) {
73
74     printf("RTG thread running\n");
75     fflush(stdout);
76
77     int reinit = 0;
78     rtg_on = 1;
79
80     uint32_t *indexed_buf = NULL;
81
82     rtg_share_data.format = &rtg_display_format;
83     rtg_share_data.width = &rtg_display_width;
84     rtg_share_data.height = &rtg_display_height;
85     rtg_share_data.pitch = &rtg_pitch;
86     rtg_share_data.offset_x = &rtg_offset_x;
87     rtg_share_data.offset_y = &rtg_offset_y;
88     rtg_share_data.memory = rtg_mem;
89     rtg_share_data.running = &rtg_on;
90     rtg_share_data.addr = &framebuffer_addr_adj;
91     struct rtg_shared_data *data = &rtg_share_data;
92
93     uint16_t width = rtg_display_width;
94     uint16_t height = rtg_display_height;
95     uint16_t format = rtg_display_format;
96     uint16_t pitch = rtg_pitch;
97
98     Texture raylib_texture, raylib_cursor_texture;
99     Texture raylib_clut_texture;
100     Image raylib_fb, raylib_cursor, raylib_clut;
101
102     InitWindow(GetScreenWidth(), GetScreenHeight(), "Pistorm RTG");
103     HideCursor();
104     SetTargetFPS(60);
105
106         Color bef = { 0, 64, 128, 255 };
107     float scale_x = 1.0f, scale_y = 1.0f;
108
109     Shader clut_shader = LoadShader(NULL, "platforms/amiga/rtg/clut.shader");
110     Shader swizzle_shader = LoadShader(NULL, "platforms/amiga/rtg/argbswizzle.shader");
111     int clut_loc = GetShaderLocation(clut_shader, "texture1");
112
113     raylib_clut.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
114     raylib_clut.width = 256;
115     raylib_clut.height = 1;
116     raylib_clut.mipmaps = 1;
117     raylib_clut.data = palette;
118
119     raylib_clut_texture = LoadTextureFromImage(raylib_clut);
120
121     raylib_cursor.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
122     raylib_cursor.width = 256;
123     raylib_cursor.height = 256;
124     raylib_cursor.mipmaps = 1;
125     raylib_cursor.data = cursor_data;
126     raylib_cursor_texture = LoadTextureFromImage(raylib_cursor);
127
128     Rectangle srcrect, dstscale;
129     Vector2 origin;
130
131 reinit_raylib:;
132     if (reinit) {
133         printf("Reinitializing raylib...\n");
134         width = rtg_display_width;
135         height = rtg_display_height;
136         format = rtg_display_format;
137         pitch = rtg_pitch;
138         if (indexed_buf) {
139             free(indexed_buf);
140             indexed_buf = NULL;
141         }
142         UnloadTexture(raylib_texture);
143         reinit = 0;
144     }
145
146     printf("Creating %dx%d raylib window...\n", width, height);
147
148     printf("Setting up raylib framebuffer image.\n");
149     raylib_fb.format = rtg_to_raylib[format];
150
151     switch (format) {
152         case RTGFMT_RBG565:
153             raylib_fb.width = width;
154             indexed_buf = calloc(1, width * height * 2);
155             break;
156         default:
157             raylib_fb.width = pitch / rtg_pixel_size[format];
158             break;
159     }
160     raylib_fb.height = height;
161         raylib_fb.mipmaps = 1;
162         raylib_fb.data = &data->memory[*data->addr];
163     printf("Width: %d\nPitch: %d\nBPP: %d\n", raylib_fb.width, pitch, rtg_pixel_size[format]);
164
165     raylib_texture = LoadTextureFromImage(raylib_fb);
166
167     printf("Loaded framebuffer texture.\n");
168
169     srcrect.x = srcrect.y = 0;
170     srcrect.width = width;
171     srcrect.height = height;
172     dstscale.x = dstscale.y = 0;
173     dstscale.width = width;
174     dstscale.height = height;
175
176     if (dstscale.height * 2 <= GetScreenHeight()) {
177         if (width == 320) {
178             if (GetScreenHeight() == 720) {
179                 dstscale.width = 960;
180                 dstscale.height = 720;
181             } else if (GetScreenHeight() == 1080) {
182                 dstscale.width = 1440;
183                 dstscale.height = 1080;
184             } else if (GetScreenHeight() == 1200) {
185                 dstscale.width = 1600;
186                 dstscale.height = 1200;
187             }
188         } else {
189             while (dstscale.height + height <= GetScreenHeight()) {
190                 dstscale.height += height;
191                 dstscale.width += width;
192             }
193         }
194     } else if (dstscale.width > GetScreenWidth() || dstscale.height > GetScreenHeight()) {
195         if (dstscale.height > GetScreenHeight()) {
196             DEBUG("[H > SH]\n");
197             DEBUG("Adjusted width from %d to", (int)dstscale.width);
198             dstscale.width = dstscale.width * ((float)GetScreenHeight() / (float)height);
199             DEBUG("%d.\n", (int)dstscale.width);
200             DEBUG("Adjusted height from %d to", (int)dstscale.height);
201             dstscale.height = GetScreenHeight();
202             DEBUG("%d.\n", (int)dstscale.height);
203         }
204         if (dstscale.width > GetScreenWidth()) {
205             // First scaling attempt failed, do not double adjust, re-adjust
206             dstscale.width = width;
207             dstscale.height = height;
208             DEBUG("[W > SW]\n");
209             DEBUG("Adjusted height from %d to", (int)dstscale.height);
210             dstscale.height = dstscale.height * ((float)GetScreenWidth() / (float)width);
211             DEBUG("%d.\n", (int)dstscale.height);
212             DEBUG("Adjusted width from %d to", (int)dstscale.width);
213             dstscale.width = GetScreenWidth();
214             DEBUG("%d.\n", (int)dstscale.width);
215         }
216     }
217
218     scale_x = dstscale.width / (float)width;
219     scale_y = dstscale.height / (float)height;
220
221     origin.x = (dstscale.width - GetScreenWidth()) * 0.5;
222     origin.y = (dstscale.height - GetScreenHeight()) * 0.5;
223
224     while (1) {
225         if (rtg_on) {
226             BeginDrawing();
227             rtg_output_in_vblank = 0;
228             updating_screen = 1;
229
230             switch (format) {
231                 case RTGFMT_8BIT:
232                     UpdateTexture(raylib_clut_texture, palette);
233                     BeginShaderMode(clut_shader);
234                     SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
235                     break;
236                 case RTGFMT_RGB32:
237                     BeginShaderMode(swizzle_shader);
238                     break;
239             }
240             
241             DrawTexturePro(raylib_texture, srcrect, dstscale, origin, 0.0f, RAYWHITE);
242
243             switch (format) {
244                 case RTGFMT_8BIT:
245                 case RTGFMT_RGB32:
246                     EndShaderMode();
247                     break;
248             }
249
250             if (mouse_cursor_enabled) {
251                 float mc_x = mouse_cursor_x - rtg_offset_x;
252                 float mc_y = mouse_cursor_y - rtg_offset_y;
253                 Rectangle cursor_srcrect = { 0, 0, mouse_cursor_w, mouse_cursor_h };
254                 Rectangle dstrect = { mc_x * scale_x, mc_y * scale_y, (float)mouse_cursor_w * scale_x, (float)mouse_cursor_h * scale_y };
255                 DrawTexturePro(raylib_cursor_texture, cursor_srcrect, dstrect, origin, 0.0f, RAYWHITE);
256             }
257
258             if (debug_palette) {
259                 if (format == RTGFMT_8BIT) {
260                     Rectangle srcrect = { 0, 0, 256, 1 };
261                     Rectangle dstrect = { 0, 0, 1024, 8 };
262                     DrawTexturePro(raylib_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
263                 }
264             }
265
266             if (show_fps) {
267                 DrawFPS(GetScreenWidth() - 128, 0);
268             }
269
270             EndDrawing();
271             rtg_output_in_vblank = 1;
272             if (format == RTGFMT_RBG565) {
273                 for (int y = 0; y < height; y++) {
274                     for (int x = 0; x < width; x++) {
275                         ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
276                     }
277                 }
278                 UpdateTexture(raylib_texture, indexed_buf);
279             }
280             else {
281                 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
282             }
283             if (cursor_image_updated) {
284                 UpdateTexture(raylib_cursor_texture, cursor_data);
285                 cursor_image_updated = 0;
286             }
287             updating_screen = 0;
288         } else {
289             BeginDrawing();
290             ClearBackground(bef);
291             DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
292             EndDrawing();
293         }
294         if (pitch != *data->pitch || height != *data->height || width != *data->width || format != *data->format) {
295             printf("Reinitializing due to something change.\n");
296             reinit = 1;
297             goto shutdown_raylib;
298         }
299         if (emulator_exiting) {
300             goto shutdown_raylib;
301         }
302     }
303
304     rtg_initialized = 0;
305     printf("RTG thread shut down.\n");
306
307 shutdown_raylib:;
308
309     if (reinit)
310         goto reinit_raylib;
311
312     if (indexed_buf)
313         free(indexed_buf);
314
315     UnloadTexture(raylib_texture);
316     UnloadShader(clut_shader);
317
318     CloseWindow();
319
320     return args;
321 }
322
323 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
324     //palette[index] = xrgb;
325     unsigned char *src = (unsigned char *)&xrgb;
326     unsigned char *dst = (unsigned char *)&palette[index];
327     dst[0] = src[2];
328     dst[1] = src[1];
329     dst[2] = src[0];
330     dst[3] = 0xFF;
331 }
332
333 void rtg_init_display() {
334     int err;
335     rtg_on = 1;
336
337     if (!rtg_initialized) {
338         err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
339         if (err != 0) {
340             rtg_on = 0;
341             display_enabled = 0xFF;
342             printf("can't create RTG thread :[%s]", strerror(err));
343         }
344         else {
345             rtg_initialized = 1;
346             pthread_setname_np(thread_id, "pistorm: rtg");
347             printf("RTG Thread created successfully\n");
348         }
349     }
350     printf("RTG display enabled.\n");
351 }
352
353 void rtg_shutdown_display() {
354     printf("RTG display disabled.\n");
355     rtg_on = 0;
356     display_enabled = 0xFF;
357 }
358
359 void rtg_enable_mouse_cursor() {
360     mouse_cursor_enabled = 1;
361 }
362
363 void rtg_set_mouse_cursor_pos(int16_t x, int16_t y) {
364     mouse_cursor_x = x;
365     mouse_cursor_y = y;
366     //printf("Set mouse cursor pos to %d, %d.\n", x, y);
367 }
368
369 static uint8_t clut_cursor_data[256*256];
370
371 void update_mouse_cursor(uint8_t *src) {
372     if (src != NULL) {
373         memset(clut_cursor_data, 0x00, 256*256);
374         uint8_t cur_bit = 0x80;
375         uint8_t line_pitch = (mouse_cursor_w / 8) * 2;
376
377         for (uint8_t y = 0; y < mouse_cursor_h; y++) {
378             for (uint8_t x = 0; x < mouse_cursor_w; x++) {
379                 if (src[(x / 8) + (line_pitch * y)] & cur_bit)
380                     clut_cursor_data[x + (y * 256)] |= 0x01;
381                 if (src[(x / 8) + (line_pitch * y) + (mouse_cursor_w / 8)] & cur_bit)
382                     clut_cursor_data[x + (y * 256)] |= 0x02;
383                 cur_bit >>= 1;
384                 if (cur_bit == 0x00)
385                     cur_bit = 0x80;
386             }
387             cur_bit = 0x80;
388         }
389     }
390
391     for (int y = 0; y < mouse_cursor_h; y++) {
392         for (int x = 0; x < mouse_cursor_w; x++) {
393             cursor_data[x + (y * 256)] = cursor_palette[clut_cursor_data[x + (y * 256)]];
394         }
395     }
396
397     while (rtg_on && !updating_screen)
398         usleep(0);
399     cursor_image_updated = 1;
400 }
401
402 void rtg_set_cursor_clut_entry(uint8_t r, uint8_t g, uint8_t b, uint8_t idx) {
403     uint32_t color = 0;
404     unsigned char *dst = (unsigned char *)&color;
405
406     dst[0] = r;
407     dst[1] = g;
408     dst[2] = b;
409     dst[3] = 0xFF;
410     if (cursor_palette[idx + 1] != color) {
411         cursor_palette[0] = 0;
412         cursor_palette[idx + 1] = color;
413         update_mouse_cursor(NULL);
414     }
415 }
416
417 static uint8_t old_mouse_w, old_mouse_h;
418 static uint8_t old_mouse_data[256];
419
420 void rtg_set_mouse_cursor_image(uint8_t *src, uint8_t w, uint8_t h) {
421     uint8_t new_cursor_data = 0;
422
423     mouse_cursor_w = w;
424     mouse_cursor_h = h;
425
426     if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0)
427         new_cursor_data = 1;
428
429     if (old_mouse_w != w || old_mouse_h != h || new_cursor_data) {
430         old_mouse_w = w;
431         old_mouse_h = h;
432         update_mouse_cursor(src);
433     }
434 }
435
436 void rtg_show_fps(uint8_t enable) {
437     show_fps = (enable != 0);
438 }
439
440 void rtg_palette_debug(uint8_t enable) {
441     debug_palette = (enable != 0);
442 }