amiga_imgv

view src/sdl/gfx.c @ 6:ae0ada629b03

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