Rev 8202 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8202 | Rev 8218 | ||
---|---|---|---|
Line 1... | Line -... | ||
1 | /* |
- | |
2 | - | ||
3 | Makefile in this folder |
- | |
4 | -- maxcodehack |
- | |
5 | */ |
- | |
6 | - | ||
7 | /* Simple program: Test bitmap blits */ |
- | |
8 | - | ||
9 | #include |
- | |
10 | #include |
- | |
11 | #include |
- | |
12 | - | ||
13 | // We can use Newlib functions with Menuetlibc library SDL! |
- | |
14 | #include |
- | |
15 | - | ||
16 | #include "SDL.h" |
1 | #include "SDL.h" |
17 | #include "picture.xbm" |
2 | #include |
18 | - | ||
19 | SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits) |
- | |
20 | { |
- | |
21 | SDL_Surface *bitmap; |
- | |
22 | Uint8 *line; |
- | |
23 | - | ||
24 | /* Allocate the bitmap */ |
- | |
25 | bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0); |
- | |
26 | if ( bitmap == NULL ) { |
- | |
27 | fprintf(stderr, "Couldn't allocate bitmap: %s\n", |
- | |
28 | SDL_GetError()); |
- | |
29 | return(NULL); |
- | |
30 | } |
- | |
Line 31... | Line -... | ||
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 | { int i, j; Uint8 *buf, byte; |
- | |
39 | for ( buf=line, i=0; i |
3 | |
40 | byte = *buf; |
- | |
41 | *buf = 0; |
- | |
42 | for ( j=7; j>=0; --j ) { |
- | |
43 | *buf |= (byte&0x01)< |
4 | SDL_Surface* screen; |
44 | byte >>= 1; |
- | |
45 | } |
- | |
46 | } |
- | |
47 | } |
- | |
48 | line += bitmap->pitch; |
- | |
49 | bits += w; |
- | |
50 | } |
- | |
51 | return(bitmap); |
- | |
Line 52... | Line 5... | ||
52 | } |
5 | static int done = 0; |
53 | 6 | ||
54 | int main(int argc, char *argv[]) |
- | |
55 | { |
- | |
56 | SDL_Surface *screen; |
- | |
57 | SDL_Surface *bitmap; |
- | |
58 | Uint8 video_bpp; |
- | |
59 | Uint32 videoflags; |
- | |
60 | Uint8 *buffer; |
7 | int main() |
61 | int i, done; |
- | |
62 | SDL_Event event; |
- | |
63 | 8 | { |
|
64 | /* Initialize SDL */ |
- | |
65 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
- | |
66 | fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); |
- | |
67 | exit(1); |
9 | SDL_Event event; |
68 | } |
- | |
69 | atexit(SDL_Quit); |
- | |
70 | - | ||
71 | video_bpp = 0; |
- | |
72 | videoflags = SDL_SWSURFACE; |
10 | if(SDL_Init(SDL_INIT_VIDEO) < 0) exit(0); |
73 | /* Set 640x480 video mode */ |
- | |
74 | if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { |
- | |
75 | fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", |
11 | atexit(SDL_Quit); |
76 | video_bpp, SDL_GetError()); |
12 | screen = SDL_SetVideoMode(320, 200, 8, SDL_SWSURFACE); |
77 | exit(2); |
- | |
78 | } |
- | |
79 | - | ||
80 | /* Set the surface pixels and refresh! */ |
- | |
81 | if ( SDL_LockSurface(screen) < 0 ) { |
13 | while (!done) |
82 | fprintf(stderr, "Couldn't lock the display surface: %s\n", |
- | |
83 | SDL_GetError()); |
- | |
84 | exit(2); |
- | |
85 | } |
- | |
86 | buffer=(Uint8 *)screen->pixels; |
- | |
87 | for ( i=0; i |
- | |
88 | memset(buffer,(i*255)/screen->h, screen->pitch); |
- | |
89 | buffer += screen->pitch; |
- | |
90 | } |
- | |
91 | SDL_UnlockSurface(screen); |
- | |
92 | SDL_UpdateRect(screen, 0, 0, 0, 0); |
- | |
93 | - | ||
94 | /* Load the bitmap */ |
- | |
95 | bitmap = LoadXBM(screen, picture_width, picture_height, |
- | |
96 | (Uint8 *)picture_bits); |
- | |
97 | if ( bitmap == NULL ) { |
14 | { |
98 | exit(1); |
- | |
99 | } |
- | |
100 | - | ||
101 | /* Wait for a keystroke */ |
- | |
102 | done = 0; |
- | |
103 | while ( !done ) { |
- | |
104 | /* Check for events */ |
15 | while (SDL_PollEvent(&event)) |
105 | while ( SDL_PollEvent(&event) ) { |
- | |
106 | switch (event.type) { |
- | |
107 | case SDL_MOUSEBUTTONDOWN: { |
- | |
108 | SDL_Rect dst; |
- | |
109 | - | ||
110 | dst.x = event.button.x - bitmap->w/2; |
- | |
111 | dst.y = event.button.y - bitmap->h/2; |
- | |
112 | dst.w = bitmap->w; |
- | |
113 | dst.h = bitmap->h; |
- | |
114 | SDL_BlitSurface(bitmap, NULL, |
- | |
115 | screen, &dst); |
16 | { |
116 | SDL_UpdateRects(screen,1,&dst); |
- | |
117 | } |
17 | switch (event.type) |
118 | break; |
- | |
119 | case SDL_KEYDOWN: |
- | |
120 | /* Any key press quits the app... */ |
- | |
121 | done = 1; |
18 | { |
122 | break; |
19 | case SDL_KEYDOWN: |
123 | case SDL_QUIT: |
20 | case SDL_QUIT: |
124 | done = 1; |
21 | done = 1; |
125 | break; |
22 | break; |
126 | default: |
23 | default: |
127 | break; |
- | |
128 | } |
- | |
129 | - | ||
130 | // We can use Newlib functions with Menuetlibc library SDL! |
- | |
131 | // For example: |
24 | break; |
132 | draw_bar(10, 10, 50, 50, 0xFFFFFF); |
25 | } |
133 | } |
- | |
134 | } |
- | |
135 | SDL_FreeSurface(bitmap); |
- | |
136 | SDL_Quit(); |
26 | } |