shift translation for DOS too
[dosdemo] / 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 "demo.h"
7 #include "tinyfps.h"
8 #include "timer.h"
9 #include "cfgopt.h"
10 #include "sball.h"
11 #include "vmath.h"
12
13 static void handle_event(SDL_Event *ev);
14 static void toggle_fullscreen(void);
15
16 static int handle_sball_event(sball_event *ev);
17 static void recalc_sball_matrix(float *xform);
18
19 static int sdlkey_to_demokey(int sdlkey, unsigned int mod);
20
21
22 static int quit;
23 static SDL_Surface *fbsurf;
24
25 static int fbscale = 2;
26 static int xsz, ysz;
27 static unsigned int sdl_flags = SDL_SWSURFACE;
28
29 static int use_sball;
30 static vec3_t pos = {0, 0, 0};
31 static quat_t rot = {0, 0, 0, 1};
32
33
34 int main(int argc, char **argv)
35 {
36         int s, i, j;
37         char *env;
38         unsigned short *sptr, *dptr;
39
40         if((env = getenv("FBSCALE")) && (s = atoi(env))) {
41                 fbscale = s;
42                 printf("Framebuffer scaling x%d\n", fbscale);
43         }
44
45         xsz = fb_width * fbscale;
46         ysz = fb_height * fbscale;
47
48         /* allocate 1 extra row as a guard band, until we fucking fix the rasterizer */
49         if(!(fb_pixels = malloc(fb_width * (fb_height + 1) * fb_bpp / CHAR_BIT))) {
50                 fprintf(stderr, "failed to allocate virtual framebuffer\n");
51                 return 1;
52         }
53         vmem = fb_pixels;
54
55         SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
56         if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, sdl_flags))) {
57                 fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
58                 free(fb_pixels);
59                 SDL_Quit();
60                 return 1;
61         }
62         SDL_WM_SetCaption("dosdemo/SDL", 0);
63         SDL_ShowCursor(0);
64
65         time_msec = 0;
66         if(demo_init(argc, argv) == -1) {
67                 free(fb_pixels);
68                 SDL_Quit();
69                 return 1;
70         }
71
72         if(opt.sball && sball_init() == 0) {
73                 use_sball = 1;
74         }
75
76         reset_timer();
77
78         while(!quit) {
79                 SDL_Event ev;
80                 while(SDL_PollEvent(&ev)) {
81                         handle_event(&ev);
82                         if(quit) goto break_evloop;
83                 }
84
85                 if(use_sball) {
86                         while(sball_pending()) {
87                                 sball_event ev;
88                                 sball_getevent(&ev);
89                                 handle_sball_event(&ev);
90                         }
91                         recalc_sball_matrix(sball_matrix);
92                 }
93
94                 time_msec = get_msec();
95                 demo_draw();
96
97                 if(SDL_MUSTLOCK(fbsurf)) {
98                         SDL_LockSurface(fbsurf);
99                 }
100
101                 sptr = fb_pixels;
102                 dptr = (unsigned short*)fbsurf->pixels + (fbsurf->w - xsz) / 2;
103                 for(i=0; i<fb_height; i++) {
104                         for(j=0; j<fb_width; j++) {
105                                 int x, y;
106                                 unsigned short pixel = *sptr++;
107
108                                 for(y=0; y<fbscale; y++) {
109                                         for(x=0; x<fbscale; x++) {
110                                                 dptr[y * fbsurf->w + x] = pixel;
111                                         }
112                                 }
113                                 dptr += fbscale;
114                         }
115                         dptr += (fbsurf->w - fb_width) * fbscale;
116                 }
117
118                 if(SDL_MUSTLOCK(fbsurf)) {
119                         SDL_UnlockSurface(fbsurf);
120                 }
121                 SDL_Flip(fbsurf);
122         }
123
124 break_evloop:
125         demo_cleanup();
126         SDL_Quit();
127         return 0;
128 }
129
130 void demo_quit(void)
131 {
132         quit = 1;
133 }
134
135 void wait_vsync(void)
136 {
137         unsigned long start = SDL_GetTicks();
138         unsigned long until = (start | 0xf) + 1;
139         while(SDL_GetTicks() <= until);
140 }
141
142 void swap_buffers(void *pixels)
143 {
144         demo_post_draw(pixels ? pixels : fb_pixels);
145
146         /* do nothing, all pointers point to the same buffer */
147         if(opt.vsync) {
148                 wait_vsync();
149         }
150 }
151
152 static int bnmask(int sdlbn)
153 {
154         switch(sdlbn) {
155         case SDL_BUTTON_LEFT:
156                 return MOUSE_BN_LEFT;
157         case SDL_BUTTON_RIGHT:
158                 return MOUSE_BN_RIGHT;
159         case SDL_BUTTON_MIDDLE:
160                 return MOUSE_BN_MIDDLE;
161         default:
162                 break;
163         }
164         return 0;
165 }
166
167 static void handle_event(SDL_Event *ev)
168 {
169         int key;
170
171         switch(ev->type) {
172         case SDL_QUIT:
173                 quit = 1;
174                 break;
175
176         case SDL_KEYDOWN:
177         case SDL_KEYUP:
178                 if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
179                                 ev->key.state == SDL_PRESSED) {
180                         toggle_fullscreen();
181                         break;
182                 }
183                 key = sdlkey_to_demokey(ev->key.keysym.sym, ev->key.keysym.mod);
184                 demo_keyboard(key, ev->key.state == SDL_PRESSED ? 1 : 0);
185                 break;
186
187         case SDL_MOUSEMOTION:
188                 mouse_x = ev->motion.x / fbscale;
189                 mouse_y = ev->motion.y / fbscale;
190                 break;
191
192         case SDL_MOUSEBUTTONDOWN:
193                 mouse_bmask |= bnmask(ev->button.button);
194                 if(0) {
195         case SDL_MOUSEBUTTONUP:
196                         mouse_bmask &= ~bnmask(ev->button.button);
197                 }
198                 mouse_x = ev->button.x / fbscale;
199                 mouse_y = ev->button.y / fbscale;
200                 break;
201
202         default:
203                 break;
204         }
205 }
206
207 static void toggle_fullscreen(void)
208 {
209         SDL_Surface *newsurf;
210         unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
211
212         if(!(newsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, newflags))) {
213                 fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
214                 return;
215         }
216
217         fbsurf = newsurf;
218         sdl_flags = newflags;
219 }
220
221
222
223 #define TX(ev)  ((ev)->motion.motion[0])
224 #define TY(ev)  ((ev)->motion.motion[1])
225 #define TZ(ev)  ((ev)->motion.motion[2])
226 #define RX(ev)  ((ev)->motion.motion[3])
227 #define RY(ev)  ((ev)->motion.motion[4])
228 #define RZ(ev)  ((ev)->motion.motion[5])
229
230 static int handle_sball_event(sball_event *ev)
231 {
232         switch(ev->type) {
233         case SBALL_EV_MOTION:
234                 if(RX(ev) | RY(ev) | RZ(ev)) {
235                         float rx = (float)RX(ev);
236                         float ry = (float)RY(ev);
237                         float rz = (float)RZ(ev);
238                         float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
239                         if(axis_len > 0.0) {
240                                 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
241                                                 -ry / axis_len, -rz / axis_len);
242                         }
243                 }
244
245                 pos.x += TX(ev) * 0.001;
246                 pos.y += TY(ev) * 0.001;
247                 pos.z += TZ(ev) * 0.001;
248                 break;
249
250         case SBALL_EV_BUTTON:
251                 if(ev->button.pressed) {
252                         pos = v3_cons(0, 0, 0);
253                         rot = quat_cons(1, 0, 0, 0);
254                 }
255                 break;
256         }
257
258         return 0;
259 }
260
261 static void recalc_sball_matrix(float *xform)
262 {
263         quat_to_mat(xform, rot);
264         xform[12] = pos.x;
265         xform[13] = pos.y;
266         xform[14] = pos.z;
267 }
268
269 #define SSORG   '\''
270 #define SSEND   '`'
271 static char symshift[] = {
272         '"', 0, 0, 0, 0, '<', '_', '>', '?',
273         ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',
274         0, ':', 0, '+', 0, 0, 0,
275         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,
276         '{', '|', '}', 0, 0, '~'
277 };
278
279
280 static int sdlkey_to_demokey(int sdlkey, unsigned int mod)
281 {
282         if(sdlkey < 128) {
283                 if(mod & (KMOD_SHIFT)) {
284                         if(sdlkey >= 'a' && sdlkey <= 'z') {
285                                 sdlkey = toupper(sdlkey);
286                         } else if(sdlkey >= SSORG && sdlkey <= SSEND) {
287                                 sdlkey = symshift[sdlkey - SSORG];
288                         }
289                 }
290                 return sdlkey;
291         }
292         if(sdlkey < 256) return 0;
293         return sdlkey - 128;
294 }