2 /* Simple program: Test bitmap blits */
11 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
12 static void quit(int rc)
18 SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
23 /* Allocate the bitmap */
24 bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
25 if ( bitmap == NULL ) {
26 fprintf(stderr, "Couldn't allocate bitmap: %s\n",
32 line = (Uint8 *)bitmap->pixels;
35 memcpy(line, bits, w);
36 /* X11 Bitmap images have the bits reversed */
37 { int i, j; Uint8 *buf, byte;
38 for ( buf=line, i=0; i<w; ++i, ++buf ) {
41 for ( j=7; j>=0; --j ) {
42 *buf |= (byte&0x01)<<j;
47 line += bitmap->pitch;
53 int main(int argc, char *argv[])
65 SDL_Color palette[256];
69 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
70 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
75 videoflags = SDL_SWSURFACE;
78 if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
79 video_bpp = atoi(argv[argc]);
82 if ( strcmp(argv[argc], "-warp") == 0 ) {
83 videoflags |= SDL_HWPALETTE;
85 if ( strcmp(argv[argc], "-hw") == 0 ) {
86 videoflags |= SDL_HWSURFACE;
88 if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
89 videoflags |= SDL_FULLSCREEN;
92 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
98 /* Set 640x480 video mode */
99 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
100 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
101 video_bpp, SDL_GetError());
106 /* Set a gray colormap, reverse order from white to black */
107 for ( i=0; i<256; ++i ) {
108 palette[i].r = 255-i;
109 palette[i].g = 255-i;
110 palette[i].b = 255-i;
112 SDL_SetColors(screen, palette, 0, 256);
115 /* Set the surface pixels and refresh! */
116 if ( SDL_LockSurface(screen) < 0 ) {
117 fprintf(stderr, "Couldn't lock the display surface: %s\n",
121 buffer=(Uint8 *)screen->pixels;
122 if (screen->format->BytesPerPixel!=2) {
123 for ( i=0; i<screen->h; ++i ) {
124 memset(buffer,(i*255)/screen->h, screen->pitch);
125 buffer += screen->pitch;
130 for ( i=0; i<screen->h; ++i ) {
131 gradient=((i*255)/screen->h);
132 color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
133 buffer16=(Uint16*)buffer;
134 for (k=0; k<screen->w; k++)
138 buffer += screen->pitch;
141 SDL_UnlockSurface(screen);
142 SDL_UpdateRect(screen, 0, 0, 0, 0);
144 /* Load the bitmap */
145 bitmap = LoadXBM(screen, picture_width, picture_height,
146 (Uint8 *)picture_bits);
147 if ( bitmap == NULL ) {
151 /* Wait for a keystroke */
154 /* Check for events */
155 while ( SDL_PollEvent(&event) ) {
156 switch (event.type) {
157 case SDL_MOUSEBUTTONDOWN: {
160 dst.x = event.button.x - bitmap->w/2;
161 dst.y = event.button.y - bitmap->h/2;
164 SDL_BlitSurface(bitmap, NULL,
166 SDL_UpdateRects(screen,1,&dst);
170 /* Any key press quits the app... */
181 SDL_FreeSurface(bitmap);