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.
30 /*---------------------------------------------------------------------------*/
32 static int game_state = 0;
34 static struct s_file file;
35 static struct s_file back;
37 static float clock = 0.f; /* Clock time */
38 static int clock_down = 1; /* Clock go up or down? */
40 static float game_ix; /* Input rotation about X axis */
41 static float game_iz; /* Input rotation about Z axis */
42 static float game_rx; /* Floor rotation about X axis */
43 static float game_rz; /* Floor rotation about Z axis */
45 static float view_a; /* Ideal view rotation about Y axis */
46 static float view_ry; /* Angular velocity about Y axis */
47 static float view_dc; /* Ideal view distance above ball */
48 static float view_dp; /* Ideal view distance above ball */
49 static float view_dz; /* Ideal view distance behind ball */
50 static float view_fov; /* Field of view */
52 static float view_c[3]; /* Current view center */
53 static float view_v[3]; /* Current view vector */
54 static float view_p[3]; /* Current view position */
55 static float view_e[3][3]; /* Current view orientation */
58 static int coins = 0; /* Collected coins */
59 static int goal_c = 0; /* Goal coins remaining (0 = open) */
60 static float goal_k = 0; /* Goal animation */
61 static int swch_e = 1; /* Switching enabled flag */
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 */
69 /*---------------------------------------------------------------------------*/
71 static void view_init(void)
76 view_fov = (float) config_get_d(CONFIG_VIEW_FOV);
77 view_dp = (float) config_get_d(CONFIG_VIEW_DP) / 100.0f;
78 view_dc = (float) config_get_d(CONFIG_VIEW_DC) / 100.0f;
79 view_dz = (float) config_get_d(CONFIG_VIEW_DZ) / 100.0f;
101 int game_init(const char *file_name,
102 const char *back_name,
103 const char *grad_name, int t, int g)
105 clock = (float) t / 100.f;
106 clock_down = (t > 0);
117 /* Initialize jump and goal states. */
123 goal_k = (g == 0) ? 1.0f : 0.0f;
125 /* Initialise the level, background, particles, fade, and view. */
130 part_reset(GOAL_HEIGHT);
132 back_init(grad_name, config_get_d(CONFIG_GEOMETRY));
134 if (sol_load(&back, config_data(back_name),
135 config_get_d(CONFIG_TEXTURES), 0) &&
136 sol_load(&file, file_name,
137 config_get_d(CONFIG_TEXTURES), config_get_d(CONFIG_SHADOW)))
138 return (game_state = 1);
140 return (game_state = 0);
154 /*---------------------------------------------------------------------------*/
158 return (int) (clock * 100.f);
171 char *curr_intro(void)
173 return (file.ac > 0) ? file.av : NULL;
176 /*---------------------------------------------------------------------------*/
178 static void game_draw_balls(const struct s_file *fp)
180 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
183 m_basis(M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
187 glTranslatef(fp->uv[0].p[0],
188 fp->uv[0].p[1] + BALL_FUDGE,
191 glScalef(fp->uv[0].r,
202 static void game_draw_coins(const struct s_file *fp)
204 float r = 360.f * SDL_GetTicks() / 1000.f;
209 for (ci = 0; ci < fp->cc; ci++)
210 if (fp->cv[ci].n > 0)
214 glTranslatef(fp->cv[ci].p[0],
217 glRotatef(r, 0.0f, 1.0f, 0.0f);
218 coin_draw(fp->cv[ci].n, r);
226 static void game_draw_goals(const struct s_file *fp, float rx, float ry)
231 for (zi = 0; zi < fp->zc; zi++)
235 glTranslatef(fp->zv[zi].p[0],
239 part_draw_goal(rx, ry, fp->zv[zi].r, goal_k);
241 glScalef(fp->zv[zi].r, goal_k, fp->zv[zi].r);
248 static void game_draw_jumps(const struct s_file *fp)
252 for (ji = 0; ji < fp->jc; ji++)
256 glTranslatef(fp->jv[ji].p[0],
260 glScalef(fp->jv[ji].r, 1.f, fp->jv[ji].r);
267 static void game_draw_swchs(const struct s_file *fp)
271 for (xi = 0; xi < fp->xc; xi++)
275 glTranslatef(fp->xv[xi].p[0],
279 glScalef(fp->xv[xi].r, 1.f, fp->xv[xi].r);
280 swch_draw(fp->xv[xi].f);
286 /*---------------------------------------------------------------------------*/
288 static void game_refl_all(int s)
290 const float *ball_p = file.uv->p;
294 /* Rotate the environment about the position of the ball. */
296 glTranslatef(+ball_p[0], +ball_p[1], +ball_p[2]);
297 glRotatef(-game_rz, view_e[2][0], view_e[2][1], view_e[2][2]);
298 glRotatef(-game_rx, view_e[0][0], view_e[0][1], view_e[0][2]);
299 glTranslatef(-ball_p[0], -ball_p[1], -ball_p[2]);
301 /* Draw the floor. */
308 /*---------------------------------------------------------------------------*/
310 static void game_draw_light(void)
312 const float light_p[2][4] = {
313 { -8.0f, +32.0f, -8.0f, 1.0f },
314 { +8.0f, +32.0f, +8.0f, 1.0f },
316 const float light_c[2][4] = {
317 { 1.0f, 0.8f, 0.8f, 1.0f },
318 { 0.8f, 1.0f, 0.8f, 1.0f },
321 /* Configure the lighting. */
324 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
325 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
326 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
329 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
330 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
331 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
334 static void game_draw_back(int pose, int d, const float p[3])
336 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
337 float t = SDL_GetTicks() / 1000.f + 120.0f;
343 glRotatef(game_rz * 2, view_e[2][0], view_e[2][1], view_e[2][2]);
344 glRotatef(game_rx * 2, view_e[0][0], view_e[0][1], view_e[0][2]);
347 glTranslatef(p[0], p[1], p[2]);
350 if (config_get_d(CONFIG_BACKGROUND))
352 /* Draw all background layers back to front. */
354 sol_back(&back, BACK_DIST, FAR_DIST, t);
356 sol_back(&back, 0, BACK_DIST, t);
358 /* Draw all foreground geometry in the background file. */
367 static void game_draw_fore(int pose, float rx, float ry, int d, const float p[3])
369 const float *ball_p = file.uv->p;
370 const float ball_r = file.uv->r;
372 glPushAttrib(GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
376 /* Rotate the environment about the position of the ball. */
378 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
379 glRotatef(-game_rz * d, view_e[2][0], view_e[2][1], view_e[2][2]);
380 glRotatef(-game_rx * d, view_e[0][0], view_e[0][1], view_e[0][2]);
381 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
392 glEnable(GL_CLIP_PLANE0);
393 glClipPlane(GL_CLIP_PLANE0, e);
396 /* Draw the floor. */
400 if (config_get_d(CONFIG_SHADOW))
402 shad_draw_set(ball_p, ball_r);
407 /* Draw the game elements. */
410 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
414 part_draw_coin(-rx * d, -ry);
415 game_draw_coins(&file);
416 game_draw_balls(&file);
418 game_draw_goals(&file, -rx * d, -ry);
419 game_draw_jumps(&file);
420 game_draw_swchs(&file);
422 glDisable(GL_CLIP_PLANE0);
429 void game_draw(int pose, float st)
431 float fov = view_fov;
433 if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
437 config_push_persp(fov, 0.1f, FAR_DIST);
448 /* Compute and apply the view. */
450 v_sub(v, view_c, view_p);
452 rx = V_DEG(fatan2f(-v[1], fsqrtf(v[0] * v[0] + v[2] * v[2])));
453 ry = V_DEG(fatan2f(+v[0], -v[2])) + st;
455 glTranslatef(0.f, 0.f, -v_len(v));
456 glRotatef(rx, 1.f, 0.f, 0.f);
457 glRotatef(ry, 0.f, 1.f, 0.f);
458 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
460 if (config_get_d(CONFIG_REFLECTION))
462 /* Draw the mirror only into the stencil buffer. */
464 glDisable(GL_DEPTH_TEST);
465 glEnable(GL_STENCIL_TEST);
466 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
467 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
468 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
472 /* Draw the scene reflected into color and depth buffers. */
474 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
475 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
476 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
477 glEnable(GL_DEPTH_TEST);
482 glScalef(+1.f, -1.f, +1.f);
485 game_draw_back(pose, -1, pdn);
486 game_draw_fore(pose, rx, ry, -1, pdn);
491 glDisable(GL_STENCIL_TEST);
494 /* Draw the scene normally. */
497 game_refl_all(pose ? 0 : config_get_d(CONFIG_SHADOW));
498 game_draw_back(pose, +1, pup);
499 game_draw_fore(pose, rx, ry, +1, pup);
504 /* Draw the fade overlay. */
510 /*---------------------------------------------------------------------------*/
512 static void game_update_grav(float h[3], const float g[3])
514 struct s_file *fp = &file;
517 float y[3] = { 0.f, 1.f, 0.f };
523 /* Compute the gravity vector from the given world rotations. */
525 v_sub(z, view_p, fp->uv->p);
531 m_rot (Z, z, V_RAD(game_rz));
532 m_rot (X, x, V_RAD(game_rx));
537 static void game_update_view(float dt)
539 float dc = view_dc * (jump_b ? 2.0f * fabsf(jump_dt - 0.5f) : 1.0f);
540 float dx = view_ry * dt * 5.0f;
543 view_a += view_ry * dt * 90.f;
545 /* Center the view about the ball. */
547 v_cpy(view_c, file.uv->p);
548 v_inv(view_v, file.uv->v);
550 switch (config_get_d(CONFIG_CAMERA))
552 case 1: /* Camera 1: Viewpoint chases the ball position. */
554 v_sub(view_e[2], view_p, view_c);
557 case 2: /* Camera 2: View vector is given by view angle. */
559 view_e[2][0] = fsinf(V_RAD(view_a));
561 view_e[2][2] = fcosf(V_RAD(view_a));
567 default: /* Default: View vector approaches the ball velocity vector. */
569 k = v_dot(view_v, view_v);
571 v_sub(view_e[2], view_p, view_c);
572 v_mad(view_e[2], view_e[2], view_v, k * dt / 4);
577 /* Orthonormalize the basis of the view in its new position. */
579 v_crs(view_e[0], view_e[1], view_e[2]);
580 v_crs(view_e[2], view_e[0], view_e[1]);
581 v_nrm(view_e[0], view_e[0]);
582 v_nrm(view_e[2], view_e[2]);
584 /* Compute the new view position. */
586 k = 1.0f + v_dot(view_e[2], view_v) / 10.0f;
588 view_k = view_k + (k - view_k) * dt;
590 if (view_k < 0.5) view_k = 0.5;
592 v_cpy(view_p, file.uv->p);
593 v_mad(view_p, view_p, view_e[0], dx * view_k);
594 v_mad(view_p, view_p, view_e[1], view_dp * view_k);
595 v_mad(view_p, view_p, view_e[2], view_dz * view_k);
597 /* Compute the new view center. */
599 v_cpy(view_c, file.uv->p);
600 v_mad(view_c, view_c, view_e[1], dc);
602 /* Note the current view angle. */
604 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
607 static void game_update_time(float dt, int b)
609 if (goal_c == 0 && goal_k < 1.0f)
612 /* The ticking clock. */
627 static int game_update_state(int bt)
629 struct s_file *fp = &file;
634 /* Test for a coin grab. */
636 if (bt && (n = sol_coin_test(fp, p, COIN_RADIUS)) > 0)
642 /* Check for goal open. */
648 audio_play(AUD_SWITCH, 1.f);
652 audio_play(AUD_COIN, 1.f);
655 audio_play(AUD_COIN, 1.f);
658 /* Test for a switch. */
660 if ((swch_e = sol_swch_test(fp, swch_e, 0)) != e && e)
661 audio_play(AUD_SWITCH, 1.f);
663 /* Test for a jump. */
665 if (jump_e == 1 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 1)
671 audio_play(AUD_JUMP, 1.f);
673 if (jump_e == 0 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 0)
676 /* Test for a goal. */
678 if (bt && goal_c == 0 && sol_goal_test(fp, p, 0))
680 audio_play(AUD_GOAL, 1.0f);
684 /* Test for time-out. */
686 if (bt && clock_down && clock <= 0.f)
689 /* Test for fall-out. */
691 if (bt && fp->uv[0].p[1] < fp->vv[0].p[1])
698 * On most hardware, rendering requires much more computing power than
699 * physics. Since physics takes less time than graphics, it make sense to
700 * detach the physics update time step from the graphics frame rate. By
701 * performing multiple physics updates for each graphics update, we get away
702 * with higher quality physics with little impact on overall performance.
704 * Toward this end, we establish a baseline maximum physics time step. If
705 * the measured frame time exceeds this maximum, we cut the time step in
706 * half, and do two updates. If THIS time step exceeds the maximum, we do
707 * four updates. And so on. In this way, the physics system is allowed to
708 * seek an optimal update rate independant of, yet in integral sync with, the
709 * graphics frame rate.
712 int game_step(const float g[3], float dt, int bt)
714 struct s_file *fp = &file;
726 /* Smooth jittery or discontinuous input. */
730 game_rx += (game_ix - game_rx) * t / RESPONSE;
731 game_rz += (game_iz - game_rz) * t / RESPONSE;
739 game_update_grav(h, g);
750 fp->uv[0].p[0] = jump_p[0];
751 fp->uv[0].p[1] = jump_p[1];
752 fp->uv[0].p[2] = jump_p[2];
761 while (t > MAX_DT && n < MAX_DN)
767 for (i = 0; i < n; i++)
768 if (b < (d = sol_step(fp, h, t, 0, NULL)))
771 /* Mix the sound of a ball bounce. */
774 audio_play(AUD_BUMP, (b - 0.5f) * 2.0f);
778 game_update_view(dt);
779 game_update_time(dt, bt);
781 return game_update_state(bt);
786 /*---------------------------------------------------------------------------*/
788 void game_no_aa(void)
790 float max = game_ix * game_ix + game_iz * game_iz;
791 if (max > ANGLE_BOUND * ANGLE_BOUND)
793 max = ANGLE_BOUND / sqrt(max);
799 void game_set_x(int k)
801 game_ix = -(ANGLE_BOUND) * k / JOY_MAX;
807 void game_set_z(int k)
809 game_iz = +ANGLE_BOUND * k / JOY_MAX;
815 void game_set_pos(int x, int y)
817 game_ix += 40.f * y / config_get_d(CONFIG_MOUSE_SENSE);
818 game_iz += 40.f * x / config_get_d(CONFIG_MOUSE_SENSE);
823 if (game_ix > +ANGLE_BOUND) game_ix = +ANGLE_BOUND;
824 if (game_ix < -ANGLE_BOUND) game_ix = -ANGLE_BOUND;
825 if (game_iz > +ANGLE_BOUND) game_iz = +ANGLE_BOUND;
826 if (game_iz < -ANGLE_BOUND) game_iz = -ANGLE_BOUND;
830 void game_set_rot(float r)
835 /*---------------------------------------------------------------------------*/
837 void game_set_fly(float k)
839 struct s_file *fp = &file;
841 float x[3] = { 1.f, 0.f, 0.f };
842 float y[3] = { 0.f, 1.f, 0.f };
843 float z[3] = { 0.f, 0.f, 1.f };
844 float c0[3] = { 0.f, 0.f, 0.f };
845 float p0[3] = { 0.f, 0.f, 0.f };
846 float c1[3] = { 0.f, 0.f, 0.f };
847 float p1[3] = { 0.f, 0.f, 0.f };
854 /* k = 0.0 view is at the ball. */
858 v_cpy(c0, fp->uv[0].p);
859 v_cpy(p0, fp->uv[0].p);
862 v_mad(p0, p0, y, view_dp);
863 v_mad(p0, p0, z, view_dz);
864 v_mad(c0, c0, y, view_dc);
866 /* k = +1.0 view is s_view 0 */
868 if (k >= 0 && fp->wc > 0)
870 v_cpy(p1, fp->wv[0].p);
871 v_cpy(c1, fp->wv[0].q);
874 /* k = -1.0 view is s_view 1 */
876 if (k <= 0 && fp->wc > 1)
878 v_cpy(p1, fp->wv[1].p);
879 v_cpy(c1, fp->wv[1].q);
882 /* Interpolate the views. */
885 v_mad(view_p, p0, v, k * k);
888 v_mad(view_c, c0, v, k * k);
890 /* Orthonormalize the view basis. */
892 v_sub(view_e[2], view_p, view_c);
893 v_crs(view_e[0], view_e[1], view_e[2]);
894 v_crs(view_e[2], view_e[0], view_e[1]);
895 v_nrm(view_e[0], view_e[0]);
896 v_nrm(view_e[2], view_e[2]);
899 void game_look(float phi, float theta)
901 view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
902 view_c[1] = view_p[1] + fsinf(V_RAD(phi));
903 view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
906 /*---------------------------------------------------------------------------*/
908 void game_kill_fade(void)
914 void game_step_fade(float dt)
916 if ((fade_k < 1.0f && fade_d > 0.0f) ||
917 (fade_k > 0.0f && fade_d < 0.0f))
918 fade_k += fade_d * dt;
932 void game_fade(float d)
937 /*---------------------------------------------------------------------------*/
939 int put_game_state(FILE *fout)
943 /* Write the view and tilt state. */
945 put_float(fout, &game_rx);
946 put_float(fout, &game_rz);
947 put_array(fout, view_c, 3);
948 put_array(fout, view_p, 3);
950 /* Write the game simulation state. */
952 put_file_state(fout, &file);
959 int get_game_state(FILE *fin)
963 /* Read the view and tilt state. */
965 get_float(fin, &game_rx);
966 get_float(fin, &game_rz);
967 get_array(fin, view_c, 3);
968 get_array(fin, view_p, 3);
970 /* Read the game simulation state. */
972 get_file_state(fin, &file);
974 return (feof(fin) ? 0 : 1);
979 /*---------------------------------------------------------------------------*/