]> icculus.org git repositories - btb/d2x.git/blob - console/internal.c
make console optional, other fixes
[btb/d2x.git] / console / internal.c
1 /*  internal.c
2  *  Written By: Garrett Banuk <mongoose@mongeese.org>
3  *  This is free, just be sure to give me credit when using it
4  *  in any of your programs.
5  */
6
7 /* internal.[c,h] are various routines used internally
8  * by SDL_console and DrawText. */
9
10 #include "SDL.h"
11
12
13 /*
14  * Return the pixel value at (x, y)
15  * NOTE: The surface must be locked before calling this!
16  */
17 Uint32 DT_GetPixel(SDL_Surface *surface, int x, int y) {
18         int bpp = surface->format->BytesPerPixel;
19         /* Here p is the address to the pixel we want to retrieve */
20         Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * bpp;
21
22         switch (bpp) {
23         case 1:
24                 return *p;
25         case 2:
26                 return *(Uint16 *) p;
27         case 3:
28                 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
29                         return p[0] << 16 | p[1] << 8 | p[2];
30                 else
31                         return p[0] | p[1] << 8 | p[2] << 16;
32         case 4:
33                 return *(Uint32 *) p;
34         default:
35                 return 0;       /* shouldn't happen, but avoids warnings */
36         }
37 }
38
39 /*
40  * Set the pixel at (x, y) to the given value
41  * NOTE: The surface must be locked before calling this!
42  */
43 void DT_PutPixel(SDL_Surface *surface, int x, int y, Uint32 pixel) {
44         int bpp = surface->format->BytesPerPixel;
45         /* Here p is the address to the pixel we want to set */
46         Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * bpp;
47
48         switch (bpp) {
49         case 1:
50                 *p = pixel;
51                 break;
52         case 2:
53                 *(Uint16 *) p = pixel;
54                 break;
55         case 3:
56                 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
57                         p[0] = (pixel >> 16) & 0xff;
58                         p[1] = (pixel >> 8) & 0xff;
59                         p[2] = pixel & 0xff;
60                 } else {
61                         p[0] = pixel & 0xff;
62                         p[1] = (pixel >> 8) & 0xff;
63                         p[2] = (pixel >> 16) & 0xff;
64                 }
65                 break;
66         case 4:
67                 *(Uint32 *) p = pixel;
68                 break;
69         default:
70                 break;
71         }
72 }
73
74
75