1 /* some useful functions for zeRace game */
5 SDL_Surface *font=NULL;
7 /* prints a message using the bitmap font */
8 void print(SDL_Surface *dst,int x,int y,unsigned char *text)
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");
16 if (*text>=' ' && *text<' '+16*10)
17 srcpos.x=(*text-' ')%16*10;
18 srcpos.y=(*text-' ')/16*19;
23 SDL_BlitSurface(font,&srcpos,dst,&dstpos);
29 /* reads a string into the "text" variable */
30 void readstring(SDL_Surface *dst,int x,int y,unsigned char *text,int limit)
38 while (SDL_PollEvent(&event)) switch (event.type)
41 switch (event.key.keysym.sym)
53 text[strlen(text)-1]='\0';
54 print(dst,x+strlen(text)*10,y," ");
58 if ((event.key.keysym.unicode & 0xff80)==0 && strlen(text)<limit)
60 text[strlen(text)+1]='\0';
61 text[strlen(text)]=event.key.keysym.unicode & 0x7f;
68 print(dst,x+strlen(text)*10,y,"_");
75 * Return the pixel value at (x, y)
76 * NOTE: The surface must be locked before calling this!
78 Uint32 getpixel(SDL_Surface *surface, int x, int y)
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;
92 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
93 return p[0] << 16 | p[1] << 8 | p[2];
95 return p[0] | p[1] << 8 | p[2] << 16;
101 return 0; /* shouldn't happen, but avoids warnings */
106 * Set the pixel at (x, y) to the given value
107 * NOTE: The surface must be locked before calling this!
109 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
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;
121 *(Uint16 *)p = pixel;
125 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
126 p[0] = (pixel >> 16) & 0xff;
127 p[1] = (pixel >> 8) & 0xff;
131 p[1] = (pixel >> 8) & 0xff;
132 p[2] = (pixel >> 16) & 0xff;
137 *(Uint32 *)p = pixel;