comparison test/testbitmap.c @ 1668:4da1ee79c9af SDL-1.3

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