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 = set_video_mode(fb_width, fb_height, fb_bpp))) {
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 while(sball_getevent(&ev)) {
88 handle_sball_event(&ev);
90 recalc_sball_matrix(sball_matrix);
93 time_msec = get_msec();
114 void swap_buffers(void *pixels)
120 /* just memcpy to the front buffer */
125 memcpy(vmem, pixels, fbsize);
129 #define TX(ev) ((ev)->motion.motion[0])
130 #define TY(ev) ((ev)->motion.motion[1])
131 #define TZ(ev) ((ev)->motion.motion[2])
132 #define RX(ev) ((ev)->motion.motion[3])
133 #define RY(ev) ((ev)->motion.motion[4])
134 #define RZ(ev) ((ev)->motion.motion[5])
136 static int handle_sball_event(sball_event *ev)
139 case SBALL_EV_MOTION:
140 if(RX(ev) | RY(ev) | RZ(ev)) {
141 float rx = (float)RX(ev);
142 float ry = (float)RY(ev);
143 float rz = (float)RZ(ev);
144 float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
146 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
147 -ry / axis_len, -rz / axis_len);
151 pos.x += TX(ev) * 0.001;
152 pos.y += TY(ev) * 0.001;
153 pos.z += TZ(ev) * 0.001;
156 case SBALL_EV_BUTTON:
157 if(ev->button.pressed) {
158 pos = v3_cons(0, 0, 0);
159 rot = quat_cons(1, 0, 0, 0);
167 void recalc_sball_matrix(float *xform)
169 quat_to_mat(xform, rot);