1 // SPDX-License-Identifier: MIT
3 #include "config_file/config_file.h"
7 #include "raylib/raylib.h"
16 //#define DEBUG_RAYLIB_RTG
18 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
20 uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0;
21 extern uint8_t *rtg_mem;
22 extern uint32_t framebuffer_addr;
23 extern uint32_t framebuffer_addr_adj;
25 extern uint16_t rtg_display_width, rtg_display_height;
26 extern uint16_t rtg_display_format;
27 extern uint16_t rtg_pitch, rtg_total_rows;
28 extern uint16_t rtg_offset_x, rtg_offset_y;
30 static pthread_t thread_id;
31 static uint8_t mouse_cursor_enabled = 0, cursor_image_updated = 0, updating_screen = 0;
32 static uint8_t mouse_cursor_w = 16, mouse_cursor_h = 16;
33 static int16_t mouse_cursor_x = 0, mouse_cursor_y = 0;
35 struct rtg_shared_data {
36 uint16_t *width, *height;
37 uint16_t *format, *pitch;
38 uint16_t *offset_x, *offset_y;
44 struct rtg_shared_data rtg_share_data;
45 static uint32_t palette[256];
46 static uint32_t cursor_palette[256];
48 uint32_t cursor_data[256 * 256];
50 void rtg_update_screen() {}
52 uint32_t rtg_to_raylib[RTGFMT_NUM] = {
53 PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
54 PIXELFORMAT_UNCOMPRESSED_R5G6B5,
55 PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
56 PIXELFORMAT_UNCOMPRESSED_R5G5B5A1,
59 uint32_t rtg_pixel_size[RTGFMT_NUM] = {
66 void *rtgThread(void *args) {
68 printf("RTG thread running\n");
74 uint32_t *indexed_buf = NULL;
76 rtg_share_data.format = &rtg_display_format;
77 rtg_share_data.width = &rtg_display_width;
78 rtg_share_data.height = &rtg_display_height;
79 rtg_share_data.pitch = &rtg_pitch;
80 rtg_share_data.offset_x = &rtg_offset_x;
81 rtg_share_data.offset_y = &rtg_offset_y;
82 rtg_share_data.memory = rtg_mem;
83 rtg_share_data.running = &rtg_on;
84 rtg_share_data.addr = &framebuffer_addr_adj;
85 struct rtg_shared_data *data = &rtg_share_data;
87 uint16_t width = rtg_display_width;
88 uint16_t height = rtg_display_height;
89 uint16_t format = rtg_display_format;
90 uint16_t pitch = rtg_pitch;
92 Texture raylib_texture, raylib_cursor_texture;
93 Texture raylib_clut_texture;
94 Image raylib_fb, raylib_cursor, raylib_clut;
96 InitWindow(GetScreenWidth(), GetScreenHeight(), "Pistorm RTG");
100 Color bef = { 0, 64, 128, 255 };
101 float scale_x = 1.0f, scale_y = 1.0f;
103 Shader clut_shader = LoadShader(NULL, "platforms/amiga/rtg/clut.shader");
104 Shader swizzle_shader = LoadShader(NULL, "platforms/amiga/rtg/argbswizzle.shader");
105 int clut_loc = GetShaderLocation(clut_shader, "texture1");
107 raylib_clut.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
108 raylib_clut.width = 256;
109 raylib_clut.height = 1;
110 raylib_clut.mipmaps = 1;
111 raylib_clut.data = palette;
113 raylib_clut_texture = LoadTextureFromImage(raylib_clut);
115 raylib_cursor.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
116 raylib_cursor.width = 256;
117 raylib_cursor.height = 256;
118 raylib_cursor.mipmaps = 1;
119 raylib_cursor.data = cursor_data;
120 raylib_cursor_texture = LoadTextureFromImage(raylib_cursor);
122 Rectangle srcrect, dstscale;
127 printf("Reinitializing raylib...\n");
128 width = rtg_display_width;
129 height = rtg_display_height;
130 format = rtg_display_format;
136 UnloadTexture(raylib_texture);
140 printf("Creating %dx%d raylib window...\n", width, height);
142 printf("Setting up raylib framebuffer image.\n");
143 raylib_fb.format = rtg_to_raylib[format];
147 raylib_fb.width = width;
148 indexed_buf = calloc(1, width * height * 2);
151 raylib_fb.width = pitch / rtg_pixel_size[format];
154 raylib_fb.height = height;
155 raylib_fb.mipmaps = 1;
156 raylib_fb.data = &data->memory[*data->addr];
157 printf("Width: %d\nPitch: %d\nBPP: %d\n", raylib_fb.width, pitch, rtg_pixel_size[format]);
159 raylib_texture = LoadTextureFromImage(raylib_fb);
161 srcrect.x = srcrect.y = 0;
162 srcrect.width = width;
163 srcrect.height = height;
164 dstscale.x = dstscale.y = 0;
165 dstscale.width = width;
166 dstscale.height = height;
172 if (dstscale.height * 2 < GetScreenHeight()) {
174 if (GetScreenHeight() == 720) {
175 dstscale.width = 960;
176 dstscale.height = 720;
177 } else if (GetScreenHeight() == 1080) {
178 dstscale.width = 1440;
179 dstscale.height = 1080;
182 while (dstscale.height < GetScreenHeight()) {
183 dstscale.height += height;
184 dstscale.width += width;
187 scale_x = dstscale.width / (float)width;
188 scale_y = dstscale.height / (float)height;
198 UpdateTexture(raylib_clut_texture, palette);
199 BeginShaderMode(clut_shader);
200 SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
203 BeginShaderMode(swizzle_shader);
207 DrawTexturePro(raylib_texture, srcrect, dstscale, origin, 0.0f, RAYWHITE);
216 if (mouse_cursor_enabled) {
217 float mc_x = mouse_cursor_x - rtg_offset_x;
218 float mc_y = mouse_cursor_y - rtg_offset_y;
219 Rectangle cursor_srcrect = { 0, 0, mouse_cursor_w, mouse_cursor_h };
220 Rectangle dstrect = { mc_x * scale_x, mc_y * scale_y, (float)mouse_cursor_w * scale_x, (float)mouse_cursor_h * scale_y };
221 DrawTexturePro(raylib_cursor_texture, cursor_srcrect, dstrect, origin, 0.0f, RAYWHITE);
224 #ifdef DEBUG_RAYLIB_RTG
225 if (format == RTGFMT_8BIT) {
226 Rectangle srcrect = { 0, 0, 256, 1 };
227 Rectangle dstrect = { 0, 0, 1024, 8 };
228 //DrawTexture(raylib_clut_texture, 0, 0, RAYWHITE);
229 DrawTexturePro(raylib_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
231 DrawTexturePro(raylib_cursor_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
235 DrawFPS(width - 200, 0);
237 if (format == RTGFMT_RBG565) {
238 for (int y = 0; y < height; y++) {
239 for (int x = 0; x < width; x++) {
240 ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
243 UpdateTexture(raylib_texture, indexed_buf);
246 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
248 if (cursor_image_updated) {
249 UpdateTexture(raylib_cursor_texture, cursor_data);
250 cursor_image_updated = 0;
255 ClearBackground(bef);
256 DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
259 if (pitch != *data->pitch || height != *data->height || width != *data->width || format != *data->format) {
260 printf("Reinitializing due to something change.\n");
262 goto shutdown_raylib;
265 goto shutdown_raylib;
270 printf("RTG thread shut down.\n");
280 UnloadTexture(raylib_texture);
281 UnloadShader(clut_shader);
288 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
289 //palette[index] = xrgb;
290 unsigned char *src = (unsigned char *)&xrgb;
291 unsigned char *dst = (unsigned char *)&palette[index];
298 void rtg_init_display() {
302 if (!rtg_initialized) {
303 err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
306 printf("can't create RTG thread :[%s]", strerror(err));
310 pthread_setname_np(thread_id, "pistorm: rtg");
311 printf("RTG Thread created successfully\n");
314 printf("RTG display enabled.\n");
317 void rtg_shutdown_display() {
318 printf("RTG display disabled.\n");
322 void rtg_enable_mouse_cursor() {
323 mouse_cursor_enabled = 1;
326 void rtg_set_mouse_cursor_pos(int16_t x, int16_t y) {
329 //printf("Set mouse cursor pos to %d, %d.\n", x, y);
332 static uint8_t clut_cursor_data[256*256];
334 void update_mouse_cursor(uint8_t *src) {
336 memset(clut_cursor_data, 0x00, 256*256);
337 uint8_t cur_bit = 0x80;
338 uint8_t line_pitch = (mouse_cursor_w / 8) * 2;
340 for (uint8_t y = 0; y < mouse_cursor_h; y++) {
341 for (uint8_t x = 0; x < mouse_cursor_w; x++) {
342 if (src[(x / 8) + (line_pitch * y)] & cur_bit)
343 clut_cursor_data[x + (y * 256)] |= 0x01;
344 if (src[(x / 8) + (line_pitch * y) + (mouse_cursor_w / 8)] & cur_bit)
345 clut_cursor_data[x + (y * 256)] |= 0x02;
354 for (int y = 0; y < mouse_cursor_h; y++) {
355 for (int x = 0; x < mouse_cursor_w; x++) {
356 cursor_data[x + (y * 256)] = cursor_palette[clut_cursor_data[x + (y * 256)]];
360 printf ("Updated mouse cursor data.\n");
362 while (rtg_on && !updating_screen)
364 cursor_image_updated = 1;
367 void rtg_set_cursor_clut_entry(uint8_t r, uint8_t g, uint8_t b, uint8_t idx) {
369 unsigned char *dst = (unsigned char *)&color;
375 if (cursor_palette[idx + 1] != color) {
376 cursor_palette[0] = 0;
377 cursor_palette[idx + 1] = color;
378 update_mouse_cursor(NULL);
382 static uint8_t old_mouse_w, old_mouse_h;
383 static uint8_t old_mouse_data[256];
385 void rtg_set_mouse_cursor_image(uint8_t *src, uint8_t w, uint8_t h) {
386 uint8_t new_cursor_data = 0;
391 if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0) {
392 printf("New cursor data.\n");
395 printf("No new cursor data.\n");
398 if (old_mouse_w != w || old_mouse_h != h || new_cursor_data) {
399 printf("Set new %dx%d mouse cursor image.\n", mouse_cursor_w, mouse_cursor_h);
402 update_mouse_cursor(src);