rev |
line source |
nuclear@3
|
1 #include <stdio.h>
|
nuclear@3
|
2 #include <SDL/SDL.h>
|
nuclear@3
|
3 #include "gfx.h"
|
nuclear@3
|
4 #include "image.h"
|
nuclear@3
|
5
|
nuclear@3
|
6 static SDL_Surface *fbsurf;
|
nuclear@3
|
7 static int scr_width, scr_height;
|
nuclear@3
|
8 static int fb_width, fb_height;
|
nuclear@3
|
9 static int num_bitplanes;
|
nuclear@7
|
10 static int init_flags;
|
nuclear@3
|
11
|
nuclear@3
|
12
|
nuclear@3
|
13 int gfx_init(int nbpl, unsigned int flags)
|
nuclear@3
|
14 {
|
nuclear@7
|
15 init_flags = flags;
|
nuclear@3
|
16 num_bitplanes = nbpl;
|
nuclear@3
|
17 scr_width = fb_width = (flags & GFX_HIRES) ? 640 : 320;
|
nuclear@3
|
18 scr_height = fb_height = (flags & GFX_ILACE) ? 512 : 256;
|
nuclear@3
|
19
|
nuclear@3
|
20 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
|
nuclear@3
|
21 fprintf(stderr, "failed to initialize SDL\n");
|
nuclear@3
|
22 return -1;
|
nuclear@3
|
23 }
|
nuclear@3
|
24 if(!(fbsurf = SDL_SetVideoMode(scr_width, scr_height, 32, SDL_SWSURFACE))) {
|
nuclear@3
|
25 fprintf(stderr, "failed to set video mode %dx%d\n", scr_width, scr_height);
|
nuclear@3
|
26 SDL_Quit();
|
nuclear@3
|
27 return -1;
|
nuclear@3
|
28 }
|
nuclear@4
|
29 SDL_WM_SetCaption("imgv SDL version", 0);
|
nuclear@3
|
30
|
nuclear@3
|
31 return 0;
|
nuclear@3
|
32 }
|
nuclear@3
|
33
|
nuclear@3
|
34 void gfx_shutdown(void)
|
nuclear@3
|
35 {
|
nuclear@3
|
36 SDL_Quit();
|
nuclear@3
|
37 }
|
nuclear@3
|
38
|
nuclear@3
|
39
|
nuclear@3
|
40 int gfx_screen_width(void)
|
nuclear@3
|
41 {
|
nuclear@3
|
42 return scr_width;
|
nuclear@3
|
43 }
|
nuclear@3
|
44
|
nuclear@3
|
45 int gfx_screen_height(void)
|
nuclear@3
|
46 {
|
nuclear@3
|
47 return scr_height;
|
nuclear@3
|
48 }
|
nuclear@3
|
49
|
nuclear@3
|
50
|
nuclear@3
|
51 void *gfx_set_framebuffer(void *fb, int width, int height)
|
nuclear@3
|
52 {
|
nuclear@11
|
53 fb_width = width;
|
nuclear@11
|
54 fb_height = height;
|
nuclear@10
|
55 return 0;
|
nuclear@3
|
56 }
|
nuclear@3
|
57
|
nuclear@3
|
58 void *gfx_get_framebuffer(void)
|
nuclear@3
|
59 {
|
nuclear@3
|
60 return 0;
|
nuclear@3
|
61 }
|
nuclear@3
|
62
|
nuclear@3
|
63
|
nuclear@3
|
64 int get_framebuffer_width(void)
|
nuclear@3
|
65 {
|
nuclear@3
|
66 return fb_width;
|
nuclear@3
|
67 }
|
nuclear@3
|
68
|
nuclear@3
|
69 int get_framebuffer_height(void)
|
nuclear@3
|
70 {
|
nuclear@3
|
71 return fb_height;
|
nuclear@3
|
72 }
|
nuclear@3
|
73
|
nuclear@3
|
74
|
nuclear@3
|
75 void gfx_begin_copperlist(void)
|
nuclear@3
|
76 {
|
nuclear@3
|
77 }
|
nuclear@3
|
78
|
nuclear@3
|
79
|
nuclear@3
|
80 int gfx_next_event(union gfx_event *ev, int block)
|
nuclear@3
|
81 {
|
nuclear@3
|
82 SDL_Event sdlev;
|
nuclear@3
|
83
|
nuclear@3
|
84 if(block) {
|
nuclear@3
|
85 SDL_WaitEvent(&sdlev);
|
nuclear@3
|
86 } else {
|
nuclear@3
|
87 if(!SDL_PollEvent(&sdlev)) {
|
nuclear@3
|
88 return 0;
|
nuclear@3
|
89 }
|
nuclear@3
|
90 }
|
nuclear@3
|
91
|
nuclear@3
|
92 switch(sdlev.type) {
|
nuclear@3
|
93 case SDL_QUIT:
|
nuclear@3
|
94 ev->type = GFX_EV_QUIT;
|
nuclear@3
|
95 return 1;
|
nuclear@3
|
96
|
nuclear@3
|
97 case SDL_KEYDOWN:
|
nuclear@3
|
98 case SDL_KEYUP:
|
nuclear@3
|
99 ev->type = GFX_EV_KEY;
|
nuclear@3
|
100 ev->key.key = sdlev.key.keysym.sym;
|
nuclear@3
|
101 ev->key.pressed = sdlev.key.state == SDL_PRESSED;
|
nuclear@3
|
102 return 1;
|
nuclear@3
|
103
|
nuclear@3
|
104 default:
|
nuclear@3
|
105 break;
|
nuclear@3
|
106 }
|
nuclear@3
|
107 return 0;
|
nuclear@3
|
108 }
|
nuclear@3
|
109
|
nuclear@3
|
110
|
nuclear@3
|
111 void gfx_wait_vpos(int x)
|
nuclear@3
|
112 {
|
nuclear@3
|
113 }
|
nuclear@3
|
114
|
nuclear@3
|
115 void gfx_wait_vblank(void)
|
nuclear@3
|
116 {
|
nuclear@3
|
117 }
|
nuclear@3
|
118
|
nuclear@3
|
119 #define ARED(x) ((((x) & 0xf00) >> 4) | (((x) & 0xf00) >> 8))
|
nuclear@3
|
120 #define AGREEN(x) (((x) & 0xf0) | (((x) & 0xf0) >> 4))
|
nuclear@3
|
121 #define ABLUE(x) ((((x) & 0xf) << 4) | ((x) & 0xf))
|
nuclear@3
|
122
|
nuclear@4
|
123 #define RSHIFT (fbsurf->format->Rshift)
|
nuclear@4
|
124 #define GSHIFT (fbsurf->format->Gshift)
|
nuclear@4
|
125 #define BSHIFT (fbsurf->format->Bshift)
|
nuclear@4
|
126 #define RMASK (fbsurf->format->Rmask)
|
nuclear@4
|
127 #define GMASK (fbsurf->format->Gmask)
|
nuclear@4
|
128 #define BMASK (fbsurf->format->Bmask)
|
nuclear@4
|
129
|
nuclear@6
|
130 #define PACKRGB(r, g, b) ((((r) << RSHIFT) & RMASK) | \
|
nuclear@6
|
131 (((g) << GSHIFT) & GMASK) | \
|
nuclear@6
|
132 (((b) << BSHIFT) & BMASK))
|
nuclear@6
|
133
|
nuclear@3
|
134 void gfx_show_image(struct ham_image *img)
|
nuclear@3
|
135 {
|
nuclear@11
|
136 int i, j, k, ncolors;
|
nuclear@11
|
137 uint32_t color, palette[256];
|
nuclear@3
|
138 uint32_t *dest;
|
nuclear@3
|
139 unsigned char *src;
|
nuclear@6
|
140 struct palchange *chg = img->chglist;
|
nuclear@3
|
141
|
nuclear@11
|
142 ncolors = 1 << img->nbitplanes;
|
nuclear@11
|
143
|
nuclear@11
|
144 for(i=0; i<ncolors; i++) {
|
nuclear@3
|
145 uint16_t pcol = img->palette[i];
|
nuclear@3
|
146 int red = ARED(pcol);
|
nuclear@3
|
147 int green = AGREEN(pcol);
|
nuclear@3
|
148 int blue = ABLUE(pcol);
|
nuclear@6
|
149 palette[i] = PACKRGB(red, green, blue);
|
nuclear@3
|
150 }
|
nuclear@3
|
151
|
nuclear@3
|
152 if(SDL_MUSTLOCK(fbsurf)) {
|
nuclear@3
|
153 SDL_LockSurface(fbsurf);
|
nuclear@3
|
154 }
|
nuclear@3
|
155
|
nuclear@3
|
156 dest = fbsurf->pixels;
|
nuclear@3
|
157 src = img->pixels;
|
nuclear@3
|
158 for(i=0; i<img->height; i++) {
|
nuclear@11
|
159 if(i >= scr_height) break;
|
nuclear@11
|
160
|
nuclear@6
|
161 while(chg && chg->line <= i) {
|
nuclear@6
|
162 int idx = (chg->entry & 0xf000) >> 12;
|
nuclear@6
|
163 int red = ARED(chg->entry);
|
nuclear@6
|
164 int green = AGREEN(chg->entry);
|
nuclear@6
|
165 int blue = ABLUE(chg->entry);
|
nuclear@6
|
166 palette[idx] = PACKRGB(red, green, blue);
|
nuclear@6
|
167 chg = chg->next;
|
nuclear@6
|
168 }
|
nuclear@6
|
169
|
nuclear@3
|
170 for(j=0; j<img->width; j++) {
|
nuclear@3
|
171 unsigned char idx = 0;
|
nuclear@7
|
172 unsigned char ham = 0;
|
nuclear@4
|
173 int bit = 7 - (j & 7);
|
nuclear@4
|
174
|
nuclear@3
|
175 for(k=0; k<img->nbitplanes; k++) {
|
nuclear@5
|
176 idx |= (((*(src + k * img->width / 8) >> bit) & 1) << k);
|
nuclear@3
|
177 }
|
nuclear@4
|
178
|
nuclear@7
|
179 if(init_flags & GFX_HAM) {
|
nuclear@7
|
180 ham = (idx >> 4) & 3;
|
nuclear@7
|
181 }
|
nuclear@4
|
182
|
nuclear@4
|
183 switch(ham) {
|
nuclear@4
|
184 case 0:
|
nuclear@4
|
185 color = palette[idx];
|
nuclear@4
|
186 break;
|
nuclear@4
|
187 case 1:
|
nuclear@6
|
188 color = (color & ~BMASK) | (((uint32_t)ABLUE(idx)) << BSHIFT);
|
nuclear@4
|
189 break;
|
nuclear@4
|
190 case 2:
|
nuclear@6
|
191 color = (color & ~RMASK) | (((uint32_t)ABLUE(idx)) << RSHIFT);
|
nuclear@4
|
192 break;
|
nuclear@4
|
193 case 3:
|
nuclear@6
|
194 color = (color & ~GMASK) | (((uint32_t)ABLUE(idx)) << GSHIFT);
|
nuclear@4
|
195 }
|
nuclear@4
|
196
|
nuclear@4
|
197 *dest++ = color;
|
nuclear@4
|
198 if(!bit) {
|
nuclear@3
|
199 ++src;
|
nuclear@3
|
200 }
|
nuclear@3
|
201 }
|
nuclear@3
|
202 src += img->width / 8 * (img->nbitplanes - 1);
|
nuclear@3
|
203 }
|
nuclear@3
|
204
|
nuclear@3
|
205 if(SDL_MUSTLOCK(fbsurf)) {
|
nuclear@3
|
206 SDL_UnlockSurface(fbsurf);
|
nuclear@3
|
207 }
|
nuclear@3
|
208
|
nuclear@3
|
209 SDL_Flip(fbsurf);
|
nuclear@3
|
210 }
|