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