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