1 // SPDX-License-Identifier: MIT
3 #include "config_file/config_file.h"
7 #include "raylib/raylib.h"
16 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
18 //#define DEBUG_RAYLIB_RTG
20 #ifdef DEBUG_RAYLIB_RTG
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;
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;
36 uint32_t cur_rtg_frame = 0;
38 static pthread_t thread_id;
39 static uint8_t mouse_cursor_enabled = 0, cursor_image_updated = 0, updating_screen = 0, debug_palette = 0, show_fps = 0;
40 static uint8_t mouse_cursor_w = 16, mouse_cursor_h = 16;
41 static int16_t mouse_cursor_x = 0, mouse_cursor_y = 0;
43 struct rtg_shared_data {
44 uint16_t *width, *height;
45 uint16_t *format, *pitch;
46 uint16_t *offset_x, *offset_y;
52 struct rtg_shared_data rtg_share_data;
53 static uint32_t palette[256];
54 static uint32_t cursor_palette[256];
56 uint32_t cursor_data[256 * 256];
58 void rtg_update_screen() {}
60 uint32_t rtg_to_raylib[RTGFMT_NUM] = {
61 PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
62 PIXELFORMAT_UNCOMPRESSED_R5G6B5,
63 PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
64 PIXELFORMAT_UNCOMPRESSED_R5G5B5A1,
67 uint32_t rtg_pixel_size[RTGFMT_NUM] = {
74 void *rtgThread(void *args) {
76 printf("RTG thread running\n");
82 uint32_t *indexed_buf = NULL;
84 rtg_share_data.format = &rtg_display_format;
85 rtg_share_data.width = &rtg_display_width;
86 rtg_share_data.height = &rtg_display_height;
87 rtg_share_data.pitch = &rtg_pitch;
88 rtg_share_data.offset_x = &rtg_offset_x;
89 rtg_share_data.offset_y = &rtg_offset_y;
90 rtg_share_data.memory = rtg_mem;
91 rtg_share_data.running = &rtg_on;
92 rtg_share_data.addr = &framebuffer_addr_adj;
93 struct rtg_shared_data *data = &rtg_share_data;
95 uint16_t width = rtg_display_width;
96 uint16_t height = rtg_display_height;
97 uint16_t format = rtg_display_format;
98 uint16_t pitch = rtg_pitch;
100 Texture raylib_texture, raylib_cursor_texture;
101 Texture raylib_clut_texture;
102 Image raylib_fb, raylib_cursor, raylib_clut;
104 InitWindow(GetScreenWidth(), GetScreenHeight(), "Pistorm RTG");
108 Color bef = { 0, 64, 128, 255 };
109 float scale_x = 1.0f, scale_y = 1.0f;
111 Shader clut_shader = LoadShader(NULL, "platforms/amiga/rtg/clut.shader");
112 Shader swizzle_shader = LoadShader(NULL, "platforms/amiga/rtg/argbswizzle.shader");
113 int clut_loc = GetShaderLocation(clut_shader, "texture1");
115 raylib_clut.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
116 raylib_clut.width = 256;
117 raylib_clut.height = 1;
118 raylib_clut.mipmaps = 1;
119 raylib_clut.data = palette;
121 raylib_clut_texture = LoadTextureFromImage(raylib_clut);
123 raylib_cursor.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
124 raylib_cursor.width = 256;
125 raylib_cursor.height = 256;
126 raylib_cursor.mipmaps = 1;
127 raylib_cursor.data = cursor_data;
128 raylib_cursor_texture = LoadTextureFromImage(raylib_cursor);
130 Rectangle srcrect, dstscale;
135 printf("Reinitializing raylib...\n");
136 width = rtg_display_width;
137 height = rtg_display_height;
138 format = rtg_display_format;
144 UnloadTexture(raylib_texture);
148 printf("Creating %dx%d raylib window...\n", width, height);
150 printf("Setting up raylib framebuffer image.\n");
151 raylib_fb.format = rtg_to_raylib[format];
155 raylib_fb.width = width;
156 indexed_buf = calloc(1, width * height * 2);
159 raylib_fb.width = pitch / rtg_pixel_size[format];
162 raylib_fb.height = height;
163 raylib_fb.mipmaps = 1;
164 raylib_fb.data = &data->memory[*data->addr];
165 printf("Width: %d\nPitch: %d\nBPP: %d\n", raylib_fb.width, pitch, rtg_pixel_size[format]);
167 raylib_texture = LoadTextureFromImage(raylib_fb);
169 printf("Loaded framebuffer texture.\n");
171 srcrect.x = srcrect.y = 0;
172 srcrect.width = width;
173 srcrect.height = height;
174 dstscale.x = dstscale.y = 0;
175 dstscale.width = width;
176 dstscale.height = height;
178 if (dstscale.height * 2 <= GetScreenHeight()) {
180 if (GetScreenHeight() == 720) {
181 dstscale.width = 960;
182 dstscale.height = 720;
183 } else if (GetScreenHeight() == 1080) {
184 dstscale.width = 1440;
185 dstscale.height = 1080;
186 } else if (GetScreenHeight() == 1200) {
187 dstscale.width = 1600;
188 dstscale.height = 1200;
191 while (dstscale.height + height <= GetScreenHeight()) {
192 dstscale.height += height;
193 dstscale.width += width;
196 } else if (dstscale.width > GetScreenWidth() || dstscale.height > GetScreenHeight()) {
197 if (dstscale.height > GetScreenHeight()) {
199 DEBUG("Adjusted width from %d to", (int)dstscale.width);
200 dstscale.width = dstscale.width * ((float)GetScreenHeight() / (float)height);
201 DEBUG("%d.\n", (int)dstscale.width);
202 DEBUG("Adjusted height from %d to", (int)dstscale.height);
203 dstscale.height = GetScreenHeight();
204 DEBUG("%d.\n", (int)dstscale.height);
206 if (dstscale.width > GetScreenWidth()) {
207 // First scaling attempt failed, do not double adjust, re-adjust
208 dstscale.width = width;
209 dstscale.height = height;
211 DEBUG("Adjusted height from %d to", (int)dstscale.height);
212 dstscale.height = dstscale.height * ((float)GetScreenWidth() / (float)width);
213 DEBUG("%d.\n", (int)dstscale.height);
214 DEBUG("Adjusted width from %d to", (int)dstscale.width);
215 dstscale.width = GetScreenWidth();
216 DEBUG("%d.\n", (int)dstscale.width);
220 scale_x = dstscale.width / (float)width;
221 scale_y = dstscale.height / (float)height;
223 origin.x = (dstscale.width - GetScreenWidth()) * 0.5;
224 origin.y = (dstscale.height - GetScreenHeight()) * 0.5;
229 rtg_output_in_vblank = 0;
234 UpdateTexture(raylib_clut_texture, palette);
235 BeginShaderMode(clut_shader);
236 SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
239 BeginShaderMode(swizzle_shader);
243 DrawTexturePro(raylib_texture, srcrect, dstscale, origin, 0.0f, RAYWHITE);
252 if (mouse_cursor_enabled) {
253 float mc_x = mouse_cursor_x - rtg_offset_x;
254 float mc_y = mouse_cursor_y - rtg_offset_y;
255 Rectangle cursor_srcrect = { 0, 0, mouse_cursor_w, mouse_cursor_h };
256 Rectangle dstrect = { mc_x * scale_x, mc_y * scale_y, (float)mouse_cursor_w * scale_x, (float)mouse_cursor_h * scale_y };
257 DrawTexturePro(raylib_cursor_texture, cursor_srcrect, dstrect, origin, 0.0f, RAYWHITE);
261 if (format == RTGFMT_8BIT) {
262 Rectangle srcrect = { 0, 0, 256, 1 };
263 Rectangle dstrect = { 0, 0, 1024, 8 };
264 DrawTexturePro(raylib_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
269 DrawFPS(GetScreenWidth() - 128, 0);
273 rtg_output_in_vblank = 1;
275 if (format == RTGFMT_RBG565) {
276 for (int y = 0; y < height; y++) {
277 for (int x = 0; x < width; x++) {
278 ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
281 UpdateTexture(raylib_texture, indexed_buf);
284 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
286 if (cursor_image_updated) {
287 UpdateTexture(raylib_cursor_texture, cursor_data);
288 cursor_image_updated = 0;
293 ClearBackground(bef);
294 DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
297 if (pitch != *data->pitch || height != *data->height || width != *data->width || format != *data->format) {
298 printf("Reinitializing due to something change.\n");
300 goto shutdown_raylib;
302 if (emulator_exiting) {
303 goto shutdown_raylib;
308 printf("RTG thread shut down.\n");
318 UnloadTexture(raylib_texture);
319 UnloadShader(clut_shader);
326 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
327 //palette[index] = xrgb;
328 unsigned char *src = (unsigned char *)&xrgb;
329 unsigned char *dst = (unsigned char *)&palette[index];
336 void rtg_init_display() {
340 if (!rtg_initialized) {
341 err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
344 display_enabled = 0xFF;
345 printf("can't create RTG thread :[%s]", strerror(err));
349 pthread_setname_np(thread_id, "pistorm: rtg");
350 printf("RTG Thread created successfully\n");
353 printf("RTG display enabled.\n");
356 void rtg_shutdown_display() {
357 printf("RTG display disabled.\n");
359 display_enabled = 0xFF;
362 void rtg_enable_mouse_cursor() {
363 mouse_cursor_enabled = 1;
366 void rtg_set_mouse_cursor_pos(int16_t x, int16_t y) {
369 //printf("Set mouse cursor pos to %d, %d.\n", x, y);
372 static uint8_t clut_cursor_data[256*256];
374 void update_mouse_cursor(uint8_t *src) {
376 memset(clut_cursor_data, 0x00, 256*256);
377 uint8_t cur_bit = 0x80;
378 uint8_t line_pitch = (mouse_cursor_w / 8) * 2;
380 for (uint8_t y = 0; y < mouse_cursor_h; y++) {
381 for (uint8_t x = 0; x < mouse_cursor_w; x++) {
382 if (src[(x / 8) + (line_pitch * y)] & cur_bit)
383 clut_cursor_data[x + (y * 256)] |= 0x01;
384 if (src[(x / 8) + (line_pitch * y) + (mouse_cursor_w / 8)] & cur_bit)
385 clut_cursor_data[x + (y * 256)] |= 0x02;
394 for (int y = 0; y < mouse_cursor_h; y++) {
395 for (int x = 0; x < mouse_cursor_w; x++) {
396 cursor_data[x + (y * 256)] = cursor_palette[clut_cursor_data[x + (y * 256)]];
400 while (rtg_on && !updating_screen)
402 cursor_image_updated = 1;
405 void rtg_set_cursor_clut_entry(uint8_t r, uint8_t g, uint8_t b, uint8_t idx) {
407 unsigned char *dst = (unsigned char *)&color;
413 if (cursor_palette[idx + 1] != color) {
414 cursor_palette[0] = 0;
415 cursor_palette[idx + 1] = color;
416 update_mouse_cursor(NULL);
420 static uint8_t old_mouse_w, old_mouse_h;
421 static uint8_t old_mouse_data[256];
423 void rtg_set_mouse_cursor_image(uint8_t *src, uint8_t w, uint8_t h) {
424 uint8_t new_cursor_data = 0;
429 if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0)
432 if (old_mouse_w != w || old_mouse_h != h || new_cursor_data) {
435 update_mouse_cursor(src);
439 void rtg_show_fps(uint8_t enable) {
440 show_fps = (enable != 0);
443 void rtg_palette_debug(uint8_t enable) {
444 debug_palette = (enable != 0);