test/testbitmap.c
changeset 0 74212992fb08
child 691 609543e2b3a1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testbitmap.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,147 @@
     1.4 +
     1.5 +/* Simple program:  Test bitmap blits */
     1.6 +
     1.7 +#include <stdio.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <string.h>
    1.10 +
    1.11 +#include "SDL.h"
    1.12 +#include "picture.xbm"
    1.13 +
    1.14 +SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
    1.15 +{
    1.16 +	SDL_Surface *bitmap;
    1.17 +	Uint8 *line;
    1.18 +
    1.19 +	/* Allocate the bitmap */
    1.20 +	bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
    1.21 +	if ( bitmap == NULL ) {
    1.22 +		fprintf(stderr, "Couldn't allocate bitmap: %s\n",
    1.23 +						SDL_GetError());
    1.24 +		return(NULL);
    1.25 +	}
    1.26 +
    1.27 +	/* Copy the pixels */
    1.28 +	line = (Uint8 *)bitmap->pixels;
    1.29 +	w = (w+7)/8;
    1.30 +	while ( h-- ) {
    1.31 +		memcpy(line, bits, w);
    1.32 +		/* X11 Bitmap images have the bits reversed */
    1.33 +		{ int i, j; Uint8 *buf, byte;
    1.34 +			for ( buf=line, i=0; i<w; ++i, ++buf ) {
    1.35 +				byte = *buf;
    1.36 +				*buf = 0;
    1.37 +				for ( j=7; j>=0; --j ) {
    1.38 +					*buf |= (byte&0x01)<<j;
    1.39 +					byte >>= 1;
    1.40 +				}
    1.41 +			}
    1.42 +		}
    1.43 +		line += bitmap->pitch;
    1.44 +		bits += w;
    1.45 +	}
    1.46 +	return(bitmap);
    1.47 +}
    1.48 +
    1.49 +int main(int argc, char *argv[])
    1.50 +{
    1.51 +	SDL_Surface *screen;
    1.52 +	SDL_Surface *bitmap;
    1.53 +	Uint8  video_bpp;
    1.54 +	Uint32 videoflags;
    1.55 +	Uint8 *buffer;
    1.56 +	int i, done;
    1.57 +	SDL_Event event;
    1.58 +
    1.59 +	/* Initialize SDL */
    1.60 +	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    1.61 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.62 +		exit(1);
    1.63 +	}
    1.64 +	atexit(SDL_Quit);
    1.65 +
    1.66 +	video_bpp = 0;
    1.67 +	videoflags = SDL_SWSURFACE;
    1.68 +	while ( argc > 1 ) {
    1.69 +		--argc;
    1.70 +		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
    1.71 +			video_bpp = atoi(argv[argc]);
    1.72 +			--argc;
    1.73 +		} else
    1.74 +		if ( strcmp(argv[argc], "-warp") == 0 ) {
    1.75 +			videoflags |= SDL_HWPALETTE;
    1.76 +		} else
    1.77 +		if ( strcmp(argv[argc], "-hw") == 0 ) {
    1.78 +			videoflags |= SDL_HWSURFACE;
    1.79 +		} else
    1.80 +		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
    1.81 +			videoflags |= SDL_FULLSCREEN;
    1.82 +		} else {
    1.83 +			fprintf(stderr,
    1.84 +			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
    1.85 +								argv[0]);
    1.86 +			exit(1);
    1.87 +		}
    1.88 +	}
    1.89 +
    1.90 +	/* Set 640x480 video mode */
    1.91 +	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
    1.92 +		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
    1.93 +						video_bpp, SDL_GetError());
    1.94 +		exit(2);
    1.95 +	}
    1.96 +
    1.97 +	/* Set the surface pixels and refresh! */
    1.98 +	if ( SDL_LockSurface(screen) < 0 ) {
    1.99 +		fprintf(stderr, "Couldn't lock the display surface: %s\n",
   1.100 +							SDL_GetError());
   1.101 +		exit(2);
   1.102 +	}
   1.103 +	buffer=(Uint8 *)screen->pixels;
   1.104 +	for ( i=0; i<screen->h; ++i ) {
   1.105 +		memset(buffer,(i*255)/screen->h, screen->pitch);
   1.106 +		buffer += screen->pitch;
   1.107 +	}
   1.108 +	SDL_UnlockSurface(screen);
   1.109 +	SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.110 +
   1.111 +	/* Load the bitmap */
   1.112 +	bitmap = LoadXBM(screen, picture_width, picture_height,
   1.113 +					(Uint8 *)picture_bits);
   1.114 +	if ( bitmap == NULL ) {
   1.115 +		exit(1);
   1.116 +	}
   1.117 +
   1.118 +	/* Wait for a keystroke */
   1.119 +	done = 0;
   1.120 +	while ( !done ) {
   1.121 +		/* Check for events */
   1.122 +		while ( SDL_PollEvent(&event) ) {
   1.123 +			switch (event.type) {
   1.124 +				case SDL_MOUSEBUTTONDOWN: {
   1.125 +					SDL_Rect dst;
   1.126 +
   1.127 +					dst.x = event.button.x - bitmap->w/2;
   1.128 +					dst.y = event.button.y - bitmap->h/2;
   1.129 +					dst.w = bitmap->w;
   1.130 +					dst.h = bitmap->h;
   1.131 +					SDL_BlitSurface(bitmap, NULL,
   1.132 +								screen, &dst);
   1.133 +					SDL_UpdateRects(screen,1,&dst);
   1.134 +					}
   1.135 +					break;
   1.136 +				case SDL_KEYDOWN:
   1.137 +					/* Any key press quits the app... */
   1.138 +					done = 1;
   1.139 +					break;
   1.140 +				case SDL_QUIT:
   1.141 +					done = 1;
   1.142 +					break;
   1.143 +				default:
   1.144 +					break;
   1.145 +			}
   1.146 +		}
   1.147 +	}
   1.148 +	SDL_FreeSurface(bitmap);
   1.149 +	return(0);
   1.150 +}