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);
21 static SDL_Surface *fbsurf;
23 static int fbscale = 2;
25 static unsigned int sdl_flags = SDL_SWSURFACE;
28 static vec3_t pos = {0, 0, 0};
29 static quat_t rot = {0, 0, 0, 1};
32 int main(int argc, char **argv)
36 unsigned short *sptr, *dptr;
38 if((env = getenv("FBSCALE")) && (s = atoi(env))) {
40 printf("Framebuffer scaling x%d\n", fbscale);
43 xsz = fb_width * fbscale;
44 ysz = fb_height * fbscale;
46 /* allocate 1 extra row as a guard band, until we fucking fix the rasterizer */
47 if(!(fb_pixels = malloc(fb_width * (fb_height + 1) * fb_bpp / CHAR_BIT))) {
48 fprintf(stderr, "failed to allocate virtual framebuffer\n");
53 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
54 if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, sdl_flags))) {
55 fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
60 SDL_WM_SetCaption("dosdemo/SDL", 0);
64 if(demo_init(argc, argv) == -1) {
70 if(opt.sball && sball_init() == 0) {
78 while(SDL_PollEvent(&ev)) {
80 if(quit) goto break_evloop;
84 while(sball_pending()) {
87 handle_sball_event(&ev);
89 recalc_sball_matrix(sball_matrix);
92 time_msec = get_msec();
96 if(SDL_MUSTLOCK(fbsurf)) {
97 SDL_LockSurface(fbsurf);
101 dptr = (unsigned short*)fbsurf->pixels + (fbsurf->w - xsz) / 2;
102 for(i=0; i<fb_height; i++) {
103 for(j=0; j<fb_width; j++) {
105 unsigned short pixel = *sptr++;
107 for(y=0; y<fbscale; y++) {
108 for(x=0; x<fbscale; x++) {
109 dptr[y * fbsurf->w + x] = pixel;
114 dptr += (fbsurf->w - fb_width) * fbscale;
117 if(SDL_MUSTLOCK(fbsurf)) {
118 SDL_UnlockSurface(fbsurf);
134 void wait_vsync(void)
136 unsigned long start = SDL_GetTicks();
137 unsigned long until = (start | 0xf) + 1;
138 while(SDL_GetTicks() <= until);
141 void swap_buffers(void *pixels)
143 /* do nothing, all pointers point to the same buffer */
149 static int bnmask(int sdlbn)
152 case SDL_BUTTON_LEFT:
153 return MOUSE_BN_LEFT;
154 case SDL_BUTTON_RIGHT:
155 return MOUSE_BN_RIGHT;
156 case SDL_BUTTON_MIDDLE:
157 return MOUSE_BN_MIDDLE;
164 static void handle_event(SDL_Event *ev)
173 if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
174 ev->key.state == SDL_PRESSED) {
178 demo_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0);
181 case SDL_MOUSEMOTION:
182 mouse_x = ev->motion.x / fbscale;
183 mouse_y = ev->motion.y / fbscale;
186 case SDL_MOUSEBUTTONDOWN:
187 mouse_bmask |= bnmask(ev->button.button);
189 case SDL_MOUSEBUTTONUP:
190 mouse_bmask &= ~bnmask(ev->button.button);
192 mouse_x = ev->button.x / fbscale;
193 mouse_y = ev->button.y / fbscale;
201 static void toggle_fullscreen(void)
203 SDL_Surface *newsurf;
204 unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
206 if(!(newsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, newflags))) {
207 fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
212 sdl_flags = newflags;
217 #define TX(ev) ((ev)->motion.motion[0])
218 #define TY(ev) ((ev)->motion.motion[1])
219 #define TZ(ev) ((ev)->motion.motion[2])
220 #define RX(ev) ((ev)->motion.motion[3])
221 #define RY(ev) ((ev)->motion.motion[4])
222 #define RZ(ev) ((ev)->motion.motion[5])
224 static int handle_sball_event(sball_event *ev)
227 case SBALL_EV_MOTION:
228 if(RX(ev) | RY(ev) | RZ(ev)) {
229 float rx = (float)RX(ev);
230 float ry = (float)RY(ev);
231 float rz = (float)RZ(ev);
232 float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
234 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
235 -ry / axis_len, -rz / axis_len);
239 pos.x += TX(ev) * 0.001;
240 pos.y += TY(ev) * 0.001;
241 pos.z += TZ(ev) * 0.001;
244 case SBALL_EV_BUTTON:
245 if(ev->button.pressed) {
246 pos = v3_cons(0, 0, 0);
247 rot = quat_cons(1, 0, 0, 0);
255 void recalc_sball_matrix(float *xform)
257 quat_to_mat(xform, rot);