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 / 8;
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 /* allocate a couple extra rows as a guard band, until we fucking fix the rasterizer */
50 if(!(fb_pixels = malloc(fbsize + (fb_width * fb_bpp / 8) * 2))) {
51 fprintf(stderr, "failed to allocate backbuffer\n");
54 fb_pixels += fb_width;
56 if(!(vmem = set_video_mode(fb_width, fb_height, fb_bpp, 1))) {
60 if(demo_init(argc, argv) == -1) {
65 if(opt.sball && sball_init() == 0) {
74 while((key = kb_getkey()) != -1) {
75 demo_keyboard(key, 1);
79 demo_keyboard(getch(), 1);
82 if(quit) goto break_evloop;
85 mouse_bmask = read_mouse(&mouse_x, &mouse_y);
87 if(use_sball && sball_pending()) {
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)
122 /* just memcpy to the front buffer */
127 memcpy(vmem, pixels, fbsize);
131 #define TX(ev) ((ev)->motion.motion[0])
132 #define TY(ev) ((ev)->motion.motion[1])
133 #define TZ(ev) ((ev)->motion.motion[2])
134 #define RX(ev) ((ev)->motion.motion[3])
135 #define RY(ev) ((ev)->motion.motion[4])
136 #define RZ(ev) ((ev)->motion.motion[5])
138 static int handle_sball_event(sball_event *ev)
141 case SBALL_EV_MOTION:
142 if(RX(ev) | RY(ev) | RZ(ev)) {
143 float rx = (float)RX(ev);
144 float ry = (float)RY(ev);
145 float rz = (float)RZ(ev);
146 float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
148 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
149 -ry / axis_len, -rz / axis_len);
153 pos.x += TX(ev) * 0.001;
154 pos.y += TY(ev) * 0.001;
155 pos.z += TZ(ev) * 0.001;
158 case SBALL_EV_BUTTON:
159 if(ev->button.pressed) {
160 pos = v3_cons(0, 0, 0);
161 rot = quat_cons(1, 0, 0, 0);
169 void recalc_sball_matrix(float *xform)
171 quat_to_mat(xform, rot);