test/testbitmap.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 691 609543e2b3a1
permissions -rw-r--r--
Initial revision
slouken@0
     1
slouken@0
     2
/* Simple program:  Test bitmap blits */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
#include "picture.xbm"
slouken@0
    10
slouken@0
    11
SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
slouken@0
    12
{
slouken@0
    13
	SDL_Surface *bitmap;
slouken@0
    14
	Uint8 *line;
slouken@0
    15
slouken@0
    16
	/* Allocate the bitmap */
slouken@0
    17
	bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
slouken@0
    18
	if ( bitmap == NULL ) {
slouken@0
    19
		fprintf(stderr, "Couldn't allocate bitmap: %s\n",
slouken@0
    20
						SDL_GetError());
slouken@0
    21
		return(NULL);
slouken@0
    22
	}
slouken@0
    23
slouken@0
    24
	/* Copy the pixels */
slouken@0
    25
	line = (Uint8 *)bitmap->pixels;
slouken@0
    26
	w = (w+7)/8;
slouken@0
    27
	while ( h-- ) {
slouken@0
    28
		memcpy(line, bits, w);
slouken@0
    29
		/* X11 Bitmap images have the bits reversed */
slouken@0
    30
		{ int i, j; Uint8 *buf, byte;
slouken@0
    31
			for ( buf=line, i=0; i<w; ++i, ++buf ) {
slouken@0
    32
				byte = *buf;
slouken@0
    33
				*buf = 0;
slouken@0
    34
				for ( j=7; j>=0; --j ) {
slouken@0
    35
					*buf |= (byte&0x01)<<j;
slouken@0
    36
					byte >>= 1;
slouken@0
    37
				}
slouken@0
    38
			}
slouken@0
    39
		}
slouken@0
    40
		line += bitmap->pitch;
slouken@0
    41
		bits += w;
slouken@0
    42
	}
slouken@0
    43
	return(bitmap);
slouken@0
    44
}
slouken@0
    45
slouken@0
    46
int main(int argc, char *argv[])
slouken@0
    47
{
slouken@0
    48
	SDL_Surface *screen;
slouken@0
    49
	SDL_Surface *bitmap;
slouken@0
    50
	Uint8  video_bpp;
slouken@0
    51
	Uint32 videoflags;
slouken@0
    52
	Uint8 *buffer;
slouken@0
    53
	int i, done;
slouken@0
    54
	SDL_Event event;
slouken@0
    55
slouken@0
    56
	/* Initialize SDL */
slouken@0
    57
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
    58
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
    59
		exit(1);
slouken@0
    60
	}
slouken@0
    61
	atexit(SDL_Quit);
slouken@0
    62
slouken@0
    63
	video_bpp = 0;
slouken@0
    64
	videoflags = SDL_SWSURFACE;
slouken@0
    65
	while ( argc > 1 ) {
slouken@0
    66
		--argc;
slouken@0
    67
		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
slouken@0
    68
			video_bpp = atoi(argv[argc]);
slouken@0
    69
			--argc;
slouken@0
    70
		} else
slouken@0
    71
		if ( strcmp(argv[argc], "-warp") == 0 ) {
slouken@0
    72
			videoflags |= SDL_HWPALETTE;
slouken@0
    73
		} else
slouken@0
    74
		if ( strcmp(argv[argc], "-hw") == 0 ) {
slouken@0
    75
			videoflags |= SDL_HWSURFACE;
slouken@0
    76
		} else
slouken@0
    77
		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
slouken@0
    78
			videoflags |= SDL_FULLSCREEN;
slouken@0
    79
		} else {
slouken@0
    80
			fprintf(stderr,
slouken@0
    81
			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
slouken@0
    82
								argv[0]);
slouken@0
    83
			exit(1);
slouken@0
    84
		}
slouken@0
    85
	}
slouken@0
    86
slouken@0
    87
	/* Set 640x480 video mode */
slouken@0
    88
	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
slouken@0
    89
		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
slouken@0
    90
						video_bpp, SDL_GetError());
slouken@0
    91
		exit(2);
slouken@0
    92
	}
slouken@0
    93
slouken@0
    94
	/* Set the surface pixels and refresh! */
slouken@0
    95
	if ( SDL_LockSurface(screen) < 0 ) {
slouken@0
    96
		fprintf(stderr, "Couldn't lock the display surface: %s\n",
slouken@0
    97
							SDL_GetError());
slouken@0
    98
		exit(2);
slouken@0
    99
	}
slouken@0
   100
	buffer=(Uint8 *)screen->pixels;
slouken@0
   101
	for ( i=0; i<screen->h; ++i ) {
slouken@0
   102
		memset(buffer,(i*255)/screen->h, screen->pitch);
slouken@0
   103
		buffer += screen->pitch;
slouken@0
   104
	}
slouken@0
   105
	SDL_UnlockSurface(screen);
slouken@0
   106
	SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@0
   107
slouken@0
   108
	/* Load the bitmap */
slouken@0
   109
	bitmap = LoadXBM(screen, picture_width, picture_height,
slouken@0
   110
					(Uint8 *)picture_bits);
slouken@0
   111
	if ( bitmap == NULL ) {
slouken@0
   112
		exit(1);
slouken@0
   113
	}
slouken@0
   114
slouken@0
   115
	/* Wait for a keystroke */
slouken@0
   116
	done = 0;
slouken@0
   117
	while ( !done ) {
slouken@0
   118
		/* Check for events */
slouken@0
   119
		while ( SDL_PollEvent(&event) ) {
slouken@0
   120
			switch (event.type) {
slouken@0
   121
				case SDL_MOUSEBUTTONDOWN: {
slouken@0
   122
					SDL_Rect dst;
slouken@0
   123
slouken@0
   124
					dst.x = event.button.x - bitmap->w/2;
slouken@0
   125
					dst.y = event.button.y - bitmap->h/2;
slouken@0
   126
					dst.w = bitmap->w;
slouken@0
   127
					dst.h = bitmap->h;
slouken@0
   128
					SDL_BlitSurface(bitmap, NULL,
slouken@0
   129
								screen, &dst);
slouken@0
   130
					SDL_UpdateRects(screen,1,&dst);
slouken@0
   131
					}
slouken@0
   132
					break;
slouken@0
   133
				case SDL_KEYDOWN:
slouken@0
   134
					/* Any key press quits the app... */
slouken@0
   135
					done = 1;
slouken@0
   136
					break;
slouken@0
   137
				case SDL_QUIT:
slouken@0
   138
					done = 1;
slouken@0
   139
					break;
slouken@0
   140
				default:
slouken@0
   141
					break;
slouken@0
   142
			}
slouken@0
   143
		}
slouken@0
   144
	}
slouken@0
   145
	SDL_FreeSurface(bitmap);
slouken@0
   146
	return(0);
slouken@0
   147
}