21 static int handle_sball_event(sball_event *ev);
22 static void recalc_sball_matrix(float *xform);
29 static vec3_t pos = {0, 0, 0};
30 static quat_t rot = {0, 0, 0, 1};
32 int main(int argc, char **argv)
34 fbsize = fb_width * fb_height * fb_bpp / CHAR_BIT;
36 init_logger("demo.log");
43 if((use_mouse = have_mouse())) {
44 printf("initializing mouse input\n");
45 set_mouse_limits(0, 0, fb_width, fb_height);
46 set_mouse(fb_width / 2, fb_height / 2);
49 if(!(fb_pixels = malloc(fbsize))) {
50 fprintf(stderr, "failed to allocate backbuffer\n");
54 if(!(vmem_back = set_video_mode(fb_width, fb_height, fb_bpp))) {
57 if(!(vmem_front = page_flip(FLIP_NOW))) {
58 fprintf(stderr, "page flipping not supported. falling back to double buffering\n");
59 vmem_front = vmem_back;
61 assert(vmem_back != vmem_front);
64 if(demo_init(argc, argv) == -1) {
69 if(opt.sball && sball_init() == 0) {
78 while((key = kb_getkey()) != -1) {
79 demo_keyboard(key, 1);
83 demo_keyboard(getch(), 1);
86 if(quit) goto break_evloop;
89 mouse_bmask = read_mouse(&mouse_x, &mouse_y);
91 if(use_sball && sball_pending()) {
93 while(sball_getevent(&ev)) {
94 handle_sball_event(&ev);
96 recalc_sball_matrix(sball_matrix);
99 time_msec = get_msec();
120 void swap_buffers(void *pixels)
123 /* just memcpy to the front buffer */
128 memcpy(vmem_front, pixels, fbsize);
131 /* attempt page flipping */
135 if((next = page_flip(opt.vsync ? FLIP_VBLANK_WAIT : FLIP_NOW))) {
136 assert(next == vmem_back);
137 vmem_back = vmem_front;
140 /* failed to page flip, assume we drew in the front buffer then
141 * and just wait for vsync if necessary
151 #define TX(ev) ((ev)->motion.motion[0])
152 #define TY(ev) ((ev)->motion.motion[1])
153 #define TZ(ev) ((ev)->motion.motion[2])
154 #define RX(ev) ((ev)->motion.motion[3])
155 #define RY(ev) ((ev)->motion.motion[4])
156 #define RZ(ev) ((ev)->motion.motion[5])
158 static int handle_sball_event(sball_event *ev)
161 case SBALL_EV_MOTION:
162 if(RX(ev) | RY(ev) | RZ(ev)) {
163 float rx = (float)RX(ev);
164 float ry = (float)RY(ev);
165 float rz = (float)RZ(ev);
166 float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
168 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
169 -ry / axis_len, -rz / axis_len);
173 pos.x += TX(ev) * 0.001;
174 pos.y += TY(ev) * 0.001;
175 pos.z += TZ(ev) * 0.001;
178 case SBALL_EV_BUTTON:
179 if(ev->button.pressed) {
180 pos = v3_cons(0, 0, 0);
181 rot = quat_cons(1, 0, 0, 0);
189 void recalc_sball_matrix(float *xform)
191 quat_to_mat(xform, rot);