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