/[pcsx2_0.9.7]/trunk/3rdparty/SDL-1.3.0-5387/test/testbitmap.c
ViewVC logotype

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/testbitmap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 401 - (show annotations) (download)
Fri Feb 25 17:31:09 2011 UTC (9 years, 9 months ago) by william
File MIME type: text/plain
File size: 5128 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.4358 local: v0.9.7.313-latest) in ./trunk
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 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
12 static void
13 quit(int rc)
14 {
15 SDL_Quit();
16 exit(rc);
17 }
18
19 SDL_Surface *
20 LoadXBM(SDL_Surface * screen, int w, int h, Uint8 * bits)
21 {
22 SDL_Surface *bitmap;
23 Uint8 *line;
24
25 /* Allocate the bitmap */
26 bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
27 if (bitmap == NULL) {
28 fprintf(stderr, "Couldn't allocate bitmap: %s\n", SDL_GetError());
29 return (NULL);
30 }
31
32 /* Copy the pixels */
33 line = (Uint8 *) bitmap->pixels;
34 w = (w + 7) / 8;
35 while (h--) {
36 memcpy(line, bits, w);
37 /* X11 Bitmap images have the bits reversed */
38 {
39 int i, j;
40 Uint8 *buf, byte;
41 for (buf = line, i = 0; i < w; ++i, ++buf) {
42 byte = *buf;
43 *buf = 0;
44 for (j = 7; j >= 0; --j) {
45 *buf |= (byte & 0x01) << j;
46 byte >>= 1;
47 }
48 }
49 }
50 line += bitmap->pitch;
51 bits += w;
52 }
53 return (bitmap);
54 }
55
56 int
57 main(int argc, char *argv[])
58 {
59 SDL_Surface *screen;
60 SDL_Surface *bitmap;
61 Uint8 video_bpp;
62 Uint32 videoflags;
63 Uint8 *buffer;
64 int i, k, done;
65 SDL_Event event;
66 Uint16 *buffer16;
67 Uint16 color;
68 Uint8 gradient;
69 SDL_Color palette[256];
70
71 int video_w, video_h;
72
73 /* Initialize SDL */
74 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
75 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
76 return (1);
77 }
78
79 video_bpp = 0;
80 videoflags = SDL_SWSURFACE;
81 while (argc > 1) {
82 --argc;
83 if (strcmp(argv[argc - 1], "-bpp") == 0) {
84 video_bpp = atoi(argv[argc]);
85 --argc;
86 } else if (strcmp(argv[argc], "-warp") == 0) {
87 videoflags |= SDL_HWPALETTE;
88 } else if (strcmp(argv[argc], "-hw") == 0) {
89 videoflags |= SDL_HWSURFACE;
90 } else if (strcmp(argv[argc], "-fullscreen") == 0) {
91 videoflags |= SDL_FULLSCREEN;
92 } else {
93 fprintf(stderr,
94 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
95 argv[0]);
96 quit(1);
97 }
98 }
99
100 #ifdef __IPHONEOS__
101 video_w = 320;
102 video_h = 480;
103 #else
104 video_w = 640;
105 video_h = 480;
106 #endif
107
108 /* Set 640x480 video mode */
109 if ((screen =
110 SDL_SetVideoMode(video_w, video_h, video_bpp, videoflags)) == NULL) {
111 fprintf(stderr, "Couldn't set %dx%d %d video mode: %s\n", video_w,
112 video_h, video_bpp, SDL_GetError());
113 quit(2);
114 }
115
116 if (video_bpp == 8) {
117 /* Set a gray colormap, reverse order from white to black */
118 for (i = 0; i < 256; ++i) {
119 palette[i].r = 255 - i;
120 palette[i].g = 255 - i;
121 palette[i].b = 255 - i;
122 }
123 SDL_SetColors(screen, palette, 0, 256);
124 }
125
126 /* Set the surface pixels and refresh! */
127 if (SDL_LockSurface(screen) < 0) {
128 fprintf(stderr, "Couldn't lock the display surface: %s\n",
129 SDL_GetError());
130 quit(2);
131 }
132 buffer = (Uint8 *) screen->pixels;
133 if (screen->format->BytesPerPixel != 2) {
134 for (i = 0; i < screen->h; ++i) {
135 memset(buffer, (i * 255) / screen->h, screen->pitch);
136 buffer += screen->pitch;
137 }
138 } else {
139 for (i = 0; i < screen->h; ++i) {
140 gradient = ((i * 255) / screen->h);
141 color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
142 buffer16 = (Uint16 *) buffer;
143 for (k = 0; k < screen->w; k++) {
144 *(buffer16 + k) = color;
145 }
146 buffer += screen->pitch;
147 }
148 }
149 SDL_UnlockSurface(screen);
150 SDL_UpdateRect(screen, 0, 0, 0, 0);
151
152 /* Load the bitmap */
153 bitmap = LoadXBM(screen, picture_width, picture_height,
154 (Uint8 *) picture_bits);
155 if (bitmap == NULL) {
156 quit(1);
157 }
158
159 /* Wait for a keystroke */
160 done = 0;
161 while (!done) {
162 /* Check for events */
163 while (SDL_PollEvent(&event)) {
164 switch (event.type) {
165 case SDL_MOUSEBUTTONDOWN:
166 {
167 SDL_Rect dst;
168
169 dst.x = event.button.x - bitmap->w / 2;
170 dst.y = event.button.y - bitmap->h / 2;
171 dst.w = bitmap->w;
172 dst.h = bitmap->h;
173 SDL_BlitSurface(bitmap, NULL, screen, &dst);
174 SDL_UpdateRects(screen, 1, &dst);
175 }
176 break;
177 case SDL_KEYDOWN:
178 /* Any key press quits the app... */
179 done = 1;
180 break;
181 case SDL_QUIT:
182 done = 1;
183 break;
184 default:
185 break;
186 }
187 }
188 }
189 SDL_FreeSurface(bitmap);
190 SDL_Quit();
191 return (0);
192 }

  ViewVC Help
Powered by ViewVC 1.1.22