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 static int grow = 0; /* Should the ball be changing size? */
72 static float grow_orig = 0; /* the original ball size */
73 static float grow_goal = 0; /* how big or small to get! */
74 static float grow_t = 0.0; /* timer for the ball to grow... */
75 static float grow_strt = 0; /* starting value for growth */
76 static int got_orig = 0; /* Do we know original ball size? */
78 #define GROW_TIME 0.5f /* sec for the ball to get to size. */
79 #define GROW_BIG 1.5f /* large factor */
80 #define GROW_SMALL 0.5f /* small factor */
82 /*---------------------------------------------------------------------------*/
84 static void grow_set(const struct s_file *fp, int type)
88 grow_orig = fp->uv->r;
89 grow_goal = grow_orig;
90 grow_strt = grow_orig;
94 if (type == ITEM_SHRINK)
96 if (grow_goal == grow_orig * GROW_SMALL)
98 else if (grow_goal == grow_orig * GROW_BIG)
101 grow_goal = grow_orig;
105 grow_goal = grow_orig * GROW_SMALL;
109 if (type == ITEM_GROW)
111 if (grow_goal == grow_orig * GROW_BIG)
113 else if (grow_goal == grow_orig * GROW_SMALL)
116 grow_goal = grow_orig;
120 grow_goal = grow_orig * GROW_BIG;
128 grow_strt = fp->uv->r;
132 static void grow_ball(const struct s_file *fp, float dt)
136 /* Calculate new size based on how long since you touched the coin... */
140 if (grow_t >= GROW_TIME)
146 dr = grow_strt + ((grow_goal-grow_strt) * (1.0f / (GROW_TIME / grow_t)));
148 /* No sinking through the floor! Keeps ball's bottom constant. */
149 fp->uv->p[1] += (dr - fp->uv->r);
153 static void view_init(void)
158 view_fov = (float) config_get_d(CONFIG_VIEW_FOV);
159 view_dp = (float) config_get_d(CONFIG_VIEW_DP) / 100.0f;
160 view_dc = (float) config_get_d(CONFIG_VIEW_DC) / 100.0f;
161 view_dz = (float) config_get_d(CONFIG_VIEW_DZ) / 100.0f;
183 int game_init(const struct level *level, int t, int g)
185 clock = (float) t / 100.f;
186 clock_down = (t > 0);
192 if (!sol_load_gl(&file, config_data(level->file),
193 config_get_d(CONFIG_TEXTURES),
194 config_get_d(CONFIG_SHADOW)))
195 return (game_state = 0);
206 /* Initialize jump and goal states. */
212 goal_k = (g == 0) ? 1.0f : 0.0f;
214 ball_b = level->is_bonus;
216 /* Initialise the level, background, particles, fade, and view. */
221 part_reset(GOAL_HEIGHT);
223 back_init(level->grad, config_get_d(CONFIG_GEOMETRY));
225 sol_load_gl(&back, config_data(level->back),
226 config_get_d(CONFIG_TEXTURES), 0);
228 /* Initialize ball size tracking... */
246 /*---------------------------------------------------------------------------*/
250 return (int) (clock * 100.f);
263 /*---------------------------------------------------------------------------*/
265 static void game_draw_balls(const struct s_file *fp)
267 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
270 m_basis(M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
274 glTranslatef(fp->uv[0].p[0],
275 fp->uv[0].p[1] + BALL_FUDGE,
278 glScalef(fp->uv[0].r,
289 static void game_draw_coins(const struct s_file *fp)
291 float r = 360.f * SDL_GetTicks() / 1000.f;
296 for (ci = 0; ci < fp->cc; ci++)
298 if (fp->cv[ci].n > 0)
302 glTranslatef(fp->cv[ci].p[0],
305 glRotatef(r, 0.0f, 1.0f, 0.0f);
306 coin_draw(fp->cv[ci].n, r);
315 static void game_draw_items(const struct s_file *fp)
317 float r = 360.f * SDL_GetTicks() / 1000.f;
321 item_push_text(ITEM_SHRINK);
323 for (hi = 0; hi < fp->hc; hi++)
325 if (fp->hv[hi].t == ITEM_SHRINK)
329 glTranslatef(fp->hv[hi].p[0],
332 glRotatef(r, 0.0f, 1.0f, 0.0f);
333 item_draw(fp->hv[hi].t, r);
340 /* FIXME: there has got to be a better way than two seperate loops,
341 * once for each texture, but someone else is going to have to do
345 item_push_text(ITEM_GROW);
347 for (hi = 0; hi < fp->hc; hi++)
349 if (fp->hv[hi].t == ITEM_GROW)
353 glTranslatef(fp->hv[hi].p[0],
356 glRotatef(r, 0.0f, 1.0f, 0.0f);
357 item_draw(fp->hv[hi].t, r);
365 static void game_draw_goals(const struct s_file *fp, float rx, float ry)
370 for (zi = 0; zi < fp->zc; zi++)
374 glTranslatef(fp->zv[zi].p[0],
378 part_draw_goal(rx, ry, fp->zv[zi].r, goal_k, fp->zv[zi].c);
380 glScalef(fp->zv[zi].r, goal_k, fp->zv[zi].r);
387 static void game_draw_jumps(const struct s_file *fp)
391 for (ji = 0; ji < fp->jc; ji++)
395 glTranslatef(fp->jv[ji].p[0],
399 glScalef(fp->jv[ji].r, 1.f, fp->jv[ji].r);
406 static void game_draw_swchs(const struct s_file *fp)
410 for (xi = 0; xi < fp->xc; xi++)
416 glTranslatef(fp->xv[xi].p[0],
420 glScalef(fp->xv[xi].r, 1.f, fp->xv[xi].r);
421 swch_draw(fp->xv[xi].f, fp->xv[xi].e);
427 /*---------------------------------------------------------------------------*/
429 static void game_refl_all(int s)
431 const float *ball_p = file.uv->p;
435 /* Rotate the environment about the position of the ball. */
437 glTranslatef(+ball_p[0], +ball_p[1], +ball_p[2]);
438 glRotatef(-game_rz, view_e[2][0], view_e[2][1], view_e[2][2]);
439 glRotatef(-game_rx, view_e[0][0], view_e[0][1], view_e[0][2]);
440 glTranslatef(-ball_p[0], -ball_p[1], -ball_p[2]);
442 /* Draw the floor. */
449 /*---------------------------------------------------------------------------*/
451 static void game_draw_light(void)
453 const float light_p[2][4] = {
454 { -8.0f, +32.0f, -8.0f, 1.0f },
455 { +8.0f, +32.0f, +8.0f, 1.0f },
457 const float light_c[2][4] = {
458 { 1.0f, 0.8f, 0.8f, 1.0f },
459 { 0.8f, 1.0f, 0.8f, 1.0f },
462 /* Configure the lighting. */
465 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
466 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
467 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
470 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
471 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
472 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
475 static void game_draw_back(int pose, int d, const float p[3])
477 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
478 float t = SDL_GetTicks() / 1000.f + 120.0f;
484 glRotatef(game_rz * 2, view_e[2][0], view_e[2][1], view_e[2][2]);
485 glRotatef(game_rx * 2, view_e[0][0], view_e[0][1], view_e[0][2]);
488 glTranslatef(p[0], p[1], p[2]);
491 if (config_get_d(CONFIG_BACKGROUND))
493 /* Draw all background layers back to front. */
495 sol_back(&back, BACK_DIST, FAR_DIST, t);
497 sol_back(&back, 0, BACK_DIST, t);
499 /* Draw all foreground geometry in the background file. */
508 static void game_draw_fore(int pose, float rx, float ry, int d, const float p[3])
510 const float *ball_p = file.uv->p;
511 const float ball_r = file.uv->r;
513 glPushAttrib(GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
517 /* Rotate the environment about the position of the ball. */
519 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
520 glRotatef(-game_rz * d, view_e[2][0], view_e[2][1], view_e[2][2]);
521 glRotatef(-game_rx * d, view_e[0][0], view_e[0][1], view_e[0][2]);
522 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
533 glEnable(GL_CLIP_PLANE0);
534 glClipPlane(GL_CLIP_PLANE0, e);
537 /* Draw the floor. */
541 if (config_get_d(CONFIG_SHADOW) && drawball)
543 shad_draw_set(ball_p, ball_r);
548 /* Draw the game elements. */
551 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
555 part_draw_coin(-rx * d, -ry);
556 game_draw_coins(&file);
557 game_draw_items(&file);
559 game_draw_balls(&file);
561 game_draw_goals(&file, -rx * d, -ry);
562 game_draw_jumps(&file);
563 game_draw_swchs(&file);
565 glDisable(GL_CLIP_PLANE0);
572 void game_draw(int pose, float st)
574 float fov = view_fov;
576 if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
580 config_push_persp(fov, 0.1f, FAR_DIST);
591 /* Compute and apply the view. */
593 v_sub(v, view_c, view_p);
595 rx = V_DEG(fatan2f(-v[1], fsqrtf(v[0] * v[0] + v[2] * v[2])));
596 ry = V_DEG(fatan2f(+v[0], -v[2])) + st;
598 glTranslatef(0.f, 0.f, -v_len(v));
599 glRotatef(rx, 1.f, 0.f, 0.f);
600 glRotatef(ry, 0.f, 1.f, 0.f);
601 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
603 if (config_get_d(CONFIG_REFLECTION))
605 /* Draw the mirror only into the stencil buffer. */
607 glDisable(GL_DEPTH_TEST);
608 glEnable(GL_STENCIL_TEST);
609 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
610 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
611 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
615 /* Draw the scene reflected into color and depth buffers. */
617 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
618 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
619 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
620 glEnable(GL_DEPTH_TEST);
625 glScalef(+1.f, -1.f, +1.f);
628 game_draw_back(pose, -1, pdn);
629 game_draw_fore(pose, rx, ry, -1, pdn);
634 glDisable(GL_STENCIL_TEST);
637 /* Draw the scene normally. */
640 game_refl_all(pose ? 0 : config_get_d(CONFIG_SHADOW));
641 game_draw_back(pose, +1, pup);
642 game_draw_fore(pose, rx, ry, +1, pup);
647 /* Draw the fade overlay. */
653 /*---------------------------------------------------------------------------*/
655 static void game_update_grav(float h[3], const float g[3])
657 struct s_file *fp = &file;
660 float y[3] = { 0.f, 1.f, 0.f };
666 /* Compute the gravity vector from the given world rotations. */
668 v_sub(z, view_p, fp->uv->p);
674 m_rot (Z, z, V_RAD(game_rz));
675 m_rot (X, x, V_RAD(game_rx));
680 static void game_update_view(float dt)
682 float dc = view_dc * (jump_b ? 2.0f * fabsf(jump_dt - 0.5f) : 1.0f);
683 float dx = view_ry * dt * 5.0f;
686 view_a += view_ry * dt * 90.f;
688 /* Center the view about the ball. */
690 v_cpy(view_c, file.uv->p);
691 v_inv(view_v, file.uv->v);
693 switch (config_get_d(CONFIG_CAMERA))
695 case 1: /* Camera 1: Viewpoint chases the ball position. */
697 v_sub(view_e[2], view_p, view_c);
700 case 2: /* Camera 2: View vector is given by view angle. */
702 view_e[2][0] = fsinf(V_RAD(view_a));
704 view_e[2][2] = fcosf(V_RAD(view_a));
710 default: /* Default: View vector approaches the ball velocity vector. */
712 k = v_dot(view_v, view_v);
714 v_sub(view_e[2], view_p, view_c);
715 v_mad(view_e[2], view_e[2], view_v, k * dt / 4);
720 /* Orthonormalize the basis of the view in its new position. */
722 v_crs(view_e[0], view_e[1], view_e[2]);
723 v_crs(view_e[2], view_e[0], view_e[1]);
724 v_nrm(view_e[0], view_e[0]);
725 v_nrm(view_e[2], view_e[2]);
727 /* Compute the new view position. */
729 k = 1.0f + v_dot(view_e[2], view_v) / 10.0f;
731 view_k = view_k + (k - view_k) * dt;
733 if (view_k < 0.5) view_k = 0.5;
735 v_cpy(view_p, file.uv->p);
736 v_mad(view_p, view_p, view_e[0], dx * view_k);
737 v_mad(view_p, view_p, view_e[1], view_dp * view_k);
738 v_mad(view_p, view_p, view_e[2], view_dz * view_k);
740 /* Compute the new view center. */
742 v_cpy(view_c, file.uv->p);
743 v_mad(view_c, view_c, view_e[1], dc);
745 /* Note the current view angle. */
747 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
750 static void game_update_time(float dt, int b)
752 if (goal_c == 0 && goal_k < 1.0f)
755 /* The ticking clock. */
770 static int game_update_state(int *state_value)
772 struct s_file *fp = &file;
775 int bt = state_value != NULL;
779 /* Test for a coin grab. */
781 if (bt && (n = sol_coin_test(fp, p, COIN_RADIUS)) > 0)
786 /* Add coins if regular, change radius if not. */
796 /* Check for goal open. */
802 audio_play(AUD_SWITCH, 1.f);
806 audio_play(AUD_COIN, 1.f);
809 audio_play(AUD_COIN, 1.f);
812 /* Test for an item. */
813 if (bt && (t = sol_item_test(fp, p, COIN_RADIUS)) != ITEM_NONE)
820 audio_play(AUD_COIN, 1.f);
823 /* Test for a switch. */
824 if (sol_swch_test(fp, 0))
825 audio_play(AUD_SWITCH, 1.f);
827 /* Test for a jump. */
829 if (jump_e == 1 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 1)
835 audio_play(AUD_JUMP, 1.f);
837 if (jump_e == 0 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 0)
840 /* Test for a goal. */
842 if (bt && goal_c == 0 && (g = sol_goal_test(fp, p, 0)))
845 audio_play(AUD_GOAL, 1.0f);
846 return g->c ? GAME_SPEC : GAME_GOAL;
849 /* Test for time-out. */
851 if (bt && clock_down && clock <= 0.f)
853 const GLfloat *p = fp->uv->p;
854 const GLfloat c[5] = {1.0f, 1.0f, 0.0f, 0.0f, 1.0f};
862 audio_play(AUD_TIME, 1.0f);
866 /* Test for fall-out. */
868 if (bt && fp->uv[0].p[1] < fp->vv[0].p[1])
870 audio_play(AUD_FALL, 1.0f);
878 * On most hardware, rendering requires much more computing power than
879 * physics. Since physics takes less time than graphics, it make sense to
880 * detach the physics update time step from the graphics frame rate. By
881 * performing multiple physics updates for each graphics update, we get away
882 * with higher quality physics with little impact on overall performance.
884 * Toward this end, we establish a baseline maximum physics time step. If
885 * the measured frame time exceeds this maximum, we cut the time step in
886 * half, and do two updates. If THIS time step exceeds the maximum, we do
887 * four updates. And so on. In this way, the physics system is allowed to
888 * seek an optimal update rate independant of, yet in integral sync with, the
889 * graphics frame rate.
892 int game_step(const float g[3], float dt, int *state_value)
894 struct s_file *fp = &file;
906 /* Smooth jittery or discontinuous input. */
910 game_rx += (game_ix - game_rx) * t / RESPONSE;
911 game_rz += (game_iz - game_rz) * t / RESPONSE;
922 game_update_grav(h, g);
935 fp->uv[0].p[0] = jump_p[0];
936 fp->uv[0].p[1] = jump_p[1];
937 fp->uv[0].p[2] = jump_p[2];
946 while (t > MAX_DT && n < MAX_DN)
952 for (i = 0; i < n; i++)
953 if (b < (d = sol_step(fp, h, t, 0, NULL)))
956 /* Mix the sound of a ball bounce. */
959 audio_play(AUD_BUMP, (b - 0.5f) * 2.0f);
963 game_update_view(dt);
964 game_update_time(dt, state_value != NULL);
966 return game_update_state(state_value);
971 /*---------------------------------------------------------------------------*/
973 void game_no_aa(void)
975 float max = game_ix * game_ix + game_iz * game_iz;
976 if (max > ANGLE_BOUND * ANGLE_BOUND)
978 max = ANGLE_BOUND / sqrt(max);
984 void game_set_x(int k)
986 game_ix = -(ANGLE_BOUND) * k / JOY_MAX;
992 void game_set_z(int k)
994 game_iz = +ANGLE_BOUND * k / JOY_MAX;
1000 void game_set_pos(int x, int y)
1002 game_ix += 40.f * y / config_get_d(CONFIG_MOUSE_SENSE);
1003 game_iz += 40.f * x / config_get_d(CONFIG_MOUSE_SENSE);
1008 if (game_ix > +ANGLE_BOUND) game_ix = +ANGLE_BOUND;
1009 if (game_ix < -ANGLE_BOUND) game_ix = -ANGLE_BOUND;
1010 if (game_iz > +ANGLE_BOUND) game_iz = +ANGLE_BOUND;
1011 if (game_iz < -ANGLE_BOUND) game_iz = -ANGLE_BOUND;
1015 void game_set_rot(float r)
1020 /*---------------------------------------------------------------------------*/
1022 void game_set_fly(float k)
1024 struct s_file *fp = &file;
1026 float x[3] = { 1.f, 0.f, 0.f };
1027 float y[3] = { 0.f, 1.f, 0.f };
1028 float z[3] = { 0.f, 0.f, 1.f };
1029 float c0[3] = { 0.f, 0.f, 0.f };
1030 float p0[3] = { 0.f, 0.f, 0.f };
1031 float c1[3] = { 0.f, 0.f, 0.f };
1032 float p1[3] = { 0.f, 0.f, 0.f };
1035 z[0] = fsinf(V_RAD(view_a));
1036 z[2] = fcosf(V_RAD(view_a));
1038 v_cpy(view_e[0], x);
1039 v_cpy(view_e[1], y);
1040 v_cpy(view_e[2], z);
1042 /* k = 0.0 view is at the ball. */
1046 v_cpy(c0, fp->uv[0].p);
1047 v_cpy(p0, fp->uv[0].p);
1050 v_mad(p0, p0, y, view_dp);
1051 v_mad(p0, p0, z, view_dz);
1052 v_mad(c0, c0, y, view_dc);
1054 /* k = +1.0 view is s_view 0 */
1056 if (k >= 0 && fp->wc > 0)
1058 v_cpy(p1, fp->wv[0].p);
1059 v_cpy(c1, fp->wv[0].q);
1062 /* k = -1.0 view is s_view 1 */
1064 if (k <= 0 && fp->wc > 1)
1066 v_cpy(p1, fp->wv[1].p);
1067 v_cpy(c1, fp->wv[1].q);
1070 /* Interpolate the views. */
1073 v_mad(view_p, p0, v, k * k);
1076 v_mad(view_c, c0, v, k * k);
1078 /* Orthonormalize the view basis. */
1080 v_sub(view_e[2], view_p, view_c);
1081 v_crs(view_e[0], view_e[1], view_e[2]);
1082 v_crs(view_e[2], view_e[0], view_e[1]);
1083 v_nrm(view_e[0], view_e[0]);
1084 v_nrm(view_e[2], view_e[2]);
1087 void game_look(float phi, float theta)
1089 view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
1090 view_c[1] = view_p[1] + fsinf(V_RAD(phi));
1091 view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
1094 /*---------------------------------------------------------------------------*/
1096 void game_kill_fade(void)
1102 void game_step_fade(float dt)
1104 if ((fade_k < 1.0f && fade_d > 0.0f) ||
1105 (fade_k > 0.0f && fade_d < 0.0f))
1106 fade_k += fade_d * dt;
1120 void game_fade(float d)
1125 /*---------------------------------------------------------------------------*/
1127 int put_game_state(FILE *fout)
1131 /* Write the view and tilt state. */
1133 put_float(fout, &game_rx);
1134 put_float(fout, &game_rz);
1135 put_array(fout, view_c, 3);
1136 put_array(fout, view_p, 3);
1138 /* Write the game simulation state. */
1140 put_file_state(fout, &file);
1147 int get_game_state(FILE *fin)
1151 /* Read the view and tilt state. */
1153 get_float(fin, &game_rx);
1154 get_float(fin, &game_rz);
1155 get_array(fin, view_c, 3);
1156 get_array(fin, view_p, 3);
1158 /* Read the game simulation state. */
1160 get_file_state(fin, &file);
1162 return (feof(fin) ? 0 : 1);
1167 /*---------------------------------------------------------------------------*/