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 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 printf("Loaded framebuffer texture.\n");
169 srcrect.x = srcrect.y = 0;
170 srcrect.width = width;
171 srcrect.height = height;
172 dstscale.x = dstscale.y = 0;
173 dstscale.width = width;
174 dstscale.height = height;
176 if (dstscale.height * 2 <= GetScreenHeight()) {
178 if (GetScreenHeight() == 720) {
179 dstscale.width = 960;
180 dstscale.height = 720;
181 } else if (GetScreenHeight() == 1080) {
182 dstscale.width = 1440;
183 dstscale.height = 1080;
184 } else if (GetScreenHeight() == 1200) {
185 dstscale.width = 1600;
186 dstscale.height = 1200;
189 while (dstscale.height + height <= GetScreenHeight()) {
190 dstscale.height += height;
191 dstscale.width += width;
194 } else if (dstscale.width > GetScreenWidth() || dstscale.height > GetScreenHeight()) {
195 if (dstscale.height > GetScreenHeight()) {
197 DEBUG("Adjusted width from %d to", (int)dstscale.width);
198 dstscale.width = dstscale.width * ((float)GetScreenHeight() / (float)height);
199 DEBUG("%d.\n", (int)dstscale.width);
200 DEBUG("Adjusted height from %d to", (int)dstscale.height);
201 dstscale.height = GetScreenHeight();
202 DEBUG("%d.\n", (int)dstscale.height);
204 if (dstscale.width > GetScreenWidth()) {
205 // First scaling attempt failed, do not double adjust, re-adjust
206 dstscale.width = width;
207 dstscale.height = height;
209 DEBUG("Adjusted height from %d to", (int)dstscale.height);
210 dstscale.height = dstscale.height * ((float)GetScreenWidth() / (float)width);
211 DEBUG("%d.\n", (int)dstscale.height);
212 DEBUG("Adjusted width from %d to", (int)dstscale.width);
213 dstscale.width = GetScreenWidth();
214 DEBUG("%d.\n", (int)dstscale.width);
218 scale_x = dstscale.width / (float)width;
219 scale_y = dstscale.height / (float)height;
221 origin.x = (dstscale.width - GetScreenWidth()) * 0.5;
222 origin.y = (dstscale.height - GetScreenHeight()) * 0.5;
227 rtg_output_in_vblank = 0;
232 UpdateTexture(raylib_clut_texture, palette);
233 BeginShaderMode(clut_shader);
234 SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
237 BeginShaderMode(swizzle_shader);
241 DrawTexturePro(raylib_texture, srcrect, dstscale, origin, 0.0f, RAYWHITE);
250 if (mouse_cursor_enabled) {
251 float mc_x = mouse_cursor_x - rtg_offset_x;
252 float mc_y = mouse_cursor_y - rtg_offset_y;
253 Rectangle cursor_srcrect = { 0, 0, mouse_cursor_w, mouse_cursor_h };
254 Rectangle dstrect = { mc_x * scale_x, mc_y * scale_y, (float)mouse_cursor_w * scale_x, (float)mouse_cursor_h * scale_y };
255 DrawTexturePro(raylib_cursor_texture, cursor_srcrect, dstrect, origin, 0.0f, RAYWHITE);
259 if (format == RTGFMT_8BIT) {
260 Rectangle srcrect = { 0, 0, 256, 1 };
261 Rectangle dstrect = { 0, 0, 1024, 8 };
262 DrawTexturePro(raylib_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
267 DrawFPS(GetScreenWidth() - 128, 0);
271 rtg_output_in_vblank = 1;
272 if (format == RTGFMT_RBG565) {
273 for (int y = 0; y < height; y++) {
274 for (int x = 0; x < width; x++) {
275 ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
278 UpdateTexture(raylib_texture, indexed_buf);
281 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
283 if (cursor_image_updated) {
284 UpdateTexture(raylib_cursor_texture, cursor_data);
285 cursor_image_updated = 0;
290 ClearBackground(bef);
291 DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
294 if (pitch != *data->pitch || height != *data->height || width != *data->width || format != *data->format) {
295 printf("Reinitializing due to something change.\n");
297 goto shutdown_raylib;
299 if (emulator_exiting) {
300 goto shutdown_raylib;
305 printf("RTG thread shut down.\n");
315 UnloadTexture(raylib_texture);
316 UnloadShader(clut_shader);
323 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
324 //palette[index] = xrgb;
325 unsigned char *src = (unsigned char *)&xrgb;
326 unsigned char *dst = (unsigned char *)&palette[index];
333 void rtg_init_display() {
337 if (!rtg_initialized) {
338 err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
341 display_enabled = 0xFF;
342 printf("can't create RTG thread :[%s]", strerror(err));
346 pthread_setname_np(thread_id, "pistorm: rtg");
347 printf("RTG Thread created successfully\n");
350 printf("RTG display enabled.\n");
353 void rtg_shutdown_display() {
354 printf("RTG display disabled.\n");
356 display_enabled = 0xFF;
359 void rtg_enable_mouse_cursor() {
360 mouse_cursor_enabled = 1;
363 void rtg_set_mouse_cursor_pos(int16_t x, int16_t y) {
366 //printf("Set mouse cursor pos to %d, %d.\n", x, y);
369 static uint8_t clut_cursor_data[256*256];
371 void update_mouse_cursor(uint8_t *src) {
373 memset(clut_cursor_data, 0x00, 256*256);
374 uint8_t cur_bit = 0x80;
375 uint8_t line_pitch = (mouse_cursor_w / 8) * 2;
377 for (uint8_t y = 0; y < mouse_cursor_h; y++) {
378 for (uint8_t x = 0; x < mouse_cursor_w; x++) {
379 if (src[(x / 8) + (line_pitch * y)] & cur_bit)
380 clut_cursor_data[x + (y * 256)] |= 0x01;
381 if (src[(x / 8) + (line_pitch * y) + (mouse_cursor_w / 8)] & cur_bit)
382 clut_cursor_data[x + (y * 256)] |= 0x02;
391 for (int y = 0; y < mouse_cursor_h; y++) {
392 for (int x = 0; x < mouse_cursor_w; x++) {
393 cursor_data[x + (y * 256)] = cursor_palette[clut_cursor_data[x + (y * 256)]];
397 while (rtg_on && !updating_screen)
399 cursor_image_updated = 1;
402 void rtg_set_cursor_clut_entry(uint8_t r, uint8_t g, uint8_t b, uint8_t idx) {
404 unsigned char *dst = (unsigned char *)&color;
410 if (cursor_palette[idx + 1] != color) {
411 cursor_palette[0] = 0;
412 cursor_palette[idx + 1] = color;
413 update_mouse_cursor(NULL);
417 static uint8_t old_mouse_w, old_mouse_h;
418 static uint8_t old_mouse_data[256];
420 void rtg_set_mouse_cursor_image(uint8_t *src, uint8_t w, uint8_t h) {
421 uint8_t new_cursor_data = 0;
426 if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0)
429 if (old_mouse_w != w || old_mouse_h != h || new_cursor_data) {
432 update_mouse_cursor(src);
436 void rtg_show_fps(uint8_t enable) {
437 show_fps = (enable != 0);
440 void rtg_palette_debug(uint8_t enable) {
441 debug_palette = (enable != 0);