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)
38 unsigned short *sptr, *dptr;
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 /* 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");
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);
62 SDL_WM_SetCaption("dosdemo/SDL", 0);
66 if(demo_init(argc, argv) == -1) {
72 if(opt.sball && sball_init() == 0) {
80 while(SDL_PollEvent(&ev)) {
82 if(quit) goto break_evloop;
86 while(sball_pending()) {
89 handle_sball_event(&ev);
91 recalc_sball_matrix(sball_matrix);
94 time_msec = get_msec();
97 if(SDL_MUSTLOCK(fbsurf)) {
98 SDL_LockSurface(fbsurf);
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++) {
106 unsigned short pixel = *sptr++;
108 for(y=0; y<fbscale; y++) {
109 for(x=0; x<fbscale; x++) {
110 dptr[y * fbsurf->w + x] = pixel;
115 dptr += (fbsurf->w - fb_width) * fbscale;
118 if(SDL_MUSTLOCK(fbsurf)) {
119 SDL_UnlockSurface(fbsurf);
135 void wait_vsync(void)
137 unsigned long start = SDL_GetTicks();
138 unsigned long until = (start | 0xf) + 1;
139 while(SDL_GetTicks() <= until);
142 void swap_buffers(void *pixels)
144 demo_post_draw(pixels ? pixels : fb_pixels);
146 /* do nothing, all pointers point to the same buffer */
152 static int bnmask(int 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;
167 static void handle_event(SDL_Event *ev)
178 if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
179 ev->key.state == SDL_PRESSED) {
183 key = sdlkey_to_demokey(ev->key.keysym.sym, ev->key.keysym.mod);
184 demo_keyboard(key, ev->key.state == SDL_PRESSED ? 1 : 0);
187 case SDL_MOUSEMOTION:
188 mouse_x = ev->motion.x / fbscale;
189 mouse_y = ev->motion.y / fbscale;
192 case SDL_MOUSEBUTTONDOWN:
193 mouse_bmask |= bnmask(ev->button.button);
195 case SDL_MOUSEBUTTONUP:
196 mouse_bmask &= ~bnmask(ev->button.button);
198 mouse_x = ev->button.x / fbscale;
199 mouse_y = ev->button.y / fbscale;
207 static void toggle_fullscreen(void)
209 SDL_Surface *newsurf;
210 unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
212 if(!(newsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, newflags))) {
213 fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
218 sdl_flags = newflags;
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])
230 static int handle_sball_event(sball_event *ev)
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);
240 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
241 -ry / axis_len, -rz / axis_len);
245 pos.x += TX(ev) * 0.001;
246 pos.y += TY(ev) * 0.001;
247 pos.z += TZ(ev) * 0.001;
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);
261 static void recalc_sball_matrix(float *xform)
263 quat_to_mat(xform, rot);
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, '~'
280 static int sdlkey_to_demokey(int sdlkey, unsigned int mod)
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];
292 if(sdlkey < 256) return 0;