gba-trycatch
view src/main_sdl.c @ 5:850be43b3135
sdl version
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 16 Jun 2014 22:01:45 +0300 |
parents | |
children | 73b5f2e5d18a |
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;
17 int main(void)
18 {
19 int i, j;
21 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
23 if(!(surf = SDL_SetVideoMode(WIDTH * SDLSCALE, HEIGHT * SDLSCALE, 16, SDL_SWSURFACE))) {
24 fprintf(stderr, "failed to initialize graphics\n");
25 return 1;
26 }
27 SDL_WM_SetCaption("trycatch", 0);
29 bbuf.x = WIDTH;
30 bbuf.y = HEIGHT;
31 bbuf.bpp = 16;
32 if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * 2))) {
33 fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT);
34 SDL_Quit();
35 return 1;
36 }
38 back_buffer = front_buffer = &bbuf;
40 for(;;) {
41 SDL_Event ev;
42 uint16_t *dest, *src;
44 while(SDL_PollEvent(&ev)) {
45 if(proc_events(&ev) == -1) {
46 goto done;
47 }
48 }
50 game_draw();
52 if(SDL_MUSTLOCK(surf)) {
53 SDL_LockSurface(surf);
54 }
56 src = bbuf.pixels;
57 dest = surf->pixels;
59 for(i=0; i<HEIGHT; i++) {
60 for(j=0; j<WIDTH; j++) {
61 dest[0] = dest[1] = dest[WIDTH] = dest[WIDTH + 1] = *src++;
62 dest += 2;
63 }
64 dest += WIDTH;
65 }
67 if(SDL_MUSTLOCK(surf)) {
68 SDL_UnlockSurface(surf);
69 }
70 keystate = 0;
71 }
73 done:
74 SDL_Quit();
75 return 0;
76 }
78 void flip(void)
79 {
80 SDL_Flip(surf);
81 }
83 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
84 {
85 int i;
86 unsigned short *pixels = pbuf->pixels;
88 for(i=0; i<pbuf->x * pbuf->y; i++) {
89 *pixels++ = color;
90 }
91 }
93 int get_key_state(int key)
94 {
95 return keystate & key;
96 }
98 unsigned long get_millisec(void)
99 {
100 return SDL_GetTicks();
101 }
103 static int proc_events(SDL_Event *ev)
104 {
105 switch(ev->type) {
106 case SDL_KEYDOWN:
107 case SDL_KEYUP:
108 handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_KEYDOWN);
109 break;
111 case SDL_QUIT:
112 return -1;
113 }
115 return 0;
116 }
118 static void handle_keyboard(int key, int state)
119 {
120 int gba_key = -1;
122 switch(key) {
123 case SDLK_ESCAPE:
124 {
125 SDL_Event ev;
126 ev.type = SDL_QUIT;
127 SDL_PushEvent(&ev);
128 }
129 break;
131 case 'a':
132 case 'A':
133 case SDLK_LEFT:
134 gba_key = KEY_LEFT;
135 break;
137 case 'd':
138 case 'D':
139 case SDLK_RIGHT:
140 gba_key = KEY_RIGHT;
141 break;
143 case 'w':
144 case 'W':
145 case SDLK_UP:
146 gba_key = KEY_UP;
147 break;
149 case 's':
150 case 'S':
151 case SDLK_DOWN:
152 gba_key = KEY_DOWN;
153 break;
155 case '\n':
156 gba_key = KEY_A;
157 break;
158 case '\b':
159 gba_key = KEY_B;
160 break;
162 case ',':
163 gba_key = KEY_SELECT;
164 break;
165 case '.':
166 gba_key = KEY_START;
167 break;
169 case '[':
170 gba_key = KEY_L;
171 break;
172 case ']':
173 gba_key = KEY_R;
174 break;
176 default:
177 return;
178 }
180 keystate |= gba_key;
181 game_keyb(gba_key, state);
182 }