14 static void handle_event(SDL_Event *ev);
15 static void toggle_fullscreen(void);
17 static int handle_sball_event(sball_event *ev);
18 static void recalc_sball_matrix(float *xform);
20 static int sdlkey_to_demokey(int sdlkey, unsigned int mod);
24 static SDL_Surface *fbsurf;
26 static int fbscale = 2;
28 static unsigned int sdl_flags = SDL_SWSURFACE;
31 static vec3_t pos = {0, 0, 0};
32 static quat_t rot = {0, 0, 0, 1};
35 int main(int argc, char **argv)
40 if((env = getenv("FBSCALE")) && (s = atoi(env))) {
42 printf("Framebuffer scaling x%d\n", fbscale);
45 xsz = FB_WIDTH * fbscale;
46 ysz = FB_HEIGHT * fbscale;
48 /* now start_loadscr sets up fb_pixels to the space used by the loading image,
49 * so no need to allocate another framebuffer
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");
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);
66 SDL_WM_SetCaption("dosdemo/SDL", 0);
69 if(read_cpuid(&cpuid) == 0) {
74 if(demo_init(argc, argv) == -1) {
81 if(opt.sball && sball_init() == 0) {
89 while(SDL_PollEvent(&ev)) {
91 if(quit) goto break_evloop;
95 while(sball_pending()) {
98 handle_sball_event(&ev);
100 recalc_sball_matrix(sball_matrix);
103 time_msec = get_msec();
118 void wait_vsync(void)
120 unsigned long start = SDL_GetTicks();
121 unsigned long until = (start | 0xf) + 1;
122 while(SDL_GetTicks() <= until);
125 void swap_buffers(void *pixels)
128 unsigned short *sptr, *dptr;
130 demo_post_draw(pixels ? pixels : fb_pixels);
136 if(SDL_MUSTLOCK(fbsurf)) {
137 SDL_LockSurface(fbsurf);
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++) {
145 unsigned short pixel = *sptr++;
147 for(y=0; y<fbscale; y++) {
148 for(x=0; x<fbscale; x++) {
149 dptr[y * fbsurf->w + x] = pixel;
154 dptr += (fbsurf->w - FB_WIDTH) * fbscale;
157 if(SDL_MUSTLOCK(fbsurf)) {
158 SDL_UnlockSurface(fbsurf);
163 static int bnmask(int 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;
178 static void handle_event(SDL_Event *ev)
189 if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
190 ev->key.state == SDL_PRESSED) {
194 key = sdlkey_to_demokey(ev->key.keysym.sym, ev->key.keysym.mod);
195 demo_keyboard(key, ev->key.state == SDL_PRESSED ? 1 : 0);
198 case SDL_MOUSEMOTION:
199 mouse_x = ev->motion.x / fbscale;
200 mouse_y = ev->motion.y / fbscale;
203 case SDL_MOUSEBUTTONDOWN:
204 mouse_bmask |= bnmask(ev->button.button);
206 case SDL_MOUSEBUTTONUP:
207 mouse_bmask &= ~bnmask(ev->button.button);
209 mouse_x = ev->button.x / fbscale;
210 mouse_y = ev->button.y / fbscale;
218 static void toggle_fullscreen(void)
220 SDL_Surface *newsurf;
221 unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
223 if(!(newsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, newflags))) {
224 fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
229 sdl_flags = newflags;
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])
241 static int handle_sball_event(sball_event *ev)
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);
251 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
252 -ry / axis_len, -rz / axis_len);
256 pos.x += TX(ev) * 0.001;
257 pos.y += TY(ev) * 0.001;
258 pos.z += TZ(ev) * 0.001;
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);
272 static void recalc_sball_matrix(float *xform)
274 quat_to_mat(xform, rot);
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, '~'
291 static int sdlkey_to_demokey(int sdlkey, unsigned int mod)
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];
303 if(sdlkey < 256) return 0;