From 0d78c4779d89910bfb12f289e1c3cfd06e99a57f Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Wed, 16 Jul 2008 17:22:55 +0100 Subject: [PATCH] Add new Fade class --- src/Fade.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Fade.hpp | 40 +++++++++++++++++++++++ src/Font.cpp | 2 +- src/Game.cpp | 30 ++++++------------ src/Game.hpp | 8 ++--- src/Makefile.am | 2 +- 6 files changed, 139 insertions(+), 27 deletions(-) create mode 100644 src/Fade.cpp create mode 100644 src/Fade.hpp diff --git a/src/Fade.cpp b/src/Fade.cpp new file mode 100644 index 0000000..003ed71 --- /dev/null +++ b/src/Fade.cpp @@ -0,0 +1,84 @@ +/* Fade.cpp -- Generic fade in/out effect. + * Copyright (C) 2008 Nick Gasson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Fade.hpp" +#include "OpenGL.hpp" + +const double Fade::DEFAULT_FADE_SPEED(0.05); + +Fade::Fade(double s) + : state(fNone), alpha(0.0), speed(s) +{ + +} + +void Fade::BeginFadeIn() +{ + assert(state == fNone); + + state = fIn; + alpha = 1.0; +} + +void Fade::BeginFadeOut() +{ + assert(state == fNone); + + state = fOut; + alpha = 0.0; +} + +bool Fade::Process() +{ + switch (state) { + case fOut: + alpha += speed; + if (alpha >= 1.0) { + state = fNone; + return true; + } + else + return false; + case fIn: + alpha -= speed; + if (alpha <= 0.0) { + state = fNone; + return true; + } + else + return false; + default: + assert(false); + } +} + +void Fade::Display() +{ + double w = OpenGL::GetInstance().GetWidth(); + double h = OpenGL::GetInstance().GetHeight(); + + glEnable(GL_BLEND); + glDisable(GL_TEXTURE_2D); + glColor4d(0.0, 0.0, 0.0, alpha); + glLoadIdentity(); + glBegin(GL_QUADS); + glVertex3d(0.0, 0.0, 0.0); + glVertex3d(0.0, h, 0.0); + glVertex3d(w, h, 0.0); + glVertex3d(w, 0.0, 0.0); + glEnd(); +} diff --git a/src/Fade.hpp b/src/Fade.hpp new file mode 100644 index 0000000..a5a0649 --- /dev/null +++ b/src/Fade.hpp @@ -0,0 +1,40 @@ +/* Fade.hpp -- Generic fade in/out effect. + * Copyright (C) 2008 Nick Gasson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef INC_FADE_HPP +#define INC_FADE_HPP + +#include "Platform.hpp" + +class Fade { +public: + static const double DEFAULT_FADE_SPEED; + + Fade(double s = DEFAULT_FADE_SPEED); + + void BeginFadeIn(); + void BeginFadeOut(); + void Display(); + bool Process(); +private: + enum State { fNone, fIn, fOut }; + + State state; + double alpha, speed; +}; + +#endif diff --git a/src/Font.cpp b/src/Font.cpp index 563abe4..0f2e9d4 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -1,5 +1,5 @@ /* - * FreeType.cpp -- A wrapper around FreeType. + * Font.cpp -- A wrapper around FreeType. * Copyright (C) 2006 Nick Gasson * * This program is free software; you can redistribute it and/or modify diff --git a/src/Game.cpp b/src/Game.cpp index cafe7d4..06e196d 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -62,7 +62,6 @@ Game::Game() surface(&viewport), speedmeter(&ship), state(gsNone), - fadeTexture("images/fade.png"), levelComp("images/levelcomp.png"), smallShip("images/shipsmall.png"), starImage("images/star.png"), @@ -77,13 +76,6 @@ Game::Game() void Game::Load() { - // Create the fade - fade.x = 0; - fade.y = 0; - fade.width = OpenGL::GetInstance().GetWidth(); - fade.height = OpenGL::GetInstance().GetHeight(); - fade.uTexture = fadeTexture.GetGLTexture(); - starrotate = 0.0f; death_timeout = 0; state = gsNone; @@ -285,10 +277,8 @@ void Game::Process() if (state == gsDeathWait) { if (--death_timeout == 0) { // Fade out - if (lives == 0 || (lives == 1 && life_alpha < LIFE_ALPHA_BASE)) { + if (lives == 0 || (lives == 1 && life_alpha < LIFE_ALPHA_BASE)) state = gsFadeToDeath; - fade_alpha = LIFE_ALPHA_BASE + 1.0f; - } else if (lives > 0) { if (life_alpha < LIFE_ALPHA_BASE) { life_alpha = LIFE_ALPHA_BASE + 1.0f; @@ -296,35 +286,33 @@ void Game::Process() } state = gsFadeToRestart; - fade_alpha = 0.0f; } + + fade.BeginFadeOut(); } } else if (state == gsGameOver) { if (--death_timeout == 0) { // Fade out state = gsFadeToDeath; - fade_alpha = 0.0f; + fade.BeginFadeOut(); } } else if (state == gsFadeIn) { // Fade in - fade_alpha -= GAME_FADE_IN_SPEED; - if (fade_alpha < 0.0f) + if (fade.Process()) state = gsInGame; } else if (state == gsFadeToRestart) { // Fade out - fade_alpha += GAME_FADE_OUT_SPEED; - if (fade_alpha > 1.0f) { + if (fade.Process()) { // Restart the level StartLevel(level); opengl.SkipDisplay(); } } else if (state == gsFadeToDeath) { - fade_alpha += GAME_FADE_OUT_SPEED; - if (fade_alpha > 1.0f) { + if (fade.Process()) { // Return to main menu ScreenManager &sm = ScreenManager::GetInstance(); HighScores *hs = static_cast(sm.GetScreenById("HIGH SCORES")); @@ -505,7 +493,7 @@ void Game::StartLevel(int level) // Start the game levelcomp_timeout = 0; state = gsFadeIn; - fade_alpha = 1.0f; + fade.BeginFadeIn(); life_alpha = LIFE_ALPHA_BASE + 1.0f; } @@ -689,7 +677,7 @@ void Game::Display() // Draw the fade if (state == gsFadeIn || state == gsFadeToDeath || state == gsFadeToRestart) - opengl.DrawBlend(&fade, fade_alpha); + fade.Display(); // Draw game over message if (lives == 0 || (lives == 1 && life_alpha < LIFE_ALPHA_BASE)) { diff --git a/src/Game.hpp b/src/Game.hpp index 89103a3..3ede68c 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -27,6 +27,7 @@ #include "Input.hpp" #include "Font.hpp" #include "SoundEffect.hpp" +#include "Fade.hpp" #include "Viewport.hpp" #include "ObjectGrid.hpp" @@ -111,8 +112,7 @@ private: SpeedMeter speedmeter; int death_timeout, level, lives; bool bDebugMode; - float flGravity, starrotate, fade_alpha, life_alpha; - TextureQuad fade; + float flGravity, starrotate, life_alpha; int score, newscore, nextnewlife, newscore_width; int countdown_timeout, leveltext_timeout, levelcomp_timeout; @@ -121,11 +121,11 @@ private: gsPaused }; GameState state; - Texture fadeTexture; - Image levelComp, smallShip; Image starImage, gameOver; + Fade fade; + Font normalFont, scoreFont, bigFont; SoundEffect impactSound; diff --git a/src/Makefile.am b/src/Makefile.am index aeb3248..c190f90 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -16,7 +16,7 @@ lander_SOURCES = Main.cpp Game.hpp Game.cpp \ Image.cpp AnimatedImage.hpp AnimatedImage.cpp \ Texture.hpp Texture.cpp Options.hpp Options.cpp \ ConfigFile.hpp ConfigFile.cpp SoundEffect.hpp \ - SoundEffect.cpp + SoundEffect.cpp Fade.hpp Fade.cpp localedir = $(datadir)/locale -- 2.39.2