eobish

view src/fblibsdl.c @ 8:c0e8bbf96849

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 19 Jan 2015 15:49:36 +0200
parents ce0548d24918
children
line source
1 #ifdef FBLIB_SDL
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <SDL/SDL.h>
6 #include "fblib.h"
7 #include "fblibimp.h"
9 static int scale;
10 static SDL_Surface *surf;
11 static unsigned char *scalebuf; /* only if scale != 1 */
12 static int pixbytes; /* pixel size in bytes */
14 int fb_init(int width, int height, int bpp)
15 {
16 static int sdlinit_done;
17 char *env, title[64];
19 if((env = getenv("FBLIB_SCALE"))) {
20 scale = atoi(env);
21 }
22 if(!scale) scale = 1;
24 fb_width = width;
25 fb_height = height;
26 fb_bpp = bpp;
28 pixbytes = (bpp + 7) / 8;
30 if(!sdlinit_done) {
31 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
32 fprintf(stderr, "failed to initialize SDL!\n");
33 return -1;
34 }
35 sdlinit_done = 1;
36 }
38 if(!(surf = SDL_SetVideoMode(width * scale, height * scale, bpp, SDL_SWSURFACE))) {
39 fprintf(stderr, "failed to set video mode\n");
40 return -1;
41 }
42 sprintf(title, "fblib window (%dx)", scale);
43 SDL_WM_SetCaption(title, 0);
45 if(scale != 1) {
46 if(!(scalebuf = malloc(width * height * pixbytes))) {
47 fprintf(stderr, "failed to allocate back buffer\n");
48 SDL_Quit();
49 return -1;
50 }
51 } else {
52 scalebuf = 0;
53 }
55 return 0;
56 }
58 void fb_shutdown(void)
59 {
60 free(scalebuf);
61 SDL_Quit();
62 }
64 void *fb_begin_frame(void)
65 {
66 if(!surf) return 0;
68 if(scalebuf) {
69 return scalebuf;
70 }
72 if(SDL_MUSTLOCK(surf)) {
73 SDL_LockSurface(surf);
74 }
75 return surf->pixels;
76 }
78 void fb_end_frame(void)
79 {
80 if(scalebuf) {
81 int i, j, k;
82 unsigned char *dest;
84 if(SDL_MUSTLOCK(surf)) {
85 SDL_LockSurface(surf);
86 }
87 dest = surf->pixels;
89 for(i=0; i<surf->h; i++) {
90 int y = i / scale;
91 unsigned char *scan = scalebuf + y * fb_width * pixbytes;
93 for(j=0; j<surf->w; j++) {
94 int x = j / scale;
96 for(k=0; k<pixbytes; k++) {
97 *dest++ = scan[x * pixbytes + k];
98 }
99 }
100 }
101 }
103 if(SDL_MUSTLOCK(surf)) {
104 SDL_UnlockSurface(surf);
105 }
107 SDL_Flip(surf);
108 }
110 void fb_set_palette_range(int start, int count, int *colors)
111 {
112 int i, *col = colors;
113 SDL_Colour sdlcol[256];
115 for(i=0; i<count; i++) {
116 sdlcol[i].r = *col++;
117 sdlcol[i].g = *col++;
118 sdlcol[i].b = *col++;
119 }
121 SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, sdlcol, start, count);
122 }
124 int fb_process_events(void)
125 {
126 SDL_Event ev;
128 while(SDL_PollEvent(&ev)) {
129 switch(ev.type) {
130 case SDL_QUIT:
131 return -1;
133 case SDL_KEYDOWN:
134 case SDL_KEYUP:
135 {
136 int key = ev.key.keysym.sym;
137 int state = ev.key.state == SDL_PRESSED;
139 if(key < 256) {
140 fb_inp.key[key] = state;
141 }
142 if(fb_cb.keyb) {
143 if(fb_cb.keyb(key, state, fb_cb.keyb_data) == -1) {
144 return -1;
145 }
146 } else {
147 if(key == SDLK_ESCAPE) {
148 return -1;
149 }
150 }
151 }
152 break;
154 case SDL_MOUSEBUTTONDOWN:
155 case SDL_MOUSEBUTTONUP:
156 {
157 int state = ev.button.state == SDL_PRESSED;
158 fb_inp.mx = ev.motion.x / scale;
159 fb_inp.my = ev.motion.y / scale;
160 fb_inp.mbutton[ev.button.which] = state;
162 if(fb_cb.button) {
163 if(fb_cb.button(ev.button.which, state, fb_inp.mx, fb_inp.my, fb_cb.button_data) == -1) {
164 return -1;
165 }
166 }
167 }
168 break;
170 case SDL_MOUSEMOTION:
171 fb_inp.mx = ev.motion.x / scale;
172 fb_inp.my = ev.motion.y / scale;
173 if(fb_cb.motion) {
174 if(fb_cb.motion(fb_inp.mx, fb_inp.my, fb_cb.motion_data) == -1) {
175 return -1;
176 }
177 }
178 break;
179 }
180 }
182 return 0;
183 }
185 unsigned long fb_get_time(void)
186 {
187 return SDL_GetTicks();
188 }
190 #endif /* FBLIB_SDL */