eobish
diff src/fblibsdl.c @ 2:cdbcae5b3b98
added fblib
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 18 Jan 2015 03:16:37 +0200 |
parents | |
children | ce0548d24918 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/fblibsdl.c Sun Jan 18 03:16:37 2015 +0200 1.3 @@ -0,0 +1,199 @@ 1.4 +#ifdef FBLIB_SDL 1.5 +#include <stdio.h> 1.6 +#include <stdlib.h> 1.7 +#include <string.h> 1.8 +#include <SDL/SDL.h> 1.9 +#include "fblib.h" 1.10 +#include "fblibimp.h" 1.11 + 1.12 +static int scale; 1.13 +static SDL_Surface *surf; 1.14 +static unsigned char *scalebuf; /* only if scale != 1 */ 1.15 +static int pixbytes; /* pixel size in bytes */ 1.16 + 1.17 +int fb_init(int width, int height, int bpp) 1.18 +{ 1.19 + static int sdlinit_done; 1.20 + char *env, title[64]; 1.21 + 1.22 + if((env = getenv("FBLIB_SCALE"))) { 1.23 + scale = atoi(env); 1.24 + } 1.25 + if(!scale) scale = 1; 1.26 + 1.27 + fb_width = width; 1.28 + fb_height = height; 1.29 + fb_bpp = bpp; 1.30 + 1.31 + pixbytes = (bpp + 7) / 8; 1.32 + 1.33 + if(!sdlinit_done) { 1.34 + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { 1.35 + fprintf(stderr, "failed to initialize SDL!\n"); 1.36 + return -1; 1.37 + } 1.38 + sdlinit_done = 1; 1.39 + } 1.40 + 1.41 + if(!(surf = SDL_SetVideoMode(width * scale, height * scale, bpp, SDL_SWSURFACE))) { 1.42 + fprintf(stderr, "failed to set video mode\n"); 1.43 + return -1; 1.44 + } 1.45 + sprintf(title, "fblib window (%dx)", scale); 1.46 + SDL_WM_SetCaption(title, 0); 1.47 + 1.48 + if(scale != 1) { 1.49 + if(!(scalebuf = malloc(width * height * pixbytes))) { 1.50 + fprintf(stderr, "failed to allocate back buffer\n"); 1.51 + SDL_Quit(); 1.52 + return -1; 1.53 + } 1.54 + } else { 1.55 + scalebuf = 0; 1.56 + } 1.57 + 1.58 + return 0; 1.59 +} 1.60 + 1.61 +void fb_shutdown(void) 1.62 +{ 1.63 + free(scalebuf); 1.64 + SDL_Quit(); 1.65 +} 1.66 + 1.67 +int fb_get_width(void) 1.68 +{ 1.69 + return fb_width; 1.70 +} 1.71 + 1.72 +int fb_get_height(void) 1.73 +{ 1.74 + return fb_height; 1.75 +} 1.76 + 1.77 +int fb_get_bpp(void) 1.78 +{ 1.79 + return fb_bpp; 1.80 +} 1.81 + 1.82 +void *fb_begin_frame(void) 1.83 +{ 1.84 + if(!surf) return 0; 1.85 + 1.86 + if(scalebuf) { 1.87 + return scalebuf; 1.88 + } 1.89 + 1.90 + if(SDL_MUSTLOCK(surf)) { 1.91 + SDL_LockSurface(surf); 1.92 + } 1.93 + return surf->pixels; 1.94 +} 1.95 + 1.96 +void fb_end_frame(void) 1.97 +{ 1.98 + if(scalebuf) { 1.99 + int i, j, k; 1.100 + unsigned char *dest; 1.101 + 1.102 + if(SDL_MUSTLOCK(surf)) { 1.103 + SDL_LockSurface(surf); 1.104 + } 1.105 + dest = surf->pixels; 1.106 + 1.107 + for(i=0; i<surf->h; i++) { 1.108 + int y = i / scale; 1.109 + unsigned char *scan = scalebuf + y * fb_width * pixbytes; 1.110 + 1.111 + for(j=0; j<surf->w; j++) { 1.112 + int x = j / scale; 1.113 + 1.114 + for(k=0; k<pixbytes; k++) { 1.115 + *dest++ = scan[x * pixbytes + k]; 1.116 + } 1.117 + } 1.118 + } 1.119 + } 1.120 + 1.121 + if(SDL_MUSTLOCK(surf)) { 1.122 + SDL_UnlockSurface(surf); 1.123 + } 1.124 + 1.125 + SDL_Flip(surf); 1.126 +} 1.127 + 1.128 +void fb_set_palette_range(int start, int count, int *colors) 1.129 +{ 1.130 + int i, *col = colors; 1.131 + SDL_Colour sdlcol[256]; 1.132 + 1.133 + for(i=0; i<count; i++) { 1.134 + sdlcol[i].r = *col++; 1.135 + sdlcol[i].g = *col++; 1.136 + sdlcol[i].b = *col++; 1.137 + } 1.138 + 1.139 + SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, sdlcol, start, count); 1.140 +} 1.141 + 1.142 +int fb_process_events(void) 1.143 +{ 1.144 + SDL_Event ev; 1.145 + 1.146 + while(SDL_PollEvent(&ev)) { 1.147 + switch(ev.type) { 1.148 + case SDL_QUIT: 1.149 + return -1; 1.150 + 1.151 + case SDL_KEYDOWN: 1.152 + case SDL_KEYUP: 1.153 + { 1.154 + int key = ev.key.keysym.sym; 1.155 + int state = ev.key.state == SDL_PRESSED; 1.156 + 1.157 + if(key < 256) { 1.158 + fb_inp.key[key] = state; 1.159 + } 1.160 + if(fb_cb.keyb) { 1.161 + fb_cb.keyb(key, state, fb_cb.keyb_data); 1.162 + } else { 1.163 + if(key == SDLK_ESCAPE) { 1.164 + return -1; 1.165 + } 1.166 + } 1.167 + } 1.168 + break; 1.169 + 1.170 + case SDL_MOUSEBUTTONDOWN: 1.171 + case SDL_MOUSEBUTTONUP: 1.172 + { 1.173 + int state = ev.button.state == SDL_PRESSED; 1.174 + fb_inp.mx = ev.motion.x / scale; 1.175 + fb_inp.my = ev.motion.y / scale; 1.176 + fb_inp.mbutton[ev.button.which] = state; 1.177 + 1.178 + if(fb_cb.button) { 1.179 + fb_cb.button(ev.button.which, state, fb_inp.mx, fb_inp.my, fb_cb.button_data); 1.180 + } 1.181 + } 1.182 + break; 1.183 + 1.184 + case SDL_MOUSEMOTION: 1.185 + fb_inp.mx = ev.motion.x / scale; 1.186 + fb_inp.my = ev.motion.y / scale; 1.187 + if(fb_cb.motion) { 1.188 + fb_cb.motion(fb_inp.mx, fb_inp.my, fb_cb.motion_data); 1.189 + } 1.190 + break; 1.191 + } 1.192 + } 1.193 + 1.194 + return 0; 1.195 +} 1.196 + 1.197 +unsigned long fb_get_time(void) 1.198 +{ 1.199 + return SDL_GetTicks(); 1.200 +} 1.201 + 1.202 +#endif /* FBLIB_SDL */