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