13 static void handle_event(SDL_Event *ev);
14 static void toggle_fullscreen(void);
16 static int handle_sball_event(sball_event *ev);
17 static void recalc_sball_matrix(float *xform);
19 static int sdlkey_to_demokey(int sdlkey, unsigned int mod);
23 static SDL_Surface *fbsurf;
25 static int fbscale = 2;
27 static unsigned int sdl_flags = SDL_SWSURFACE;
30 static vec3_t pos = {0, 0, 0};
31 static quat_t rot = {0, 0, 0, 1};
34 int main(int argc, char **argv)
39 if((env = getenv("FBSCALE")) && (s = atoi(env))) {
41 printf("Framebuffer scaling x%d\n", fbscale);
44 xsz = FB_WIDTH * fbscale;
45 ysz = FB_HEIGHT * fbscale;
47 /* now start_loadscr sets up fb_pixels to the space used by the loading image,
48 * so no need to allocate another framebuffer
51 /* allocate 1 extra row as a guard band, until we fucking fix the rasterizer */
52 if(!(fb_pixels = malloc(FB_WIDTH * (FB_HEIGHT + 1) * FB_BPP / CHAR_BIT))) {
53 fprintf(stderr, "failed to allocate virtual framebuffer\n");
58 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
59 if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, sdl_flags))) {
60 fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", FB_WIDTH, FB_HEIGHT, FB_BPP);
65 SDL_WM_SetCaption("dosdemo/SDL", 0);
69 if(demo_init(argc, argv) == -1) {
76 if(opt.sball && sball_init() == 0) {
84 while(SDL_PollEvent(&ev)) {
86 if(quit) goto break_evloop;
90 while(sball_pending()) {
93 handle_sball_event(&ev);
95 recalc_sball_matrix(sball_matrix);
98 time_msec = get_msec();
113 void wait_vsync(void)
115 unsigned long start = SDL_GetTicks();
116 unsigned long until = (start | 0xf) + 1;
117 while(SDL_GetTicks() <= until);
120 void swap_buffers(void *pixels)
123 unsigned short *sptr, *dptr;
125 demo_post_draw(pixels ? pixels : fb_pixels);
131 if(SDL_MUSTLOCK(fbsurf)) {
132 SDL_LockSurface(fbsurf);
136 dptr = (unsigned short*)fbsurf->pixels + (fbsurf->w - xsz) / 2;
137 for(i=0; i<FB_HEIGHT; i++) {
138 for(j=0; j<FB_WIDTH; j++) {
140 unsigned short pixel = *sptr++;
142 for(y=0; y<fbscale; y++) {
143 for(x=0; x<fbscale; x++) {
144 dptr[y * fbsurf->w + x] = pixel;
149 dptr += (fbsurf->w - FB_WIDTH) * fbscale;
152 if(SDL_MUSTLOCK(fbsurf)) {
153 SDL_UnlockSurface(fbsurf);
158 static int bnmask(int sdlbn)
161 case SDL_BUTTON_LEFT:
162 return MOUSE_BN_LEFT;
163 case SDL_BUTTON_RIGHT:
164 return MOUSE_BN_RIGHT;
165 case SDL_BUTTON_MIDDLE:
166 return MOUSE_BN_MIDDLE;
173 static void handle_event(SDL_Event *ev)
184 if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
185 ev->key.state == SDL_PRESSED) {
189 key = sdlkey_to_demokey(ev->key.keysym.sym, ev->key.keysym.mod);
190 demo_keyboard(key, ev->key.state == SDL_PRESSED ? 1 : 0);
193 case SDL_MOUSEMOTION:
194 mouse_x = ev->motion.x / fbscale;
195 mouse_y = ev->motion.y / fbscale;
198 case SDL_MOUSEBUTTONDOWN:
199 mouse_bmask |= bnmask(ev->button.button);
201 case SDL_MOUSEBUTTONUP:
202 mouse_bmask &= ~bnmask(ev->button.button);
204 mouse_x = ev->button.x / fbscale;
205 mouse_y = ev->button.y / fbscale;
213 static void toggle_fullscreen(void)
215 SDL_Surface *newsurf;
216 unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
218 if(!(newsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, newflags))) {
219 fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
224 sdl_flags = newflags;
229 #define TX(ev) ((ev)->motion.motion[0])
230 #define TY(ev) ((ev)->motion.motion[1])
231 #define TZ(ev) ((ev)->motion.motion[2])
232 #define RX(ev) ((ev)->motion.motion[3])
233 #define RY(ev) ((ev)->motion.motion[4])
234 #define RZ(ev) ((ev)->motion.motion[5])
236 static int handle_sball_event(sball_event *ev)
239 case SBALL_EV_MOTION:
240 if(RX(ev) | RY(ev) | RZ(ev)) {
241 float rx = (float)RX(ev);
242 float ry = (float)RY(ev);
243 float rz = (float)RZ(ev);
244 float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
246 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
247 -ry / axis_len, -rz / axis_len);
251 pos.x += TX(ev) * 0.001;
252 pos.y += TY(ev) * 0.001;
253 pos.z += TZ(ev) * 0.001;
256 case SBALL_EV_BUTTON:
257 if(ev->button.pressed) {
258 pos = v3_cons(0, 0, 0);
259 rot = quat_cons(1, 0, 0, 0);
267 static void recalc_sball_matrix(float *xform)
269 quat_to_mat(xform, rot);
277 static char symshift[] = {
278 '"', 0, 0, 0, 0, '<', '_', '>', '?',
279 ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',
280 0, ':', 0, '+', 0, 0, 0,
281 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,
282 '{', '|', '}', 0, 0, '~'
286 static int sdlkey_to_demokey(int sdlkey, unsigned int mod)
289 if(mod & (KMOD_SHIFT)) {
290 if(sdlkey >= 'a' && sdlkey <= 'z') {
291 sdlkey = toupper(sdlkey);
292 } else if(sdlkey >= SSORG && sdlkey <= SSEND) {
293 sdlkey = symshift[sdlkey - SSORG];
298 if(sdlkey < 256) return 0;