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