amiga_imgv

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