]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg-output.c
5423671ba76fd42e97cb413d0f8309cb4baaa111
[pistorm] / platforms / amiga / rtg / rtg-output.c
1 #include <SDL2/SDL.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <pthread.h>
7 #include <string.h>
8 #include "rtg.h"
9
10 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
11
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;
16
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;
21
22 static pthread_t thread_id;
23
24 struct rtg_shared_data {
25     uint16_t *width, *height;
26     uint16_t *format, *pitch;
27     uint16_t *offset_x, *offset_y;
28     uint8_t *memory;
29     uint32_t *addr;
30     uint8_t *running;
31 };
32
33 SDL_Window *win = NULL;
34 SDL_Renderer *renderer = NULL;
35 SDL_Texture *img = NULL;
36
37 struct rtg_shared_data rtg_share_data;
38 static uint32_t palette[256];
39
40 void rtg_update_screen() {}
41
42 uint32_t rtg_to_sdl2[RTGFMT_NUM] = {
43     SDL_PIXELFORMAT_ARGB8888,
44     SDL_PIXELFORMAT_RGB565,
45     SDL_PIXELFORMAT_ARGB8888,
46     SDL_PIXELFORMAT_RGB555,
47 };
48
49 void *rtgThread(void *args) {
50
51     printf("RTG thread running\n");
52     fflush(stdout);
53
54     int reinit = 0;
55     rtg_on = 1;
56
57     uint32_t *indexed_buf = NULL;
58
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;
69
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;
74
75     printf("Initializing SDL2...\n");
76     if (SDL_Init(0) < 0) {
77         printf("Failed to initialize SDL2.\n");
78     }
79
80     printf("Initializing SDL2 Video...\n");
81     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
82         printf("Failed to initialize SDL2 Video..\n");
83     }
84
85 reinit_sdl:;
86     if (reinit) {
87         printf("Reinitializing SDL2...\n");
88         width = rtg_display_width;
89         height = rtg_display_height;
90         format = rtg_display_format;
91         pitch = rtg_pitch;
92         if (indexed_buf) {
93             free(indexed_buf);
94             indexed_buf = NULL;
95         }
96         reinit = 0;
97     }
98
99     printf("Creating %dx%d SDL2 window...\n", width, height);
100     win = SDL_CreateWindow("Pistorm RTG", 0, 0, width, height, 0);
101     if (!win) {
102         RTG_INIT_ERR("Failed create SDL2 window.\n");
103     }
104     else {
105         printf("Created %dx%d window.\n", width, height);
106     }
107
108     printf("Creating SDL2 renderer...\n");
109     renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
110     if (!renderer) {
111         RTG_INIT_ERR("Failed create SDL2 renderer.\n");
112     }
113     else {
114         printf("Created SDL2 renderer.\n");
115     }
116
117     printf("Creating SDL2 texture...\n");
118     img = SDL_CreateTexture(renderer, rtg_to_sdl2[format], SDL_TEXTUREACCESS_TARGET, width, height);
119     if (!img) {
120         RTG_INIT_ERR("Failed create SDL2 texture.\n");
121     }
122     else {
123         printf("Created %dx%d texture.\n", width, height);
124     }
125
126     switch (format) {
127         case RTGFMT_8BIT:
128             indexed_buf = calloc(1, width * height * 4);
129             break;
130         case RTGFMT_RBG565:
131             indexed_buf = calloc(1, width * height * 2);
132             break;
133         default:
134             break;
135     }
136
137     uint64_t frame_start = 0, frame_end = 0;
138     float elapsed = 0.0f;
139
140     while (1) {
141         if (renderer && win && img) {
142             frame_start = SDL_GetPerformanceCounter();
143             SDL_RenderClear(renderer);
144             if (*data->running) {
145                 switch (format) {
146                     case RTGFMT_RGB32:
147                         SDL_UpdateTexture(img, NULL, &data->memory[*data->addr], pitch);
148                         break;
149                     case RTGFMT_RBG565:
150                         SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 2);
151                         break;
152                     case RTGFMT_8BIT:
153                         SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 4);
154                         break;
155                 }
156                 SDL_RenderCopy(renderer, img, NULL, NULL);
157             }
158             SDL_RenderPresent(renderer);
159             //usleep(16667); //ghetto 60hz
160             if (height != *data->height || width != *data->width || format != *data->format) {
161                 printf("Reinitializing due to something change.\n");
162                 reinit = 1;
163                 goto shutdown_sdl;
164             }
165             switch (format) {
166                 case RTGFMT_8BIT:
167                     for (int y = 0; y < height; y++) {
168                         for (int x = 0; x < width; x++) {
169                             indexed_buf[x + (y * width)] = palette[data->memory[*data->addr + x + (y * pitch)]];
170                         }
171                     }
172                     break;
173                 case RTGFMT_RBG565:
174                     for (int y = 0; y < height; y++) {
175                         for (int x = 0; x < width; x++) {
176                             ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
177                         }
178                     }
179                     break;
180             }
181             frame_end = SDL_GetPerformanceCounter();
182             elapsed = (frame_end - frame_start) / (float)SDL_GetPerformanceFrequency() * 1000.0f;
183             pitch = rtg_pitch;
184             SDL_Delay(floor(16.66666f - elapsed));
185         }
186         else
187             break;
188     }
189
190     rtg_initialized = 0;
191     printf("RTG thread shut down.\n");
192
193 shutdown_sdl:;
194     if (img) SDL_DestroyTexture(img);
195     if (renderer) SDL_DestroyRenderer(renderer);
196     if (win) SDL_DestroyWindow(win);
197
198     win = NULL;
199     img = NULL;
200     renderer = NULL;
201
202     if (reinit)
203         goto reinit_sdl;
204     
205     if (indexed_buf)
206         free(indexed_buf);
207
208     SDL_QuitSubSystem(SDL_INIT_VIDEO);
209     SDL_Quit();
210
211     return args;
212 }
213
214 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
215     palette[index] = xrgb;
216 }
217
218 void rtg_init_display() {
219     int err;
220     rtg_on = 1;
221
222     if (!rtg_initialized) {
223         err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
224         if (err != 0) {
225             rtg_on = 0;
226             printf("can't create RTG thread :[%s]", strerror(err));
227         }
228         else {
229             rtg_initialized = 1;
230             printf("RTG Thread created successfully\n");
231         }
232     }
233     printf("RTG display enabled.\n");
234 }
235
236 void rtg_shutdown_display() {
237     printf("RTG display disabled.\n");
238     rtg_on = 0;
239 }