X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fgamescr.c;h=c31700d42e635c4f7089536674d8090801eeca17;hb=e785b2f83fd3aba82cb07ca5dd8d590d817d376c;hp=f2a2ee4f3f8921f5e2ed54cd54a86a114160aaf1;hpb=3bdc1e0adfc4261ffd74e65b094ca3f1a05dc6b6;p=vrtris diff --git a/src/gamescr.c b/src/gamescr.c index f2a2ee4..c31700d 100644 --- a/src/gamescr.c +++ b/src/gamescr.c @@ -2,12 +2,23 @@ #include #include #include +#ifdef BUILD_VR +#include +#endif +#include #include "opengl.h" #include "game.h" #include "screen.h" #include "cmesh.h" #include "blocks.h" #include "logger.h" +#include "gameinp.h" +#include "color.h" + +#define FONTSZ 75 + +int init_starfield(void); +void draw_starfield(void); static int init(void); static void cleanup(void); @@ -15,7 +26,7 @@ static void start(void); static void stop(void); static void update(float dt); static void draw(void); -static void draw_block(int block, const int *pos, int rot); +static void draw_block(int block, const int *pos, int rot, float sat, float alpha); static void drawpf(void); static void reshape(int x, int y); static void keyboard(int key, int pressed); @@ -50,8 +61,11 @@ struct game_screen game_screen = { static struct cmesh *blkmesh, *wellmesh; static unsigned int tex_well; -static float cam_theta, cam_phi, cam_dist = 30; -static int bnstate[16]; +static struct dtx_font *scorefont; + +static float cam_theta, cam_phi, cam_dist; +static float cam_height; +static unsigned int bnstate; static int prev_mx, prev_my; static long tick_interval; @@ -76,6 +90,11 @@ static int pause; static int score, level, lines; static int just_spawned; +#ifdef BUILD_VR +static int vrbn_a = 0, vrbn_x = 4; +static float vrscale = 40.0f; +#endif + #define NUM_LEVELS 21 static const long level_speed[NUM_LEVELS] = { 887, 820, 753, 686, 619, 552, 469, 368, 285, 184, @@ -84,17 +103,31 @@ static const long level_speed[NUM_LEVELS] = { static const float blkcolor[][4] = { {1.0, 0.65, 0.0, 1}, - {0.16, 1.0, 0.0, 1}, + {0.16, 1.0, 0.4, 1}, {0.65, 0.65, 1.0, 1}, - {1.0, 1.0, 0.0, 1}, + {1.0, 0.9, 0.1, 1}, {0.0, 1.0, 1.0, 1}, {1.0, 0.5, 1.0, 1}, - {1.0, 0.25, 0.0, 1} + {1.0, 0.35, 0.2, 1}, + {0.5, 0.5, 0.5, 1} }; +#define GAMEOVER_FILL_RATE 50 + static int init(void) { + if(!(scorefont = dtx_open_font("data/score.font", 0))) { + error_log("failed to open score font\n"); + return -1; + } + dtx_prepare_range(scorefont, FONTSZ, 32, 127); + dtx_save_glyphmap("foo.ppm", dtx_get_glyphmap(scorefont, 0)); + + if(init_starfield() == -1) { + return -1; + } + if(!(blkmesh = cmesh_alloc()) || cmesh_load(blkmesh, "data/noisecube.obj") == -1) { error_log("failed to load block mesh\n"); return -1; @@ -116,12 +149,17 @@ static int init(void) static void cleanup(void) { cmesh_free(blkmesh); + cmesh_free(wellmesh); + glDeleteTextures(1, &tex_well); + dtx_close_font(scorefont); } static void start(void) { srand(time(0)); + glClearColor(0.12, 0.12, 0.18, 1); + pause = 0; gameover = 0; num_complines = 0; @@ -132,10 +170,125 @@ static void start(void) next_block = rand() % NUM_BLOCKS; memset(pfield, 0, PF_COLS * PF_ROWS * sizeof *pfield); + + ginp_repeat(500, 75, GINP_LEFT | GINP_RIGHT | GINP_DOWN); + + dtx_use_font(scorefont, FONTSZ); + + cam_theta = 0; + cam_phi = 0; + cam_dist = 30; + cam_height = 0; + +#ifdef BUILD_VR + if(goatvr_invr()) { + int bn = goatvr_lookup_button("A"); + if(bn >= 0) vrbn_a = bn; + + bn = goatvr_lookup_button("X"); + if(bn >= 0) vrbn_x = bn; + + /* switch to VR-optimized camera parameters */ + cam_theta = 0; + cam_phi = -2.5; + cam_dist = 20; + cam_height = 3.5; + vrscale = 40.0f; + + goatvr_set_units_scale(vrscale); + } +#endif } static void stop(void) { + goatvr_set_units_scale(1.0f); +} + +#define JTHRES 0.6 + +#define CHECK_BUTTON(idx, gbn) \ + if(joy_bnstate & (1 << idx)) { \ + ginp_bnstate |= gbn; \ + } + +static void update_input(float dtsec) +{ +#ifdef BUILD_VR + int num_vr_sticks; + + if(goatvr_invr() && (num_vr_sticks = goatvr_num_sticks()) > 0) { + float p[2]; + + goatvr_stick_pos(0, p); + p[1] *= 0.65; /* drops harder to trigger accidentally */ + + if(fabs(p[0]) > fabs(joy_axis[GPAD_LSTICK_X])) { + joy_axis[GPAD_LSTICK_X] = p[0]; + } + if(fabs(p[1]) > fabs(joy_axis[GPAD_LSTICK_Y])) { + joy_axis[GPAD_LSTICK_Y] = -p[1]; + } + + if(goatvr_button_state(vrbn_a)) { + joy_bnstate |= 1 << GPAD_A; + } + if(goatvr_button_state(vrbn_x)) { + joy_bnstate |= 1 << GPAD_START; + } + if(goatvr_action(0, GOATVR_ACTION_TRIGGER) || goatvr_action(1, GOATVR_ACTION_TRIGGER)) { + joy_bnstate |= 1 << GPAD_UP; + } + } +#endif /* BUILD_VR */ + + ginp_bnstate = 0; + + /* joystick axis */ + if(joy_axis[GPAD_LSTICK_X] >= JTHRES) { + ginp_bnstate |= GINP_RIGHT; + } else if(joy_axis[GPAD_LSTICK_X] <= -JTHRES) { + ginp_bnstate |= GINP_LEFT; + } + + if(joy_axis[GPAD_LSTICK_Y] >= JTHRES) { + ginp_bnstate |= GINP_DOWN; + } else if(joy_axis[GPAD_LSTICK_Y] <= -JTHRES) { + ginp_bnstate |= GINP_UP; + } + + CHECK_BUTTON(GPAD_LEFT, GINP_LEFT); + CHECK_BUTTON(GPAD_RIGHT, GINP_RIGHT); + CHECK_BUTTON(GPAD_UP, GINP_UP); + CHECK_BUTTON(GPAD_DOWN, GINP_DOWN); + CHECK_BUTTON(GPAD_A, GINP_ROTATE); + CHECK_BUTTON(GPAD_START, GINP_PAUSE); + + update_ginp(); + + if(GINP_PRESS(GINP_LEFT)) { + game_keyboard('a', 1); + } + if(GINP_PRESS(GINP_RIGHT)) { + game_keyboard('d', 1); + } + if(GINP_PRESS(GINP_DOWN)) { + game_keyboard('s', 1); + } + if(GINP_PRESS(GINP_UP)) { + game_keyboard('\t', 1); + } + if(GINP_PRESS(GINP_ROTATE)) { + game_keyboard('w', 1); + } + if(GINP_PRESS(GINP_PAUSE)) { + game_keyboard('p', 1); + } + +#ifdef BUILD_VR + memset(joy_axis, 0, sizeof joy_axis); + joy_bnstate = 0; +#endif } static void update(float dtsec) @@ -143,16 +296,17 @@ static void update(float dtsec) static long prev_tick; long dt; + update_input(dtsec); + if(pause) { prev_tick = time_msec; return; } dt = time_msec - prev_tick; - /* if(gameover) { int i, row = PF_ROWS - gameover; - int *ptr; + unsigned int *ptr; if(dt < GAMEOVER_FILL_RATE) { return; @@ -161,15 +315,14 @@ static void update(float dtsec) if(row >= 0) { ptr = pfield + row * PF_COLS; for(i=0; i= 0) { - draw_block(cur_block, pos, cur_rot); + draw_block(cur_block, pos, cur_rot, 1.0f, 1.0f); } + glPopMatrix(); + glPushAttrib(GL_ENABLE_BIT); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glPushMatrix(); + t = (float)time_msec / 1000.0f; + glTranslatef(-PF_COLS / 2 + 0.5 + PF_COLS + 3, PF_ROWS / 2 - 0.5, 0); + glTranslatef(1.5, -1, 0); + glRotatef(cos(t) * 8.0f, 1, 0, 0); + glRotatef(sin(t * 1.2f) * 10.0f, 0, 1, 0); + glTranslatef(-1.5, 1, 0); + draw_block(next_block, nextblk_pos, 0, 0.25f, 0.75f); glPopMatrix(); + glPopAttrib(); + + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + + glPushMatrix(); + glTranslatef(-11, 6, 0); + glScalef(0.05, 0.05, 0.05); + + glColor3f(1, 1, 1); + dtx_string("Score"); + glTranslatef(0, -dtx_line_height() * 1.5, 0); + glPushMatrix(); + glScalef(1.5, 1.5, 1.5); + dtx_printf("%d", score); + glPopMatrix(); + + glTranslatef(0, -dtx_line_height() * 2, 0); + dtx_string("Level"); + glTranslatef(0, -dtx_line_height() * 1.5, 0); + glPushMatrix(); + glScalef(1.5, 1.5, 1.5); + dtx_printf("%d", level); + glPopMatrix(); + + glTranslatef(0, -dtx_line_height() * 2, 0); + dtx_string("Lines"); + glTranslatef(0, -dtx_line_height() * 1.5, 0); + glPushMatrix(); + glScalef(1.5, 1.5, 1.5); + dtx_printf("%d", lines); + glPopMatrix(); + + glPopMatrix(); + glPopAttrib(); } -static const float blkspec[] = {1, 1, 1, 1}; +static const float blkspec[] = {0.85, 0.85, 0.85, 1}; -static void draw_block(int block, const int *pos, int rot) +static void draw_block(int block, const int *pos, int rot, float sat, float alpha) { int i; unsigned char *p = blocks[block][rot]; + float col[4], hsv[3]; - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[block]); + rgb_to_hsv(blkcolor[block][0], blkcolor[block][1], blkcolor[block][2], + hsv, hsv + 1, hsv + 2); + hsv_to_rgb(hsv[0], hsv[1] * sat, hsv[2], col, col + 1, col + 2); + col[3] = alpha; + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f); @@ -283,7 +497,7 @@ static void drawpf(void) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f); - if(val & PF_FULL) { + if((val & (PF_FULL | PF_VIS)) == (PF_FULL | PF_VIS)) { glPushMatrix(); glTranslatef(j, -i, 0); cmesh_draw(blkmesh); @@ -306,6 +520,7 @@ static void keyboard(int key, int pressed) switch(key) { case 'a': + case KEY_LEFT: if(!pause) { next_pos[1] = pos[1] - 1; if(collision(cur_block, next_pos)) { @@ -317,6 +532,7 @@ static void keyboard(int key, int pressed) break; case 'd': + case KEY_RIGHT: if(!pause) { next_pos[1] = pos[1] + 1; if(collision(cur_block, next_pos)) { @@ -328,6 +544,8 @@ static void keyboard(int key, int pressed) break; case 'w': + case KEY_UP: + case ' ': if(!pause) { prev_rot = cur_rot; cur_rot = (cur_rot + 1) & 3; @@ -340,6 +558,7 @@ static void keyboard(int key, int pressed) break; case 's': + case KEY_DOWN: /* ignore drops until the first update after a spawn */ if(cur_block >= 0 && !just_spawned && !pause) { next_pos[0] = pos[0] + 1; @@ -353,6 +572,7 @@ static void keyboard(int key, int pressed) case '\n': case '\t': + case '0': if(!pause && cur_block >= 0) { next_pos[0] = pos[0] + 1; while(!collision(cur_block, next_pos)) { @@ -395,7 +615,11 @@ static void keyboard(int key, int pressed) static void mouse(int bn, int pressed, int x, int y) { - bnstate[bn] = pressed; + if(pressed) { + bnstate |= 1 << bn; + } else { + bnstate &= ~(1 << bn); + } prev_mx = x; prev_my = y; } @@ -407,21 +631,39 @@ static void motion(int x, int y) prev_mx = x; prev_my = y; - if(bnstate[0]) { + if(bnstate & 1) { cam_theta += dx * 0.5; cam_phi += dy * 0.5; if(cam_phi < -90) cam_phi = -90; if(cam_phi > 90) cam_phi = 90; } - if(bnstate[2]) { + if(bnstate & 2) { + cam_height += dy * 0.1; + } + if(bnstate & 4) { cam_dist += dy * 0.1; if(cam_dist < 0) cam_dist = 0; } + +#ifdef DBG_PRINT_VIEW + if(bnstate) { + debug_log("Camera params\n"); + debug_log(" theta: %g phi: %g dist: %g height: %g\n", cam_theta, + cam_phi, cam_dist, cam_height); + } +#endif } static void wheel(int dir) { + /* debug code, used to figure out the best scales */ + /* + vrscale += dir * 0.01; + if(vrscale < 0.01) vrscale = 0.01; + goatvr_set_units_scale(vrscale); + debug_log("VR scale: %g\n", vrscale); + */ } static void update_cur_block(void)