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