foo
[eradicate] / src / sdl / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <limits.h>
5 #include <SDL/SDL.h>
6 #include "game.h"
7 #include "timer.h"
8
9 #define FB_WIDTH        640
10 #define FB_HEIGHT       480
11
12 static void handle_event(SDL_Event *ev);
13 static void toggle_fullscreen(void);
14
15 static int sdlkey_to_gamekey(int sdlkey, unsigned int mod);
16
17
18 static int quit;
19 static SDL_Surface *fbsurf;
20
21 static int fbscale = 2;
22 static int xsz, ysz;
23 static unsigned int sdl_flags = SDL_SWSURFACE;
24
25
26 int main(int argc, char **argv)
27 {
28         int s;
29         char *env;
30
31         if((env = getenv("FBSCALE")) && (s = atoi(env))) {
32                 fbscale = s;
33                 printf("Framebuffer scaling x%d\n", fbscale);
34         }
35
36         xsz = FB_WIDTH * fbscale;
37         ysz = FB_HEIGHT * fbscale;
38
39         /* allocate 1 extra row as a guard band, until we fucking fix the rasterizer */
40         if(!(fb_pixels = malloc(FB_WIDTH * (FB_HEIGHT + 1) * FB_BPP / CHAR_BIT))) {
41                 fprintf(stderr, "failed to allocate virtual framebuffer\n");
42                 return 1;
43         }
44
45         SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
46         if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, sdl_flags))) {
47                 fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", FB_WIDTH, FB_HEIGHT, FB_BPP);
48                 free(fb_pixels);
49                 SDL_Quit();
50                 return 1;
51         }
52         SDL_WM_SetCaption("eradicate/SDL", 0);
53         SDL_ShowCursor(0);
54
55         time_msec = 0;
56         if(init(argc, argv) == -1) {
57                 free(fb_pixels);
58                 SDL_Quit();
59                 return 1;
60         }
61
62         reset_timer();
63
64         while(!quit) {
65                 SDL_Event ev;
66                 while(SDL_PollEvent(&ev)) {
67                         handle_event(&ev);
68                         if(quit) goto break_evloop;
69                 }
70
71                 time_msec = get_msec();
72                 draw();
73         }
74
75 break_evloop:
76         cleanup();
77         SDL_Quit();
78         return 0;
79 }
80
81 void game_quit(void)
82 {
83         quit = 1;
84 }
85
86 void wait_vsync(void)
87 {
88         unsigned long start = SDL_GetTicks();
89         unsigned long until = (start | 0xf) + 1;
90         while(SDL_GetTicks() <= until);
91 }
92
93 void blit_frame(void *pixels, int vsync)
94 {
95         int i, j;
96         unsigned short *sptr, *dptr;
97
98         if(vsync) {
99                 wait_vsync();
100         }
101
102         if(SDL_MUSTLOCK(fbsurf)) {
103                 SDL_LockSurface(fbsurf);
104         }
105
106         sptr = fb_pixels;
107         dptr = (unsigned short*)fbsurf->pixels + (fbsurf->w - xsz) / 2;
108         for(i=0; i<FB_HEIGHT; i++) {
109                 for(j=0; j<FB_WIDTH; j++) {
110                         int x, y;
111                         unsigned short pixel = *sptr++;
112
113                         for(y=0; y<fbscale; y++) {
114                                 for(x=0; x<fbscale; x++) {
115                                         dptr[y * fbsurf->w + x] = pixel;
116                                 }
117                         }
118                         dptr += fbscale;
119                 }
120                 dptr += (fbsurf->w - FB_WIDTH) * fbscale;
121         }
122
123         if(SDL_MUSTLOCK(fbsurf)) {
124                 SDL_UnlockSurface(fbsurf);
125         }
126         SDL_Flip(fbsurf);
127 }
128
129 /*
130 static int bnmask(int sdlbn)
131 {
132         switch(sdlbn) {
133         case SDL_BUTTON_LEFT:
134                 return MOUSE_BN_LEFT;
135         case SDL_BUTTON_RIGHT:
136                 return MOUSE_BN_RIGHT;
137         case SDL_BUTTON_MIDDLE:
138                 return MOUSE_BN_MIDDLE;
139         default:
140                 break;
141         }
142         return 0;
143 }
144 */
145
146 static void handle_event(SDL_Event *ev)
147 {
148         int key;
149
150         switch(ev->type) {
151         case SDL_QUIT:
152                 quit = 1;
153                 break;
154
155         case SDL_KEYDOWN:
156         case SDL_KEYUP:
157                 if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
158                                 ev->key.state == SDL_PRESSED) {
159                         toggle_fullscreen();
160                         break;
161                 }
162                 key = sdlkey_to_gamekey(ev->key.keysym.sym, ev->key.keysym.mod);
163                 key_event(key, ev->key.state == SDL_PRESSED ? 1 : 0);
164                 break;
165
166                 /*
167         case SDL_MOUSEMOTION:
168                 mouse_x = ev->motion.x / fbscale;
169                 mouse_y = ev->motion.y / fbscale;
170                 break;
171
172         case SDL_MOUSEBUTTONDOWN:
173                 mouse_bmask |= bnmask(ev->button.button);
174                 if(0) {
175         case SDL_MOUSEBUTTONUP:
176                         mouse_bmask &= ~bnmask(ev->button.button);
177                 }
178                 mouse_x = ev->button.x / fbscale;
179                 mouse_y = ev->button.y / fbscale;
180                 break;
181                 */
182
183         default:
184                 break;
185         }
186 }
187
188 static void toggle_fullscreen(void)
189 {
190         SDL_Surface *newsurf;
191         unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
192
193         if(!(newsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, newflags))) {
194                 fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
195                 return;
196         }
197
198         fbsurf = newsurf;
199         sdl_flags = newflags;
200 }
201
202 #define SSORG   '\''
203 #define SSEND   '`'
204 static char symshift[] = {
205         '"', 0, 0, 0, 0, '<', '_', '>', '?',
206         ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',
207         0, ':', 0, '+', 0, 0, 0,
208         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209         '{', '|', '}', 0, 0, '~'
210 };
211
212
213 static int sdlkey_to_gamekey(int sdlkey, unsigned int mod)
214 {
215         if(sdlkey < 128) {
216                 if(mod & (KMOD_SHIFT)) {
217                         if(sdlkey >= 'a' && sdlkey <= 'z') {
218                                 sdlkey = toupper(sdlkey);
219                         } else if(sdlkey >= SSORG && sdlkey <= SSEND) {
220                                 sdlkey = symshift[sdlkey - SSORG];
221                         }
222                 }
223                 return sdlkey;
224         }
225         if(sdlkey < 256) return 0;
226         return sdlkey - 128;
227 }