test/testbitmap.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 23 Aug 2003 23:18:49 +0000
changeset 691 609543e2b3a1
parent 0 74212992fb08
child 1151 be9c9c8f6d53
permissions -rw-r--r--
Date: Fri, 15 Aug 2003 09:13:59 +0300
From: "Mike Gorchak"
Subject: Patches for tests and QNX6

1) graywin - added support for the gray gradient in the 15/16 bpp modes. Added SDL_VIDEOEXPOSE event handling.
2) testalpha - added support for the gray gradient in the 15/16 bpp modes.
3) testbitmap - added support for the gray gradient in the 8/15/16 bpp modes.
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@691
    53
	int i, k, done;
slouken@0
    54
	SDL_Event event;
slouken@691
    55
	Uint16 *buffer16;
slouken@691
    56
        Uint16 color;
slouken@691
    57
        Uint8  gradient;
slouken@691
    58
	SDL_Color palette[256];
slouken@691
    59
slouken@0
    60
slouken@0
    61
	/* Initialize SDL */
slouken@0
    62
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
    63
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
    64
		exit(1);
slouken@0
    65
	}
slouken@0
    66
	atexit(SDL_Quit);
slouken@0
    67
slouken@0
    68
	video_bpp = 0;
slouken@0
    69
	videoflags = SDL_SWSURFACE;
slouken@0
    70
	while ( argc > 1 ) {
slouken@0
    71
		--argc;
slouken@0
    72
		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
slouken@0
    73
			video_bpp = atoi(argv[argc]);
slouken@0
    74
			--argc;
slouken@0
    75
		} else
slouken@0
    76
		if ( strcmp(argv[argc], "-warp") == 0 ) {
slouken@0
    77
			videoflags |= SDL_HWPALETTE;
slouken@0
    78
		} else
slouken@0
    79
		if ( strcmp(argv[argc], "-hw") == 0 ) {
slouken@0
    80
			videoflags |= SDL_HWSURFACE;
slouken@0
    81
		} else
slouken@0
    82
		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
slouken@0
    83
			videoflags |= SDL_FULLSCREEN;
slouken@0
    84
		} else {
slouken@0
    85
			fprintf(stderr,
slouken@0
    86
			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
slouken@0
    87
								argv[0]);
slouken@0
    88
			exit(1);
slouken@0
    89
		}
slouken@0
    90
	}
slouken@0
    91
slouken@0
    92
	/* Set 640x480 video mode */
slouken@0
    93
	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
slouken@0
    94
		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
slouken@0
    95
						video_bpp, SDL_GetError());
slouken@0
    96
		exit(2);
slouken@0
    97
	}
slouken@0
    98
slouken@691
    99
	if (video_bpp==8) {
slouken@691
   100
		/* Set a gray colormap, reverse order from white to black */
slouken@691
   101
		for ( i=0; i<256; ++i ) {
slouken@691
   102
			palette[i].r = 255-i;
slouken@691
   103
			palette[i].g = 255-i;
slouken@691
   104
			palette[i].b = 255-i;
slouken@691
   105
		}
slouken@691
   106
		SDL_SetColors(screen, palette, 0, 256);
slouken@691
   107
	}
slouken@691
   108
slouken@0
   109
	/* Set the surface pixels and refresh! */
slouken@0
   110
	if ( SDL_LockSurface(screen) < 0 ) {
slouken@0
   111
		fprintf(stderr, "Couldn't lock the display surface: %s\n",
slouken@0
   112
							SDL_GetError());
slouken@0
   113
		exit(2);
slouken@0
   114
	}
slouken@0
   115
	buffer=(Uint8 *)screen->pixels;
slouken@691
   116
	if (screen->format->BytesPerPixel!=2) {
slouken@691
   117
        	for ( i=0; i<screen->h; ++i ) {
slouken@691
   118
        		memset(buffer,(i*255)/screen->h, screen->pitch);
slouken@691
   119
        		buffer += screen->pitch;
slouken@691
   120
        	}
slouken@691
   121
        }
slouken@691
   122
        else
slouken@691
   123
        {
slouken@691
   124
		for ( i=0; i<screen->h; ++i ) {
slouken@691
   125
			gradient=((i*255)/screen->h);
slouken@691
   126
                        color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
slouken@691
   127
                        buffer16=(Uint16*)buffer;
slouken@691
   128
                        for (k=0; k<screen->w; k++)
slouken@691
   129
                        {
slouken@691
   130
                            *(buffer16+k)=color;
slouken@691
   131
                        }
slouken@691
   132
			buffer += screen->pitch;
slouken@691
   133
		}
slouken@691
   134
        }
slouken@0
   135
	SDL_UnlockSurface(screen);
slouken@0
   136
	SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@0
   137
slouken@0
   138
	/* Load the bitmap */
slouken@0
   139
	bitmap = LoadXBM(screen, picture_width, picture_height,
slouken@0
   140
					(Uint8 *)picture_bits);
slouken@0
   141
	if ( bitmap == NULL ) {
slouken@0
   142
		exit(1);
slouken@0
   143
	}
slouken@0
   144
slouken@0
   145
	/* Wait for a keystroke */
slouken@0
   146
	done = 0;
slouken@0
   147
	while ( !done ) {
slouken@0
   148
		/* Check for events */
slouken@0
   149
		while ( SDL_PollEvent(&event) ) {
slouken@0
   150
			switch (event.type) {
slouken@0
   151
				case SDL_MOUSEBUTTONDOWN: {
slouken@0
   152
					SDL_Rect dst;
slouken@0
   153
slouken@0
   154
					dst.x = event.button.x - bitmap->w/2;
slouken@0
   155
					dst.y = event.button.y - bitmap->h/2;
slouken@0
   156
					dst.w = bitmap->w;
slouken@0
   157
					dst.h = bitmap->h;
slouken@0
   158
					SDL_BlitSurface(bitmap, NULL,
slouken@0
   159
								screen, &dst);
slouken@0
   160
					SDL_UpdateRects(screen,1,&dst);
slouken@0
   161
					}
slouken@0
   162
					break;
slouken@0
   163
				case SDL_KEYDOWN:
slouken@0
   164
					/* Any key press quits the app... */
slouken@0
   165
					done = 1;
slouken@0
   166
					break;
slouken@0
   167
				case SDL_QUIT:
slouken@0
   168
					done = 1;
slouken@0
   169
					break;
slouken@0
   170
				default:
slouken@0
   171
					break;
slouken@0
   172
			}
slouken@0
   173
		}
slouken@0
   174
	}
slouken@0
   175
	SDL_FreeSurface(bitmap);
slouken@0
   176
	return(0);
slouken@0
   177
}