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