X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fgame.c;h=3194196faedbcb15b3dd9ddf5ca790403c586794;hb=8c3ae60742d38e2dbcd9811b16966933df0a5c29;hp=8ee198908196e6883316a2f1916e27d59331e4d3;hpb=0a754b5aac897ffde09e93027aed78c95b81b99b;p=vrlugburz diff --git a/src/game.c b/src/game.c index 8ee1989..3194196 100644 --- a/src/game.c +++ b/src/game.c @@ -1,21 +1,24 @@ +#include #include #include "cgmath/cgmath.h" #include "game.h" #include "opengl.h" #include "level.h" +#include "player.h" #include "scenefile.h" #include "sdr.h" static void draw_level(void); struct level lvl; +struct player player; int win_width, win_height; float win_aspect; int mouse_x, mouse_y; int bnstate[8]; -float cam_theta, cam_phi, cam_dist = 10; +float cam_dist; float view_matrix[16], proj_matrix[16]; unsigned int sdr_foo; @@ -41,6 +44,12 @@ int game_init(void) if(load_level(&lvl, "data/test.lvl") == -1) { return -1; } + gen_level_geom(&lvl); + + init_player(&player); + player.lvl = &lvl; + player.cx = lvl.px; + player.cy = lvl.py; return 0; } @@ -51,8 +60,63 @@ void game_shutdown(void) free_program(sdr_foo); } +#define STEP_INTERVAL 128 + +void update(float dt) +{ + static long prev_step; + int dir; + int step[][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}}; + + cgm_vec3 vdir = {0, 0, -1}; + + cgm_vmul_m3v3(&vdir, player.view_xform); + + player.dir = (int)(2.0f * (-atan2(vdir.z, vdir.x) + M_PI) / M_PI + 0.5f) & 3; + + if(time_msec - prev_step >= STEP_INTERVAL) { + if(input_state[INP_FWD]) { + player.cx += step[player.dir][0]; + player.cy += step[player.dir][1]; + prev_step = time_msec; + printf("step[%d] %d,%d\n", player.dir, player.cx, player.cy); + } + if(input_state[INP_BACK]) { + player.cx -= step[player.dir][0]; + player.cy -= step[player.dir][1]; + prev_step = time_msec; + printf("step[%d] %d,%d\n", player.dir, player.cx, player.cy); + } + if(input_state[INP_LEFT]) { + dir = (player.dir + 3) & 3; + player.cx += step[dir][0]; + player.cy += step[dir][1]; + prev_step = time_msec; + printf("step[%d] %d,%d\n", player.dir, player.cx, player.cy); + } + if(input_state[INP_RIGHT]) { + dir = (player.dir + 1) & 3; + player.cx += step[dir][0]; + player.cy += step[dir][1]; + prev_step = time_msec; + printf("step[%d] %d,%d\n", player.dir, player.cx, player.cy); + } + memset(input_state, 0, sizeof input_state); + } + + upd_player_xform(&player); +} + void game_display(void) { + float dt; + static long prev_msec; + + dt = (prev_msec - time_msec) / 1000.0f; + prev_msec = time_msec; + + update(dt); + glClearColor(0.1, 0.1, 0.1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -61,12 +125,10 @@ void game_display(void) glMatrixMode(GL_PROJECTION); glLoadMatrixf(proj_matrix); - cgm_midentity(view_matrix); - cgm_mpretranslate(view_matrix, 0, -1.7, -cam_dist); - cgm_mprerotate(view_matrix, cam_phi, 1, 0, 0); - cgm_mprerotate(view_matrix, cam_theta, 0, 1, 0); glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(view_matrix); + glLoadIdentity(); + glTranslatef(0, 0, -cam_dist); + glMultMatrixf(player.view_xform); draw_level(); @@ -78,16 +140,24 @@ static void draw_level(void) { int i, j, k; struct cell *cell; + float xform[16]; glUseProgram(sdr_foo); cell = lvl.cells; for(i=0; inum_mgrp; k++) { draw_meshgroup(cell->mgrp + k); } cell++; + + glPopMatrix(); } } @@ -108,6 +178,24 @@ void game_keyboard(int key, int press) game_quit(); return; } + + switch(key) { + case 'w': + input_state[INP_FWD] = press; + break; + + case 'a': + input_state[INP_LEFT] = press; + break; + + case 's': + input_state[INP_BACK] = press; + break; + + case 'd': + input_state[INP_RIGHT] = press; + break; + } } void game_mbutton(int bn, int press, int x, int y) @@ -127,10 +215,10 @@ void game_mmotion(int x, int y) if(!(dx | dy)) return; if(bnstate[0]) { - cam_theta += cgm_deg_to_rad(dx * 0.5f); - cam_phi += cgm_deg_to_rad(dy * 0.5f); - if(cam_phi < -M_PI/2) cam_phi = -M_PI/2; - if(cam_phi > M_PI/2) cam_phi = M_PI/2; + player.theta -= cgm_deg_to_rad(dx * 0.5f); + player.phi -= cgm_deg_to_rad(dy * 0.5f); + if(player.phi < -M_PI/2) player.phi = -M_PI/2; + if(player.phi > M_PI/2) player.phi = M_PI/2; } if(bnstate[2]) { cam_dist += dy * 0.1;