amiga_imgv

view src/sdl/gfx.c @ 10:5614fbb9c1de

Backed out changeset 01bd8bbc46d4
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Oct 2017 22:53:41 +0200
parents 01bd8bbc46d4
children 3d9aaefb8ba6
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 return 0;
54 }
56 void *gfx_get_framebuffer(void)
57 {
58 return 0;
59 }
62 int get_framebuffer_width(void)
63 {
64 return fb_width;
65 }
67 int get_framebuffer_height(void)
68 {
69 return fb_height;
70 }
73 void gfx_begin_copperlist(void)
74 {
75 }
78 int gfx_next_event(union gfx_event *ev, int block)
79 {
80 SDL_Event sdlev;
82 if(block) {
83 SDL_WaitEvent(&sdlev);
84 } else {
85 if(!SDL_PollEvent(&sdlev)) {
86 return 0;
87 }
88 }
90 switch(sdlev.type) {
91 case SDL_QUIT:
92 ev->type = GFX_EV_QUIT;
93 return 1;
95 case SDL_KEYDOWN:
96 case SDL_KEYUP:
97 ev->type = GFX_EV_KEY;
98 ev->key.key = sdlev.key.keysym.sym;
99 ev->key.pressed = sdlev.key.state == SDL_PRESSED;
100 return 1;
102 default:
103 break;
104 }
105 return 0;
106 }
109 void gfx_wait_vpos(int x)
110 {
111 }
113 void gfx_wait_vblank(void)
114 {
115 }
117 #define ARED(x) ((((x) & 0xf00) >> 4) | (((x) & 0xf00) >> 8))
118 #define AGREEN(x) (((x) & 0xf0) | (((x) & 0xf0) >> 4))
119 #define ABLUE(x) ((((x) & 0xf) << 4) | ((x) & 0xf))
121 #define RSHIFT (fbsurf->format->Rshift)
122 #define GSHIFT (fbsurf->format->Gshift)
123 #define BSHIFT (fbsurf->format->Bshift)
124 #define RMASK (fbsurf->format->Rmask)
125 #define GMASK (fbsurf->format->Gmask)
126 #define BMASK (fbsurf->format->Bmask)
128 #define PACKRGB(r, g, b) ((((r) << RSHIFT) & RMASK) | \
129 (((g) << GSHIFT) & GMASK) | \
130 (((b) << BSHIFT) & BMASK))
132 void gfx_show_image(struct ham_image *img)
133 {
134 int i, j, k;
135 uint32_t color, palette[16];
136 uint32_t *dest;
137 unsigned char *src;
138 struct palchange *chg = img->chglist;
140 for(i=0; i<16; i++) {
141 uint16_t pcol = img->palette[i];
142 int red = ARED(pcol);
143 int green = AGREEN(pcol);
144 int blue = ABLUE(pcol);
145 palette[i] = PACKRGB(red, green, blue);
146 }
148 if(SDL_MUSTLOCK(fbsurf)) {
149 SDL_LockSurface(fbsurf);
150 }
152 dest = fbsurf->pixels;
153 src = img->pixels;
154 for(i=0; i<img->height; i++) {
155 while(chg && chg->line <= i) {
156 int idx = (chg->entry & 0xf000) >> 12;
157 int red = ARED(chg->entry);
158 int green = AGREEN(chg->entry);
159 int blue = ABLUE(chg->entry);
160 palette[idx] = PACKRGB(red, green, blue);
161 chg = chg->next;
162 }
164 for(j=0; j<img->width; j++) {
165 unsigned char idx = 0;
166 unsigned char ham = 0;
167 int bit = 7 - (j & 7);
169 for(k=0; k<img->nbitplanes; k++) {
170 idx |= (((*(src + k * img->width / 8) >> bit) & 1) << k);
171 }
173 if(init_flags & GFX_HAM) {
174 ham = (idx >> 4) & 3;
175 }
177 switch(ham) {
178 case 0:
179 color = palette[idx];
180 break;
181 case 1:
182 color = (color & ~BMASK) | (((uint32_t)ABLUE(idx)) << BSHIFT);
183 break;
184 case 2:
185 color = (color & ~RMASK) | (((uint32_t)ABLUE(idx)) << RSHIFT);
186 break;
187 case 3:
188 color = (color & ~GMASK) | (((uint32_t)ABLUE(idx)) << GSHIFT);
189 }
191 *dest++ = color;
192 if(!bit) {
193 ++src;
194 }
195 }
196 src += img->width / 8 * (img->nbitplanes - 1);
197 }
199 if(SDL_MUSTLOCK(fbsurf)) {
200 SDL_UnlockSurface(fbsurf);
201 }
203 SDL_Flip(fbsurf);
204 }