X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=blobdiff_plain;f=src%2Fsdl%2Fmain.c;h=b9ed60414ed694cae33553dca2a4e33d58046c77;hp=e7e161e8e79b2c987f8617132f532c23a51dd2ea;hb=9ecf7c26db456b3cef34a7d6d79ffb164cad17c5;hpb=21bfd2598698721227d6d9d55d9825334dd9e58c diff --git a/src/sdl/main.c b/src/sdl/main.c index e7e161e..b9ed604 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -1,15 +1,22 @@ #include #include +#include #include #include #include "demo.h" #include "tinyfps.h" #include "timer.h" #include "cfgopt.h" +#include "sball.h" +#include "vmath.h" static void handle_event(SDL_Event *ev); static void toggle_fullscreen(void); +static int handle_sball_event(sball_event *ev); +static void recalc_sball_matrix(float *xform); + + static int quit; static SDL_Surface *fbsurf; @@ -17,6 +24,11 @@ static int fbscale = 2; static int xsz, ysz; static unsigned int sdl_flags = SDL_SWSURFACE; +static int use_sball; +static vec3_t pos = {0, 0, 0}; +static quat_t rot = {0, 0, 0, 1}; + + int main(int argc, char **argv) { int s, i, j; @@ -54,6 +66,11 @@ int main(int argc, char **argv) SDL_Quit(); return 1; } + + if(opt.sball && sball_init() == 0) { + use_sball = 1; + } + reset_timer(); while(!quit) { @@ -63,6 +80,15 @@ int main(int argc, char **argv) if(quit) goto break_evloop; } + if(use_sball) { + while(sball_pending()) { + sball_event ev; + sball_getevent(&ev); + handle_sball_event(&ev); + } + recalc_sball_matrix(sball_matrix); + } + time_msec = get_msec(); demo_draw(); drawFps(fb_pixels); @@ -185,3 +211,51 @@ static void toggle_fullscreen(void) 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; +} + +void recalc_sball_matrix(float *xform) +{ + quat_to_mat(xform, rot); + xform[12] = pos.x; + xform[13] = pos.y; + xform[14] = pos.z; +}