3 SDL_Surface *font=NULL;
5 /* prints a message using the bitmap font */
6 void print(SDL_Surface *dst,int x,int y,unsigned char *text)
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");
14 if (*text>=' ' && *text<' '+16*10)
15 srcpos.x=(*text-' ')%16*10;
16 srcpos.y=(*text-' ')/16*19;
21 SDL_BlitSurface(font,&srcpos,dst,&dstpos);
27 /* reads a string into the "text" variable */
28 void readstring(SDL_Surface *dst,int x,int y,unsigned char *text,int limit)
36 while (SDL_PollEvent(&event)) switch (event.type)
39 switch (event.key.keysym.sym)
51 text[strlen(text)-1]='\0';
52 print(dst,x+strlen(text)*10,y," ");
56 if ((event.key.keysym.unicode & 0xff80)==0 && strlen(text)<limit)
58 text[strlen(text)+1]='\0';
59 text[strlen(text)]=event.key.keysym.unicode & 0x7f;
66 print(dst,x+strlen(text)*10,y,"_");
73 * Return the pixel value at (x, y)
74 * NOTE: The surface must be locked before calling this!
76 Uint32 getpixel(SDL_Surface *surface, int x, int y)
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;
90 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
91 return p[0] << 16 | p[1] << 8 | p[2];
93 return p[0] | p[1] << 8 | p[2] << 16;
99 return 0; /* shouldn't happen, but avoids warnings */
104 * Set the pixel at (x, y) to the given value
105 * NOTE: The surface must be locked before calling this!
107 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
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;
119 *(Uint16 *)p = pixel;
123 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
124 p[0] = (pixel >> 16) & 0xff;
125 p[1] = (pixel >> 8) & 0xff;
129 p[1] = (pixel >> 8) & 0xff;
130 p[2] = (pixel >> 16) & 0xff;
135 *(Uint32 *)p = pixel;