gba-x3dtest

view src/main_sdl.c @ 9:b0ed38f13261

working on the rasterizer
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Jun 2014 05:16:10 +0300
parents fb0a0d6a8b52
children 2070a81127f2
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++;
89 for(k=0; k<sdlscale; k++) {
90 for(l=0; l<sdlscale; l++) {
91 dest[k * sdlscale * WIDTH + l] = pixel;
92 }
93 }
94 dest += sdlscale;
95 }
96 dest += WIDTH * sdlscale * (sdlscale - 1);
97 }
99 if(SDL_MUSTLOCK(surf)) {
100 SDL_UnlockSurface(surf);
101 }
102 keystate = 0;
103 }
105 done:
106 SDL_Quit();
107 return 0;
108 }
110 void flip(void)
111 {
112 SDL_Flip(surf);
113 }
115 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
116 {
117 int i, sz = pbuf->x * pbuf->y;
118 unsigned short *pixels = pbuf->pixels;
120 #ifdef PALMODE
121 color |= color << 8;
122 sz /= 2;
123 #endif
125 for(i=0; i<sz; i++) {
126 *pixels++ = color;
127 }
128 }
130 void set_palette(int idx, int r, int g, int b)
131 {
132 SDL_Color col;
133 col.r = r;
134 col.g = g;
135 col.b = b;
137 if(SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
138 fprintf(stderr, "set_palette failed to set the required color\n");
139 }
140 }
143 int get_key_state(int key)
144 {
145 return keystate & key;
146 }
148 unsigned long get_millisec(void)
149 {
150 return SDL_GetTicks();
151 }
153 static int proc_events(SDL_Event *ev)
154 {
155 switch(ev->type) {
156 case SDL_KEYDOWN:
157 case SDL_KEYUP:
158 handle_keyboard(ev->key.keysym.sym, ev->key.state);
159 break;
161 case SDL_QUIT:
162 return -1;
163 }
165 return 0;
166 }
168 static void handle_keyboard(int key, int state)
169 {
170 int gba_key = -1;
172 switch(key) {
173 case SDLK_ESCAPE:
174 {
175 SDL_Event ev;
176 ev.type = SDL_QUIT;
177 SDL_PushEvent(&ev);
178 }
179 break;
181 case 'a':
182 case 'A':
183 case SDLK_LEFT:
184 gba_key = KEY_LEFT;
185 break;
187 case 'd':
188 case 'D':
189 case SDLK_RIGHT:
190 gba_key = KEY_RIGHT;
191 break;
193 case 'w':
194 case 'W':
195 case SDLK_UP:
196 gba_key = KEY_UP;
197 break;
199 case 's':
200 case 'S':
201 case SDLK_DOWN:
202 gba_key = KEY_DOWN;
203 break;
205 case '\n':
206 case '\r':
207 gba_key = KEY_A;
208 break;
209 case '\b':
210 gba_key = KEY_B;
211 break;
213 case ',':
214 gba_key = KEY_SELECT;
215 break;
216 case '.':
217 gba_key = KEY_START;
218 break;
220 case '[':
221 gba_key = KEY_L;
222 break;
223 case ']':
224 gba_key = KEY_R;
225 break;
227 default:
228 return;
229 }
231 keystate |= gba_key;
232 game_keyb(gba_key, state);
233 }