gba-x3dtest

view src/main_sdl.c @ 20:2e903e27e35a

fixed x3d_disable_texture added runtime teture checks in the rasterizer
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 01 Jul 2014 23:23:37 +0300
parents 2070a81127f2
children
line source
1 #include "config.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <SDL.h>
5 #include "gbasys.h"
6 #include "game.h"
8 #ifdef PALMODE
9 #define BPP 8
10 #define PIXSZ 1
11 #else
12 #define BPP 16
13 #define PIXSZ 2
14 #endif
16 static int proc_events(SDL_Event *ev);
17 static void handle_keyboard(int key, int state);
19 struct pixel_buffer *back_buffer, *front_buffer;
21 static SDL_Surface *surf;
22 static struct pixel_buffer bbuf;
23 static unsigned int keystate;
24 static int sdlscale = 2;
26 int main(void)
27 {
28 int i, j, k, l;
29 char *env;
31 if((env = getenv("SDLSCALE"))) {
32 if(!(sdlscale = atoi(env))) {
33 fprintf(stderr, "invalid SDLSCALE envvar value (%s)\n", env);
34 sdlscale = 2;
35 }
36 }
38 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
40 if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, BPP, SDL_SWSURFACE))) {
41 fprintf(stderr, "failed to initialize graphics\n");
42 return 1;
43 }
44 SDL_WM_SetCaption("trycatch", 0);
46 bbuf.x = WIDTH;
47 bbuf.y = HEIGHT;
48 bbuf.bpp = BPP;
49 if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * PIXSZ))) {
50 fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT);
51 SDL_Quit();
52 return 1;
53 }
55 back_buffer = front_buffer = &bbuf;
57 if(game_init() == -1) {
58 SDL_Quit();
59 return 1;
60 }
62 for(;;) {
63 SDL_Event ev;
64 #ifdef PALMODE
65 uint8_t *dest, *src;
66 #else
67 uint16_t *dest, *src;
68 #endif
70 while(SDL_PollEvent(&ev)) {
71 if(proc_events(&ev) == -1) {
72 goto done;
73 }
74 }
76 game_draw();
78 if(SDL_MUSTLOCK(surf)) {
79 SDL_LockSurface(surf);
80 }
82 src = bbuf.pixels;
83 dest = surf->pixels;
85 for(i=0; i<HEIGHT; i++) {
86 for(j=0; j<WIDTH; j++) {
87 uint16_t pixel = *src++;
88 unsigned int red = GET_R(pixel);
89 unsigned int green = GET_G(pixel);
90 unsigned int blue = GET_B(pixel);
92 pixel = ((blue >> 3) << 11) | ((green >> 2) << 5) | (red >> 3);
94 for(k=0; k<sdlscale; k++) {
95 for(l=0; l<sdlscale; l++) {
96 dest[k * sdlscale * WIDTH + l] = pixel;
97 }
98 }
99 dest += sdlscale;
100 }
101 dest += WIDTH * sdlscale * (sdlscale - 1);
102 }
104 if(SDL_MUSTLOCK(surf)) {
105 SDL_UnlockSurface(surf);
106 }
107 }
109 done:
110 SDL_Quit();
111 return 0;
112 }
114 void flip(void)
115 {
116 SDL_Flip(surf);
117 }
119 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
120 {
121 int i, sz = pbuf->x * pbuf->y;
122 unsigned short *pixels = pbuf->pixels;
124 #ifdef PALMODE
125 color |= color << 8;
126 sz /= 2;
127 #endif
129 for(i=0; i<sz; i++) {
130 *pixels++ = color;
131 }
132 }
134 void set_palette(int idx, int r, int g, int b)
135 {
136 SDL_Color col;
137 col.r = r;
138 col.g = g;
139 col.b = b;
141 if(SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
142 fprintf(stderr, "set_palette failed to set the required color\n");
143 }
144 }
147 int get_key_state(int key)
148 {
149 return keystate & key;
150 }
152 unsigned long get_millisec(void)
153 {
154 return SDL_GetTicks();
155 }
157 static int proc_events(SDL_Event *ev)
158 {
159 switch(ev->type) {
160 case SDL_KEYDOWN:
161 case SDL_KEYUP:
162 handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0);
163 break;
165 case SDL_QUIT:
166 return -1;
167 }
169 return 0;
170 }
172 static void handle_keyboard(int key, int state)
173 {
174 int gba_key = -1;
176 switch(key) {
177 case SDLK_ESCAPE:
178 {
179 SDL_Event ev;
180 ev.type = SDL_QUIT;
181 SDL_PushEvent(&ev);
182 }
183 break;
185 case 'a':
186 case 'A':
187 case SDLK_LEFT:
188 gba_key = KEY_LEFT;
189 break;
191 case 'd':
192 case 'D':
193 case SDLK_RIGHT:
194 gba_key = KEY_RIGHT;
195 break;
197 case 'w':
198 case 'W':
199 case SDLK_UP:
200 gba_key = KEY_UP;
201 break;
203 case 's':
204 case 'S':
205 case SDLK_DOWN:
206 gba_key = KEY_DOWN;
207 break;
209 case '\n':
210 case '\r':
211 gba_key = KEY_A;
212 break;
213 case '\b':
214 gba_key = KEY_B;
215 break;
217 case ',':
218 gba_key = KEY_SELECT;
219 break;
220 case '.':
221 gba_key = KEY_START;
222 break;
224 case '[':
225 gba_key = KEY_L;
226 break;
227 case ']':
228 gba_key = KEY_R;
229 break;
231 default:
232 return;
233 }
235 if(gba_key == -1) {
236 return;
237 }
239 if(state) {
240 keystate |= gba_key;
241 } else {
242 keystate &= ~gba_key;
243 }
244 game_keyb(gba_key, state);
245 }