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 printf("initializing mouse input\n");
44 set_mouse_limits(0, 0, fb_width, fb_height);
45 set_mouse(fb_width / 2, fb_height / 2);
48 if(!(fb_pixels = malloc(fbsize))) {
49 fprintf(stderr, "failed to allocate backbuffer\n");
53 if(!(vmem_front = set_video_mode(fb_width, fb_height, fb_bpp))) {
56 /* TODO implement multiple video memory pages for flipping */
57 vmem_back = vmem_front;
59 if(demo_init(argc, argv) == -1) {
64 if(opt.sball && sball_init() == 0) {
73 while((key = kb_getkey()) != -1) {
74 demo_keyboard(key, 1);
78 demo_keyboard(getch(), 1);
81 if(quit) goto break_evloop;
84 mouse_bmask = read_mouse(&mouse_x, &mouse_y);
86 if(use_sball && sball_pending()) {
88 printf("got sball event\n");
89 while(sball_getevent(&ev)) {
90 handle_sball_event(&ev);
92 recalc_sball_matrix(sball_matrix);
95 time_msec = get_msec();
116 void swap_buffers(void *pixels)
118 /* TODO implement page flipping */
124 memcpy(vmem_front, pixels, fbsize);
135 #define TX(ev) ((ev)->motion.motion[0])
136 #define TY(ev) ((ev)->motion.motion[1])
137 #define TZ(ev) ((ev)->motion.motion[2])
138 #define RX(ev) ((ev)->motion.motion[3])
139 #define RY(ev) ((ev)->motion.motion[4])
140 #define RZ(ev) ((ev)->motion.motion[5])
142 static int handle_sball_event(sball_event *ev)
145 case SBALL_EV_MOTION:
146 if(RX(ev) | RY(ev) | RZ(ev)) {
147 float rx = (float)RX(ev);
148 float ry = (float)RY(ev);
149 float rz = (float)RZ(ev);
150 float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
152 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
153 -ry / axis_len, -rz / axis_len);
157 pos.x += TX(ev) * 0.001;
158 pos.y += TY(ev) * 0.001;
159 pos.z += TZ(ev) * 0.001;
162 case SBALL_EV_BUTTON:
163 if(ev->button.pressed) {
164 pos = v3_cons(0, 0, 0);
165 rot = quat_cons(1, 0, 0, 0);
173 void recalc_sball_matrix(float *xform)
175 quat_to_mat(xform, rot);