rev |
line source |
nuclear@5
|
1 #include "config.h"
|
nuclear@5
|
2 #include <stdio.h>
|
nuclear@5
|
3 #include <stdlib.h>
|
nuclear@5
|
4 #include <SDL.h>
|
nuclear@5
|
5 #include "gbasys.h"
|
nuclear@5
|
6 #include "game.h"
|
nuclear@5
|
7
|
nuclear@9
|
8 #ifdef PALMODE
|
nuclear@9
|
9 #define BPP 8
|
nuclear@9
|
10 #define PIXSZ 1
|
nuclear@9
|
11 #else
|
nuclear@9
|
12 #define BPP 16
|
nuclear@9
|
13 #define PIXSZ 2
|
nuclear@9
|
14 #endif
|
nuclear@9
|
15
|
nuclear@5
|
16 static int proc_events(SDL_Event *ev);
|
nuclear@5
|
17 static void handle_keyboard(int key, int state);
|
nuclear@5
|
18
|
nuclear@5
|
19 struct pixel_buffer *back_buffer, *front_buffer;
|
nuclear@5
|
20
|
nuclear@5
|
21 static SDL_Surface *surf;
|
nuclear@5
|
22 static struct pixel_buffer bbuf;
|
nuclear@5
|
23 static unsigned int keystate;
|
nuclear@6
|
24 static int sdlscale = 2;
|
nuclear@5
|
25
|
nuclear@5
|
26 int main(void)
|
nuclear@5
|
27 {
|
nuclear@6
|
28 int i, j, k, l;
|
nuclear@6
|
29 char *env;
|
nuclear@6
|
30
|
nuclear@6
|
31 if((env = getenv("SDLSCALE"))) {
|
nuclear@6
|
32 if(!(sdlscale = atoi(env))) {
|
nuclear@6
|
33 fprintf(stderr, "invalid SDLSCALE envvar value (%s)\n", env);
|
nuclear@6
|
34 sdlscale = 2;
|
nuclear@6
|
35 }
|
nuclear@6
|
36 }
|
nuclear@5
|
37
|
nuclear@5
|
38 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
|
nuclear@5
|
39
|
nuclear@9
|
40 if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, BPP, SDL_SWSURFACE))) {
|
nuclear@5
|
41 fprintf(stderr, "failed to initialize graphics\n");
|
nuclear@5
|
42 return 1;
|
nuclear@5
|
43 }
|
nuclear@5
|
44 SDL_WM_SetCaption("trycatch", 0);
|
nuclear@5
|
45
|
nuclear@5
|
46 bbuf.x = WIDTH;
|
nuclear@5
|
47 bbuf.y = HEIGHT;
|
nuclear@9
|
48 bbuf.bpp = BPP;
|
nuclear@9
|
49 if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * PIXSZ))) {
|
nuclear@5
|
50 fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT);
|
nuclear@5
|
51 SDL_Quit();
|
nuclear@5
|
52 return 1;
|
nuclear@5
|
53 }
|
nuclear@5
|
54
|
nuclear@5
|
55 back_buffer = front_buffer = &bbuf;
|
nuclear@5
|
56
|
nuclear@8
|
57 if(game_init() == -1) {
|
nuclear@8
|
58 SDL_Quit();
|
nuclear@8
|
59 return 1;
|
nuclear@8
|
60 }
|
nuclear@8
|
61
|
nuclear@5
|
62 for(;;) {
|
nuclear@5
|
63 SDL_Event ev;
|
nuclear@9
|
64 #ifdef PALMODE
|
nuclear@9
|
65 uint8_t *dest, *src;
|
nuclear@9
|
66 #else
|
nuclear@5
|
67 uint16_t *dest, *src;
|
nuclear@9
|
68 #endif
|
nuclear@5
|
69
|
nuclear@5
|
70 while(SDL_PollEvent(&ev)) {
|
nuclear@5
|
71 if(proc_events(&ev) == -1) {
|
nuclear@5
|
72 goto done;
|
nuclear@5
|
73 }
|
nuclear@5
|
74 }
|
nuclear@5
|
75
|
nuclear@5
|
76 game_draw();
|
nuclear@5
|
77
|
nuclear@5
|
78 if(SDL_MUSTLOCK(surf)) {
|
nuclear@5
|
79 SDL_LockSurface(surf);
|
nuclear@5
|
80 }
|
nuclear@5
|
81
|
nuclear@5
|
82 src = bbuf.pixels;
|
nuclear@5
|
83 dest = surf->pixels;
|
nuclear@5
|
84
|
nuclear@5
|
85 for(i=0; i<HEIGHT; i++) {
|
nuclear@5
|
86 for(j=0; j<WIDTH; j++) {
|
nuclear@6
|
87 uint16_t pixel = *src++;
|
nuclear@13
|
88 unsigned int red = GET_R(pixel);
|
nuclear@13
|
89 unsigned int green = GET_G(pixel);
|
nuclear@13
|
90 unsigned int blue = GET_B(pixel);
|
nuclear@13
|
91
|
nuclear@13
|
92 pixel = ((blue >> 3) << 11) | ((green >> 2) << 5) | (red >> 3);
|
nuclear@6
|
93
|
nuclear@6
|
94 for(k=0; k<sdlscale; k++) {
|
nuclear@6
|
95 for(l=0; l<sdlscale; l++) {
|
nuclear@6
|
96 dest[k * sdlscale * WIDTH + l] = pixel;
|
nuclear@6
|
97 }
|
nuclear@6
|
98 }
|
nuclear@6
|
99 dest += sdlscale;
|
nuclear@5
|
100 }
|
nuclear@6
|
101 dest += WIDTH * sdlscale * (sdlscale - 1);
|
nuclear@5
|
102 }
|
nuclear@5
|
103
|
nuclear@5
|
104 if(SDL_MUSTLOCK(surf)) {
|
nuclear@5
|
105 SDL_UnlockSurface(surf);
|
nuclear@5
|
106 }
|
nuclear@5
|
107 keystate = 0;
|
nuclear@5
|
108 }
|
nuclear@5
|
109
|
nuclear@5
|
110 done:
|
nuclear@5
|
111 SDL_Quit();
|
nuclear@5
|
112 return 0;
|
nuclear@5
|
113 }
|
nuclear@5
|
114
|
nuclear@5
|
115 void flip(void)
|
nuclear@5
|
116 {
|
nuclear@5
|
117 SDL_Flip(surf);
|
nuclear@5
|
118 }
|
nuclear@5
|
119
|
nuclear@5
|
120 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
|
nuclear@5
|
121 {
|
nuclear@9
|
122 int i, sz = pbuf->x * pbuf->y;
|
nuclear@5
|
123 unsigned short *pixels = pbuf->pixels;
|
nuclear@5
|
124
|
nuclear@9
|
125 #ifdef PALMODE
|
nuclear@9
|
126 color |= color << 8;
|
nuclear@9
|
127 sz /= 2;
|
nuclear@9
|
128 #endif
|
nuclear@9
|
129
|
nuclear@9
|
130 for(i=0; i<sz; i++) {
|
nuclear@5
|
131 *pixels++ = color;
|
nuclear@5
|
132 }
|
nuclear@5
|
133 }
|
nuclear@5
|
134
|
nuclear@9
|
135 void set_palette(int idx, int r, int g, int b)
|
nuclear@9
|
136 {
|
nuclear@9
|
137 SDL_Color col;
|
nuclear@9
|
138 col.r = r;
|
nuclear@9
|
139 col.g = g;
|
nuclear@9
|
140 col.b = b;
|
nuclear@9
|
141
|
nuclear@9
|
142 if(SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
|
nuclear@9
|
143 fprintf(stderr, "set_palette failed to set the required color\n");
|
nuclear@9
|
144 }
|
nuclear@9
|
145 }
|
nuclear@9
|
146
|
nuclear@9
|
147
|
nuclear@5
|
148 int get_key_state(int key)
|
nuclear@5
|
149 {
|
nuclear@5
|
150 return keystate & key;
|
nuclear@5
|
151 }
|
nuclear@5
|
152
|
nuclear@5
|
153 unsigned long get_millisec(void)
|
nuclear@5
|
154 {
|
nuclear@5
|
155 return SDL_GetTicks();
|
nuclear@5
|
156 }
|
nuclear@5
|
157
|
nuclear@5
|
158 static int proc_events(SDL_Event *ev)
|
nuclear@5
|
159 {
|
nuclear@5
|
160 switch(ev->type) {
|
nuclear@5
|
161 case SDL_KEYDOWN:
|
nuclear@5
|
162 case SDL_KEYUP:
|
nuclear@9
|
163 handle_keyboard(ev->key.keysym.sym, ev->key.state);
|
nuclear@5
|
164 break;
|
nuclear@5
|
165
|
nuclear@5
|
166 case SDL_QUIT:
|
nuclear@5
|
167 return -1;
|
nuclear@5
|
168 }
|
nuclear@5
|
169
|
nuclear@5
|
170 return 0;
|
nuclear@5
|
171 }
|
nuclear@5
|
172
|
nuclear@5
|
173 static void handle_keyboard(int key, int state)
|
nuclear@5
|
174 {
|
nuclear@5
|
175 int gba_key = -1;
|
nuclear@5
|
176
|
nuclear@5
|
177 switch(key) {
|
nuclear@5
|
178 case SDLK_ESCAPE:
|
nuclear@5
|
179 {
|
nuclear@5
|
180 SDL_Event ev;
|
nuclear@5
|
181 ev.type = SDL_QUIT;
|
nuclear@5
|
182 SDL_PushEvent(&ev);
|
nuclear@5
|
183 }
|
nuclear@5
|
184 break;
|
nuclear@5
|
185
|
nuclear@5
|
186 case 'a':
|
nuclear@5
|
187 case 'A':
|
nuclear@5
|
188 case SDLK_LEFT:
|
nuclear@5
|
189 gba_key = KEY_LEFT;
|
nuclear@5
|
190 break;
|
nuclear@5
|
191
|
nuclear@5
|
192 case 'd':
|
nuclear@5
|
193 case 'D':
|
nuclear@5
|
194 case SDLK_RIGHT:
|
nuclear@5
|
195 gba_key = KEY_RIGHT;
|
nuclear@5
|
196 break;
|
nuclear@5
|
197
|
nuclear@5
|
198 case 'w':
|
nuclear@5
|
199 case 'W':
|
nuclear@5
|
200 case SDLK_UP:
|
nuclear@5
|
201 gba_key = KEY_UP;
|
nuclear@5
|
202 break;
|
nuclear@5
|
203
|
nuclear@5
|
204 case 's':
|
nuclear@5
|
205 case 'S':
|
nuclear@5
|
206 case SDLK_DOWN:
|
nuclear@5
|
207 gba_key = KEY_DOWN;
|
nuclear@5
|
208 break;
|
nuclear@5
|
209
|
nuclear@5
|
210 case '\n':
|
nuclear@9
|
211 case '\r':
|
nuclear@5
|
212 gba_key = KEY_A;
|
nuclear@5
|
213 break;
|
nuclear@5
|
214 case '\b':
|
nuclear@5
|
215 gba_key = KEY_B;
|
nuclear@5
|
216 break;
|
nuclear@5
|
217
|
nuclear@5
|
218 case ',':
|
nuclear@5
|
219 gba_key = KEY_SELECT;
|
nuclear@5
|
220 break;
|
nuclear@5
|
221 case '.':
|
nuclear@5
|
222 gba_key = KEY_START;
|
nuclear@5
|
223 break;
|
nuclear@5
|
224
|
nuclear@5
|
225 case '[':
|
nuclear@5
|
226 gba_key = KEY_L;
|
nuclear@5
|
227 break;
|
nuclear@5
|
228 case ']':
|
nuclear@5
|
229 gba_key = KEY_R;
|
nuclear@5
|
230 break;
|
nuclear@5
|
231
|
nuclear@5
|
232 default:
|
nuclear@5
|
233 return;
|
nuclear@5
|
234 }
|
nuclear@5
|
235
|
nuclear@5
|
236 keystate |= gba_key;
|
nuclear@5
|
237 game_keyb(gba_key, state);
|
nuclear@5
|
238 }
|