comparison test/testbitmap.c @ 0:74212992fb08

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