6
#define RECTS_NUM 40000
7
char DATAPATH[200]=DATADIR;
8
const char PATH[PATHNUM][200]={DATADIR,".","data","/usr/local/share/bumprace"
9
,"/usr/lib/bumprace","../data","/usr/share/bumprace",DATADIR};
10
SDL_Surface *Screen,*BackBuffer,*fadebuffer;
11
SDL_Rect blitrect,blitrects[RECTS_NUM];
14
void ComplainAndExit(void)
16
fprintf(stderr, "Problem: %s\n", SDL_GetError());
20
int abrand(int a,int b) //random number between a and b (inclusive)
22
return(a+(rand() % (b-a+1)));
25
int (*_PutPixel)(SDL_Surface *Surface, Sint32 X, Sint32 Y, Uint32 Color);
27
int fast_putpixel1(SDL_Surface *Surface, Sint32 X, Sint32 Y, Uint32 Color)
29
if (X < 0 || X > Surface->w || Y < 0 || Y > Surface->h)
32
*((Uint8 *)Surface->pixels + Y * Surface->pitch + X) = Color;
37
int fast_putpixel2(SDL_Surface *Surface, Sint32 X, Sint32 Y, Uint32 Color)
39
if (X < 0 || X > Surface->w || Y < 0 || Y > Surface->h)
42
*((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X) = Color;
47
int fast_putpixel3(SDL_Surface *Surface, Sint32 X, Sint32 Y, Uint32 Color)
52
if (X < 0 || X > Surface->w || Y < 0 || Y > Surface->h)
55
/* Gack - slow, but endian correct */
56
pix = (Uint8 *)Surface->pixels + Y * Surface->pitch + X*3;
57
shift = Surface->format->Rshift;
58
*(pix+shift/8) = Color>>shift;
59
shift = Surface->format->Gshift;
60
*(pix+shift/8) = Color>>shift;
61
shift = Surface->format->Bshift;
62
*(pix+shift/8) = Color>>shift;
67
int fast_putpixel4(SDL_Surface *Surface, Sint32 X, Sint32 Y, Uint32 Color)
69
if (X < 0 || X > Surface->w || Y < 0 || Y > Surface->h)
72
*((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X) = Color;
77
void init_SDL() // sets the video mode
80
const SDL_VideoInfo *info;
82
if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0 ) {ComplainAndExit();}
83
info = SDL_GetVideoInfo();
84
if (info->vfmt->BitsPerPixel==8) bpp=16;
86
// Set the video mode (800x600 at 16-bit depth)
88
Screen = SDL_SetVideoMode(800, 600, BPP, SDL_FULLSCREEN);
90
{ Screen = SDL_SetVideoMode(800, 600, BPP, 0); }
91
if ( Screen == NULL ) {ComplainAndExit();}
93
BackBuffer = SDL_AllocSurface(Screen->flags,
96
Screen->format->BitsPerPixel,
97
Screen->format->Rmask,
98
Screen->format->Gmask,
99
Screen->format->Bmask, 0);
100
if (BackBuffer == NULL)
101
printf("ERROR: Couldn't create BackBuffer: %s\n", SDL_GetError());
102
fadebuffer = SDL_AllocSurface(Screen->flags,
105
Screen->format->BitsPerPixel,
106
Screen->format->Rmask,
107
Screen->format->Gmask,
108
Screen->format->Bmask, 0);
109
if (fadebuffer == NULL)
110
printf("ERROR: Couldn't create fadebuffer: %s\n", SDL_GetError());
111
// Figure out what putpixel routine to use
112
switch (Screen->format->BytesPerPixel)
115
_PutPixel = fast_putpixel1;
118
_PutPixel = fast_putpixel2;
121
_PutPixel = fast_putpixel3;
124
_PutPixel = fast_putpixel4;
131
if ( SDL_MUSTLOCK(Screen) ) {
132
if ( SDL_LockSurface(Screen) < 0 )
138
if ( SDL_MUSTLOCK(Screen) ) {
139
SDL_UnlockSurface(Screen); }
142
// Performs Callback at each line point. This came straight from the
143
// asphyxia vga trainers
144
int DoLine (SDL_Surface *Surface, Sint32 X1, Sint32 Y1, Sint32 X2, Sint32 Y2, Uint32 Color, int Callback (SDL_Surface *Surf, Sint32 X, Sint32 Y, Uint32 Color))
146
Sint32 dx, dy, sdx, sdy, x, y, px, py;
151
sdx = (dx < 0) ? -1 : 1;
152
sdy = (dy < 0) ? -1 : 1;
164
for (x = 0; x < dx; x++)
166
Callback(Surface, px, py, Color);
179
for (y = 0; y < dy; y++)
182
Callback(Surface, px, py, Color);
196
// The user's line drawing function
197
void Line(Sint32 X1, Sint32 Y1, Sint32 X2, Sint32 Y2, Uint32 Color)
0
202
// Make sure X1 is before X2
208
// Make sure Y1 is before Y2
1
216
/* Draw the line */
217
DoLine(Screen, X1, Y1, X2, Y2, Color, &PutPixel);
225
if (dofadeout==0) return;
232
PutPixel(Screen,x,y*2,SDL_MapRGB(Screen->format,0,0,0));
242
PutPixel(Screen,x,y*2+1,SDL_MapRGB(Screen->format,0,0,0));
251
SDL_UpdateRects(Screen,blitrects_num,blitrects);
255
void AddRect(int x1, int y1, int x2, int y2)
2
259
/* Make sure X1 is before X2 */
265
/* Make sure Y1 is before Y2 */
273
blitrect.w = x2-x1+1;
274
blitrect.h = y2-y1+1;
275
if (x1<0) printf("x is too small in function AddRect! - %d\n",x1);else
276
if (y1<0) printf("y is too small in function AddRect! - %d\n",y1);else
277
if (x2>=800) printf("x is too big in function AddRect! - %d\n",x2);else
278
if (y2>=600) printf("y is too big in function AddRect! - %d\n",y2);else {
279
blitrects[blitrects_num]=blitrect;
280
if (++blitrects_num>=RECTS_NUM-2)
281
{printf("Too many blits!\n");blitrects_num--;Update();}
285
void AddThisRect(SDL_Rect blitrect)
287
blitrects[blitrects_num]=blitrect;
288
if (++blitrects_num>=RECTS_NUM-2)
289
{printf("Too many blits!\n");blitrects_num--;Update();}
292
void Blit(int Xpos,int Ypos,SDL_Surface *image) //blits one GIF or BMP from the memory to the screen
296
blitrect.w = image->w;
297
blitrect.h = image->h;
299
if (Xpos<-image->w) printf("WRONG BLIT: Xpos is too small! - %d\n",Xpos); else
300
if (Xpos>=800) printf("WRONG BLIT: Xpos is too big! - %d\n",Xpos); else
301
if (Ypos<-image->h) printf("WRONG BLIT: Ypos is too small! - %d\n",Ypos); else
302
if (Ypos>=600) printf("WRONG BLIT: Ypos is too big! - %d\n",Ypos); else
303
if ( SDL_BlitSurface(image, NULL, Screen, &blitrect) < 0 )
305
SDL_FreeSurface(image);
308
blitrects[blitrects_num]=blitrect;
312
SDL_Surface *LoadImage(char *datafile, int transparent) // reads one png into the memory
314
SDL_Surface *pic,*pic2;
318
sprintf(filename,"%s/gfx/%s",DATAPATH,datafile);
319
pic=IMG_Load(filename);
320
while ( pic == NULL ) {
321
strcpy(DATAPATH,PATH[i]);
322
sprintf(filename,"%s/gfx/%s",DATAPATH,datafile);
323
pic=IMG_Load(filename);
328
fprintf(stderr,"Couldn't load %s: %s\n", filename, SDL_GetError());
332
if (transparent==3) return(pic);
334
SDL_SetColorKey(pic,SDL_SRCCOLORKEY|SDL_RLEACCEL,SDL_MapRGB(pic->format,0xFF,0xFF,0xFF));
336
SDL_SetColorKey(pic,SDL_SRCCOLORKEY|SDL_RLEACCEL,SDL_MapRGB(pic->format,0x00,0x00,0x00));
337
pic2 = SDL_DisplayFormat(pic);
338
SDL_FreeSurface(pic);
340
// SDL_UpdateRect(screen,0,0,0,0);
344
void BlitToBB(int Xpos,int Ypos,SDL_Surface *image) //blits one GIF or BMP from the memory to the screen
348
blitrect.w = image->w;
349
blitrect.h = image->h;
350
if ( SDL_BlitSurface(image, NULL, BackBuffer, &blitrect) < 0 )
352
SDL_FreeSurface(image);
357
void BlitPart(int Xpos,int Ypos,SDL_Surface *image, SDL_Rect srcrect)
359
blitrect.x = srcrect.x;
360
blitrect.y = srcrect.y;
361
blitrect.w = srcrect.w;
362
blitrect.h = srcrect.h;
363
if ( SDL_BlitSurface(image, &srcrect , Screen, &blitrect) < 0 )
365
SDL_FreeSurface(image);
368
blitrects[blitrects_num]=blitrect;
372
void FadeScreen(float speed)
376
SDL_BlitSurface(Screen,NULL,fadebuffer,NULL);
378
for (i=255*speed;i>=0;i-=SDL_GetTicks()-now)
381
SDL_BlitSurface(fadebuffer,&blitrect,Screen,&blitrect);
382
SDL_SetAlpha(BackBuffer,SDL_SRCALPHA,255-(int)(i/speed));
383
Blit(0,0,BackBuffer);
386
SDL_SetAlpha(BackBuffer,SDL_SRCALPHA,255);
387
Blit(0,0,BackBuffer);
391
int PutPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y, Uint32 Color)
393
if (X<0) printf("X < 0 in function PutPixel! - %d\n",X); else
394
if (X>=800) printf("X >= 800 in function PutPixel! - %d\n",X); else
395
if (Y<0) printf("Y < 0 in function PutPixel! - %d\n",Y); else
396
if (Y>=600) printf("Y >= 600 in function PutPixel! - %d\n",Y); else
398
_PutPixel(Surface,X,Y,Color);
404
int PutPixelC(SDL_Surface *Surface, Sint32 X, Sint32 Y, Uint32 Color)
406
if (X<0) printf("X < 0 in function PutPixelC! - %d\n",X); else
407
if (X>=800) printf("X >= 800 in function PutPixelC! - %d\n",X); else
408
if (Y<0) printf("Y < 0 in function PutPixelC! - %d\n",Y); else
409
if (Y>=600) printf("Y >= 600 in function PutPixelC! - %d\n",Y); else
411
_PutPixel(Surface,X,Y,Color);
417
int PutBackPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
425
SDL_BlitSurface(BackBuffer, &rect, Surface, &rect);
429
Uint32 GetPixel (SDL_Surface *Surface, Sint32 X, Sint32 Y)
436
Bpp = Surface->format->BytesPerPixel;
438
bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
443
return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
446
return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
448
case 3: { // Format/endian independent
450
r = *((bits)+Surface->format->Rshift/8);
451
g = *((bits)+Surface->format->Gshift/8);
452
b = *((bits)+Surface->format->Bshift/8);
453
return SDL_MapRGB(Surface->format, r, g, b);
457
return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);