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;
27 extern uint8_t *rtg_mem;
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 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;
41 struct rtg_shared_data {
42 uint16_t *width, *height;
43 uint16_t *format, *pitch;
44 uint16_t *offset_x, *offset_y;
50 struct rtg_shared_data rtg_share_data;
51 static uint32_t palette[256];
52 static uint32_t cursor_palette[256];
54 uint32_t cursor_data[256 * 256];
56 void rtg_update_screen() {}
58 uint32_t rtg_to_raylib[RTGFMT_NUM] = {
59 PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
60 PIXELFORMAT_UNCOMPRESSED_R5G6B5,
61 PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
62 PIXELFORMAT_UNCOMPRESSED_R5G5B5A1,
65 uint32_t rtg_pixel_size[RTGFMT_NUM] = {
72 void *rtgThread(void *args) {
74 printf("RTG thread running\n");
80 uint32_t *indexed_buf = NULL;
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;
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;
98 Texture raylib_texture, raylib_cursor_texture;
99 Texture raylib_clut_texture;
100 Image raylib_fb, raylib_cursor, raylib_clut;
102 InitWindow(GetScreenWidth(), GetScreenHeight(), "Pistorm RTG");
106 Color bef = { 0, 64, 128, 255 };
107 float scale_x = 1.0f, scale_y = 1.0f;
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");
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;
119 raylib_clut_texture = LoadTextureFromImage(raylib_clut);
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);
128 Rectangle srcrect, dstscale;
133 printf("Reinitializing raylib...\n");
134 width = rtg_display_width;
135 height = rtg_display_height;
136 format = rtg_display_format;
142 UnloadTexture(raylib_texture);
146 printf("Creating %dx%d raylib window...\n", width, height);
148 printf("Setting up raylib framebuffer image.\n");
149 raylib_fb.format = rtg_to_raylib[format];
153 raylib_fb.width = width;
154 indexed_buf = calloc(1, width * height * 2);
157 raylib_fb.width = pitch / rtg_pixel_size[format];
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]);
165 raylib_texture = LoadTextureFromImage(raylib_fb);
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;
174 if (dstscale.height * 2 <= GetScreenHeight()) {
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;
184 while (dstscale.height + height <= GetScreenHeight()) {
185 dstscale.height += height;
186 dstscale.width += width;
189 } else if (dstscale.width > GetScreenWidth() || dstscale.height > GetScreenHeight()) {
190 if (dstscale.height > GetScreenHeight()) {
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);
199 if (dstscale.width > GetScreenWidth()) {
200 // First scaling attempt failed, do not double adjust, re-adjust
201 dstscale.width = width;
202 dstscale.height = height;
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);
213 scale_x = dstscale.width / (float)width;
214 scale_y = dstscale.height / (float)height;
216 origin.x = (dstscale.width - GetScreenWidth()) * 0.5;
217 origin.y = (dstscale.height - GetScreenHeight()) * 0.5;
226 UpdateTexture(raylib_clut_texture, palette);
227 BeginShaderMode(clut_shader);
228 SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
231 BeginShaderMode(swizzle_shader);
235 DrawTexturePro(raylib_texture, srcrect, dstscale, origin, 0.0f, RAYWHITE);
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);
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);
261 DrawFPS(GetScreenWidth() - 128, 0);
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))]);
271 UpdateTexture(raylib_texture, indexed_buf);
274 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
276 if (cursor_image_updated) {
277 UpdateTexture(raylib_cursor_texture, cursor_data);
278 cursor_image_updated = 0;
283 ClearBackground(bef);
284 DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
287 if (pitch != *data->pitch || height != *data->height || width != *data->width || format != *data->format) {
288 printf("Reinitializing due to something change.\n");
290 goto shutdown_raylib;
292 if (emulator_exiting) {
293 goto shutdown_raylib;
298 printf("RTG thread shut down.\n");
308 UnloadTexture(raylib_texture);
309 UnloadShader(clut_shader);
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];
326 void rtg_init_display() {
330 if (!rtg_initialized) {
331 err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
334 printf("can't create RTG thread :[%s]", strerror(err));
338 pthread_setname_np(thread_id, "pistorm: rtg");
339 printf("RTG Thread created successfully\n");
342 printf("RTG display enabled.\n");
345 void rtg_shutdown_display() {
346 printf("RTG display disabled.\n");
350 void rtg_enable_mouse_cursor() {
351 mouse_cursor_enabled = 1;
354 void rtg_set_mouse_cursor_pos(int16_t x, int16_t y) {
357 //printf("Set mouse cursor pos to %d, %d.\n", x, y);
360 static uint8_t clut_cursor_data[256*256];
362 void update_mouse_cursor(uint8_t *src) {
364 memset(clut_cursor_data, 0x00, 256*256);
365 uint8_t cur_bit = 0x80;
366 uint8_t line_pitch = (mouse_cursor_w / 8) * 2;
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;
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)]];
388 while (rtg_on && !updating_screen)
390 cursor_image_updated = 1;
393 void rtg_set_cursor_clut_entry(uint8_t r, uint8_t g, uint8_t b, uint8_t idx) {
395 unsigned char *dst = (unsigned char *)&color;
401 if (cursor_palette[idx + 1] != color) {
402 cursor_palette[0] = 0;
403 cursor_palette[idx + 1] = color;
404 update_mouse_cursor(NULL);
408 static uint8_t old_mouse_w, old_mouse_h;
409 static uint8_t old_mouse_data[256];
411 void rtg_set_mouse_cursor_image(uint8_t *src, uint8_t w, uint8_t h) {
412 uint8_t new_cursor_data = 0;
417 if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0)
420 if (old_mouse_w != w || old_mouse_h != h || new_cursor_data) {
423 update_mouse_cursor(src);
427 void rtg_show_fps(uint8_t enable) {
428 show_fps = (enable != 0);
431 void rtg_palette_debug(uint8_t enable) {
432 debug_palette = (enable != 0);