Rev 8200 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8202 | maxcodehac | 1 | /* |
6382 | ashmew2 | 2 | |
8202 | maxcodehac | 3 | Makefile in this folder |
4 | -- maxcodehack |
||
5 | */ |
||
6382 | ashmew2 | 6 | |
8202 | maxcodehac | 7 | /* Simple program: Test bitmap blits */ |
6382 | ashmew2 | 8 | |
8202 | maxcodehac | 9 | #include |
10 | #include |
||
11 | #include |
||
6382 | ashmew2 | 12 | |
8202 | maxcodehac | 13 | // We can use Newlib functions with Menuetlibc library SDL! |
14 | #include |
||
6382 | ashmew2 | 15 | |
8202 | maxcodehac | 16 | #include "SDL.h" |
17 | #include "picture.xbm" |
||
8200 | maxcodehac | 18 | |
8202 | maxcodehac | 19 | SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits) |
20 | { |
||
21 | SDL_Surface *bitmap; |
||
22 | Uint8 *line; |
||
6382 | ashmew2 | 23 | |
8202 | maxcodehac | 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 | } |
||
6382 | ashmew2 | 31 | |
8202 | maxcodehac | 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 |
||
40 | byte = *buf; |
||
41 | *buf = 0; |
||
42 | for ( j=7; j>=0; --j ) { |
||
43 | *buf |= (byte&0x01)< |
||
44 | byte >>= 1; |
||
45 | } |
||
46 | } |
||
47 | } |
||
48 | line += bitmap->pitch; |
||
49 | bits += w; |
||
50 | } |
||
51 | return(bitmap); |
||
52 | } |
||
6382 | ashmew2 | 53 | |
8202 | maxcodehac | 54 | int main(int argc, char *argv[]) |
6382 | ashmew2 | 55 | { |
8202 | maxcodehac | 56 | SDL_Surface *screen; |
57 | SDL_Surface *bitmap; |
||
58 | Uint8 video_bpp; |
||
59 | Uint32 videoflags; |
||
60 | Uint8 *buffer; |
||
61 | int i, done; |
||
62 | SDL_Event event; |
||
6382 | ashmew2 | 63 | |
8202 | maxcodehac | 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); |
||
68 | } |
||
69 | atexit(SDL_Quit); |
||
6382 | ashmew2 | 70 | |
8202 | maxcodehac | 71 | video_bpp = 0; |
72 | videoflags = SDL_SWSURFACE; |
||
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", |
||
76 | video_bpp, SDL_GetError()); |
||
77 | exit(2); |
||
78 | } |
||
6382 | ashmew2 | 79 | |
8202 | maxcodehac | 80 | /* Set the surface pixels and refresh! */ |
81 | if ( SDL_LockSurface(screen) < 0 ) { |
||
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); |
||
6382 | ashmew2 | 93 | |
8202 | maxcodehac | 94 | /* Load the bitmap */ |
95 | bitmap = LoadXBM(screen, picture_width, picture_height, |
||
96 | (Uint8 *)picture_bits); |
||
97 | if ( bitmap == NULL ) { |
||
98 | exit(1); |
||
99 | } |
||
6382 | ashmew2 | 100 | |
8202 | maxcodehac | 101 | /* Wait for a keystroke */ |
102 | done = 0; |
||
103 | while ( !done ) { |
||
104 | /* Check for events */ |
||
105 | while ( SDL_PollEvent(&event) ) { |
||
106 | switch (event.type) { |
||
107 | case SDL_MOUSEBUTTONDOWN: { |
||
108 | SDL_Rect dst; |
||
6382 | ashmew2 | 109 | |
8202 | maxcodehac | 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); |
||
116 | SDL_UpdateRects(screen,1,&dst); |
||
117 | } |
||
118 | break; |
||
119 | case SDL_KEYDOWN: |
||
120 | /* Any key press quits the app... */ |
||
121 | done = 1; |
||
122 | break; |
||
123 | case SDL_QUIT: |
||
124 | done = 1; |
||
125 | break; |
||
126 | default: |
||
127 | break; |
||
128 | } |
||
129 | |||
130 | // We can use Newlib functions with Menuetlibc library SDL! |
||
131 | // For example: |
||
132 | draw_bar(10, 10, 50, 50, 0xFFFFFF); |
||
133 | } |
||
134 | } |
||
135 | SDL_FreeSurface(bitmap); |
||
136 | SDL_Quit(); |
||
137 | return 0; |
||
6382 | ashmew2 | 138 | }>> |