+
+static void toggle_fullscreen(void)
+{
+ SDL_Surface *newsurf;
+ unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
+
+ if(!(newsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, newflags))) {
+ fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
+ return;
+ }
+
+ fbsurf = newsurf;
+ sdl_flags = newflags;
+}
+
+
+
+#define TX(ev) ((ev)->motion.motion[0])
+#define TY(ev) ((ev)->motion.motion[1])
+#define TZ(ev) ((ev)->motion.motion[2])
+#define RX(ev) ((ev)->motion.motion[3])
+#define RY(ev) ((ev)->motion.motion[4])
+#define RZ(ev) ((ev)->motion.motion[5])
+
+static int handle_sball_event(sball_event *ev)
+{
+ switch(ev->type) {
+ case SBALL_EV_MOTION:
+ if(RX(ev) | RY(ev) | RZ(ev)) {
+ float rx = (float)RX(ev);
+ float ry = (float)RY(ev);
+ float rz = (float)RZ(ev);
+ float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
+ if(axis_len > 0.0) {
+ rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
+ -ry / axis_len, -rz / axis_len);
+ }
+ }
+
+ pos.x += TX(ev) * 0.001;
+ pos.y += TY(ev) * 0.001;
+ pos.z += TZ(ev) * 0.001;
+ break;
+
+ case SBALL_EV_BUTTON:
+ if(ev->button.pressed) {
+ pos = v3_cons(0, 0, 0);
+ rot = quat_cons(1, 0, 0, 0);
+ }
+ break;
+ }
+
+ return 0;
+}
+
+static void recalc_sball_matrix(float *xform)
+{
+ quat_to_mat(xform, rot);
+ xform[12] = pos.x;
+ xform[13] = pos.y;
+ xform[14] = pos.z;
+}
+
+#define SSORG '\''
+#define SSEND '`'
+static char symshift[] = {
+ '"', 0, 0, 0, 0, '<', '_', '>', '?',
+ ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',
+ 0, ':', 0, '+', 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ '{', '|', '}', 0, 0, '~'
+};
+
+
+static int sdlkey_to_demokey(int sdlkey, unsigned int mod)
+{
+ if(sdlkey < 128) {
+ if(mod & (KMOD_SHIFT)) {
+ if(sdlkey >= 'a' && sdlkey <= 'z') {
+ sdlkey = toupper(sdlkey);
+ } else if(sdlkey >= SSORG && sdlkey <= SSEND) {
+ sdlkey = symshift[sdlkey - SSORG];
+ }
+ }
+ return sdlkey;
+ }
+ if(sdlkey < 256) return 0;
+ return sdlkey - 128;
+}