Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
182 lines (165 loc) · 4.84 KB

testbitmap.c

File metadata and controls

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