test/testbitmap.c
changeset 1895 c121d94672cb
parent 1151 be9c9c8f6d53
child 2431 7e449145b6ca
child 2765 f55c87ae336b
     1.1 --- a/test/testbitmap.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/test/testbitmap.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -9,176 +9,174 @@
     1.4  #include "picture.xbm"
     1.5  
     1.6  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     1.7 -static void quit(int rc)
     1.8 +static void
     1.9 +quit(int rc)
    1.10  {
    1.11 -	SDL_Quit();
    1.12 -	exit(rc);
    1.13 +    SDL_Quit();
    1.14 +    exit(rc);
    1.15  }
    1.16  
    1.17 -SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
    1.18 +SDL_Surface *
    1.19 +LoadXBM(SDL_Surface * screen, int w, int h, Uint8 * bits)
    1.20  {
    1.21 -	SDL_Surface *bitmap;
    1.22 -	Uint8 *line;
    1.23 +    SDL_Surface *bitmap;
    1.24 +    Uint8 *line;
    1.25  
    1.26 -	/* Allocate the bitmap */
    1.27 -	bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
    1.28 -	if ( bitmap == NULL ) {
    1.29 -		fprintf(stderr, "Couldn't allocate bitmap: %s\n",
    1.30 -						SDL_GetError());
    1.31 -		return(NULL);
    1.32 -	}
    1.33 +    /* Allocate the bitmap */
    1.34 +    bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
    1.35 +    if (bitmap == NULL) {
    1.36 +        fprintf(stderr, "Couldn't allocate bitmap: %s\n", SDL_GetError());
    1.37 +        return (NULL);
    1.38 +    }
    1.39  
    1.40 -	/* Copy the pixels */
    1.41 -	line = (Uint8 *)bitmap->pixels;
    1.42 -	w = (w+7)/8;
    1.43 -	while ( h-- ) {
    1.44 -		memcpy(line, bits, w);
    1.45 -		/* X11 Bitmap images have the bits reversed */
    1.46 -		{ int i, j; Uint8 *buf, byte;
    1.47 -			for ( buf=line, i=0; i<w; ++i, ++buf ) {
    1.48 -				byte = *buf;
    1.49 -				*buf = 0;
    1.50 -				for ( j=7; j>=0; --j ) {
    1.51 -					*buf |= (byte&0x01)<<j;
    1.52 -					byte >>= 1;
    1.53 -				}
    1.54 -			}
    1.55 -		}
    1.56 -		line += bitmap->pitch;
    1.57 -		bits += w;
    1.58 -	}
    1.59 -	return(bitmap);
    1.60 +    /* Copy the pixels */
    1.61 +    line = (Uint8 *) bitmap->pixels;
    1.62 +    w = (w + 7) / 8;
    1.63 +    while (h--) {
    1.64 +        memcpy(line, bits, w);
    1.65 +        /* X11 Bitmap images have the bits reversed */
    1.66 +        {
    1.67 +            int i, j;
    1.68 +            Uint8 *buf, byte;
    1.69 +            for (buf = line, i = 0; i < w; ++i, ++buf) {
    1.70 +                byte = *buf;
    1.71 +                *buf = 0;
    1.72 +                for (j = 7; j >= 0; --j) {
    1.73 +                    *buf |= (byte & 0x01) << j;
    1.74 +                    byte >>= 1;
    1.75 +                }
    1.76 +            }
    1.77 +        }
    1.78 +        line += bitmap->pitch;
    1.79 +        bits += w;
    1.80 +    }
    1.81 +    return (bitmap);
    1.82  }
    1.83  
    1.84 -int main(int argc, char *argv[])
    1.85 +int
    1.86 +main(int argc, char *argv[])
    1.87  {
    1.88 -	SDL_Surface *screen;
    1.89 -	SDL_Surface *bitmap;
    1.90 -	Uint8  video_bpp;
    1.91 -	Uint32 videoflags;
    1.92 -	Uint8 *buffer;
    1.93 -	int i, k, done;
    1.94 -	SDL_Event event;
    1.95 -	Uint16 *buffer16;
    1.96 -        Uint16 color;
    1.97 -        Uint8  gradient;
    1.98 -	SDL_Color palette[256];
    1.99 +    SDL_Surface *screen;
   1.100 +    SDL_Surface *bitmap;
   1.101 +    Uint8 video_bpp;
   1.102 +    Uint32 videoflags;
   1.103 +    Uint8 *buffer;
   1.104 +    int i, k, done;
   1.105 +    SDL_Event event;
   1.106 +    Uint16 *buffer16;
   1.107 +    Uint16 color;
   1.108 +    Uint8 gradient;
   1.109 +    SDL_Color palette[256];
   1.110  
   1.111  
   1.112 -	/* Initialize SDL */
   1.113 -	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   1.114 -		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   1.115 -		return(1);
   1.116 -	}
   1.117 +    /* Initialize SDL */
   1.118 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.119 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.120 +        return (1);
   1.121 +    }
   1.122  
   1.123 -	video_bpp = 0;
   1.124 -	videoflags = SDL_SWSURFACE;
   1.125 -	while ( argc > 1 ) {
   1.126 -		--argc;
   1.127 -		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
   1.128 -			video_bpp = atoi(argv[argc]);
   1.129 -			--argc;
   1.130 -		} else
   1.131 -		if ( strcmp(argv[argc], "-warp") == 0 ) {
   1.132 -			videoflags |= SDL_HWPALETTE;
   1.133 -		} else
   1.134 -		if ( strcmp(argv[argc], "-hw") == 0 ) {
   1.135 -			videoflags |= SDL_HWSURFACE;
   1.136 -		} else
   1.137 -		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
   1.138 -			videoflags |= SDL_FULLSCREEN;
   1.139 -		} else {
   1.140 -			fprintf(stderr,
   1.141 -			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   1.142 -								argv[0]);
   1.143 -			quit(1);
   1.144 -		}
   1.145 -	}
   1.146 +    video_bpp = 0;
   1.147 +    videoflags = SDL_SWSURFACE;
   1.148 +    while (argc > 1) {
   1.149 +        --argc;
   1.150 +        if (strcmp(argv[argc - 1], "-bpp") == 0) {
   1.151 +            video_bpp = atoi(argv[argc]);
   1.152 +            --argc;
   1.153 +        } else if (strcmp(argv[argc], "-warp") == 0) {
   1.154 +            videoflags |= SDL_HWPALETTE;
   1.155 +        } else if (strcmp(argv[argc], "-hw") == 0) {
   1.156 +            videoflags |= SDL_HWSURFACE;
   1.157 +        } else if (strcmp(argv[argc], "-fullscreen") == 0) {
   1.158 +            videoflags |= SDL_FULLSCREEN;
   1.159 +        } else {
   1.160 +            fprintf(stderr,
   1.161 +                    "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   1.162 +                    argv[0]);
   1.163 +            quit(1);
   1.164 +        }
   1.165 +    }
   1.166  
   1.167 -	/* Set 640x480 video mode */
   1.168 -	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
   1.169 -		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
   1.170 -						video_bpp, SDL_GetError());
   1.171 -		quit(2);
   1.172 -	}
   1.173 +    /* Set 640x480 video mode */
   1.174 +    if ((screen = SDL_SetVideoMode(640, 480, video_bpp, videoflags)) == NULL) {
   1.175 +        fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
   1.176 +                video_bpp, SDL_GetError());
   1.177 +        quit(2);
   1.178 +    }
   1.179  
   1.180 -	if (video_bpp==8) {
   1.181 -		/* Set a gray colormap, reverse order from white to black */
   1.182 -		for ( i=0; i<256; ++i ) {
   1.183 -			palette[i].r = 255-i;
   1.184 -			palette[i].g = 255-i;
   1.185 -			palette[i].b = 255-i;
   1.186 -		}
   1.187 -		SDL_SetColors(screen, palette, 0, 256);
   1.188 -	}
   1.189 +    if (video_bpp == 8) {
   1.190 +        /* Set a gray colormap, reverse order from white to black */
   1.191 +        for (i = 0; i < 256; ++i) {
   1.192 +            palette[i].r = 255 - i;
   1.193 +            palette[i].g = 255 - i;
   1.194 +            palette[i].b = 255 - i;
   1.195 +        }
   1.196 +        SDL_SetColors(screen, palette, 0, 256);
   1.197 +    }
   1.198  
   1.199 -	/* Set the surface pixels and refresh! */
   1.200 -	if ( SDL_LockSurface(screen) < 0 ) {
   1.201 -		fprintf(stderr, "Couldn't lock the display surface: %s\n",
   1.202 -							SDL_GetError());
   1.203 -		quit(2);
   1.204 -	}
   1.205 -	buffer=(Uint8 *)screen->pixels;
   1.206 -	if (screen->format->BytesPerPixel!=2) {
   1.207 -        	for ( i=0; i<screen->h; ++i ) {
   1.208 -        		memset(buffer,(i*255)/screen->h, screen->pitch);
   1.209 -        		buffer += screen->pitch;
   1.210 -        	}
   1.211 +    /* Set the surface pixels and refresh! */
   1.212 +    if (SDL_LockSurface(screen) < 0) {
   1.213 +        fprintf(stderr, "Couldn't lock the display surface: %s\n",
   1.214 +                SDL_GetError());
   1.215 +        quit(2);
   1.216 +    }
   1.217 +    buffer = (Uint8 *) screen->pixels;
   1.218 +    if (screen->format->BytesPerPixel != 2) {
   1.219 +        for (i = 0; i < screen->h; ++i) {
   1.220 +            memset(buffer, (i * 255) / screen->h, screen->pitch);
   1.221 +            buffer += screen->pitch;
   1.222          }
   1.223 -        else
   1.224 -        {
   1.225 -		for ( i=0; i<screen->h; ++i ) {
   1.226 -			gradient=((i*255)/screen->h);
   1.227 -                        color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
   1.228 -                        buffer16=(Uint16*)buffer;
   1.229 -                        for (k=0; k<screen->w; k++)
   1.230 -                        {
   1.231 -                            *(buffer16+k)=color;
   1.232 -                        }
   1.233 -			buffer += screen->pitch;
   1.234 -		}
   1.235 +    } else {
   1.236 +        for (i = 0; i < screen->h; ++i) {
   1.237 +            gradient = ((i * 255) / screen->h);
   1.238 +            color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
   1.239 +            buffer16 = (Uint16 *) buffer;
   1.240 +            for (k = 0; k < screen->w; k++) {
   1.241 +                *(buffer16 + k) = color;
   1.242 +            }
   1.243 +            buffer += screen->pitch;
   1.244          }
   1.245 -	SDL_UnlockSurface(screen);
   1.246 -	SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.247 +    }
   1.248 +    SDL_UnlockSurface(screen);
   1.249 +    SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.250  
   1.251 -	/* Load the bitmap */
   1.252 -	bitmap = LoadXBM(screen, picture_width, picture_height,
   1.253 -					(Uint8 *)picture_bits);
   1.254 -	if ( bitmap == NULL ) {
   1.255 -		quit(1);
   1.256 -	}
   1.257 +    /* Load the bitmap */
   1.258 +    bitmap = LoadXBM(screen, picture_width, picture_height,
   1.259 +                     (Uint8 *) picture_bits);
   1.260 +    if (bitmap == NULL) {
   1.261 +        quit(1);
   1.262 +    }
   1.263  
   1.264 -	/* Wait for a keystroke */
   1.265 -	done = 0;
   1.266 -	while ( !done ) {
   1.267 -		/* Check for events */
   1.268 -		while ( SDL_PollEvent(&event) ) {
   1.269 -			switch (event.type) {
   1.270 -				case SDL_MOUSEBUTTONDOWN: {
   1.271 -					SDL_Rect dst;
   1.272 +    /* Wait for a keystroke */
   1.273 +    done = 0;
   1.274 +    while (!done) {
   1.275 +        /* Check for events */
   1.276 +        while (SDL_PollEvent(&event)) {
   1.277 +            switch (event.type) {
   1.278 +            case SDL_MOUSEBUTTONDOWN:
   1.279 +                {
   1.280 +                    SDL_Rect dst;
   1.281  
   1.282 -					dst.x = event.button.x - bitmap->w/2;
   1.283 -					dst.y = event.button.y - bitmap->h/2;
   1.284 -					dst.w = bitmap->w;
   1.285 -					dst.h = bitmap->h;
   1.286 -					SDL_BlitSurface(bitmap, NULL,
   1.287 -								screen, &dst);
   1.288 -					SDL_UpdateRects(screen,1,&dst);
   1.289 -					}
   1.290 -					break;
   1.291 -				case SDL_KEYDOWN:
   1.292 -					/* Any key press quits the app... */
   1.293 -					done = 1;
   1.294 -					break;
   1.295 -				case SDL_QUIT:
   1.296 -					done = 1;
   1.297 -					break;
   1.298 -				default:
   1.299 -					break;
   1.300 -			}
   1.301 -		}
   1.302 -	}
   1.303 -	SDL_FreeSurface(bitmap);
   1.304 -	SDL_Quit();
   1.305 -	return(0);
   1.306 +                    dst.x = event.button.x - bitmap->w / 2;
   1.307 +                    dst.y = event.button.y - bitmap->h / 2;
   1.308 +                    dst.w = bitmap->w;
   1.309 +                    dst.h = bitmap->h;
   1.310 +                    SDL_BlitSurface(bitmap, NULL, screen, &dst);
   1.311 +                    SDL_UpdateRects(screen, 1, &dst);
   1.312 +                }
   1.313 +                break;
   1.314 +            case SDL_KEYDOWN:
   1.315 +                /* Any key press quits the app... */
   1.316 +                done = 1;
   1.317 +                break;
   1.318 +            case SDL_QUIT:
   1.319 +                done = 1;
   1.320 +                break;
   1.321 +            default:
   1.322 +                break;
   1.323 +            }
   1.324 +        }
   1.325 +    }
   1.326 +    SDL_FreeSurface(bitmap);
   1.327 +    SDL_Quit();
   1.328 +    return (0);
   1.329  }