20 static int handle_sball_event(sball_event *ev);
21 static void recalc_sball_matrix(float *xform);
28 static vec3_t pos = {0, 0, 0};
29 static quat_t rot = {0, 0, 0, 1};
31 int main(int argc, char **argv)
33 fbsize = fb_width * fb_height * fb_bpp / CHAR_BIT;
35 init_logger("demo.log");
42 if((use_mouse = have_mouse())) {
43 set_mouse_limits(0, 0, fb_width, fb_height);
44 set_mouse(fb_width / 2, fb_height / 2);
47 if(!(fb_pixels = malloc(fbsize))) {
48 fprintf(stderr, "failed to allocate backbuffer\n");
52 if(!(vmem_front = set_video_mode(fb_width, fb_height, fb_bpp))) {
55 /* TODO implement multiple video memory pages for flipping */
56 vmem_back = vmem_front;
58 if(demo_init(argc, argv) == -1) {
63 if(opt.sball && sball_init() == 0) {
72 while((key = kb_getkey()) != -1) {
73 demo_keyboard(key, 1);
77 demo_keyboard(getch(), 1);
80 if(quit) goto break_evloop;
83 mouse_bmask = read_mouse(&mouse_x, &mouse_y);
85 if(use_sball && sball_pending()) {
87 printf("got sball event\n");
88 while(sball_getevent(&ev)) {
89 handle_sball_event(&ev);
91 recalc_sball_matrix(sball_matrix);
94 time_msec = get_msec();
115 void swap_buffers(void *pixels)
117 /* TODO implement page flipping */
123 memcpy(vmem_front, pixels, fbsize);
134 #define TX(ev) ((ev)->motion.motion[0])
135 #define TY(ev) ((ev)->motion.motion[1])
136 #define TZ(ev) ((ev)->motion.motion[2])
137 #define RX(ev) ((ev)->motion.motion[3])
138 #define RY(ev) ((ev)->motion.motion[4])
139 #define RZ(ev) ((ev)->motion.motion[5])
141 static int handle_sball_event(sball_event *ev)
144 case SBALL_EV_MOTION:
145 if(RX(ev) | RY(ev) | RZ(ev)) {
146 float rx = (float)RX(ev);
147 float ry = (float)RY(ev);
148 float rz = (float)RZ(ev);
149 float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
151 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
152 -ry / axis_len, -rz / axis_len);
156 pos.x += TX(ev) * 0.001;
157 pos.y += TY(ev) * 0.001;
158 pos.z += TZ(ev) * 0.001;
161 case SBALL_EV_BUTTON:
162 if(ev->button.pressed) {
163 pos = v3_cons(0, 0, 0);
164 rot = quat_cons(1, 0, 0, 0);
172 void recalc_sball_matrix(float *xform)
174 quat_to_mat(xform, rot);