-
Notifications
You must be signed in to change notification settings - Fork 0
/
grafx.c
405 lines (338 loc) · 7.86 KB
/
grafx.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
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (C) 1995 Ronny Wester
Copyright (C) 2003 Jeremy Chin
Copyright (C) 2003-2007 Lucas Martin-King
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------
grafx.c - graphics related functions
Author: $Author: lmartinking $
Rev: $Revision: 250 $
URL: $HeadURL: svn://svn.icculus.org/cdogs-sdl/trunk/src/grafx.c $
ID: $Id: grafx.c 250 2007-07-06 16:38:43Z lmartinking $
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include "SDL.h"
#include "SDL_endian.h"
#include "defs.h"
#include "grafx.h"
#include "blit.h"
#include "pics.h" /* for gPalette */
#include "sprcomp.h"
#include "files.h"
#include "utils.h"
GFX_Mode gfx_modelist[] = {
#ifdef SYS_PSP
{ 320, 240 },
#endif
#ifdef SYS_NDS
{ 256, 192 },
#endif
#if !defined (SYS_PSP) & SYS_NDS
{ 320, 200 },
{ 320, 240 },
{ 400, 300 },
{ 640, 480 },
{ 800, 600 }, /* things go strange above this... */
#endif
{ 0, 0 },
};
#if defined (SYS_PSP) || SYS_NDS
#define MODE_MAX 0
#else
#define MODE_MAX 4
#endif
#define Wrap(var, min, max) \
{ \
if (var > max) var = min; \
if (var < min) var = max; \
}
static int mode_idx = 1;
GFX_Mode * Gfx_ModePrev(void)
{
mode_idx--;
Wrap(mode_idx, 0, MODE_MAX)
return &gfx_modelist[mode_idx];
}
GFX_Mode * Gfx_ModeNext(void)
{
mode_idx++;
Wrap(mode_idx, 0, MODE_MAX)
return &gfx_modelist[mode_idx];
}
static int ValidMode(int w, int h)
{
int i;
for (i = 0; ; i++) {
unsigned int m_w = gfx_modelist[i].w;
unsigned int m_h = gfx_modelist[i].h;
if (m_w == 0)
return 0;
if (m_w == w && m_h == h) {
mode_idx = i;
return 1;
}
}
return 0;
}
/* These are the default hints as used by the graphics subsystem */
int hints[HINT_END] = {
0, // HINT_FULLSCREEN
1, // HINT_WINDOW
#ifdef SYS_PSP
2, // HINT_SCALEFACTOR
#else
1, // HINT_SCALEFACTOR
#endif
#ifdef SYS_NDS
256, // HINT_WIDTH
192, // HINT_HEIGHT
#else
320, // HINT_WIDTH
240, // HINT_HEIGHT
#endif
0 // HINT_FORCEMODE
};
void Gfx_SetHint(const GFX_Hint h, const int val)
{
if (h < 0 || h >= HINT_END) return;
hints[h] = val;
}
#define Hint(h) hints[h]
int Gfx_GetHint(const GFX_Hint h)
{
if (h < 0 || h >= HINT_END) return 0;
return Hint(h);
}
SDL_Surface *screen = NULL;
/* probably not the best thing, but we need the performance */
int screen_w;
int screen_h;
/* Initialises the video subsystem.
Note: dynamic resolution change is not supported. */
int InitVideo(void)
{
char title[32];
SDL_Surface *new_screen = NULL;
int sdl_flags = 0;
int w, h = 0;
int rw, rh;
sdl_flags |= SDL_HWPALETTE;
sdl_flags |= SDL_SWSURFACE;
//#ifdef SYS_PSP
// new_screen = SDL_SetVideoMode(480, 272, 8, SDL_HWPALETTE | SDL_SWSURFACE);
//#endif
if (Hint(HINT_FULLSCREEN)) sdl_flags |= SDL_FULLSCREEN;
if (screen == NULL) {
rw = w = Hint(HINT_WIDTH);
rh = h = Hint(HINT_HEIGHT);
} else {
/* We do this because the game dies horribly if you try to
dynamically change the _virtual_ resolution */
rw = w = screen_w;
rh = h = screen_h;
}
if (Hint(HINT_SCALEFACTOR) > 1) {
rw *= Hint(HINT_SCALEFACTOR);
rh *= Hint(HINT_SCALEFACTOR);
}
if (!Hint(HINT_FORCEMODE)) {
if (!ValidMode(w, h)) {
printf("!!! Invalid Video Mode %dx%d\n", w, h);
return -1;
}
} else {
printf("\n");
printf(" BIG FAT WARNING: If this blows up in your face,\n");
printf(" and mutilates your cat, please don't cry.\n");
printf("\n");
}
printf("Window dimensions:\t%dx%d\n", rw, rh);
new_screen = SDL_SetVideoMode(rw, rh, 8, sdl_flags);
#ifdef _NDS_
SDL_ShowCursor(SDL_DISABLE);
#endif
if (new_screen == NULL) {
printf("ERROR: InitVideo: %s\n", SDL_GetError() );
return -1;
}
if (screen == NULL) { /* only do this the first time */
debug("setting caption and icon...\n");
sprintf(title, "C-Dogs %s [Port %s]", CDOGS_VERSION, CDOGS_SDL_VERSION);
SDL_WM_SetCaption(title, NULL);
SDL_WM_SetIcon(SDL_LoadBMP(GetDataFilePath("cdogs_icon.bmp")), NULL);
SDL_ShowCursor(SDL_DISABLE);
} else {
debug("Changed video mode...\n");
}
if (screen == NULL) {
screen_w = Hint(HINT_WIDTH);
screen_h = Hint(HINT_HEIGHT);
}
screen = new_screen;
SetClip(0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1);
debug("Internal dimensions:\t%dx%d\n", SCREEN_WIDTH, SCREEN_HEIGHT);
SetPalette(gPalette);
return 0;
}
void ShutDownVideo(void)
{
debug("Shutting down video...\n");
SDL_VideoQuit();
}
typedef struct _Pic {
short int w;
short int h;
char *data;
} Pic;
int ReadPics(const char *filename, void **pics, int maxPics,
color * palette)
{
FILE *f;
int eof = 0;
unsigned short int size;
int i = 0;
f = fopen(filename, "rb");
if (f != NULL) {
if (palette)
fread(palette, sizeof(TPalette), 1, f);
else
fseek(f, sizeof(TPalette), SEEK_CUR);
while (!eof && i < maxPics) {
fread(&size, sizeof(size), 1, f);
swap16(&size);
if (size) {
Pic *p = sys_mem_alloc(size);
f_read16(f, &p->w, 2);
f_read16(f, &p->h, 2);
f_read(f, &p->data, size - 4);
pics[i] = p;
if (ferror(f) || feof(f))
eof = 1;
} else {
pics[i] = NULL;
}
i++;
}
fclose(f);
}
return i;
}
int AppendPics(const char *filename, void **pics, int startIndex,
int maxPics)
{
FILE *f;
int eof = 0;
unsigned short int size;
int i = startIndex;
f = fopen(filename, "rb");
if (f != NULL) {
fseek(f, sizeof(TPalette), SEEK_CUR);
while (!eof && i < maxPics) {
fread(&size, sizeof(size), 1, f);
swap16(&size);
if (size) {
Pic *p = sys_mem_alloc(size);
f_read16(f, &p->w, 2);
f_read16(f, &p->h, 2);
f_read(f, &p->data, size - 4);
pics[i] = p;
if (ferror(f) || feof(f))
eof = 1;
} else {
pics[i] = NULL;
}
i++;
}
fclose(f);
}
return i - startIndex;
}
int CompilePics(int picCount, void **pics, void **compiledPics)
{
int i, size, total = 0;
int skipped = 0;
for (i = 0; i < picCount; i++) {
if (pics[i]) {
size = compileSprite(pics[i], NULL);
total += size;
if (size) {
compiledPics[i] = sys_mem_alloc(size);
compileSprite(pics[i], compiledPics[i]);
} else {
compiledPics[i] = NULL;
skipped++;
}
} else
compiledPics[i] = NULL;
}
if (skipped)
printf("%d solid pics not compiled\n", skipped);
return total;
}
int RLEncodePics(int picCount, void **pics, void **rlePics)
{
int i, size, total = 0;
int skipped = 0;
for (i = 0; i < picCount; i++) {
if (pics[i]) {
size = RLEncodeSprite(pics[i], NULL);
total += size;
if (size) {
rlePics[i] = sys_mem_alloc(size);
RLEncodeSprite(pics[i], rlePics[i]);
} else {
rlePics[i] = NULL;
skipped++;
}
} else
rlePics[i] = NULL;
}
if (skipped)
printf("%d solid pics not RLE'd\n", skipped);
return total;
}
#ifndef _MSC_VER
inline
#endif
int PicWidth(void *pic)
{
if (!pic)
return 0;
return ((short *) pic)[0];
}
#ifndef _MSC_VER
inline
#endif
int PicHeight(void *pic)
{
if (!pic)
return 0;
return ((short *) pic)[1];
}
void SetColorZero(int r, int g, int b)
{
SDL_Color col;
col.r = r;
col.g = g;
col.b = b;
SDL_SetPalette(screen, SDL_PHYSPAL, &col, 0, 1);
return;
}