]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg-output-raylib.c
b75fedcb01f5372253392c802dc9d93956d68b2c
[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;
27 extern uint8_t *rtg_mem;
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     srcrect.x = srcrect.y = 0;
168     srcrect.width = width;
169     srcrect.height = height;
170     dstscale.x = dstscale.y = 0;
171     dstscale.width = width;
172     dstscale.height = height;
173
174     if (dstscale.height * 2 <= GetScreenHeight()) {
175         if (width == 320) {
176             if (GetScreenHeight() == 720) {
177                 dstscale.width = 960;
178                 dstscale.height = 720;
179             } else if (GetScreenHeight() == 1080) {
180                 dstscale.width = 1440;
181                 dstscale.height = 1080;
182             }
183         } else {
184             while (dstscale.height + height <= GetScreenHeight()) {
185                 dstscale.height += height;
186                 dstscale.width += width;
187             }
188         }
189     } else if (dstscale.width > GetScreenWidth() || dstscale.height > GetScreenHeight()) {
190         if (dstscale.height > GetScreenHeight()) {
191             DEBUG("[H > SH]\n");
192             DEBUG("Adjusted width from %d to", (int)dstscale.width);
193             dstscale.width = dstscale.width * ((float)GetScreenHeight() / (float)height);
194             DEBUG("%d.\n", (int)dstscale.width);
195             DEBUG("Adjusted height from %d to", (int)dstscale.height);
196             dstscale.height = GetScreenHeight();
197             DEBUG("%d.\n", (int)dstscale.height);
198         }
199         if (dstscale.width > GetScreenWidth()) {
200             // First scaling attempt failed, do not double adjust, re-adjust
201             dstscale.width = width;
202             dstscale.height = height;
203             DEBUG("[W > SW]\n");
204             DEBUG("Adjusted height from %d to", (int)dstscale.height);
205             dstscale.height = dstscale.height * ((float)GetScreenWidth() / (float)width);
206             DEBUG("%d.\n", (int)dstscale.height);
207             DEBUG("Adjusted width from %d to", (int)dstscale.width);
208             dstscale.width = GetScreenWidth();
209             DEBUG("%d.\n", (int)dstscale.width);
210         }
211     }
212
213     scale_x = dstscale.width / (float)width;
214     scale_y = dstscale.height / (float)height;
215
216     origin.x = (dstscale.width - GetScreenWidth()) * 0.5;
217     origin.y = (dstscale.height - GetScreenHeight()) * 0.5;
218
219     while (1) {
220         if (rtg_on) {
221             BeginDrawing();
222             updating_screen = 1;
223
224             switch (format) {
225                 case RTGFMT_8BIT:
226                     UpdateTexture(raylib_clut_texture, palette);
227                     BeginShaderMode(clut_shader);
228                     SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
229                     break;
230                 case RTGFMT_RGB32:
231                     BeginShaderMode(swizzle_shader);
232                     break;
233             }
234             
235             DrawTexturePro(raylib_texture, srcrect, dstscale, origin, 0.0f, RAYWHITE);
236
237             switch (format) {
238                 case RTGFMT_8BIT:
239                 case RTGFMT_RGB32:
240                     EndShaderMode();
241                     break;
242             }
243
244             if (mouse_cursor_enabled) {
245                 float mc_x = mouse_cursor_x - rtg_offset_x;
246                 float mc_y = mouse_cursor_y - rtg_offset_y;
247                 Rectangle cursor_srcrect = { 0, 0, mouse_cursor_w, mouse_cursor_h };
248                 Rectangle dstrect = { mc_x * scale_x, mc_y * scale_y, (float)mouse_cursor_w * scale_x, (float)mouse_cursor_h * scale_y };
249                 DrawTexturePro(raylib_cursor_texture, cursor_srcrect, dstrect, origin, 0.0f, RAYWHITE);
250             }
251
252             if (debug_palette) {
253                 if (format == RTGFMT_8BIT) {
254                     Rectangle srcrect = { 0, 0, 256, 1 };
255                     Rectangle dstrect = { 0, 0, 1024, 8 };
256                     DrawTexturePro(raylib_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
257                 }
258             }
259
260             if (show_fps) {
261                 DrawFPS(GetScreenWidth() - 128, 0);
262             }
263
264             EndDrawing();
265             if (format == RTGFMT_RBG565) {
266                 for (int y = 0; y < height; y++) {
267                     for (int x = 0; x < width; x++) {
268                         ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
269                     }
270                 }
271                 UpdateTexture(raylib_texture, indexed_buf);
272             }
273             else {
274                 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
275             }
276             if (cursor_image_updated) {
277                 UpdateTexture(raylib_cursor_texture, cursor_data);
278                 cursor_image_updated = 0;
279             }
280             updating_screen = 0;
281         } else {
282             BeginDrawing();
283             ClearBackground(bef);
284             DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
285             EndDrawing();
286         }
287         if (pitch != *data->pitch || height != *data->height || width != *data->width || format != *data->format) {
288             printf("Reinitializing due to something change.\n");
289             reinit = 1;
290             goto shutdown_raylib;
291         }
292         if (emulator_exiting) {
293             goto shutdown_raylib;
294         }
295     }
296
297     rtg_initialized = 0;
298     printf("RTG thread shut down.\n");
299
300 shutdown_raylib:;
301
302     if (reinit)
303         goto reinit_raylib;
304
305     if (indexed_buf)
306         free(indexed_buf);
307
308     UnloadTexture(raylib_texture);
309     UnloadShader(clut_shader);
310
311     CloseWindow();
312
313     return args;
314 }
315
316 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
317     //palette[index] = xrgb;
318     unsigned char *src = (unsigned char *)&xrgb;
319     unsigned char *dst = (unsigned char *)&palette[index];
320     dst[0] = src[2];
321     dst[1] = src[1];
322     dst[2] = src[0];
323     dst[3] = 0xFF;
324 }
325
326 void rtg_init_display() {
327     int err;
328     rtg_on = 1;
329
330     if (!rtg_initialized) {
331         err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
332         if (err != 0) {
333             rtg_on = 0;
334             printf("can't create RTG thread :[%s]", strerror(err));
335         }
336         else {
337             rtg_initialized = 1;
338             pthread_setname_np(thread_id, "pistorm: rtg");
339             printf("RTG Thread created successfully\n");
340         }
341     }
342     printf("RTG display enabled.\n");
343 }
344
345 void rtg_shutdown_display() {
346     printf("RTG display disabled.\n");
347     rtg_on = 0;
348 }
349
350 void rtg_enable_mouse_cursor() {
351     mouse_cursor_enabled = 1;
352 }
353
354 void rtg_set_mouse_cursor_pos(int16_t x, int16_t y) {
355     mouse_cursor_x = x;
356     mouse_cursor_y = y;
357     //printf("Set mouse cursor pos to %d, %d.\n", x, y);
358 }
359
360 static uint8_t clut_cursor_data[256*256];
361
362 void update_mouse_cursor(uint8_t *src) {
363     if (src != NULL) {
364         memset(clut_cursor_data, 0x00, 256*256);
365         uint8_t cur_bit = 0x80;
366         uint8_t line_pitch = (mouse_cursor_w / 8) * 2;
367
368         for (uint8_t y = 0; y < mouse_cursor_h; y++) {
369             for (uint8_t x = 0; x < mouse_cursor_w; x++) {
370                 if (src[(x / 8) + (line_pitch * y)] & cur_bit)
371                     clut_cursor_data[x + (y * 256)] |= 0x01;
372                 if (src[(x / 8) + (line_pitch * y) + (mouse_cursor_w / 8)] & cur_bit)
373                     clut_cursor_data[x + (y * 256)] |= 0x02;
374                 cur_bit >>= 1;
375                 if (cur_bit == 0x00)
376                     cur_bit = 0x80;
377             }
378             cur_bit = 0x80;
379         }
380     }
381
382     for (int y = 0; y < mouse_cursor_h; y++) {
383         for (int x = 0; x < mouse_cursor_w; x++) {
384             cursor_data[x + (y * 256)] = cursor_palette[clut_cursor_data[x + (y * 256)]];
385         }
386     }
387
388     while (rtg_on && !updating_screen)
389         usleep(0);
390     cursor_image_updated = 1;
391 }
392
393 void rtg_set_cursor_clut_entry(uint8_t r, uint8_t g, uint8_t b, uint8_t idx) {
394     uint32_t color = 0;
395     unsigned char *dst = (unsigned char *)&color;
396
397     dst[0] = r;
398     dst[1] = g;
399     dst[2] = b;
400     dst[3] = 0xFF;
401     if (cursor_palette[idx + 1] != color) {
402         cursor_palette[0] = 0;
403         cursor_palette[idx + 1] = color;
404         update_mouse_cursor(NULL);
405     }
406 }
407
408 static uint8_t old_mouse_w, old_mouse_h;
409 static uint8_t old_mouse_data[256];
410
411 void rtg_set_mouse_cursor_image(uint8_t *src, uint8_t w, uint8_t h) {
412     uint8_t new_cursor_data = 0;
413
414     mouse_cursor_w = w;
415     mouse_cursor_h = h;
416
417     if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0)
418         new_cursor_data = 1;
419
420     if (old_mouse_w != w || old_mouse_h != h || new_cursor_data) {
421         old_mouse_w = w;
422         old_mouse_h = h;
423         update_mouse_cursor(src);
424     }
425 }
426
427 void rtg_show_fps(uint8_t enable) {
428     show_fps = (enable != 0);
429 }
430
431 void rtg_palette_debug(uint8_t enable) {
432     debug_palette = (enable != 0);
433 }