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