gba-trycatch
view src/main_sdl.c @ 8:fb0a0d6a8b52
sortof works
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 19 Jun 2014 05:53:46 +0300 |
parents | 73b5f2e5d18a |
children | b0ed38f13261 |
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 static int proc_events(SDL_Event *ev);
9 static void handle_keyboard(int key, int state);
11 struct pixel_buffer *back_buffer, *front_buffer;
13 static SDL_Surface *surf;
14 static struct pixel_buffer bbuf;
15 static unsigned int keystate;
16 static int sdlscale = 2;
18 int main(void)
19 {
20 int i, j, k, l;
21 char *env;
23 if((env = getenv("SDLSCALE"))) {
24 if(!(sdlscale = atoi(env))) {
25 fprintf(stderr, "invalid SDLSCALE envvar value (%s)\n", env);
26 sdlscale = 2;
27 }
28 }
30 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
32 if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, 16, SDL_SWSURFACE))) {
33 fprintf(stderr, "failed to initialize graphics\n");
34 return 1;
35 }
36 SDL_WM_SetCaption("trycatch", 0);
38 bbuf.x = WIDTH;
39 bbuf.y = HEIGHT;
40 bbuf.bpp = 16;
41 if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * 2))) {
42 fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT);
43 SDL_Quit();
44 return 1;
45 }
47 back_buffer = front_buffer = &bbuf;
49 if(game_init() == -1) {
50 SDL_Quit();
51 return 1;
52 }
54 for(;;) {
55 SDL_Event ev;
56 uint16_t *dest, *src;
58 while(SDL_PollEvent(&ev)) {
59 if(proc_events(&ev) == -1) {
60 goto done;
61 }
62 }
64 game_draw();
66 if(SDL_MUSTLOCK(surf)) {
67 SDL_LockSurface(surf);
68 }
70 src = bbuf.pixels;
71 dest = surf->pixels;
73 for(i=0; i<HEIGHT; i++) {
74 for(j=0; j<WIDTH; j++) {
75 uint16_t pixel = *src++;
77 for(k=0; k<sdlscale; k++) {
78 for(l=0; l<sdlscale; l++) {
79 dest[k * sdlscale * WIDTH + l] = pixel;
80 }
81 }
82 dest += sdlscale;
83 }
84 dest += WIDTH * sdlscale * (sdlscale - 1);
85 }
87 if(SDL_MUSTLOCK(surf)) {
88 SDL_UnlockSurface(surf);
89 }
90 keystate = 0;
91 }
93 done:
94 SDL_Quit();
95 return 0;
96 }
98 void flip(void)
99 {
100 SDL_Flip(surf);
101 }
103 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
104 {
105 int i;
106 unsigned short *pixels = pbuf->pixels;
108 for(i=0; i<pbuf->x * pbuf->y; i++) {
109 *pixels++ = color;
110 }
111 }
113 int get_key_state(int key)
114 {
115 return keystate & key;
116 }
118 unsigned long get_millisec(void)
119 {
120 return SDL_GetTicks();
121 }
123 static int proc_events(SDL_Event *ev)
124 {
125 switch(ev->type) {
126 case SDL_KEYDOWN:
127 case SDL_KEYUP:
128 handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_KEYDOWN);
129 break;
131 case SDL_QUIT:
132 return -1;
133 }
135 return 0;
136 }
138 static void handle_keyboard(int key, int state)
139 {
140 int gba_key = -1;
142 switch(key) {
143 case SDLK_ESCAPE:
144 {
145 SDL_Event ev;
146 ev.type = SDL_QUIT;
147 SDL_PushEvent(&ev);
148 }
149 break;
151 case 'a':
152 case 'A':
153 case SDLK_LEFT:
154 gba_key = KEY_LEFT;
155 break;
157 case 'd':
158 case 'D':
159 case SDLK_RIGHT:
160 gba_key = KEY_RIGHT;
161 break;
163 case 'w':
164 case 'W':
165 case SDLK_UP:
166 gba_key = KEY_UP;
167 break;
169 case 's':
170 case 'S':
171 case SDLK_DOWN:
172 gba_key = KEY_DOWN;
173 break;
175 case '\n':
176 gba_key = KEY_A;
177 break;
178 case '\b':
179 gba_key = KEY_B;
180 break;
182 case ',':
183 gba_key = KEY_SELECT;
184 break;
185 case '.':
186 gba_key = KEY_START;
187 break;
189 case '[':
190 gba_key = KEY_L;
191 break;
192 case ']':
193 gba_key = KEY_R;
194 break;
196 default:
197 return;
198 }
200 keystate |= gba_key;
201 game_keyb(gba_key, state);
202 }