2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
31 /*---------------------------------------------------------------------------*/
33 static int game_state = 0;
35 static struct s_file file;
36 static struct s_file back;
38 static float clock = 0.f; /* Clock time */
39 static int clock_down = 1; /* Clock go up or down? */
41 static float game_ix; /* Input rotation about X axis */
42 static float game_iz; /* Input rotation about Z axis */
43 static float game_rx; /* Floor rotation about X axis */
44 static float game_rz; /* Floor rotation about Z axis */
46 static float view_a; /* Ideal view rotation about Y axis */
47 static float view_ry; /* Angular velocity about Y axis */
48 static float view_dc; /* Ideal view distance above ball */
49 static float view_dp; /* Ideal view distance above ball */
50 static float view_dz; /* Ideal view distance behind ball */
51 static float view_fov; /* Field of view */
53 static float view_c[3]; /* Current view center */
54 static float view_v[3]; /* Current view vector */
55 static float view_p[3]; /* Current view position */
56 static float view_e[3][3]; /* Current view orientation */
59 static int coins = 0; /* Collected coins */
60 static int goal_c = 0; /* Goal coins remaining (0 = open) */
61 static float goal_k = 0; /* Goal animation */
62 static int jump_e = 1; /* Jumping enabled flag */
63 static int jump_b = 0; /* Jump-in-progress flag */
64 static float jump_dt; /* Jump duration */
65 static float jump_p[3]; /* Jump destination */
66 static float fade_k = 0.0; /* Fade in/out level */
67 static float fade_d = 0.0; /* Fade in/out direction */
68 static int drawball = 1; /* Should the ball be drawn? */
69 static int ball_b = 0; /* Is the ball a bonus ball? */
71 /*---------------------------------------------------------------------------*/
73 static void view_init(void)
75 /* Get the initial orientation angle */
77 view_a = file.uv->a - 90.f; /* angle is in the sol */
79 view_a = 0.f; /* default is north :) */
83 view_fov = (float) config_get_d(CONFIG_VIEW_FOV);
84 view_dp = (float) config_get_d(CONFIG_VIEW_DP) / 100.0f;
85 view_dc = (float) config_get_d(CONFIG_VIEW_DC) / 100.0f;
86 view_dz = (float) config_get_d(CONFIG_VIEW_DZ) / 100.0f;
108 int game_init(const struct level *level, int t, int g)
110 clock = (float) t / 100.f;
111 clock_down = (t > 0);
117 if (!sol_load_gl(&file, level->file, config_get_d(CONFIG_TEXTURES),
118 config_get_d(CONFIG_SHADOW)))
119 return (game_state = 0);
130 /* Initialize jump and goal states. */
136 goal_k = (g == 0) ? 1.0f : 0.0f;
138 ball_b = level->is_bonus;
140 /* Initialise the level, background, particles, fade, and view. */
145 part_reset(GOAL_HEIGHT);
147 back_init(level->grad, config_get_d(CONFIG_GEOMETRY));
149 sol_load_gl(&back, config_data(level->back),
150 config_get_d(CONFIG_TEXTURES), 0);
166 /*---------------------------------------------------------------------------*/
170 return (int) (clock * 100.f);
183 /*---------------------------------------------------------------------------*/
185 static void game_draw_balls(const struct s_file *fp)
187 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
190 m_basis(M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
194 glTranslatef(fp->uv[0].p[0],
195 fp->uv[0].p[1] + BALL_FUDGE,
198 glScalef(fp->uv[0].r,
209 static void game_draw_coins(const struct s_file *fp)
211 float r = 360.f * SDL_GetTicks() / 1000.f;
216 for (ci = 0; ci < fp->cc; ci++)
217 if (fp->cv[ci].n > 0)
221 glTranslatef(fp->cv[ci].p[0],
224 glRotatef(r, 0.0f, 1.0f, 0.0f);
225 coin_draw(fp->cv[ci].n, r);
233 static void game_draw_goals(const struct s_file *fp, float rx, float ry)
238 for (zi = 0; zi < fp->zc; zi++)
242 glTranslatef(fp->zv[zi].p[0],
246 part_draw_goal(rx, ry, fp->zv[zi].r, goal_k, fp->zv[zi].c);
248 glScalef(fp->zv[zi].r, goal_k, fp->zv[zi].r);
255 static void game_draw_jumps(const struct s_file *fp)
259 for (ji = 0; ji < fp->jc; ji++)
263 glTranslatef(fp->jv[ji].p[0],
267 glScalef(fp->jv[ji].r, 1.f, fp->jv[ji].r);
274 static void game_draw_swchs(const struct s_file *fp)
278 for (xi = 0; xi < fp->xc; xi++)
284 glTranslatef(fp->xv[xi].p[0],
288 glScalef(fp->xv[xi].r, 1.f, fp->xv[xi].r);
289 swch_draw(fp->xv[xi].f, fp->xv[xi].e);
295 /*---------------------------------------------------------------------------*/
297 static void game_refl_all(int s)
299 const float *ball_p = file.uv->p;
303 /* Rotate the environment about the position of the ball. */
305 glTranslatef(+ball_p[0], +ball_p[1], +ball_p[2]);
306 glRotatef(-game_rz, view_e[2][0], view_e[2][1], view_e[2][2]);
307 glRotatef(-game_rx, view_e[0][0], view_e[0][1], view_e[0][2]);
308 glTranslatef(-ball_p[0], -ball_p[1], -ball_p[2]);
310 /* Draw the floor. */
317 /*---------------------------------------------------------------------------*/
319 static void game_draw_light(void)
321 const float light_p[2][4] = {
322 { -8.0f, +32.0f, -8.0f, 1.0f },
323 { +8.0f, +32.0f, +8.0f, 1.0f },
325 const float light_c[2][4] = {
326 { 1.0f, 0.8f, 0.8f, 1.0f },
327 { 0.8f, 1.0f, 0.8f, 1.0f },
330 /* Configure the lighting. */
333 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
334 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
335 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
338 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
339 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
340 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
343 static void game_draw_back(int pose, int d, const float p[3])
345 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
346 float t = SDL_GetTicks() / 1000.f + 120.0f;
352 glRotatef(game_rz * 2, view_e[2][0], view_e[2][1], view_e[2][2]);
353 glRotatef(game_rx * 2, view_e[0][0], view_e[0][1], view_e[0][2]);
356 glTranslatef(p[0], p[1], p[2]);
359 if (config_get_d(CONFIG_BACKGROUND))
361 /* Draw all background layers back to front. */
363 sol_back(&back, BACK_DIST, FAR_DIST, t);
365 sol_back(&back, 0, BACK_DIST, t);
367 /* Draw all foreground geometry in the background file. */
376 static void game_draw_fore(int pose, float rx, float ry, int d, const float p[3])
378 const float *ball_p = file.uv->p;
379 const float ball_r = file.uv->r;
381 glPushAttrib(GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
385 /* Rotate the environment about the position of the ball. */
387 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
388 glRotatef(-game_rz * d, view_e[2][0], view_e[2][1], view_e[2][2]);
389 glRotatef(-game_rx * d, view_e[0][0], view_e[0][1], view_e[0][2]);
390 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
401 glEnable(GL_CLIP_PLANE0);
402 glClipPlane(GL_CLIP_PLANE0, e);
405 /* Draw the floor. */
409 if (config_get_d(CONFIG_SHADOW) && drawball)
411 shad_draw_set(ball_p, ball_r);
416 /* Draw the game elements. */
419 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
423 part_draw_coin(-rx * d, -ry);
424 game_draw_coins(&file);
426 game_draw_balls(&file);
428 game_draw_goals(&file, -rx * d, -ry);
429 game_draw_jumps(&file);
430 game_draw_swchs(&file);
432 glDisable(GL_CLIP_PLANE0);
439 void game_draw(int pose, float st)
441 float fov = view_fov;
443 if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
447 config_push_persp(fov, 0.1f, FAR_DIST);
458 /* Compute and apply the view. */
460 v_sub(v, view_c, view_p);
462 rx = V_DEG(fatan2f(-v[1], fsqrtf(v[0] * v[0] + v[2] * v[2])));
463 ry = V_DEG(fatan2f(+v[0], -v[2])) + st;
465 glTranslatef(0.f, 0.f, -v_len(v));
466 glRotatef(rx, 1.f, 0.f, 0.f);
467 glRotatef(ry, 0.f, 1.f, 0.f);
468 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
470 if (config_get_d(CONFIG_REFLECTION))
472 /* Draw the mirror only into the stencil buffer. */
474 glDisable(GL_DEPTH_TEST);
475 glEnable(GL_STENCIL_TEST);
476 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
477 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
478 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
482 /* Draw the scene reflected into color and depth buffers. */
484 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
485 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
486 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
487 glEnable(GL_DEPTH_TEST);
492 glScalef(+1.f, -1.f, +1.f);
495 game_draw_back(pose, -1, pdn);
496 game_draw_fore(pose, rx, ry, -1, pdn);
501 glDisable(GL_STENCIL_TEST);
504 /* Draw the scene normally. */
507 game_refl_all(pose ? 0 : config_get_d(CONFIG_SHADOW));
508 game_draw_back(pose, +1, pup);
509 game_draw_fore(pose, rx, ry, +1, pup);
514 /* Draw the fade overlay. */
520 /*---------------------------------------------------------------------------*/
522 static void game_update_grav(float h[3], const float g[3])
524 struct s_file *fp = &file;
527 float y[3] = { 0.f, 1.f, 0.f };
533 /* Compute the gravity vector from the given world rotations. */
535 v_sub(z, view_p, fp->uv->p);
541 m_rot (Z, z, V_RAD(game_rz));
542 m_rot (X, x, V_RAD(game_rx));
547 static void game_update_view(float dt)
549 float dc = view_dc * (jump_b ? 2.0f * fabsf(jump_dt - 0.5f) : 1.0f);
550 float dx = view_ry * dt * 5.0f;
553 view_a += view_ry * dt * 90.f;
555 /* Center the view about the ball. */
557 v_cpy(view_c, file.uv->p);
558 v_inv(view_v, file.uv->v);
560 switch (config_get_d(CONFIG_CAMERA))
562 case 1: /* Camera 1: Viewpoint chases the ball position. */
564 v_sub(view_e[2], view_p, view_c);
567 case 2: /* Camera 2: View vector is given by view angle. */
569 view_e[2][0] = fsinf(V_RAD(view_a));
571 view_e[2][2] = fcosf(V_RAD(view_a));
577 default: /* Default: View vector approaches the ball velocity vector. */
579 k = v_dot(view_v, view_v);
581 v_sub(view_e[2], view_p, view_c);
582 v_mad(view_e[2], view_e[2], view_v, k * dt / 4);
587 /* Orthonormalize the basis of the view in its new position. */
589 v_crs(view_e[0], view_e[1], view_e[2]);
590 v_crs(view_e[2], view_e[0], view_e[1]);
591 v_nrm(view_e[0], view_e[0]);
592 v_nrm(view_e[2], view_e[2]);
594 /* Compute the new view position. */
596 k = 1.0f + v_dot(view_e[2], view_v) / 10.0f;
598 view_k = view_k + (k - view_k) * dt;
600 if (view_k < 0.5) view_k = 0.5;
602 v_cpy(view_p, file.uv->p);
603 v_mad(view_p, view_p, view_e[0], dx * view_k);
604 v_mad(view_p, view_p, view_e[1], view_dp * view_k);
605 v_mad(view_p, view_p, view_e[2], view_dz * view_k);
607 /* Compute the new view center. */
609 v_cpy(view_c, file.uv->p);
610 v_mad(view_c, view_c, view_e[1], dc);
612 /* Note the current view angle. */
614 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
617 static void game_update_time(float dt, int b)
619 if (goal_c == 0 && goal_k < 1.0f)
622 /* The ticking clock. */
637 static int game_update_state(int *state_value)
639 struct s_file *fp = &file;
642 int bt = state_value != NULL;
646 /* Test for a coin grab. */
648 if (bt && (n = sol_coin_test(fp, p, COIN_RADIUS)) > 0)
654 /* Check for goal open. */
660 audio_play(AUD_SWITCH, 1.f);
664 audio_play(AUD_COIN, 1.f);
667 audio_play(AUD_COIN, 1.f);
670 /* Test for a switch. */
671 if (sol_swch_test(fp, 0))
672 audio_play(AUD_SWITCH, 1.f);
674 /* Test for a jump. */
676 if (jump_e == 1 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 1)
682 audio_play(AUD_JUMP, 1.f);
684 if (jump_e == 0 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 0)
687 /* Test for a goal. */
689 if (bt && goal_c == 0 && (g = sol_goal_test(fp, p, 0)))
692 audio_play(AUD_GOAL, 1.0f);
693 return g->c ? GAME_SPEC : GAME_GOAL;
696 /* Test for time-out. */
698 if (bt && clock_down && clock <= 0.f)
700 const GLfloat *p = fp->uv->p;
701 const GLfloat c[5] = {1.0f, 1.0f, 0.0f, 0.0f, 1.0f};
709 audio_play(AUD_TIME, 1.0f);
713 /* Test for fall-out. */
715 if (bt && fp->uv[0].p[1] < fp->vv[0].p[1])
717 audio_play(AUD_FALL, 1.0f);
725 * On most hardware, rendering requires much more computing power than
726 * physics. Since physics takes less time than graphics, it make sense to
727 * detach the physics update time step from the graphics frame rate. By
728 * performing multiple physics updates for each graphics update, we get away
729 * with higher quality physics with little impact on overall performance.
731 * Toward this end, we establish a baseline maximum physics time step. If
732 * the measured frame time exceeds this maximum, we cut the time step in
733 * half, and do two updates. If THIS time step exceeds the maximum, we do
734 * four updates. And so on. In this way, the physics system is allowed to
735 * seek an optimal update rate independant of, yet in integral sync with, the
736 * graphics frame rate.
739 int game_step(const float g[3], float dt, int *state_value)
741 struct s_file *fp = &file;
753 /* Smooth jittery or discontinuous input. */
757 game_rx += (game_ix - game_rx) * t / RESPONSE;
758 game_rz += (game_iz - game_rz) * t / RESPONSE;
766 game_update_grav(h, g);
779 fp->uv[0].p[0] = jump_p[0];
780 fp->uv[0].p[1] = jump_p[1];
781 fp->uv[0].p[2] = jump_p[2];
790 while (t > MAX_DT && n < MAX_DN)
796 for (i = 0; i < n; i++)
797 if (b < (d = sol_step(fp, h, t, 0, NULL)))
800 /* Mix the sound of a ball bounce. */
803 audio_play(AUD_BUMP, (b - 0.5f) * 2.0f);
807 game_update_view(dt);
808 game_update_time(dt, state_value != NULL);
810 return game_update_state(state_value);
815 /*---------------------------------------------------------------------------*/
817 void game_no_aa(void)
819 float max = game_ix * game_ix + game_iz * game_iz;
820 if (max > ANGLE_BOUND * ANGLE_BOUND)
822 max = ANGLE_BOUND / sqrt(max);
828 void game_set_x(int k)
830 game_ix = -(ANGLE_BOUND) * k / JOY_MAX;
836 void game_set_z(int k)
838 game_iz = +ANGLE_BOUND * k / JOY_MAX;
844 void game_set_pos(int x, int y)
846 game_ix += 40.f * y / config_get_d(CONFIG_MOUSE_SENSE);
847 game_iz += 40.f * x / config_get_d(CONFIG_MOUSE_SENSE);
852 if (game_ix > +ANGLE_BOUND) game_ix = +ANGLE_BOUND;
853 if (game_ix < -ANGLE_BOUND) game_ix = -ANGLE_BOUND;
854 if (game_iz > +ANGLE_BOUND) game_iz = +ANGLE_BOUND;
855 if (game_iz < -ANGLE_BOUND) game_iz = -ANGLE_BOUND;
859 void game_set_rot(float r)
864 /*---------------------------------------------------------------------------*/
866 void game_set_fly(float k)
868 struct s_file *fp = &file;
870 float x[3] = { 1.f, 0.f, 0.f };
871 float y[3] = { 0.f, 1.f, 0.f };
872 float z[3] = { 0.f, 0.f, 1.f };
873 float c0[3] = { 0.f, 0.f, 0.f };
874 float p0[3] = { 0.f, 0.f, 0.f };
875 float c1[3] = { 0.f, 0.f, 0.f };
876 float p1[3] = { 0.f, 0.f, 0.f };
879 z[0] = fsinf(V_RAD(view_a));
880 z[2] = fcosf(V_RAD(view_a));
886 /* k = 0.0 view is at the ball. */
890 v_cpy(c0, fp->uv[0].p);
891 v_cpy(p0, fp->uv[0].p);
894 v_mad(p0, p0, y, view_dp);
895 v_mad(p0, p0, z, view_dz);
896 v_mad(c0, c0, y, view_dc);
898 /* k = +1.0 view is s_view 0 */
900 if (k >= 0 && fp->wc > 0)
902 v_cpy(p1, fp->wv[0].p);
903 v_cpy(c1, fp->wv[0].q);
906 /* k = -1.0 view is s_view 1 */
908 if (k <= 0 && fp->wc > 1)
910 v_cpy(p1, fp->wv[1].p);
911 v_cpy(c1, fp->wv[1].q);
914 /* Interpolate the views. */
917 v_mad(view_p, p0, v, k * k);
920 v_mad(view_c, c0, v, k * k);
922 /* Orthonormalize the view basis. */
924 v_sub(view_e[2], view_p, view_c);
925 v_crs(view_e[0], view_e[1], view_e[2]);
926 v_crs(view_e[2], view_e[0], view_e[1]);
927 v_nrm(view_e[0], view_e[0]);
928 v_nrm(view_e[2], view_e[2]);
931 void game_look(float phi, float theta)
933 view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
934 view_c[1] = view_p[1] + fsinf(V_RAD(phi));
935 view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
938 /*---------------------------------------------------------------------------*/
940 void game_kill_fade(void)
946 void game_step_fade(float dt)
948 if ((fade_k < 1.0f && fade_d > 0.0f) ||
949 (fade_k > 0.0f && fade_d < 0.0f))
950 fade_k += fade_d * dt;
964 void game_fade(float d)
969 /*---------------------------------------------------------------------------*/
971 int put_game_state(FILE *fout)
975 /* Write the view and tilt state. */
977 put_float(fout, &game_rx);
978 put_float(fout, &game_rz);
979 put_array(fout, view_c, 3);
980 put_array(fout, view_p, 3);
982 /* Write the game simulation state. */
984 put_file_state(fout, &file);
991 int get_game_state(FILE *fin)
995 /* Read the view and tilt state. */
997 get_float(fin, &game_rx);
998 get_float(fin, &game_rz);
999 get_array(fin, view_c, 3);
1000 get_array(fin, view_p, 3);
1002 /* Read the game simulation state. */
1004 get_file_state(fin, &file);
1006 return (feof(fin) ? 0 : 1);
1011 /*---------------------------------------------------------------------------*/