amiga_imgv

annotate 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
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@3 10
nuclear@3 11
nuclear@3 12 int gfx_init(int nbpl, unsigned int flags)
nuclear@3 13 {
nuclear@3 14 num_bitplanes = nbpl;
nuclear@3 15 scr_width = fb_width = (flags & GFX_HIRES) ? 640 : 320;
nuclear@3 16 scr_height = fb_height = (flags & GFX_ILACE) ? 512 : 256;
nuclear@3 17
nuclear@3 18 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
nuclear@3 19 fprintf(stderr, "failed to initialize SDL\n");
nuclear@3 20 return -1;
nuclear@3 21 }
nuclear@3 22 if(!(fbsurf = SDL_SetVideoMode(scr_width, scr_height, 32, SDL_SWSURFACE))) {
nuclear@3 23 fprintf(stderr, "failed to set video mode %dx%d\n", scr_width, scr_height);
nuclear@3 24 SDL_Quit();
nuclear@3 25 return -1;
nuclear@3 26 }
nuclear@3 27
nuclear@3 28 return 0;
nuclear@3 29 }
nuclear@3 30
nuclear@3 31 void gfx_shutdown(void)
nuclear@3 32 {
nuclear@3 33 SDL_Quit();
nuclear@3 34 }
nuclear@3 35
nuclear@3 36
nuclear@3 37 int gfx_screen_width(void)
nuclear@3 38 {
nuclear@3 39 return scr_width;
nuclear@3 40 }
nuclear@3 41
nuclear@3 42 int gfx_screen_height(void)
nuclear@3 43 {
nuclear@3 44 return scr_height;
nuclear@3 45 }
nuclear@3 46
nuclear@3 47
nuclear@3 48 void *gfx_set_framebuffer(void *fb, int width, int height)
nuclear@3 49 {
nuclear@3 50 return 0;
nuclear@3 51 }
nuclear@3 52
nuclear@3 53 void *gfx_get_framebuffer(void)
nuclear@3 54 {
nuclear@3 55 return 0;
nuclear@3 56 }
nuclear@3 57
nuclear@3 58
nuclear@3 59 int get_framebuffer_width(void)
nuclear@3 60 {
nuclear@3 61 return fb_width;
nuclear@3 62 }
nuclear@3 63
nuclear@3 64 int get_framebuffer_height(void)
nuclear@3 65 {
nuclear@3 66 return fb_height;
nuclear@3 67 }
nuclear@3 68
nuclear@3 69
nuclear@3 70 void gfx_begin_copperlist(void)
nuclear@3 71 {
nuclear@3 72 }
nuclear@3 73
nuclear@3 74
nuclear@3 75 int gfx_next_event(union gfx_event *ev, int block)
nuclear@3 76 {
nuclear@3 77 SDL_Event sdlev;
nuclear@3 78
nuclear@3 79 if(block) {
nuclear@3 80 SDL_WaitEvent(&sdlev);
nuclear@3 81 } else {
nuclear@3 82 if(!SDL_PollEvent(&sdlev)) {
nuclear@3 83 return 0;
nuclear@3 84 }
nuclear@3 85 }
nuclear@3 86
nuclear@3 87 switch(sdlev.type) {
nuclear@3 88 case SDL_QUIT:
nuclear@3 89 ev->type = GFX_EV_QUIT;
nuclear@3 90 return 1;
nuclear@3 91
nuclear@3 92 case SDL_KEYDOWN:
nuclear@3 93 case SDL_KEYUP:
nuclear@3 94 ev->type = GFX_EV_KEY;
nuclear@3 95 ev->key.key = sdlev.key.keysym.sym;
nuclear@3 96 ev->key.pressed = sdlev.key.state == SDL_PRESSED;
nuclear@3 97 return 1;
nuclear@3 98
nuclear@3 99 default:
nuclear@3 100 break;
nuclear@3 101 }
nuclear@3 102 return 0;
nuclear@3 103 }
nuclear@3 104
nuclear@3 105
nuclear@3 106 void gfx_wait_vpos(int x)
nuclear@3 107 {
nuclear@3 108 }
nuclear@3 109
nuclear@3 110 void gfx_wait_vblank(void)
nuclear@3 111 {
nuclear@3 112 }
nuclear@3 113
nuclear@3 114 #define ARED(x) ((((x) & 0xf00) >> 4) | (((x) & 0xf00) >> 8))
nuclear@3 115 #define AGREEN(x) (((x) & 0xf0) | (((x) & 0xf0) >> 4))
nuclear@3 116 #define ABLUE(x) ((((x) & 0xf) << 4) | ((x) & 0xf))
nuclear@3 117
nuclear@3 118 void gfx_show_image(struct ham_image *img)
nuclear@3 119 {
nuclear@3 120 int i, j, k;
nuclear@3 121 uint32_t palette[16];
nuclear@3 122 uint32_t *dest;
nuclear@3 123 unsigned char *src;
nuclear@3 124
nuclear@3 125 for(i=0; i<16; i++) {
nuclear@3 126 uint16_t pcol = img->palette[i];
nuclear@3 127 int red = ARED(pcol);
nuclear@3 128 int green = AGREEN(pcol);
nuclear@3 129 int blue = ABLUE(pcol);
nuclear@3 130 palette[i] = (red << fbsurf->format->Rshift) | (green << fbsurf->format->Gshift) |
nuclear@3 131 (blue << fbsurf->format->Bshift);
nuclear@3 132 }
nuclear@3 133
nuclear@3 134 if(SDL_MUSTLOCK(fbsurf)) {
nuclear@3 135 SDL_LockSurface(fbsurf);
nuclear@3 136 }
nuclear@3 137
nuclear@3 138 dest = fbsurf->pixels;
nuclear@3 139 src = img->pixels;
nuclear@3 140 for(i=0; i<img->height; i++) {
nuclear@3 141 for(j=0; j<img->width; j++) {
nuclear@3 142 unsigned char idx = 0;
nuclear@3 143 int bit = j & 7;
nuclear@3 144 for(k=0; k<img->nbitplanes; k++) {
nuclear@3 145 idx = (idx << 1) | ((*(src + k * img->width / 8) >> bit) & 1);
nuclear@3 146 }
nuclear@3 147 *dest++ = palette[idx];
nuclear@3 148 if(bit == 7) {
nuclear@3 149 ++src;
nuclear@3 150 }
nuclear@3 151 }
nuclear@3 152 src += img->width / 8 * (img->nbitplanes - 1);
nuclear@3 153 }
nuclear@3 154
nuclear@3 155 if(SDL_MUSTLOCK(fbsurf)) {
nuclear@3 156 SDL_UnlockSurface(fbsurf);
nuclear@3 157 }
nuclear@3 158
nuclear@3 159 SDL_Flip(fbsurf);
nuclear@3 160 }