Skip to content

Commit 9b8750f

Browse files
committed
First attempt at SDL version. Makefiles not updated.
1 parent d0cae95 commit 9b8750f

File tree

2 files changed

+553
-0
lines changed

2 files changed

+553
-0
lines changed

src/sdl.c

Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
2+
/* Written by Dennis Payne, dulsi@identicalsoftware.com */
3+
4+
#ifdef HAVE_CONFIG_H
5+
# include "config.h"
6+
#endif
7+
8+
#ifdef HAVE_UNISTD_H
9+
# include <unistd.h>
10+
#endif
11+
12+
#include <SDL.h>
13+
#include <string.h>
14+
#include <stdio.h>
15+
#include <sys/time.h>
16+
17+
#include "compat.h"
18+
#include "thrust.h"
19+
#include "fast_gr.h"
20+
#include "gr_drv.h"
21+
#include "options.h"
22+
23+
#define VGAMODE G320x200x256
24+
#define PSTARTX ((320-PUSEX)>>1)
25+
#define PSTARTY ((200-PUSEY-24)>>1)
26+
27+
typedef unsigned char IColor;
28+
29+
typedef IColor IPaletteTable[256][3];
30+
typedef IPaletteTable *IPalette;
31+
32+
/* Extra global variables needed by SDL */
33+
SDL_Window *ISDLMainWindow;
34+
SDL_Renderer *ISDLMainRenderer;
35+
SDL_Texture *ISDLMainTexture;
36+
SDL_Surface *ISDLMainScreen;
37+
SDL_Surface *ISDLScreen;
38+
unsigned char *IScreenMain;
39+
IPalette IPaletteMain;
40+
41+
void
42+
clearscr(void)
43+
{
44+
// SDL_RenderClear(ISDLMainRenderer);
45+
memset(IScreenMain, 0, 320 * 200);
46+
}
47+
48+
void
49+
putarea(ui8 *source,
50+
int x, int y, int width, int height, int bytesperline,
51+
int destx, int desty)
52+
{
53+
ui8 *tmp;
54+
int res=PSTARTX+destx+320*(PSTARTY+desty);
55+
int dy, seg, lastseg, endseg;
56+
57+
if(bytesperline==320 && width==320 && 320==320 && x==0 && destx==0) {
58+
memcpy(IScreenMain + desty*320, source + y*320, height*320);
59+
}
60+
else {
61+
for (int cy = 0; cy < height; cy++)
62+
{
63+
if (desty + cy >= 200)
64+
break;
65+
if (destx + width <= 320)
66+
memcpy(IScreenMain + (desty + cy) * 320 + destx, source + (y + cy) * bytesperline + x, width);
67+
else
68+
memcpy(IScreenMain + (desty + cy) * 320 + destx, source + (y + cy) * bytesperline + x, 320 - destx);
69+
}
70+
}
71+
}
72+
73+
void
74+
putpixel(int x, int y, ui8 color)
75+
{
76+
IScreenMain[PSTARTX+x + ((PSTARTY+y) << 8) + ((PSTARTY+y) << 6)] = color;
77+
}
78+
79+
static void
80+
delaygame(long usec)
81+
{
82+
static long extratime[3] = { 0, 0, 0 };
83+
struct timeval start, end, diff;
84+
long extradelay, delay;
85+
int i;
86+
87+
extradelay = (extratime[0] + extratime[1] + extratime[2])/3;
88+
if(usec > extradelay) {
89+
gettimeofday(&start, NULL);
90+
usleep(usec - extradelay);
91+
gettimeofday(&end, NULL);
92+
diff.tv_sec = end.tv_sec - start.tv_sec;
93+
diff.tv_usec = end.tv_usec - start.tv_usec;
94+
if(diff.tv_usec < 0) {
95+
diff.tv_sec--;
96+
diff.tv_usec += 1000000;
97+
}
98+
delay = diff.tv_sec*1000000 + diff.tv_usec;
99+
extratime[0] = extratime[1];
100+
extratime[1] = extratime[2];
101+
extratime[2] = delay - (usec - extradelay);
102+
}
103+
else
104+
for(i=0; i<3; i++)
105+
extratime[i] = (extratime[i] * 9) / 10;
106+
}
107+
108+
void
109+
syncscreen(unsigned long us)
110+
{
111+
struct timeval end, diff;
112+
static struct timeval start;
113+
static int first = 1;
114+
long delay;
115+
116+
if(us)
117+
usleep(us);
118+
119+
if(first) {
120+
gettimeofday(&start, NULL);
121+
first = 0;
122+
}
123+
124+
gettimeofday(&end, NULL);
125+
if(end.tv_sec < start.tv_sec ||
126+
(end.tv_sec == start.tv_sec &&
127+
end.tv_usec < start.tv_usec))
128+
end.tv_sec += 60*60*24;
129+
diff.tv_sec = end.tv_sec - start.tv_sec;
130+
diff.tv_usec = end.tv_usec - start.tv_usec;
131+
if(diff.tv_usec < 0) {
132+
diff.tv_sec--;
133+
diff.tv_usec += 1000000;
134+
}
135+
136+
delay = 20000 - (diff.tv_sec*1000000 + diff.tv_usec);
137+
if(delay > 0) {
138+
delaygame(delay);
139+
}
140+
gettimeofday(&start, NULL);
141+
}
142+
143+
void
144+
displayscreen(unsigned long us)
145+
{
146+
void *pixels;
147+
int pitch;
148+
SDL_Rect dest;
149+
dest.x = 0;
150+
dest.y = 0;
151+
dest.w = 320;
152+
dest.h = 200;
153+
SDL_LockTexture(ISDLMainTexture, &dest, &pixels, &pitch);
154+
int x, y;
155+
unsigned char *curPos;
156+
Uint8 *realLine;
157+
Uint8 *realPos;
158+
159+
curPos = IScreenMain;
160+
realPos = (Uint8 *)pixels;
161+
for (y = 0; y < 200; ++y)
162+
{
163+
realLine = realPos;
164+
for (x = 0; x < 320; ++x)
165+
{
166+
SDL_GetRGB(*curPos, ISDLScreen->format, realPos + 2, realPos + 1, realPos);
167+
realPos += 4;
168+
++curPos;
169+
}
170+
realPos = realLine + pitch;
171+
}
172+
SDL_UnlockTexture(ISDLMainTexture);
173+
SDL_RenderClear(ISDLMainRenderer);
174+
SDL_RenderCopy(ISDLMainRenderer, ISDLMainTexture, NULL, NULL);
175+
SDL_RenderPresent(ISDLMainRenderer);
176+
if(us)
177+
usleep(us);
178+
}
179+
180+
void
181+
fadepalette(int first, int last, ui8 *RGBtable, int fade, int flag)
182+
{
183+
static int tmpRGBtable[768];
184+
int entries,i;
185+
int *c;
186+
ui8 *d;
187+
int used_first;
188+
int used_entries=0;
189+
190+
entries=last-first+1;
191+
192+
used_first = color_lookup[first];
193+
if(used_first == 0xff)
194+
printf("Aiee, bad fadepalette call.\n");
195+
c=&tmpRGBtable[3*used_first];
196+
d=RGBtable;
197+
i=0;
198+
199+
while(i<entries) {
200+
if(color_lookup[first+i] != 0xff) {
201+
*c++ = (*d++ * fade) >> 8;
202+
*c++ = (*d++ * fade) >> 8;
203+
*c = (*d * fade) >> 8;
204+
used_entries++;
205+
}
206+
else
207+
d += 2;
208+
if(++i<entries) {
209+
if(color_lookup[first+i-1] != 0xff)
210+
c++;
211+
d++;
212+
}
213+
}
214+
215+
// if(flag)
216+
// vga_waitretrace();
217+
218+
SDL_Color sdlcol[256];
219+
int color, rgb;
220+
221+
for (color = 0; color < 256; color++)
222+
{
223+
sdlcol[color].r = tmpRGBtable[color * 3] << 2;
224+
sdlcol[color].g = tmpRGBtable[color* 3 + 1] << 2;
225+
sdlcol[color].b = tmpRGBtable[color * 3 + 2] << 2;
226+
}
227+
SDL_SetPaletteColors((SDL_Palette *)IPaletteMain, sdlcol, used_first, used_entries);
228+
}
229+
230+
void
231+
fade_in(unsigned long us)
232+
{
233+
int i;
234+
235+
/* if(PSTARTY>0 && PSTARTX>0) {
236+
gl_hline(PSTARTX-3, PSTARTY-3, PSTARTX+PUSEX+2, 1);
237+
gl_hline(PSTARTX-4, PSTARTY-4, PSTARTX+PUSEX+3, 2);
238+
gl_hline(PSTARTX-5, PSTARTY-5, PSTARTX+PUSEX+4, 3);
239+
gl_hline(PSTARTX-3, PSTARTY+PUSEY+26, PSTARTX+PUSEX+2, 3);
240+
gl_hline(PSTARTX-4, PSTARTY+PUSEY+27, PSTARTX+PUSEX+3, 2);
241+
gl_hline(PSTARTX-5, PSTARTY+PUSEY+28, PSTARTX+PUSEX+4, 1);
242+
gl_line(PSTARTX-3, PSTARTY-3, PSTARTX-3, PSTARTY+PUSEY+26, 1);
243+
gl_line(PSTARTX-4, PSTARTY-4, PSTARTX-4, PSTARTY+PUSEY+27, 2);
244+
gl_line(PSTARTX-5, PSTARTY-5, PSTARTX-5, PSTARTY+PUSEY+28, 3);
245+
gl_line(PSTARTX+PUSEX+2, PSTARTY-3, PSTARTX+PUSEX+2, PSTARTY+PUSEY+26, 3);
246+
gl_line(PSTARTX+PUSEX+3, PSTARTY-4, PSTARTX+PUSEX+3, PSTARTY+PUSEY+27, 2);
247+
gl_line(PSTARTX+PUSEX+4, PSTARTY-5, PSTARTX+PUSEX+4, PSTARTY+PUSEY+28, 1);
248+
}
249+
else if(PSTARTY>0) {
250+
gl_hline(PSTARTX, PSTARTY-3, PSTARTX+PUSEX-1, 1);
251+
gl_hline(PSTARTX, PSTARTY-4, PSTARTX+PUSEX-1, 2);
252+
gl_hline(PSTARTX, PSTARTY-5, PSTARTX+PUSEX-1, 3);
253+
gl_hline(PSTARTX, PSTARTY+PUSEY+26, PSTARTX+PUSEX-1, 3);
254+
gl_hline(PSTARTX, PSTARTY+PUSEY+27, PSTARTX+PUSEX-1, 2);
255+
gl_hline(PSTARTX, PSTARTY+PUSEY+28, PSTARTX+PUSEX-1, 1);
256+
}*/
257+
258+
for(i=1; i<=64; i++)
259+
fadepalette(0, 255, bin_colors, i, 1);
260+
261+
if(us)
262+
usleep(us);
263+
}
264+
265+
void
266+
fade_out(unsigned long us)
267+
{
268+
int i;
269+
270+
if(us)
271+
usleep(us);
272+
273+
for(i=64; i; i--)
274+
fadepalette(0, 255, bin_colors, i, 1);
275+
clearscr();
276+
usleep(500000L);
277+
}
278+
279+
void
280+
graphics_preinit(void)
281+
{
282+
}
283+
284+
int
285+
graphicsinit(int argc, char **argv)
286+
{
287+
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
288+
{
289+
printf("Failed - SDL_Init\n");
290+
exit(0);
291+
}
292+
ISDLMainWindow = SDL_CreateWindow("Thrust",
293+
SDL_WINDOWPOS_UNDEFINED,
294+
SDL_WINDOWPOS_UNDEFINED,
295+
320, 200,
296+
(/*fullScreen*/ 1 ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
297+
if (ISDLMainWindow == NULL)
298+
{
299+
printf("Failed - SDL_CreateWindow\n");
300+
exit(0);
301+
}
302+
ISDLMainRenderer = SDL_CreateRenderer(ISDLMainWindow, -1, 0);
303+
if (ISDLMainRenderer == NULL)
304+
{
305+
printf("Failed - SDL_CreateRenderer\n");
306+
exit(0);
307+
}
308+
ISDLMainTexture = SDL_CreateTexture(ISDLMainRenderer,
309+
SDL_PIXELFORMAT_ARGB8888,
310+
SDL_TEXTUREACCESS_STREAMING,
311+
320, 200);
312+
if (ISDLMainTexture == NULL)
313+
{
314+
printf("Failed - SDL_CreateTexture\n");
315+
exit(0);
316+
}
317+
ISDLMainScreen = SDL_CreateRGBSurface(0, 320, 200, 32,
318+
0x00FF0000,
319+
0x0000FF00,
320+
0x000000FF,
321+
0xFF000000);
322+
ISDLScreen = SDL_CreateRGBSurface(0, 320, 200, 8, 0, 0, 0, 0);
323+
if (ISDLScreen == NULL)
324+
{
325+
printf("Failed - SDL_CreateRGBSurface\n");
326+
exit(0);
327+
}
328+
IScreenMain = (unsigned char *)ISDLScreen->pixels;
329+
IPaletteMain = (IPalette)ISDLScreen->format->palette;
330+
331+
SDL_RenderClear(ISDLMainRenderer);
332+
fadepalette(0, 255, bin_colors, 1, 0);
333+
334+
return 0;
335+
}
336+
337+
int
338+
graphicsclose(void)
339+
{
340+
SDL_Quit();
341+
342+
return 0;
343+
}
344+
345+
char *
346+
graphicsname(void)
347+
{
348+
static char name[] = "SDL";
349+
350+
return name;
351+
}

0 commit comments

Comments
 (0)