10 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
12 uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0;
13 extern uint8_t *rtg_mem;
14 extern uint32_t framebuffer_addr;
15 extern uint32_t framebuffer_addr_adj;
17 extern uint16_t rtg_display_width, rtg_display_height;
18 extern uint16_t rtg_display_format;
19 extern uint16_t rtg_pitch, rtg_total_rows;
20 extern uint16_t rtg_offset_x, rtg_offset_y;
22 static pthread_t thread_id;
24 struct rtg_shared_data {
25 uint16_t *width, *height;
26 uint16_t *format, *pitch;
27 uint16_t *offset_x, *offset_y;
33 SDL_Window *win = NULL;
34 SDL_Renderer *renderer = NULL;
35 SDL_Texture *img = NULL;
37 struct rtg_shared_data rtg_share_data;
38 static uint32_t palette[256];
40 void rtg_update_screen() {}
42 uint32_t rtg_to_sdl2[RTGFMT_NUM] = {
43 SDL_PIXELFORMAT_ARGB8888,
44 SDL_PIXELFORMAT_RGB565,
45 SDL_PIXELFORMAT_ARGB8888,
46 SDL_PIXELFORMAT_RGB555,
49 void *rtgThread(void *args) {
51 printf("RTG thread running\n");
57 uint32_t *indexed_buf = NULL;
59 rtg_share_data.format = &rtg_display_format;
60 rtg_share_data.width = &rtg_display_width;
61 rtg_share_data.height = &rtg_display_height;
62 rtg_share_data.pitch = &rtg_pitch;
63 rtg_share_data.offset_x = &rtg_offset_x;
64 rtg_share_data.offset_y = &rtg_offset_y;
65 rtg_share_data.memory = rtg_mem;
66 rtg_share_data.running = &rtg_on;
67 rtg_share_data.addr = &framebuffer_addr_adj;
68 struct rtg_shared_data *data = &rtg_share_data;
70 uint16_t width = rtg_display_width;
71 uint16_t height = rtg_display_height;
72 uint16_t format = rtg_display_format;
73 uint16_t pitch = rtg_pitch;
75 printf("Initializing SDL2...\n");
76 if (SDL_Init(0) < 0) {
77 printf("Failed to initialize SDL2.\n");
80 printf("Initializing SDL2 Video...\n");
81 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
82 printf("Failed to initialize SDL2 Video..\n");
87 printf("Reinitializing SDL2...\n");
88 width = rtg_display_width;
89 height = rtg_display_height;
90 format = rtg_display_format;
99 printf("Creating %dx%d SDL2 window...\n", width, height);
100 win = SDL_CreateWindow("Pistorm RTG", 0, 0, width, height, 0);
102 RTG_INIT_ERR("Failed create SDL2 window.\n");
105 printf("Created %dx%d window.\n", width, height);
108 printf("Creating SDL2 renderer...\n");
109 renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
111 RTG_INIT_ERR("Failed create SDL2 renderer.\n");
114 printf("Created SDL2 renderer.\n");
117 printf("Creating SDL2 texture...\n");
118 img = SDL_CreateTexture(renderer, rtg_to_sdl2[format], SDL_TEXTUREACCESS_TARGET, width, height);
120 RTG_INIT_ERR("Failed create SDL2 texture.\n");
123 printf("Created %dx%d texture.\n", width, height);
128 indexed_buf = calloc(1, width * height * 4);
131 indexed_buf = calloc(1, width * height * 2);
138 if (renderer && win && img) {
139 SDL_RenderClear(renderer);
140 if (*data->running) {
143 SDL_UpdateTexture(img, NULL, &data->memory[*data->addr], pitch);
146 SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 2);
149 SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 4);
152 SDL_RenderCopy(renderer, img, NULL, NULL);
154 SDL_RenderPresent(renderer);
155 usleep(16667); //ghetto 60hz
156 if (height != *data->height || width != *data->width || format != *data->format) {
157 printf("Reinitializing due to something change.\n");
163 for (int y = 0; y < height; y++) {
164 for (int x = 0; x < width; x++) {
165 indexed_buf[x + (y * width)] = palette[data->memory[*data->addr + x + (y * pitch)]];
170 for (int y = 0; y < height; y++) {
171 for (int x = 0; x < width; x++) {
172 ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
183 printf("RTG thread shut down.\n");
186 if (img) SDL_DestroyTexture(img);
187 if (renderer) SDL_DestroyRenderer(renderer);
188 if (win) SDL_DestroyWindow(win);
200 SDL_QuitSubSystem(SDL_INIT_VIDEO);
206 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
207 palette[index] = xrgb;
210 void rtg_init_display() {
214 if (!rtg_initialized) {
215 err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
218 printf("can't create RTG thread :[%s]", strerror(err));
222 printf("RTG Thread created successfully\n");
225 printf("RTG display enabled.\n");
228 void rtg_shutdown_display() {
229 printf("RTG display disabled.\n");