gba-trycatch
view src/main_sdl.c @ 6:73b5f2e5d18a
first triangle on screen
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 18 Jun 2014 04:13:02 +0300 |
parents | 850be43b3135 |
children | fb0a0d6a8b52 |
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 for(;;) {
50 SDL_Event ev;
51 uint16_t *dest, *src;
53 while(SDL_PollEvent(&ev)) {
54 if(proc_events(&ev) == -1) {
55 goto done;
56 }
57 }
59 game_draw();
61 if(SDL_MUSTLOCK(surf)) {
62 SDL_LockSurface(surf);
63 }
65 src = bbuf.pixels;
66 dest = surf->pixels;
68 for(i=0; i<HEIGHT; i++) {
69 for(j=0; j<WIDTH; j++) {
70 uint16_t pixel = *src++;
72 for(k=0; k<sdlscale; k++) {
73 for(l=0; l<sdlscale; l++) {
74 dest[k * sdlscale * WIDTH + l] = pixel;
75 }
76 }
77 dest += sdlscale;
78 }
79 dest += WIDTH * sdlscale * (sdlscale - 1);
80 }
82 if(SDL_MUSTLOCK(surf)) {
83 SDL_UnlockSurface(surf);
84 }
85 keystate = 0;
86 }
88 done:
89 SDL_Quit();
90 return 0;
91 }
93 void flip(void)
94 {
95 SDL_Flip(surf);
96 }
98 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
99 {
100 int i;
101 unsigned short *pixels = pbuf->pixels;
103 for(i=0; i<pbuf->x * pbuf->y; i++) {
104 *pixels++ = color;
105 }
106 }
108 int get_key_state(int key)
109 {
110 return keystate & key;
111 }
113 unsigned long get_millisec(void)
114 {
115 return SDL_GetTicks();
116 }
118 static int proc_events(SDL_Event *ev)
119 {
120 switch(ev->type) {
121 case SDL_KEYDOWN:
122 case SDL_KEYUP:
123 handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_KEYDOWN);
124 break;
126 case SDL_QUIT:
127 return -1;
128 }
130 return 0;
131 }
133 static void handle_keyboard(int key, int state)
134 {
135 int gba_key = -1;
137 switch(key) {
138 case SDLK_ESCAPE:
139 {
140 SDL_Event ev;
141 ev.type = SDL_QUIT;
142 SDL_PushEvent(&ev);
143 }
144 break;
146 case 'a':
147 case 'A':
148 case SDLK_LEFT:
149 gba_key = KEY_LEFT;
150 break;
152 case 'd':
153 case 'D':
154 case SDLK_RIGHT:
155 gba_key = KEY_RIGHT;
156 break;
158 case 'w':
159 case 'W':
160 case SDLK_UP:
161 gba_key = KEY_UP;
162 break;
164 case 's':
165 case 'S':
166 case SDLK_DOWN:
167 gba_key = KEY_DOWN;
168 break;
170 case '\n':
171 gba_key = KEY_A;
172 break;
173 case '\b':
174 gba_key = KEY_B;
175 break;
177 case ',':
178 gba_key = KEY_SELECT;
179 break;
180 case '.':
181 gba_key = KEY_START;
182 break;
184 case '[':
185 gba_key = KEY_L;
186 break;
187 case ']':
188 gba_key = KEY_R;
189 break;
191 default:
192 return;
193 }
195 keystate |= gba_key;
196 game_keyb(gba_key, state);
197 }