comparison test/testbitmap.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents be9c9c8f6d53
children 7e449145b6ca f55c87ae336b
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
7 7
8 #include "SDL.h" 8 #include "SDL.h"
9 #include "picture.xbm" 9 #include "picture.xbm"
10 10
11 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 11 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
12 static void quit(int rc) 12 static void
13 quit(int rc)
13 { 14 {
14 SDL_Quit(); 15 SDL_Quit();
15 exit(rc); 16 exit(rc);
16 } 17 }
17 18
18 SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits) 19 SDL_Surface *
20 LoadXBM(SDL_Surface * screen, int w, int h, Uint8 * bits)
19 { 21 {
20 SDL_Surface *bitmap; 22 SDL_Surface *bitmap;
21 Uint8 *line; 23 Uint8 *line;
22 24
23 /* Allocate the bitmap */ 25 /* Allocate the bitmap */
24 bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0); 26 bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
25 if ( bitmap == NULL ) { 27 if (bitmap == NULL) {
26 fprintf(stderr, "Couldn't allocate bitmap: %s\n", 28 fprintf(stderr, "Couldn't allocate bitmap: %s\n", SDL_GetError());
27 SDL_GetError()); 29 return (NULL);
28 return(NULL); 30 }
29 }
30 31
31 /* Copy the pixels */ 32 /* Copy the pixels */
32 line = (Uint8 *)bitmap->pixels; 33 line = (Uint8 *) bitmap->pixels;
33 w = (w+7)/8; 34 w = (w + 7) / 8;
34 while ( h-- ) { 35 while (h--) {
35 memcpy(line, bits, w); 36 memcpy(line, bits, w);
36 /* X11 Bitmap images have the bits reversed */ 37 /* X11 Bitmap images have the bits reversed */
37 { int i, j; Uint8 *buf, byte; 38 {
38 for ( buf=line, i=0; i<w; ++i, ++buf ) { 39 int i, j;
39 byte = *buf; 40 Uint8 *buf, byte;
40 *buf = 0; 41 for (buf = line, i = 0; i < w; ++i, ++buf) {
41 for ( j=7; j>=0; --j ) { 42 byte = *buf;
42 *buf |= (byte&0x01)<<j; 43 *buf = 0;
43 byte >>= 1; 44 for (j = 7; j >= 0; --j) {
44 } 45 *buf |= (byte & 0x01) << j;
45 } 46 byte >>= 1;
46 } 47 }
47 line += bitmap->pitch; 48 }
48 bits += w; 49 }
49 } 50 line += bitmap->pitch;
50 return(bitmap); 51 bits += w;
52 }
53 return (bitmap);
51 } 54 }
52 55
53 int main(int argc, char *argv[]) 56 int
57 main(int argc, char *argv[])
54 { 58 {
55 SDL_Surface *screen; 59 SDL_Surface *screen;
56 SDL_Surface *bitmap; 60 SDL_Surface *bitmap;
57 Uint8 video_bpp; 61 Uint8 video_bpp;
58 Uint32 videoflags; 62 Uint32 videoflags;
59 Uint8 *buffer; 63 Uint8 *buffer;
60 int i, k, done; 64 int i, k, done;
61 SDL_Event event; 65 SDL_Event event;
62 Uint16 *buffer16; 66 Uint16 *buffer16;
63 Uint16 color; 67 Uint16 color;
64 Uint8 gradient; 68 Uint8 gradient;
65 SDL_Color palette[256]; 69 SDL_Color palette[256];
66 70
67 71
68 /* Initialize SDL */ 72 /* Initialize SDL */
69 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { 73 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
70 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 74 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
71 return(1); 75 return (1);
72 } 76 }
73 77
74 video_bpp = 0; 78 video_bpp = 0;
75 videoflags = SDL_SWSURFACE; 79 videoflags = SDL_SWSURFACE;
76 while ( argc > 1 ) { 80 while (argc > 1) {
77 --argc; 81 --argc;
78 if ( strcmp(argv[argc-1], "-bpp") == 0 ) { 82 if (strcmp(argv[argc - 1], "-bpp") == 0) {
79 video_bpp = atoi(argv[argc]); 83 video_bpp = atoi(argv[argc]);
80 --argc; 84 --argc;
81 } else 85 } else if (strcmp(argv[argc], "-warp") == 0) {
82 if ( strcmp(argv[argc], "-warp") == 0 ) { 86 videoflags |= SDL_HWPALETTE;
83 videoflags |= SDL_HWPALETTE; 87 } else if (strcmp(argv[argc], "-hw") == 0) {
84 } else 88 videoflags |= SDL_HWSURFACE;
85 if ( strcmp(argv[argc], "-hw") == 0 ) { 89 } else if (strcmp(argv[argc], "-fullscreen") == 0) {
86 videoflags |= SDL_HWSURFACE; 90 videoflags |= SDL_FULLSCREEN;
87 } else 91 } else {
88 if ( strcmp(argv[argc], "-fullscreen") == 0 ) { 92 fprintf(stderr,
89 videoflags |= SDL_FULLSCREEN; 93 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
90 } else { 94 argv[0]);
91 fprintf(stderr, 95 quit(1);
92 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", 96 }
93 argv[0]); 97 }
94 quit(1);
95 }
96 }
97 98
98 /* Set 640x480 video mode */ 99 /* Set 640x480 video mode */
99 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { 100 if ((screen = SDL_SetVideoMode(640, 480, video_bpp, videoflags)) == NULL) {
100 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", 101 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
101 video_bpp, SDL_GetError()); 102 video_bpp, SDL_GetError());
102 quit(2); 103 quit(2);
103 } 104 }
104 105
105 if (video_bpp==8) { 106 if (video_bpp == 8) {
106 /* Set a gray colormap, reverse order from white to black */ 107 /* Set a gray colormap, reverse order from white to black */
107 for ( i=0; i<256; ++i ) { 108 for (i = 0; i < 256; ++i) {
108 palette[i].r = 255-i; 109 palette[i].r = 255 - i;
109 palette[i].g = 255-i; 110 palette[i].g = 255 - i;
110 palette[i].b = 255-i; 111 palette[i].b = 255 - i;
111 } 112 }
112 SDL_SetColors(screen, palette, 0, 256); 113 SDL_SetColors(screen, palette, 0, 256);
113 } 114 }
114 115
115 /* Set the surface pixels and refresh! */ 116 /* Set the surface pixels and refresh! */
116 if ( SDL_LockSurface(screen) < 0 ) { 117 if (SDL_LockSurface(screen) < 0) {
117 fprintf(stderr, "Couldn't lock the display surface: %s\n", 118 fprintf(stderr, "Couldn't lock the display surface: %s\n",
118 SDL_GetError()); 119 SDL_GetError());
119 quit(2); 120 quit(2);
120 } 121 }
121 buffer=(Uint8 *)screen->pixels; 122 buffer = (Uint8 *) screen->pixels;
122 if (screen->format->BytesPerPixel!=2) { 123 if (screen->format->BytesPerPixel != 2) {
123 for ( i=0; i<screen->h; ++i ) { 124 for (i = 0; i < screen->h; ++i) {
124 memset(buffer,(i*255)/screen->h, screen->pitch); 125 memset(buffer, (i * 255) / screen->h, screen->pitch);
125 buffer += screen->pitch; 126 buffer += screen->pitch;
126 }
127 } 127 }
128 else 128 } else {
129 { 129 for (i = 0; i < screen->h; ++i) {
130 for ( i=0; i<screen->h; ++i ) { 130 gradient = ((i * 255) / screen->h);
131 gradient=((i*255)/screen->h); 131 color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
132 color = SDL_MapRGB(screen->format, gradient, gradient, gradient); 132 buffer16 = (Uint16 *) buffer;
133 buffer16=(Uint16*)buffer; 133 for (k = 0; k < screen->w; k++) {
134 for (k=0; k<screen->w; k++) 134 *(buffer16 + k) = color;
135 { 135 }
136 *(buffer16+k)=color; 136 buffer += screen->pitch;
137 }
138 buffer += screen->pitch;
139 }
140 } 137 }
141 SDL_UnlockSurface(screen); 138 }
142 SDL_UpdateRect(screen, 0, 0, 0, 0); 139 SDL_UnlockSurface(screen);
140 SDL_UpdateRect(screen, 0, 0, 0, 0);
143 141
144 /* Load the bitmap */ 142 /* Load the bitmap */
145 bitmap = LoadXBM(screen, picture_width, picture_height, 143 bitmap = LoadXBM(screen, picture_width, picture_height,
146 (Uint8 *)picture_bits); 144 (Uint8 *) picture_bits);
147 if ( bitmap == NULL ) { 145 if (bitmap == NULL) {
148 quit(1); 146 quit(1);
149 } 147 }
150 148
151 /* Wait for a keystroke */ 149 /* Wait for a keystroke */
152 done = 0; 150 done = 0;
153 while ( !done ) { 151 while (!done) {
154 /* Check for events */ 152 /* Check for events */
155 while ( SDL_PollEvent(&event) ) { 153 while (SDL_PollEvent(&event)) {
156 switch (event.type) { 154 switch (event.type) {
157 case SDL_MOUSEBUTTONDOWN: { 155 case SDL_MOUSEBUTTONDOWN:
158 SDL_Rect dst; 156 {
157 SDL_Rect dst;
159 158
160 dst.x = event.button.x - bitmap->w/2; 159 dst.x = event.button.x - bitmap->w / 2;
161 dst.y = event.button.y - bitmap->h/2; 160 dst.y = event.button.y - bitmap->h / 2;
162 dst.w = bitmap->w; 161 dst.w = bitmap->w;
163 dst.h = bitmap->h; 162 dst.h = bitmap->h;
164 SDL_BlitSurface(bitmap, NULL, 163 SDL_BlitSurface(bitmap, NULL, screen, &dst);
165 screen, &dst); 164 SDL_UpdateRects(screen, 1, &dst);
166 SDL_UpdateRects(screen,1,&dst); 165 }
167 } 166 break;
168 break; 167 case SDL_KEYDOWN:
169 case SDL_KEYDOWN: 168 /* Any key press quits the app... */
170 /* Any key press quits the app... */ 169 done = 1;
171 done = 1; 170 break;
172 break; 171 case SDL_QUIT:
173 case SDL_QUIT: 172 done = 1;
174 done = 1; 173 break;
175 break; 174 default:
176 default: 175 break;
177 break; 176 }
178 } 177 }
179 } 178 }
180 } 179 SDL_FreeSurface(bitmap);
181 SDL_FreeSurface(bitmap); 180 SDL_Quit();
182 SDL_Quit(); 181 return (0);
183 return(0);
184 } 182 }