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.
32 /*---------------------------------------------------------------------------*/
34 static int game_state = 0;
36 static struct s_file file;
37 static struct s_file back;
39 static float clock = 0.f; /* Clock time */
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 goal_e = 0; /* Goal enabled flag */
60 static float goal_k = 0; /* Goal animation */
61 static int goal_s = 0; /* Goal reached flag */
62 static int swch_e = 1; /* Switching enabled flag */
63 static int jump_e = 1; /* Jumping enabled flag */
64 static int jump_b = 0; /* Jump-in-progress flag */
65 static float jump_dt; /* Jump duration */
66 static float jump_p[3]; /* Jump destination */
67 static float fade_k = 0.0; /* Fade in/out level */
68 static float fade_d = 0.0; /* Fade in/out direction */
70 /*---------------------------------------------------------------------------*/
72 static void view_init(void)
77 view_fov = (float) config_get_d(CONFIG_VIEW_FOV);
78 view_dp = (float) config_get_d(CONFIG_VIEW_DP) / 100.0f;
79 view_dc = (float) config_get_d(CONFIG_VIEW_DC) / 100.0f;
80 view_dz = (float) config_get_d(CONFIG_VIEW_DZ) / 100.0f;
102 int game_init(const char *file_name,
103 const char *back_name,
104 const char *grad_name, int t, int e)
106 if (level_mode() == MODE_FREE)
109 clock = (float) t / 100.f;
119 /* Initialize jump and goal states. */
125 goal_k = e ? 1.0f : 0.0f;
134 /* Initialise the level, background, particles, fade, and view. */
139 part_reset(GOAL_HEIGHT);
141 back_init(grad_name, config_get_d(CONFIG_GEOMETRY));
143 if (sol_load(&back, config_data(back_name),
144 config_get_d(CONFIG_TEXTURES), 0) &&
145 sol_load(&file, config_data(file_name),
146 config_get_d(CONFIG_TEXTURES), config_get_d(CONFIG_SHADOW)))
147 return (game_state = 1);
149 return (game_state = 0);
163 /*---------------------------------------------------------------------------*/
167 return (int) (clock * 100.f);
170 char *curr_intro(void)
172 return (file.ac > 0) ? file.av : NULL;
175 /*---------------------------------------------------------------------------*/
177 static void game_draw_balls(const struct s_file *fp)
179 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
182 m_basis(M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
186 glTranslatef(fp->uv[0].p[0],
187 fp->uv[0].p[1] + BALL_FUDGE,
190 glScalef(fp->uv[0].r,
201 static void game_draw_coins(const struct s_file *fp)
203 float r = 360.f * SDL_GetTicks() / 1000.f;
208 for (ci = 0; ci < fp->cc; ci++)
209 if (fp->cv[ci].n > 0)
213 glTranslatef(fp->cv[ci].p[0],
216 glRotatef(r, 0.0f, 1.0f, 0.0f);
217 coin_draw(fp->cv[ci].n, r);
225 static void game_draw_goals(const struct s_file *fp, float rx, float ry)
230 for (zi = 0; zi < fp->zc; zi++)
234 glTranslatef(fp->zv[zi].p[0],
238 part_draw_goal(rx, ry, fp->zv[zi].r, goal_k);
240 glScalef(fp->zv[zi].r, goal_k, fp->zv[zi].r);
247 static void game_draw_jumps(const struct s_file *fp)
251 for (ji = 0; ji < fp->jc; ji++)
255 glTranslatef(fp->jv[ji].p[0],
259 glScalef(fp->jv[ji].r, 1.f, fp->jv[ji].r);
266 static void game_draw_swchs(const struct s_file *fp)
270 for (xi = 0; xi < fp->xc; xi++)
274 glTranslatef(fp->xv[xi].p[0],
278 glScalef(fp->xv[xi].r, 1.f, fp->xv[xi].r);
279 swch_draw(fp->xv[xi].f);
285 /*---------------------------------------------------------------------------*/
287 static void game_refl_all(int s)
289 const float *ball_p = file.uv->p;
293 /* Rotate the environment about the position of the ball. */
295 glTranslatef(+ball_p[0], +ball_p[1], +ball_p[2]);
296 glRotatef(-game_rz, view_e[2][0], view_e[2][1], view_e[2][2]);
297 glRotatef(-game_rx, view_e[0][0], view_e[0][1], view_e[0][2]);
298 glTranslatef(-ball_p[0], -ball_p[1], -ball_p[2]);
300 /* Draw the floor. */
307 /*---------------------------------------------------------------------------*/
309 static void game_draw_light(void)
311 const float light_p[2][4] = {
312 { -8.0f, +32.0f, -8.0f, 1.0f },
313 { +8.0f, +32.0f, +8.0f, 1.0f },
315 const float light_c[2][4] = {
316 { 1.0f, 0.8f, 0.8f, 1.0f },
317 { 0.8f, 1.0f, 0.8f, 1.0f },
320 /* Configure the lighting. */
323 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
324 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
325 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
328 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
329 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
330 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
333 static void game_draw_back(int pose, int d, const float p[3])
335 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
336 float t = SDL_GetTicks() / 1000.f + 120.0f;
342 glRotatef(game_rz * 2, view_e[2][0], view_e[2][1], view_e[2][2]);
343 glRotatef(game_rx * 2, view_e[0][0], view_e[0][1], view_e[0][2]);
346 glTranslatef(p[0], p[1], p[2]);
349 if (config_get_d(CONFIG_BACKGROUND))
351 /* Draw all background layers back to front. */
353 sol_back(&back, BACK_DIST, FAR_DIST, t);
355 sol_back(&back, 0, BACK_DIST, t);
357 /* Draw all foreground geometry in the background file. */
366 static void game_draw_fore(int pose, float rx, float ry, int d, const float p[3])
368 const float *ball_p = file.uv->p;
369 const float ball_r = file.uv->r;
371 glPushAttrib(GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
375 /* Rotate the environment about the position of the ball. */
377 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
378 glRotatef(-game_rz * d, view_e[2][0], view_e[2][1], view_e[2][2]);
379 glRotatef(-game_rx * d, view_e[0][0], view_e[0][1], view_e[0][2]);
380 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
391 glEnable(GL_CLIP_PLANE0);
392 glClipPlane(GL_CLIP_PLANE0, e);
395 /* Draw the floor. */
399 if (config_get_d(CONFIG_SHADOW))
401 shad_draw_set(ball_p, ball_r);
406 /* Draw the game elements. */
409 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
413 part_draw_coin(-rx * d, -ry);
414 game_draw_coins(&file);
415 game_draw_balls(&file);
417 game_draw_goals(&file, -rx * d, -ry);
418 game_draw_jumps(&file);
419 game_draw_swchs(&file);
421 glDisable(GL_CLIP_PLANE0);
428 void game_draw(int pose, float st)
430 float fov = view_fov;
432 if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
436 config_push_persp(fov, 0.1f, FAR_DIST);
447 /* Compute and apply the view. */
449 v_sub(v, view_c, view_p);
451 rx = V_DEG(fatan2f(-v[1], fsqrtf(v[0] * v[0] + v[2] * v[2])));
452 ry = V_DEG(fatan2f(+v[0], -v[2])) + st;
454 glTranslatef(0.f, 0.f, -v_len(v));
455 glRotatef(rx, 1.f, 0.f, 0.f);
456 glRotatef(ry, 0.f, 1.f, 0.f);
457 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
459 if (config_get_d(CONFIG_REFLECTION))
461 /* Draw the mirror only into the stencil buffer. */
463 glDisable(GL_DEPTH_TEST);
464 glEnable(GL_STENCIL_TEST);
465 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
466 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
467 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
471 /* Draw the scene reflected into color and depth buffers. */
473 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
474 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
475 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
476 glEnable(GL_DEPTH_TEST);
481 glScalef(+1.f, -1.f, +1.f);
484 game_draw_back(pose, -1, pdn);
485 game_draw_fore(pose, rx, ry, -1, pdn);
490 glDisable(GL_STENCIL_TEST);
493 /* Draw the scene normally. */
496 game_refl_all(pose ? 0 : config_get_d(CONFIG_SHADOW));
497 game_draw_back(pose, +1, pup);
498 game_draw_fore(pose, rx, ry, +1, pup);
503 /* Draw the fade overlay. */
509 /*---------------------------------------------------------------------------*/
511 static void game_update_grav(float h[3], const float g[3])
513 struct s_file *fp = &file;
516 float y[3] = { 0.f, 1.f, 0.f };
522 /* Compute the gravity vector from the given world rotations. */
524 v_sub(z, view_p, fp->uv->p);
530 m_rot (Z, z, V_RAD(game_rz));
531 m_rot (X, x, V_RAD(game_rx));
536 static void game_update_view(float dt)
538 float dc = view_dc * (jump_b ? 2.0f * fabsf(jump_dt - 0.5f) : 1.0f);
539 float dx = view_ry * dt * 5.0f;
542 view_a += view_ry * dt * 90.f;
544 /* Center the view about the ball. */
546 v_cpy(view_c, file.uv->p);
547 v_inv(view_v, file.uv->v);
549 switch (config_get_d(CONFIG_CAMERA))
551 case 1: /* Camera 1: Viewpoint chases the ball position. */
553 v_sub(view_e[2], view_p, view_c);
556 case 2: /* Camera 2: View vector is given by view angle. */
558 view_e[2][0] = fsinf(V_RAD(view_a));
560 view_e[2][2] = fcosf(V_RAD(view_a));
566 default: /* Default: View vector approaches the ball velocity vector. */
568 k = v_dot(view_v, view_v);
570 v_sub(view_e[2], view_p, view_c);
571 v_mad(view_e[2], view_e[2], view_v, k * dt / 4);
576 /* Orthonormalize the basis of the view in its new position. */
578 v_crs(view_e[0], view_e[1], view_e[2]);
579 v_crs(view_e[2], view_e[0], view_e[1]);
580 v_nrm(view_e[0], view_e[0]);
581 v_nrm(view_e[2], view_e[2]);
583 /* Compute the new view position. */
585 k = 1.0f + v_dot(view_e[2], view_v) / 10.0f;
587 view_k = view_k + (k - view_k) * dt;
589 if (view_k < 0.5) view_k = 0.5;
591 v_cpy(view_p, file.uv->p);
592 v_mad(view_p, view_p, view_e[0], dx * view_k);
593 v_mad(view_p, view_p, view_e[1], view_dp * view_k);
594 v_mad(view_p, view_p, view_e[2], view_dz * view_k);
596 /* Compute the new view center. */
598 v_cpy(view_c, file.uv->p);
599 v_mad(view_c, view_c, view_e[1], dc);
601 /* Note the current view angle. */
603 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
606 static void game_update_time(float dt, int b)
608 int tick = (int) floor(clock);
609 int tock = (int) floor(clock * 2);
611 if (goal_e && goal_k < 1.0f)
614 /* The ticking clock. */
625 if (0 < tick && tick <= 10 && tick == (int) ceil(clock))
627 audio_play(AUD_TICK, 1.f);
628 hud_time_pulse(1.50);
630 else if (0 < tock && tock <= 10 && tock == (int) ceil(clock * 2))
632 audio_play(AUD_TOCK, 1.f);
633 hud_time_pulse(1.25);
638 static int game_update_state(void)
640 struct s_file *fp = &file;
645 /* Test for a coin grab and a possible 1UP. */
647 if ((n = sol_coin_test(fp, p, COIN_RADIUS)) > 0)
656 /* Test for a switch. */
658 if ((swch_e = sol_swch_test(fp, swch_e, 0)) != e && e)
659 audio_play(AUD_SWITCH, 1.f);
661 /* Test for a jump. */
663 if (jump_e == 1 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 1)
669 audio_play(AUD_JUMP, 1.f);
671 if (jump_e == 0 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 0)
674 /* Test for a goal. */
676 if (goal_e && sol_goal_test(fp, p, 0))
681 audio_play(AUD_GOAL, 1.0f);
686 /* Test for time-out. */
691 /* Test for fall-out. */
693 if (fp->uv[0].p[1] < fp->vv[0].p[1])
700 * On most hardware, rendering requires much more computing power than
701 * physics. Since physics takes less time than graphics, it make sense to
702 * detach the physics update time step from the graphics frame rate. By
703 * performing multiple physics updates for each graphics update, we get away
704 * with higher quality physics with little impact on overall performance.
706 * Toward this end, we establish a baseline maximum physics time step. If
707 * the measured frame time exceeds this maximum, we cut the time step in
708 * half, and do two updates. If THIS time step exceeds the maximum, we do
709 * four updates. And so on. In this way, the physics system is allowed to
710 * seek an optimal update rate independant of, yet in integral sync with, the
711 * graphics frame rate.
714 int game_step(const float g[3], float dt, int bt)
716 struct s_file *fp = &file;
728 /* Smooth jittery or discontinuous input. */
732 game_rx += (game_ix - game_rx) * t / RESPONSE;
733 game_rz += (game_iz - game_rz) * t / RESPONSE;
741 game_update_grav(h, g);
752 fp->uv[0].p[0] = jump_p[0];
753 fp->uv[0].p[1] = jump_p[1];
754 fp->uv[0].p[2] = jump_p[2];
763 while (t > MAX_DT && n < MAX_DN)
769 for (i = 0; i < n; i++)
770 if (b < (d = sol_step(fp, h, t, 0, NULL)))
773 /* Mix the sound of a ball bounce. */
776 audio_play(AUD_BUMP, (b - 0.5f) * 2.0f);
780 game_update_view(dt);
781 game_update_time(dt, bt);
783 return game_update_state();
788 /*---------------------------------------------------------------------------*/
790 void game_set_x(int k)
792 game_ix = -20.f * k / JOY_MAX;
795 void game_set_z(int k)
797 game_iz = +20.f * k / JOY_MAX;
800 void game_set_pos(int x, int y)
804 game_ix += 40.f * y / config_get_d(CONFIG_MOUSE_SENSE);
805 game_iz += 40.f * x / config_get_d(CONFIG_MOUSE_SENSE);
807 if (game_ix > +bound) game_ix = +bound;
808 if (game_ix < -bound) game_ix = -bound;
809 if (game_iz > +bound) game_iz = +bound;
810 if (game_iz < -bound) game_iz = -bound;
813 void game_set_rot(float r)
818 /*---------------------------------------------------------------------------*/
820 void game_set_fly(float k)
822 struct s_file *fp = &file;
824 float x[3] = { 1.f, 0.f, 0.f };
825 float y[3] = { 0.f, 1.f, 0.f };
826 float z[3] = { 0.f, 0.f, 1.f };
827 float c0[3] = { 0.f, 0.f, 0.f };
828 float p0[3] = { 0.f, 0.f, 0.f };
829 float c1[3] = { 0.f, 0.f, 0.f };
830 float p1[3] = { 0.f, 0.f, 0.f };
837 /* k = 0.0 view is at the ball. */
841 v_cpy(c0, fp->uv[0].p);
842 v_cpy(p0, fp->uv[0].p);
845 v_mad(p0, p0, y, view_dp);
846 v_mad(p0, p0, z, view_dz);
847 v_mad(c0, c0, y, view_dc);
849 /* k = +1.0 view is s_view 0 */
851 if (k >= 0 && fp->wc > 0)
853 v_cpy(p1, fp->wv[0].p);
854 v_cpy(c1, fp->wv[0].q);
857 /* k = -1.0 view is s_view 1 */
859 if (k <= 0 && fp->wc > 1)
861 v_cpy(p1, fp->wv[1].p);
862 v_cpy(c1, fp->wv[1].q);
865 /* Interpolate the views. */
868 v_mad(view_p, p0, v, k * k);
871 v_mad(view_c, c0, v, k * k);
873 /* Orthonormalize the view basis. */
875 v_sub(view_e[2], view_p, view_c);
876 v_crs(view_e[0], view_e[1], view_e[2]);
877 v_crs(view_e[2], view_e[0], view_e[1]);
878 v_nrm(view_e[0], view_e[0]);
879 v_nrm(view_e[2], view_e[2]);
882 void game_look(float phi, float theta)
884 view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
885 view_c[1] = view_p[1] + fsinf(V_RAD(phi));
886 view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
889 /*---------------------------------------------------------------------------*/
891 void game_kill_fade(void)
897 void game_step_fade(float dt)
899 if ((fade_k < 1.0f && fade_d > 0.0f) ||
900 (fade_k > 0.0f && fade_d < 0.0f))
901 fade_k += fade_d * dt;
915 void game_fade(float d)
920 /*---------------------------------------------------------------------------*/
922 int put_game_state(FILE *fout)
926 /* Write the view and tilt state. */
928 put_float(fout, &game_rx);
929 put_float(fout, &game_rz);
930 put_array(fout, view_c, 3);
931 put_array(fout, view_p, 3);
933 /* Write the game simulation state. */
935 put_file_state(fout, &file);
942 int get_game_state(FILE *fin)
946 /* Read the view and tilt state. */
948 get_float(fin, &game_rx);
949 get_float(fin, &game_rz);
950 get_array(fin, view_c, 3);
951 get_array(fin, view_p, 3);
953 /* Read the game simulation state. */
955 get_file_state(fin, &file);
957 return (feof(fin) ? 0 : 1);
962 /*---------------------------------------------------------------------------*/