3 SDL_Surface *font=NULL;
5 void print(SDL_Surface *dst,int x,int y,unsigned char *text)
7 SDL_Rect srcpos,dstpos;
8 if (!font) if ((font=IMG_Load("sprites/font.png"))==NULL)
10 fprintf(stderr,"could not load font file\n",SDL_GetError());
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);
28 * Return the pixel value at (x, y)
29 * NOTE: The surface must be locked before calling this!
31 Uint32 getpixel(SDL_Surface *surface, int x, int y)
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;
45 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
46 return p[0] << 16 | p[1] << 8 | p[2];
48 return p[0] | p[1] << 8 | p[2] << 16;
54 return 0; /* shouldn't happen, but avoids warnings */
59 * Set the pixel at (x, y) to the given value
60 * NOTE: The surface must be locked before calling this!
62 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
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;
78 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
79 p[0] = (pixel >> 16) & 0xff;
80 p[1] = (pixel >> 8) & 0xff;
84 p[1] = (pixel >> 8) & 0xff;
85 p[2] = (pixel >> 16) & 0xff;
95 /* Set pixel function */
96 void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
103 /* Lock the screen, if needed */
104 if(SDL_MUSTLOCK(screen)) {
105 if(SDL_LockSurface(screen) < 0)
110 color = SDL_MapRGB( screen->format, r, g, b );
112 /* How we draw the pixel depends on the bitdepth */
113 switch(screen->format->BytesPerPixel) {
115 ubuff8 = (Uint8*) screen->pixels;
116 ubuff8 += (y * screen->pitch) + x;
117 *ubuff8 = (Uint8) color;
120 ubuff16 = (Uint16*) screen->pixels;
121 ubuff16 += ((y * screen->pitch)>>2) + x;
122 *ubuff16 = (Uint16) color;
125 ubuff8 = (Uint8*) screen->pixels;
126 ubuff8 += (y * screen->pitch) + x;
128 r = (color>>screen->format->Rshift)&0xFF;
129 g = (color>>screen->format->Gshift)&0xFF;
130 b = (color>>screen->format->Bshift)&0xFF;
138 ubuff32 = (Uint32*) screen->pixels;
139 ubuff32 += ((y*screen->pitch)>>2) + x;
144 fprintf(stderr, "Error: Unknown bitdepth!\n");
147 /* Unlock the screen if needed */
148 if(SDL_MUSTLOCK(screen)) {
149 SDL_UnlockSurface(screen);