gba-x3dtest
view src/main_sdl.c @ 17:0a7f402892b3
texture mapping
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Jun 2014 06:57:51 +0300 |
parents | b0ed38f13261 |
children | f907b2c50a8b |
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 keystate = 0;
108 }
110 done:
111 SDL_Quit();
112 return 0;
113 }
115 void flip(void)
116 {
117 SDL_Flip(surf);
118 }
120 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
121 {
122 int i, sz = pbuf->x * pbuf->y;
123 unsigned short *pixels = pbuf->pixels;
125 #ifdef PALMODE
126 color |= color << 8;
127 sz /= 2;
128 #endif
130 for(i=0; i<sz; i++) {
131 *pixels++ = color;
132 }
133 }
135 void set_palette(int idx, int r, int g, int b)
136 {
137 SDL_Color col;
138 col.r = r;
139 col.g = g;
140 col.b = b;
142 if(SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
143 fprintf(stderr, "set_palette failed to set the required color\n");
144 }
145 }
148 int get_key_state(int key)
149 {
150 return keystate & key;
151 }
153 unsigned long get_millisec(void)
154 {
155 return SDL_GetTicks();
156 }
158 static int proc_events(SDL_Event *ev)
159 {
160 switch(ev->type) {
161 case SDL_KEYDOWN:
162 case SDL_KEYUP:
163 handle_keyboard(ev->key.keysym.sym, ev->key.state);
164 break;
166 case SDL_QUIT:
167 return -1;
168 }
170 return 0;
171 }
173 static void handle_keyboard(int key, int state)
174 {
175 int gba_key = -1;
177 switch(key) {
178 case SDLK_ESCAPE:
179 {
180 SDL_Event ev;
181 ev.type = SDL_QUIT;
182 SDL_PushEvent(&ev);
183 }
184 break;
186 case 'a':
187 case 'A':
188 case SDLK_LEFT:
189 gba_key = KEY_LEFT;
190 break;
192 case 'd':
193 case 'D':
194 case SDLK_RIGHT:
195 gba_key = KEY_RIGHT;
196 break;
198 case 'w':
199 case 'W':
200 case SDLK_UP:
201 gba_key = KEY_UP;
202 break;
204 case 's':
205 case 'S':
206 case SDLK_DOWN:
207 gba_key = KEY_DOWN;
208 break;
210 case '\n':
211 case '\r':
212 gba_key = KEY_A;
213 break;
214 case '\b':
215 gba_key = KEY_B;
216 break;
218 case ',':
219 gba_key = KEY_SELECT;
220 break;
221 case '.':
222 gba_key = KEY_START;
223 break;
225 case '[':
226 gba_key = KEY_L;
227 break;
228 case ']':
229 gba_key = KEY_R;
230 break;
232 default:
233 return;
234 }
236 keystate |= gba_key;
237 game_keyb(gba_key, state);
238 }