1 // SPDX-License-Identifier: MIT
6 #include "raylib/raylib.h"
15 //#define DEBUG_RAYLIB_RTG
17 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
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;
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;
29 static pthread_t thread_id;
31 struct rtg_shared_data {
32 uint16_t *width, *height;
33 uint16_t *format, *pitch;
34 uint16_t *offset_x, *offset_y;
40 struct rtg_shared_data rtg_share_data;
41 static uint32_t palette[256];
43 void rtg_update_screen() {}
45 uint32_t rtg_to_raylib[RTGFMT_NUM] = {
46 PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
47 PIXELFORMAT_UNCOMPRESSED_R5G6B5,
48 PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
49 PIXELFORMAT_UNCOMPRESSED_R5G5B5A1,
52 void *rtgThread(void *args) {
54 printf("RTG thread running\n");
60 uint32_t *indexed_buf = NULL;
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;
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;
78 Texture raylib_texture;
79 Texture raylib_clut_texture;
80 Image raylib_fb, raylib_clut;
82 InitWindow(GetScreenWidth(), GetScreenHeight(), "Pistorm RTG");
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");
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;
102 raylib_clut_texture = LoadTextureFromImage(raylib_clut);
104 Rectangle srchax, dsthax;
109 printf("Reinitializing raylib...\n");
110 width = rtg_display_width;
111 height = rtg_display_height;
112 format = rtg_display_format;
118 UnloadTexture(raylib_texture);
122 printf("Creating %dx%d raylib window...\n", width, height);
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];
131 raylib_texture = LoadTextureFromImage(raylib_fb);
135 indexed_buf = calloc(1, width * height * 2);
141 srchax.x = srchax.y = 0;
142 srchax.width = width;
143 srchax.height = height;
144 dsthax.x = dsthax.y = 0;
145 if (GetScreenHeight() == 720) {
148 } else if (GetScreenHeight() == 1080) {
150 dsthax.height = 1080;
161 UpdateTexture(raylib_clut_texture, palette);
162 BeginShaderMode(clut_shader);
163 SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
166 BeginShaderMode(swizzle_shader);
171 DrawTexturePro(raylib_texture, srchax, dsthax, originhax, 0.0f, RAYWHITE);
174 DrawTexture(raylib_texture, 0, 0, RAYWHITE);
183 #ifdef DEBUG_RAYLIB_RTG
184 DrawTexture(raylib_clut_texture, 0, 0, RAYWHITE);
187 DrawFPS(width - 200, 0);
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))]);
195 UpdateTexture(raylib_texture, indexed_buf);
198 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
202 ClearBackground(bef);
203 DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
206 if (height != *data->height || width != *data->width || format != *data->format) {
207 printf("Reinitializing due to something change.\n");
209 goto shutdown_raylib;
212 goto shutdown_raylib;
217 printf("RTG thread shut down.\n");
227 UnloadTexture(raylib_texture);
228 UnloadShader(clut_shader);
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];
245 void rtg_init_display() {
249 if (!rtg_initialized) {
250 err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
253 printf("can't create RTG thread :[%s]", strerror(err));
257 pthread_setname_np(thread_id, "pistorm: rtg");
258 printf("RTG Thread created successfully\n");
261 printf("RTG display enabled.\n");
264 void rtg_shutdown_display() {
265 printf("RTG display disabled.\n");