X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fgame.c;h=a997b692282e06c7323bc4a9cdc6d3cd4b1bde02;hb=f242719e98650720b5d3ed43aca62a4f1fe3ec10;hp=5a846ed124dd6389a421da082905b59c953d0d00;hpb=fd7b604752c0ba03c347bc2ef3ee5a8c0759bff8;p=vrlugburz diff --git a/src/game.c b/src/game.c index 5a846ed..a997b69 100644 --- a/src/game.c +++ b/src/game.c @@ -1,9 +1,27 @@ +#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_dist = 10; +float view_matrix[16], proj_matrix[16]; + +unsigned int sdr_foo; int game_init(void) { @@ -11,9 +29,27 @@ int game_init(void) return -1; } + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + if(!(sdr_foo = create_program_load("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) { + return -1; + } + glBindAttribLocation(sdr_foo, MESH_ATTR_VERTEX, "apos"); + glBindAttribLocation(sdr_foo, MESH_ATTR_NORMAL, "anorm"); + glBindAttribLocation(sdr_foo, MESH_ATTR_TANGENT, "atang"); + glBindAttribLocation(sdr_foo, MESH_ATTR_TEXCOORD, "atex"); + link_program(sdr_foo); + 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; } @@ -21,20 +57,92 @@ int game_init(void) void game_shutdown(void) { destroy_level(&lvl); + free_program(sdr_foo); +} + +#define STEP_INTERVAL 1000 + +void update(float dt) +{ + static long prev_step; + int step[][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; + + cgm_vec3 vdir = {0, 0, -1}; + + upd_player_xform(&player); + cgm_vmul_m3v3(&vdir, player.view_xform); + + player.dir = (int)(2.0f * (atan2(vdir.z, vdir.x) + M_PI) / M_PI + 0.5f); + + /* + if(time_msec - prev_step >= STEP_INTERVAL) { + if(input[INP_FWD]) { + } + } + */ } void game_display(void) { - glClearColor(1, 0, 0, 1); + 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); + cgm_midentity(proj_matrix); + cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), win_aspect, 0.5, 500.0); + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(proj_matrix); + + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(player.view_xform); + + draw_level(); + game_swap_buffers(); assert(glGetError() == GL_NO_ERROR); } +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(); + } + } + + glUseProgram(0); +} + void game_reshape(int x, int y) { glViewport(0, 0, x, y); + win_width = x; + win_height = y; + win_aspect = (float)x / (float)y; } void game_keyboard(int key, int press) @@ -47,8 +155,28 @@ void game_keyboard(int key, int press) void game_mbutton(int bn, int press, int x, int y) { + bnstate[bn] = press; + mouse_x = x; + mouse_y = y; } void game_mmotion(int x, int y) { + int dx = x - mouse_x; + int dy = y - mouse_y; + mouse_x = x; + mouse_y = y; + + if(!(dx | dy)) return; + + if(bnstate[0]) { + 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; + if(cam_dist < 0) cam_dist = 0; + } }