amiga_imgv
diff src/sdl/gfx.c @ 3:663471a80c21
broken + sdl emu
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Oct 2017 15:49:56 +0300 |
parents | |
children | 0fd37effde29 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/sdl/gfx.c Thu Oct 26 15:49:56 2017 +0300 1.3 @@ -0,0 +1,160 @@ 1.4 +#include <stdio.h> 1.5 +#include <SDL/SDL.h> 1.6 +#include "gfx.h" 1.7 +#include "image.h" 1.8 + 1.9 +static SDL_Surface *fbsurf; 1.10 +static int scr_width, scr_height; 1.11 +static int fb_width, fb_height; 1.12 +static int num_bitplanes; 1.13 + 1.14 + 1.15 +int gfx_init(int nbpl, unsigned int flags) 1.16 +{ 1.17 + num_bitplanes = nbpl; 1.18 + scr_width = fb_width = (flags & GFX_HIRES) ? 640 : 320; 1.19 + scr_height = fb_height = (flags & GFX_ILACE) ? 512 : 256; 1.20 + 1.21 + if(SDL_Init(SDL_INIT_VIDEO) == -1) { 1.22 + fprintf(stderr, "failed to initialize SDL\n"); 1.23 + return -1; 1.24 + } 1.25 + if(!(fbsurf = SDL_SetVideoMode(scr_width, scr_height, 32, SDL_SWSURFACE))) { 1.26 + fprintf(stderr, "failed to set video mode %dx%d\n", scr_width, scr_height); 1.27 + SDL_Quit(); 1.28 + return -1; 1.29 + } 1.30 + 1.31 + return 0; 1.32 +} 1.33 + 1.34 +void gfx_shutdown(void) 1.35 +{ 1.36 + SDL_Quit(); 1.37 +} 1.38 + 1.39 + 1.40 +int gfx_screen_width(void) 1.41 +{ 1.42 + return scr_width; 1.43 +} 1.44 + 1.45 +int gfx_screen_height(void) 1.46 +{ 1.47 + return scr_height; 1.48 +} 1.49 + 1.50 + 1.51 +void *gfx_set_framebuffer(void *fb, int width, int height) 1.52 +{ 1.53 + return 0; 1.54 +} 1.55 + 1.56 +void *gfx_get_framebuffer(void) 1.57 +{ 1.58 + return 0; 1.59 +} 1.60 + 1.61 + 1.62 +int get_framebuffer_width(void) 1.63 +{ 1.64 + return fb_width; 1.65 +} 1.66 + 1.67 +int get_framebuffer_height(void) 1.68 +{ 1.69 + return fb_height; 1.70 +} 1.71 + 1.72 + 1.73 +void gfx_begin_copperlist(void) 1.74 +{ 1.75 +} 1.76 + 1.77 + 1.78 +int gfx_next_event(union gfx_event *ev, int block) 1.79 +{ 1.80 + SDL_Event sdlev; 1.81 + 1.82 + if(block) { 1.83 + SDL_WaitEvent(&sdlev); 1.84 + } else { 1.85 + if(!SDL_PollEvent(&sdlev)) { 1.86 + return 0; 1.87 + } 1.88 + } 1.89 + 1.90 + switch(sdlev.type) { 1.91 + case SDL_QUIT: 1.92 + ev->type = GFX_EV_QUIT; 1.93 + return 1; 1.94 + 1.95 + case SDL_KEYDOWN: 1.96 + case SDL_KEYUP: 1.97 + ev->type = GFX_EV_KEY; 1.98 + ev->key.key = sdlev.key.keysym.sym; 1.99 + ev->key.pressed = sdlev.key.state == SDL_PRESSED; 1.100 + return 1; 1.101 + 1.102 + default: 1.103 + break; 1.104 + } 1.105 + return 0; 1.106 +} 1.107 + 1.108 + 1.109 +void gfx_wait_vpos(int x) 1.110 +{ 1.111 +} 1.112 + 1.113 +void gfx_wait_vblank(void) 1.114 +{ 1.115 +} 1.116 + 1.117 +#define ARED(x) ((((x) & 0xf00) >> 4) | (((x) & 0xf00) >> 8)) 1.118 +#define AGREEN(x) (((x) & 0xf0) | (((x) & 0xf0) >> 4)) 1.119 +#define ABLUE(x) ((((x) & 0xf) << 4) | ((x) & 0xf)) 1.120 + 1.121 +void gfx_show_image(struct ham_image *img) 1.122 +{ 1.123 + int i, j, k; 1.124 + uint32_t palette[16]; 1.125 + uint32_t *dest; 1.126 + unsigned char *src; 1.127 + 1.128 + for(i=0; i<16; i++) { 1.129 + uint16_t pcol = img->palette[i]; 1.130 + int red = ARED(pcol); 1.131 + int green = AGREEN(pcol); 1.132 + int blue = ABLUE(pcol); 1.133 + palette[i] = (red << fbsurf->format->Rshift) | (green << fbsurf->format->Gshift) | 1.134 + (blue << fbsurf->format->Bshift); 1.135 + } 1.136 + 1.137 + if(SDL_MUSTLOCK(fbsurf)) { 1.138 + SDL_LockSurface(fbsurf); 1.139 + } 1.140 + 1.141 + dest = fbsurf->pixels; 1.142 + src = img->pixels; 1.143 + for(i=0; i<img->height; i++) { 1.144 + for(j=0; j<img->width; j++) { 1.145 + unsigned char idx = 0; 1.146 + int bit = j & 7; 1.147 + for(k=0; k<img->nbitplanes; k++) { 1.148 + idx = (idx << 1) | ((*(src + k * img->width / 8) >> bit) & 1); 1.149 + } 1.150 + *dest++ = palette[idx]; 1.151 + if(bit == 7) { 1.152 + ++src; 1.153 + } 1.154 + } 1.155 + src += img->width / 8 * (img->nbitplanes - 1); 1.156 + } 1.157 + 1.158 + if(SDL_MUSTLOCK(fbsurf)) { 1.159 + SDL_UnlockSurface(fbsurf); 1.160 + } 1.161 + 1.162 + SDL_Flip(fbsurf); 1.163 +}