16bd1c6d14436d7eff90295d9bfcc02a332f42c9
[zeRace] / sdl.c
1 /* some useful functions for zeRace game */
2
3 #include "sdl.h"
4
5 SDL_Surface *font=NULL;
6
7 /* prints a message using the bitmap font */
8 void print(SDL_Surface *dst,int x,int y,unsigned char *text)
9 {
10   SDL_Rect srcpos,dstpos;
11   /* on the first call, load the font picture */
12   if (!font) if ((font=IMG_Load("sprites/font.png"))==NULL)
13     fprintf(stderr,"could not load font file\n");
14   while (*text!='\0')
15   {
16     if (*text>=' ' && *text<' '+16*10)
17     srcpos.x=(*text-' ')%16*10;
18     srcpos.y=(*text-' ')/16*19;
19     srcpos.w=10;
20     srcpos.h=19;
21     dstpos.x=x;
22     dstpos.y=y;
23     SDL_BlitSurface(font,&srcpos,dst,&dstpos);
24     x+=10;
25     text++;
26   }
27 }
28
29 /* reads a string into the "text" variable */
30 void readstring(SDL_Surface *dst,int x,int y,unsigned char *text,int limit)
31 {
32   SDL_Event event;
33   int shift=0;
34
35   SDL_EnableUNICODE(1);
36   for (;;)
37   {
38     while (SDL_PollEvent(&event)) switch (event.type)
39     {
40       case SDL_KEYDOWN:
41         switch (event.key.keysym.sym)
42         {
43           case SDLK_ESCAPE:
44           case SDLK_RETURN:
45             return;
46           case SDLK_LSHIFT:
47           case SDLK_RSHIFT:
48             shift=1;
49             break;
50           case SDLK_BACKSPACE:
51             if (strlen(text))
52             {
53               text[strlen(text)-1]='\0';
54               print(dst,x+strlen(text)*10,y,"  ");
55             }
56             break;
57           default:
58             if ((event.key.keysym.unicode & 0xff80)==0 && strlen(text)<limit)
59             {
60               text[strlen(text)+1]='\0';
61                     text[strlen(text)]=event.key.keysym.unicode & 0x7f;
62             }
63             break;
64         }
65         break;
66     }
67     print(dst,x,y,text);
68     print(dst,x+strlen(text)*10,y,"_");
69     SDL_Flip(dst);
70     SDL_Delay(10);
71   }
72 }
73
74 /*
75  * Return the pixel value at (x, y)
76  * NOTE: The surface must be locked before calling this!
77  */
78 Uint32 getpixel(SDL_Surface *surface, int x, int y)
79 {
80     int bpp = surface->format->BytesPerPixel;
81     /* Here p is the address to the pixel we want to retrieve */
82     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
83
84     switch(bpp) {
85     case 1:
86         return *p;
87
88     case 2:
89         return *(Uint16 *)p;
90
91     case 3:
92         if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
93             return p[0] << 16 | p[1] << 8 | p[2];
94         else
95             return p[0] | p[1] << 8 | p[2] << 16;
96
97     case 4:
98         return *(Uint32 *)p;
99
100     default:
101         return 0;       /* shouldn't happen, but avoids warnings */
102     }
103 }
104
105 /*
106  * Set the pixel at (x, y) to the given value
107  * NOTE: The surface must be locked before calling this!
108  */
109 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
110 {
111     int bpp = surface->format->BytesPerPixel;
112     /* Here p is the address to the pixel we want to set */
113     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
114
115     switch(bpp) {
116     case 1:
117         *p = pixel;
118         break;
119
120     case 2:
121         *(Uint16 *)p = pixel;
122         break;
123
124     case 3:
125         if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
126             p[0] = (pixel >> 16) & 0xff;
127             p[1] = (pixel >> 8) & 0xff;
128             p[2] = pixel & 0xff;
129         } else {
130             p[0] = pixel & 0xff;
131             p[1] = (pixel >> 8) & 0xff;
132             p[2] = (pixel >> 16) & 0xff;
133         }
134         break;
135
136     case 4:
137         *(Uint32 *)p = pixel;
138         break;
139     }
140 }