12 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
14 uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0;
15 extern uint8_t *rtg_mem;
16 extern uint32_t framebuffer_addr;
17 extern uint32_t framebuffer_addr_adj;
19 extern uint16_t rtg_display_width, rtg_display_height;
20 extern uint16_t rtg_display_format;
21 extern uint16_t rtg_pitch, rtg_total_rows;
22 extern uint16_t rtg_offset_x, rtg_offset_y;
24 static pthread_t thread_id;
26 struct rtg_shared_data {
27 uint16_t *width, *height;
28 uint16_t *format, *pitch;
29 uint16_t *offset_x, *offset_y;
35 SDL_Window *win = NULL;
36 SDL_Renderer *renderer = NULL;
37 SDL_Texture *img = NULL;
39 struct rtg_shared_data rtg_share_data;
40 static uint32_t palette[256];
42 void rtg_update_screen() {}
44 uint32_t rtg_to_sdl2[RTGFMT_NUM] = {
45 SDL_PIXELFORMAT_ARGB8888,
46 SDL_PIXELFORMAT_RGB565,
47 SDL_PIXELFORMAT_ARGB8888,
48 SDL_PIXELFORMAT_RGB555,
51 void *rtgThread(void *args) {
53 printf("RTG thread running\n");
59 uint32_t *indexed_buf = NULL;
61 rtg_share_data.format = &rtg_display_format;
62 rtg_share_data.width = &rtg_display_width;
63 rtg_share_data.height = &rtg_display_height;
64 rtg_share_data.pitch = &rtg_pitch;
65 rtg_share_data.offset_x = &rtg_offset_x;
66 rtg_share_data.offset_y = &rtg_offset_y;
67 rtg_share_data.memory = rtg_mem;
68 rtg_share_data.running = &rtg_on;
69 rtg_share_data.addr = &framebuffer_addr_adj;
70 struct rtg_shared_data *data = &rtg_share_data;
72 uint16_t width = rtg_display_width;
73 uint16_t height = rtg_display_height;
74 uint16_t format = rtg_display_format;
75 uint16_t pitch = rtg_pitch;
77 printf("Initializing SDL2...\n");
78 if (SDL_Init(0) < 0) {
79 printf("Failed to initialize SDL2.\n");
82 printf("Initializing SDL2 Video...\n");
83 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
84 printf("Failed to initialize SDL2 Video..\n");
89 printf("Reinitializing SDL2...\n");
90 width = rtg_display_width;
91 height = rtg_display_height;
92 format = rtg_display_format;
101 printf("Creating %dx%d SDL2 window...\n", width, height);
102 win = SDL_CreateWindow("Pistorm RTG", 0, 0, width, height, 0);
103 SDL_ShowCursor(SDL_DISABLE);
105 RTG_INIT_ERR("Failed create SDL2 window.\n");
108 printf("Created %dx%d window.\n", width, height);
111 printf("Creating SDL2 renderer...\n");
112 renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
114 RTG_INIT_ERR("Failed create SDL2 renderer.\n");
117 printf("Created SDL2 renderer.\n");
120 printf("Creating SDL2 texture...\n");
121 img = SDL_CreateTexture(renderer, rtg_to_sdl2[format], SDL_TEXTUREACCESS_TARGET, width, height);
123 RTG_INIT_ERR("Failed create SDL2 texture.\n");
126 printf("Created %dx%d texture.\n", width, height);
131 indexed_buf = calloc(1, width * height * 4);
134 indexed_buf = calloc(1, width * height * 2);
140 uint64_t frame_start = 0, frame_end = 0;
141 float elapsed = 0.0f;
144 if (renderer && win && img) {
145 frame_start = SDL_GetPerformanceCounter();
146 SDL_RenderClear(renderer);
147 if (*data->running) {
150 SDL_UpdateTexture(img, NULL, &data->memory[*data->addr], pitch);
153 SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 2);
156 SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 4);
159 SDL_RenderCopy(renderer, img, NULL, NULL);
161 SDL_RenderPresent(renderer);
162 //usleep(16667); //ghetto 60hz
163 if (height != *data->height || width != *data->width || format != *data->format) {
164 printf("Reinitializing due to something change.\n");
170 for (int y = 0; y < height; y++) {
171 for (int x = 0; x < width; x++) {
172 indexed_buf[x + (y * width)] = palette[data->memory[*data->addr + x + (y * pitch)]];
177 for (int y = 0; y < height; y++) {
178 for (int x = 0; x < width; x++) {
179 ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
184 frame_end = SDL_GetPerformanceCounter();
185 elapsed = (frame_end - frame_start) / (float)SDL_GetPerformanceFrequency() * 1000.0f;
187 SDL_Delay(floor(16.66666f - elapsed));
194 printf("RTG thread shut down.\n");
197 if (img) SDL_DestroyTexture(img);
198 if (renderer) SDL_DestroyRenderer(renderer);
199 if (win) SDL_DestroyWindow(win);
211 SDL_QuitSubSystem(SDL_INIT_VIDEO);
217 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
218 palette[index] = xrgb;
221 void rtg_init_display() {
225 if (!rtg_initialized) {
226 err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
229 printf("can't create RTG thread :[%s]", strerror(err));
233 pthread_setname_np(thread_id, "pistorm: rtg");
234 printf("RTG Thread created successfully\n");
237 printf("RTG display enabled.\n");
240 void rtg_shutdown_display() {
241 printf("RTG display disabled.\n");