amiga_imgv
view src/sdl/gfx.c @ 4:0fd37effde29
progress
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 27 Oct 2017 11:36:18 +0300 |
parents | 663471a80c21 |
children | 0d3d7b020e6a |
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 void gfx_show_image(struct ham_image *img)
127 {
128 int i, j, k;
129 uint32_t palette[16];
130 uint32_t *dest;
131 unsigned char *src;
133 for(i=0; i<16; i++) {
134 uint16_t pcol = img->palette[i];
135 int red = ARED(pcol);
136 int green = AGREEN(pcol);
137 int blue = ABLUE(pcol);
138 palette[i] = (red << RSHIFT) | (green << GSHIFT) | (blue << BSHIFT);
139 printf("palette[%d]: %d %d %d\n", i, red, green, blue);
140 }
142 if(SDL_MUSTLOCK(fbsurf)) {
143 SDL_LockSurface(fbsurf);
144 }
146 dest = fbsurf->pixels;
147 src = img->pixels;
148 for(i=0; i<img->height; i++) {
149 for(j=0; j<img->width; j++) {
150 uint32_t color;
151 unsigned char idx = 0;
152 unsigned char ham;
153 int bit = 7 - (j & 7);
155 for(k=0; k<img->nbitplanes; k++) {
156 idx = (idx << 1) | ((*(src + k * img->width / 8) >> bit) & 1);
157 }
159 printf("%d ", idx);
160 color = palette[idx];
161 /*ham = (idx >> 4) & 3;
162 color = (i & j) ? dest[-1] : 0;
164 switch(ham) {
165 case 0:
166 color = palette[idx];
167 break;
168 case 1:
169 color = (color & ~BMASK) | (((uint32_t)idx & 0xf) << BSHIFT);
170 break;
171 case 2:
172 color = (color & ~RMASK) | (((uint32_t)idx & 0xf) << RSHIFT);
173 break;
174 case 3:
175 color = (color & ~GMASK) | (((uint32_t)idx & 0xf) << GSHIFT);
176 }
177 */
179 *dest++ = color;
180 if(!bit) {
181 ++src;
182 }
183 }
184 src += img->width / 8 * (img->nbitplanes - 1);
185 }
186 putchar('\n');
188 if(SDL_MUSTLOCK(fbsurf)) {
189 SDL_UnlockSurface(fbsurf);
190 }
192 SDL_Flip(fbsurf);
193 }