-
Notifications
You must be signed in to change notification settings - Fork 2
/
sdl_vid.c
409 lines (348 loc) · 9.57 KB
/
sdl_vid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* ---------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp): Maxim
* Sobolev <sobomax@altavista.net> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet
* some day, and you think this stuff is worth it, you can buy me a beer in
* return.
*
* Maxim Sobolev
* ---------------------------------------------------------------------------
*/
/* malloc() and friends */
#include <stdlib.h>
/* Lovely SDL */
#include <SDL.h>
#include "def.h"
#include "hardware.h"
#include "title_gz.h"
#include "icon.h"
extern Uint3 *vgatable[];
extern Uint3 *ascii2vga[];
Uint3 **sprites = vgatable;
Uint3 **alphas = ascii2vga;
Sint4 xratio = 2;
Sint4 yratio = 2;
Sint4 yoffset = 0;
Sint4 hratio = 2;
Sint4 wratio = 2;
#define virt2scrx(x) (x*xratio)
#define virt2scry(y) (y*yratio+yoffset)
#define virt2scrw(w) (w*wratio)
#define virt2scrh(h) (h*hratio)
/* palette1, normal intensity */
SDL_Color vga16_pal1[] = \
{{0,0,0,0},{0,0,128,0},{0,128,0,0},{0,128,128,0},{128,0,0,0},{128,0,128,0} \
,{128,64,0,0},{128,128,128,0},{64,64,64,0},{0,0,255,0},{0,255,0,0} \
,{0,255,255,0},{255,0,0,0},{255,0,255,0},{255,255,0,0},{255,255,255,0}};
/* palette1, high intensity */
SDL_Color vga16_pal1i[] = \
{{0,0,0,0},{0,0,255,0},{0,255,0,0},{0,255,255,0},{255,0,0,0},{255,0,255,0} \
,{255,128,0,0},{196,196,196,0},{128,128,128,0},{128,128,255,0},{128,255,128,0} \
,{128,255,255,0},{255,128,128,0},{255,128,255,0},{255,255,128,0},{255,255,255,0}};
/* palette2, normal intensity */
SDL_Color vga16_pal2[] = \
{{0,0,0,0},{0,128,0,0},{128,0,0,0},{128,64,0,0},{0,0,128,0},{0,128,128,0} \
,{128,0,128,0},{128,128,128,0},{64,64,64,0},{0,255,0,0},{255,0,0,0} \
,{255,255,0,0},{0,0,255,0},{0,255,255,0},{255,0,255,0},{255,255,255,0}};
/* palette2, high intensity */
SDL_Color vga16_pal2i[] = \
{{0,0,0,0},{0,255,0,0},{255,0,0,0},{255,128,0,0},{0,0,255,0},{0,255,255,0} \
,{255,0,255,0},{196,196,196,0},{128,128,128,0},{128,255,128,0},{255,128,128,0} \
,{255,255,128,0},{128,128,255,0},{128,255,255,0},{255,128,255,0},{255,255,255,0}};
SDL_Color *npalettes[] = {vga16_pal1, vga16_pal2};
SDL_Color *ipalettes[] = {vga16_pal1i, vga16_pal2i};
Sint4 currpal=0;
Uint32 addflag=0;
SDL_Surface *screen = NULL;
/* Data structure holding pending updates */
struct PendNode {
void *prevnode;
void *nextnode;
SDL_Rect rect;
};
struct PendNode *First=NULL, *Last=NULL;
int pendnum = 0;
SDL_Surface *ch2bmap(Uint3 *sprite, Sint4 w, Sint4 h)
{
Sint4 realw, realh;
SDL_Surface *tmp;
realw = virt2scrw(w*4);
realh = virt2scrh(h);
tmp = SDL_CreateRGBSurfaceFrom(sprite, realw, realh, 8, realw, 0, 0, 0, 0);
SDL_SetColors(tmp, screen->format->palette->colors, 0, screen->format->palette->ncolors);
return(tmp);
}
void graphicsoff(void)
{
}
bool setmode(void)
{
if((screen = SDL_SetVideoMode(640, 400, 8, \
SDL_HWSURFACE | SDL_HWPALETTE | addflag)) == NULL)
return(FALSE);
else
return(TRUE);
}
void switchmode(void)
{
Uint32 saved;
SDL_Surface *tmp = NULL;
SDL_Surface *oldscreen;
vgageti(0, 0, (Uint3 *)&tmp, 80, 200);
oldscreen = screen;
saved = addflag;
if(addflag == 0)
addflag = SDL_FULLSCREEN;
else
addflag = 0;
if(setmode() == FALSE) {
addflag = saved;
if(setmode() == FALSE) {
fprintf(stderr, "Fatal: failed to change videomode and"\
"fallback mode failed as well. Exitting.\n");
exit(1);
}
}
SDL_SetColors(screen, tmp->format->palette->colors, 0, \
tmp->format->palette->ncolors);
vgaputi(0, 0, (Uint3 *)&tmp, 80, 200);
SDL_FreeSurface(tmp);
SDL_FreeSurface(oldscreen);
}
void vgainit(void)
{
SDL_Surface *tmp = NULL;
tmp = SDL_CreateRGBSurfaceFrom(Icon, 64, 64, 8, 64, 0, 0, 0, 0);
SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, 247);
tmp->format->palette->colors = IconPalette;
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_WM_SetCaption("D I G G E R", NULL);
SDL_WM_SetIcon(tmp, NULL);
if(setmode() == FALSE) {
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n", SDL_GetError());
exit(1);
}
SDL_ShowCursor(0);
}
void vgaclear(void)
{
SDL_Surface *tmp = NULL;
vgageti(0, 0, (Uint3 *)&tmp, 80, 200);
memset(tmp->pixels, 0x00, tmp->w*tmp->h);
vgaputi(0, 0, (Uint3 *)&tmp, 80, 200);
SDL_FreeSurface(tmp);
}
void setpal(SDL_Color *pal)
{
SDL_SetColors(screen, pal, 0, 16);
}
void vgainten(Sint4 inten)
{
if(inten == 1)
setpal(ipalettes[currpal]);
else
setpal(npalettes[currpal]);
}
void vgapal(Sint4 pal)
{
setpal(npalettes[pal]);
currpal = pal;
}
void doscreenupdate(void)
{
struct PendNode *p;
for(p=First;p!=NULL;)
{
SDL_UpdateRect(screen,p->rect.x,p->rect.y,p->rect.w,p->rect.h);
First = p->nextnode;
free(p);
p = First;
}
pendnum = 0;
}
void vgaputi(Sint4 x, Sint4 y, Uint3 *p, Sint4 w, Sint4 h)
{
SDL_Surface *tmp;
SDL_Palette *reserv;
struct PendNode *new, *ptr;
new = malloc(sizeof (struct PendNode));
memset(new, 0x00, (sizeof (struct PendNode)));
new->rect.x = virt2scrx(x);
new->rect.y = virt2scry(y);
new->rect.w = virt2scrw(w*4);
new->rect.h = virt2scrh(h);
memcpy(&tmp, p, (sizeof (SDL_Surface *)));
reserv = tmp->format->palette;
tmp->format->palette = screen->format->palette;
SDL_BlitSurface(tmp, NULL, screen, &new->rect);
tmp->format->palette = reserv;
/*
* Following piece of code comparing already pending updates with current with
* main goal to prevent redrawing overlapping rectangles several times.
*/
for(ptr=First;ptr!=NULL;ptr=ptr->nextnode) {
if((new->rect.x >= ptr->rect.x) &&
(new->rect.y >= ptr->rect.y) &&
((new->rect.x+new->rect.w) <= (ptr->rect.x+ptr->rect.w)) &&
((new->rect.y+new->rect.h) <= (ptr->rect.y+ptr->rect.h))) {
free(new);
return;
} else if((new->rect.x <= ptr->rect.x) &&
(new->rect.y <= ptr->rect.y) &&
((new->rect.x+new->rect.w) >= (ptr->rect.x+ptr->rect.w)) &&
((new->rect.y+new->rect.h) >= (ptr->rect.y+ptr->rect.h))) {
ptr->rect.x = new->rect.x;
ptr->rect.y = new->rect.y;
ptr->rect.w = new->rect.w;
ptr->rect.h = new->rect.h;
free(new);
return;
}
}
if (pendnum == 0)
First = new;
else {
Last->nextnode = new;
new->prevnode = Last;
}
Last = new;
pendnum++;
}
void vgageti(Sint4 x, Sint4 y, Uint3 *p, Sint4 w, Sint4 h)
{
SDL_Surface *tmp;
SDL_Rect src;
memcpy(&tmp, p, (sizeof (SDL_Surface *)));
if (tmp != NULL)
SDL_FreeSurface(tmp); /* Destroy previously allocated bitmap */
src.x = virt2scrx(x);
src.y = virt2scry(y);
src.w = virt2scrw(w*4);
src.h = virt2scrh(h);
tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, src.w, src.h, 8, 0, 0, 0, 0);
SDL_SetColors(tmp, screen->format->palette->colors, 0, screen->format->palette->ncolors);
SDL_BlitSurface(screen, &src, tmp, NULL);
memcpy(p, &tmp, (sizeof (SDL_Surface *)));
}
Sint4 vgagetpix(Sint4 x, Sint4 y)
{
SDL_Surface *tmp = NULL;
Uint4 xi,yi;
Uint4 i = 0;
Sint4 rval = 0;
Uint8 *pixels;
if ((x > 319) || (y > 199))
return (0xff);
vgageti(x, y, (Uint3 *)&tmp, 1, 1);
pixels = (Uint8 *)tmp->pixels;
for (yi=0;yi<tmp->h;yi++)
for (xi=0;xi<tmp->w;xi++)
if (pixels[i++])
rval |= 0x80 >> xi;
SDL_FreeSurface(tmp);
return(rval & 0xee);
}
void vgaputim(Sint4 x, Sint4 y, Sint4 ch, Sint4 w, Sint4 h)
{
SDL_Surface *tmp;
SDL_Surface *mask;
SDL_Surface *scr = NULL;
Uint8 *tmp_pxl, *mask_pxl, *scr_pxl;
Sint4 realsize;
Sint4 i;
tmp = ch2bmap(sprites[ch*2], w, h);
mask = ch2bmap(sprites[ch*2+1], w, h);
vgageti(x, y, (Uint3 *)&scr, w, h);
realsize = scr->w * scr->h;
tmp_pxl = (Uint8 *)tmp->pixels;
mask_pxl = (Uint8 *)mask->pixels;
scr_pxl = (Uint8 *)scr->pixels;
for(i=0;i<realsize;i++)
if(tmp_pxl[i] != 0xff)
scr_pxl[i] = (scr_pxl[i] & mask_pxl[i]) | \
tmp_pxl[i];
vgaputi(x, y, (Uint3 *)&scr, w, h);
tmp->pixels = NULL; /* We should NULL'ify these ppointers, or the VGLBitmapDestroy */
mask->pixels = NULL; /* will shoot itself in the foot by trying to dellocate statically */
SDL_FreeSurface(tmp);/* allocated arrays */
SDL_FreeSurface(mask);
SDL_FreeSurface(scr);
}
void vgawrite(Sint4 x, Sint4 y, Sint4 ch, Sint4 c)
{
SDL_Surface *tmp;
Uint8 *copy;
Uint8 color;
Sint4 w=3, h=12, size;
Sint4 i;
if(((ch - 32) >= 0x5f) || (ch < 32))
return;
tmp = ch2bmap(alphas[ch-32], w, h);
size = tmp->w*tmp->h;
copy = malloc(size);
memcpy(copy, tmp->pixels, size);
for(i = size;i!=0;) {
i--;
color = copy[i];
if (color==10) {
if (c==2)
color=12;
else {
if (c==3)
color=14;
}
} else
if (color==12) {
if (c==1)
color=2;
else
if (c==2)
color=4;
else
if (c==3)
color=6;
}
copy[i] = color;
}
tmp->pixels = copy;
vgaputi(x, y, (Uint3 *)&tmp, w, h);
SDL_FreeSurface(tmp);
}
void vgatitle(void)
{
SDL_Surface *tmp=NULL;
vgageti(0, 0, (Uint3 *)&tmp, 80, 200);
gettitle(tmp->pixels);
vgaputi(0, 0, (Uint3 *)&tmp, 80, 200);
SDL_FreeSurface(tmp);
}
void gretrace(void)
{
}
void savescreen(void)
{
/* FILE *f;
int i;
f=fopen("screen.saw", "w");
for(i=0;i<(VGLDisplay->Xsize*VGLDisplay->Ysize);i++)
fputc(VGLDisplay->Bitmap[i], f);
fclose(f);*/
}
/*
* Depreciated functions, necessary only to avoid "Undefined symbol:..." compiler
* errors.
*/
void cgainit(void) {}
void cgaclear(void) {}
void cgatitle(void) {}
void cgawrite(Sint4 x, Sint4 y, Sint4 ch, Sint4 c) {}
void cgaputim(Sint4 x, Sint4 y, Sint4 ch, Sint4 w, Sint4 h) {}
void cgageti(Sint4 x, Sint4 y, Uint3 *p, Sint4 w, Sint4 h) {}
void cgaputi(Sint4 x, Sint4 y, Uint3 *p, Sint4 w, Sint4 h) {}
void cgapal(Sint4 pal) {}
void cgainten(Sint4 inten) {}
Sint4 cgagetpix(Sint4 x, Sint4 y) {return(0);}