]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg-output-raylib.c
7b78368aac43d43f95660ed7503443fb9deed512
[pistorm] / platforms / amiga / rtg / rtg-output-raylib.c
1 // SPDX-License-Identifier: MIT
2
3 #include "emulator.h"
4 #include "rtg.h"
5
6 #include "raylib/raylib.h"
7
8 #include <pthread.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14
15 //#define DEBUG_RAYLIB_RTG
16
17 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
18
19 uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0;
20 extern uint8_t *rtg_mem;
21 extern uint32_t framebuffer_addr;
22 extern uint32_t framebuffer_addr_adj;
23
24 extern uint16_t rtg_display_width, rtg_display_height;
25 extern uint16_t rtg_display_format;
26 extern uint16_t rtg_pitch, rtg_total_rows;
27 extern uint16_t rtg_offset_x, rtg_offset_y;
28
29 static pthread_t thread_id;
30
31 struct rtg_shared_data {
32     uint16_t *width, *height;
33     uint16_t *format, *pitch;
34     uint16_t *offset_x, *offset_y;
35     uint8_t *memory;
36     uint32_t *addr;
37     uint8_t *running;
38 };
39
40 struct rtg_shared_data rtg_share_data;
41 static uint32_t palette[256];
42
43 void rtg_update_screen() {}
44
45 uint32_t rtg_to_raylib[RTGFMT_NUM] = {
46     PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
47     PIXELFORMAT_UNCOMPRESSED_R5G6B5,
48     PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
49     PIXELFORMAT_UNCOMPRESSED_R5G5B5A1,
50 };
51
52 void *rtgThread(void *args) {
53
54     printf("RTG thread running\n");
55     fflush(stdout);
56
57     int reinit = 0;
58     rtg_on = 1;
59
60     uint32_t *indexed_buf = NULL;
61
62     rtg_share_data.format = &rtg_display_format;
63     rtg_share_data.width = &rtg_display_width;
64     rtg_share_data.height = &rtg_display_height;
65     rtg_share_data.pitch = &rtg_pitch;
66     rtg_share_data.offset_x = &rtg_offset_x;
67     rtg_share_data.offset_y = &rtg_offset_y;
68     rtg_share_data.memory = rtg_mem;
69     rtg_share_data.running = &rtg_on;
70     rtg_share_data.addr = &framebuffer_addr_adj;
71     struct rtg_shared_data *data = &rtg_share_data;
72
73     uint16_t width = rtg_display_width;
74     uint16_t height = rtg_display_height;
75     uint16_t format = rtg_display_format;
76     uint16_t pitch = rtg_pitch;
77
78     Texture raylib_texture;
79     Texture raylib_clut_texture;
80     Image raylib_fb, raylib_clut;
81
82     InitWindow(GetScreenWidth(), GetScreenHeight(), "Pistorm RTG");
83     HideCursor();
84     SetTargetFPS(60);
85
86         Color bef;
87         bef.r = 0;
88         bef.g = 64;
89         bef.b = 128;
90         bef.a = 255;
91
92     Shader clut_shader = LoadShader(NULL, "platforms/amiga/rtg/clut.shader");
93     Shader swizzle_shader = LoadShader(NULL, "platforms/amiga/rtg/argbswizzle.shader");
94     int clut_loc = GetShaderLocation(clut_shader, "texture1");
95
96     raylib_clut.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
97     raylib_clut.width = 256;
98     raylib_clut.height = 1;
99     raylib_clut.mipmaps = 1;
100     raylib_clut.data = palette;
101
102     raylib_clut_texture = LoadTextureFromImage(raylib_clut);
103
104     Rectangle srchax, dsthax;
105     Vector2 originhax;
106
107 reinit_raylib:;
108     if (reinit) {
109         printf("Reinitializing raylib...\n");
110         width = rtg_display_width;
111         height = rtg_display_height;
112         format = rtg_display_format;
113         pitch = rtg_pitch;
114         if (indexed_buf) {
115             free(indexed_buf);
116             indexed_buf = NULL;
117         }
118         UnloadTexture(raylib_texture);
119         reinit = 0;
120     }
121
122     printf("Creating %dx%d raylib window...\n", width, height);
123
124     printf("Setting up raylib framebuffer image.\n");
125     raylib_fb.format = rtg_to_raylib[format];
126     raylib_fb.width = width;
127     raylib_fb.height = height;
128         raylib_fb.mipmaps = 1;
129         raylib_fb.data = &data->memory[*data->addr];
130
131     raylib_texture = LoadTextureFromImage(raylib_fb);
132
133     switch (format) {
134         case RTGFMT_RBG565:
135             indexed_buf = calloc(1, width * height * 2);
136             break;
137         default:
138             break;
139     }
140
141     srchax.x = srchax.y = 0;
142     srchax.width = width;
143     srchax.height = height;
144     dsthax.x = dsthax.y = 0;
145     if (GetScreenHeight() == 720) {
146         dsthax.width = 960;
147         dsthax.height = 720;
148     } else if (GetScreenHeight() == 1080) {
149         dsthax.width = 1440;
150         dsthax.height = 1080;
151     }
152     originhax.x = 0.0f;
153     originhax.y = 0.0f;
154
155     while (1) {
156         if (rtg_on) {
157             BeginDrawing();
158
159             switch (format) {
160                 case RTGFMT_8BIT:
161                     UpdateTexture(raylib_clut_texture, palette);
162                     BeginShaderMode(clut_shader);
163                     SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
164                     break;
165                 case RTGFMT_RGB32:
166                     BeginShaderMode(swizzle_shader);
167                     break;
168             }
169             
170             if (width == 320) {
171                 DrawTexturePro(raylib_texture, srchax, dsthax, originhax, 0.0f, RAYWHITE);
172             }
173             else {
174                 DrawTexture(raylib_texture, 0, 0, RAYWHITE);
175             }
176
177             switch (format) {
178                 case RTGFMT_8BIT:
179                 case RTGFMT_RGB32:
180                     EndShaderMode();
181                     break;
182             }
183 #ifdef DEBUG_RAYLIB_RTG
184             DrawTexture(raylib_clut_texture, 0, 0, RAYWHITE);
185 #endif
186
187             DrawFPS(width - 200, 0);
188             EndDrawing();
189             if (format == RTGFMT_RBG565) {
190                 for (int y = 0; y < height; y++) {
191                     for (int x = 0; x < width; x++) {
192                         ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
193                     }
194                 }
195                 UpdateTexture(raylib_texture, indexed_buf);
196             }
197             else {
198                 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
199             }
200         } else {
201             BeginDrawing();
202             ClearBackground(bef);
203             DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
204             EndDrawing();
205         }
206         if (height != *data->height || width != *data->width || format != *data->format) {
207             printf("Reinitializing due to something change.\n");
208             reinit = 1;
209             goto shutdown_raylib;
210         }
211         /*if (!rtg_on) {
212             goto shutdown_raylib;
213         }*/
214     }
215
216     rtg_initialized = 0;
217     printf("RTG thread shut down.\n");
218
219 shutdown_raylib:;
220
221     if (reinit)
222         goto reinit_raylib;
223
224     if (indexed_buf)
225         free(indexed_buf);
226
227     UnloadTexture(raylib_texture);
228     UnloadShader(clut_shader);
229
230     CloseWindow();
231
232     return args;
233 }
234
235 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
236     //palette[index] = xrgb;
237     unsigned char *src = (unsigned char *)&xrgb;
238     unsigned char *dst = (unsigned char *)&palette[index];
239     dst[0] = src[2];
240     dst[1] = src[1];
241     dst[2] = src[0];
242     dst[3] = 0xFF;
243 }
244
245 void rtg_init_display() {
246     int err;
247     rtg_on = 1;
248
249     if (!rtg_initialized) {
250         err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
251         if (err != 0) {
252             rtg_on = 0;
253             printf("can't create RTG thread :[%s]", strerror(err));
254         }
255         else {
256             rtg_initialized = 1;
257             pthread_setname_np(thread_id, "pistorm: rtg");
258             printf("RTG Thread created successfully\n");
259         }
260     }
261     printf("RTG display enabled.\n");
262 }
263
264 void rtg_shutdown_display() {
265     printf("RTG display disabled.\n");
266     rtg_on = 0;
267 }