version 0.1
[zeRace] / sdl.c
1 #include "sdl.h"
2
3 SDL_Surface *font=NULL;
4
5 void print(SDL_Surface *dst,int x,int y,unsigned char *text)
6 {
7   SDL_Rect srcpos,dstpos;
8   if (!font) if ((font=IMG_Load("sprites/font.png"))==NULL)
9   {
10                 fprintf(stderr,"could not load font file\n",SDL_GetError());
11   }
12   while (*text!='\0')
13   {
14     if (*text>=' ' && *text<' '+16*10)
15     srcpos.x=(*text-' ')%16*10;
16     srcpos.y=(*text-' ')/16*19;
17     srcpos.w=10;
18     srcpos.h=19;
19     dstpos.x=x;
20     dstpos.y=y;
21     SDL_BlitSurface(font,&srcpos,dst,&dstpos);
22     x+=10;
23     text++;
24   }
25 }
26
27 /*
28  * Return the pixel value at (x, y)
29  * NOTE: The surface must be locked before calling this!
30  */
31 Uint32 getpixel(SDL_Surface *surface, int x, int y)
32 {
33     int bpp = surface->format->BytesPerPixel;
34     /* Here p is the address to the pixel we want to retrieve */
35     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
36
37     switch(bpp) {
38     case 1:
39         return *p;
40
41     case 2:
42         return *(Uint16 *)p;
43
44     case 3:
45         if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
46             return p[0] << 16 | p[1] << 8 | p[2];
47         else
48             return p[0] | p[1] << 8 | p[2] << 16;
49
50     case 4:
51         return *(Uint32 *)p;
52
53     default:
54         return 0;       /* shouldn't happen, but avoids warnings */
55     }
56 }
57
58 /*
59  * Set the pixel at (x, y) to the given value
60  * NOTE: The surface must be locked before calling this!
61  */
62 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
63 {
64     int bpp = surface->format->BytesPerPixel;
65     /* Here p is the address to the pixel we want to set */
66     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
67
68     switch(bpp) {
69     case 1:
70         *p = pixel;
71         break;
72
73     case 2:
74         *(Uint16 *)p = pixel;
75         break;
76
77     case 3:
78         if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
79             p[0] = (pixel >> 16) & 0xff;
80             p[1] = (pixel >> 8) & 0xff;
81             p[2] = pixel & 0xff;
82         } else {
83             p[0] = pixel & 0xff;
84             p[1] = (pixel >> 8) & 0xff;
85             p[2] = (pixel >> 16) & 0xff;
86         }
87         break;
88
89     case 4:
90         *(Uint32 *)p = pixel;
91         break;
92     }
93 }
94
95 /* Set pixel function */
96 void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
97 {
98         Uint8 *ubuff8;
99         Uint16 *ubuff16; 
100         Uint32 *ubuff32;
101         Uint32 color;
102         
103         /* Lock the screen, if needed */
104         if(SDL_MUSTLOCK(screen)) {
105                 if(SDL_LockSurface(screen) < 0) 
106                         return;
107         }
108  
109         /* Get the color */
110         color = SDL_MapRGB( screen->format, r, g, b );
111         
112         /* How we draw the pixel depends on the bitdepth */
113         switch(screen->format->BytesPerPixel) {
114                 case 1: 
115                         ubuff8 = (Uint8*) screen->pixels;
116                         ubuff8 += (y * screen->pitch) + x; 
117                         *ubuff8 = (Uint8) color;
118                         break;
119                 case 2:
120                         ubuff16 = (Uint16*) screen->pixels;
121                         ubuff16 += ((y * screen->pitch)>>2) + x;
122                         *ubuff16 = (Uint16) color; 
123                         break;  
124                 case 3:
125                         ubuff8 = (Uint8*) screen->pixels;
126                         ubuff8 += (y * screen->pitch) + x;
127                                         
128                         r = (color>>screen->format->Rshift)&0xFF;
129                         g = (color>>screen->format->Gshift)&0xFF;
130                         b = (color>>screen->format->Bshift)&0xFF;
131
132                         ubuff8[0] = r;
133                         ubuff8[1] = g;
134                         ubuff8[2] = b;
135                         break;
136                                 
137                 case 4:
138                         ubuff32 = (Uint32*) screen->pixels;
139                         ubuff32 += ((y*screen->pitch)>>2) + x;
140                         *ubuff32 = color;
141                         break;
142         
143                 default:
144                         fprintf(stderr, "Error: Unknown bitdepth!\n");
145         }
146
147         /* Unlock the screen if needed */
148         if(SDL_MUSTLOCK(screen)) {
149                 SDL_UnlockSurface(screen);
150         }
151 }