From de5069f2e24c09dedfe818f2660557935b64052d Mon Sep 17 00:00:00 2001 From: nick Date: Mon, 31 Mar 2008 22:00:14 +0000 Subject: [PATCH] Start factoring out position and velocity code git-svn-id: http://svn.nickg.me.uk/work/lander/trunk@338 a97b1542-0b21-0410-a459-e47997c36f34 --- src/ElectricGate.cpp | 13 +++++----- src/Game.cpp | 2 +- src/Makefile.am | 5 ++-- src/Mechanics.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ src/Mechanics.hpp | 52 ++++++++++++++++++++++++++++++++++++++ src/Menu.cpp | 40 +++++++++++++---------------- src/Menu.hpp | 8 +++--- src/OpenGL.cpp | 4 +-- 8 files changed, 147 insertions(+), 37 deletions(-) create mode 100644 src/Mechanics.cpp create mode 100644 src/Mechanics.hpp diff --git a/src/ElectricGate.cpp b/src/ElectricGate.cpp index 012ad8d..1d77736 100644 --- a/src/ElectricGate.cpp +++ b/src/ElectricGate.cpp @@ -89,11 +89,9 @@ void ElectricGate::Draw() opengl.Draw(&icon); // Draw the electricity stuff - if (--timer < GATEWAY_ACTIVE) { - float r, g, b; + if (--timer < GATEWAY_ACTIVE) { int x, y, deviation; - opengl.DisableBlending(); opengl.DisableTexture(); for (int j = 0; j < 10; j++) { @@ -101,11 +99,12 @@ void ElectricGate::Draw() for (int k = 0; k < length; k++) { glLoadIdentity(); glBegin(GL_LINE_STRIP); - r = 0.0f + (float)(rand()%5)/10.0f; - g = 0.0f + (float)(rand()%5)/10.0f; - b = 1.0f - (float)(rand()%5)/10.0f; + float r = 0.0f + (float)(rand()%5)/10.0f; + float g = 0.0f + (float)(rand()%5)/10.0f; + float b = 1.0f - (float)(rand()%5)/10.0f; + float a = 1.0f - (float)(rand()%5)/10.0f; - glColor3f(r, g, b); + glColor4f(r, g, b, a); if (vertical) { x = xpos*OBJ_GRID_SIZE + 16 + deviation - viewport->GetXAdjust(); y = (ypos+k)*OBJ_GRID_SIZE + OBJ_GRID_TOP + 16 - viewport->GetYAdjust(); diff --git a/src/Game.cpp b/src/Game.cpp index e87ec5a..8df88d3 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -502,7 +502,7 @@ void Game::StartLevel(int level) } // Create gateways - int gatewaycount = level/2 + rand()%level; + int gatewaycount = level/2 + rand()%level - 1; gateways.clear(); if (gatewaycount > MAX_GATEWAYS) gatewaycount = MAX_GATEWAYS; diff --git a/src/Makefile.am b/src/Makefile.am index 371b3ee..a201242 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ bin_PROGRAMS = lander -AM_CXXFLAGS=$(LANDER_CFLAGS) $(SDL_CFLAGS) -DLINUX -DUNIX \ +AM_CXXFLAGS=$(LANDER_CFLAGS) $(SDL_CFLAGS) -DLINUX -DUNIX -DSHOW_FPS \ -DDATADIR=\"$(pkgdatadir)\" AM_LDFLAGS=$(LANDER_LIBS) $(SDL_LIBS) -lGLU lander_SOURCES = Main.cpp Lander.hpp Game.cpp \ @@ -12,6 +12,7 @@ lander_SOURCES = Main.cpp Lander.hpp Game.cpp \ ObjectGrid.hpp ObjectGrid.cpp Asteroid.hpp Asteroid.cpp \ Ship.hpp Ship.cpp Viewport.hpp Viewport.cpp LandingPad.hpp \ LandingPad.cpp Surface.hpp Surface.cpp Mine.hpp Mine.cpp \ - ElectricGate.hpp ElectricGate.cpp Key.hpp Key.cpp + ElectricGate.hpp ElectricGate.cpp Key.hpp Key.cpp \ + Mechanics.hpp Mechanics.cpp dist_pkgdata_DATA = ../lander.dat ../Default_Font.ttf ../Hollow_Font.ttf diff --git a/src/Mechanics.cpp b/src/Mechanics.cpp new file mode 100644 index 0000000..bdf9df0 --- /dev/null +++ b/src/Mechanics.cpp @@ -0,0 +1,60 @@ +/* Mechanics.cpp -- Frame-rate independent movemoment. + * 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 +#include "Mechanics.hpp" + +Position::Position(float x, float y) + : x(x), y(y) +{ + +} + +Position Position::operator+(Velocity &v) const +{ + return Position(x + v.GetXSpeed(), y + v.GetYSpeed()); +} + +Position Position::operator-(Velocity &v) const +{ + return Position(x - v.GetXSpeed(), y - v.GetYSpeed()); +} + +void Position::operator+=(Velocity &v) +{ + x += v.GetXSpeed(); + y += v.GetYSpeed(); +} + + +void Position::operator-=(Velocity &v) +{ + x -= v.GetXSpeed(); + y -= v.GetYSpeed(); +} + +Velocity::Velocity(float xspeed, float yspeed) + : xspeed(xspeed), yspeed(yspeed) +{ + +} + +Velocity Velocity::Project(float speed, float angle) +{ + return Velocity(speed * cosf(angle), speed * sinf(angle)); +} + diff --git a/src/Mechanics.hpp b/src/Mechanics.hpp new file mode 100644 index 0000000..35e3aa8 --- /dev/null +++ b/src/Mechanics.hpp @@ -0,0 +1,52 @@ +/* Mechanics.hpp -- Frame-rate independent movemoment. + * 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_MECHANICS_HPP +#define INC_MECHANICS_HPP + +class Velocity { +public: + Velocity(float xspeed, float yspeed); + Velocity() { Velocity(0.0f, 0.0f); } + + static Velocity Project(float speed, float angle); + + int GetXSpeed() const { return xspeed; } + int GetYSpeed() const { return yspeed; } + +private: + float xspeed, yspeed; +}; + +class Position { +public: + Position(float x, float y); + Position() { Position(0, 0); } + + float GetX() const { return x; } + float GetY() const { return y; } + + Position operator+(Velocity &v) const; + Position operator-(Velocity &v) const; + void operator+=(Velocity &v); + void operator-=(Velocity &v); + +private: + float x, y; +}; + +#endif diff --git a/src/Menu.cpp b/src/Menu.cpp index 1837be1..0667dde 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -22,7 +22,7 @@ const float MenuStar::ROTATE_SPEED(0.005f); const float MenuStar::ENLARGE_RATE(0.003f); const float MenuStar::INIT_SCALE(0.1f); -const int MenuStar::SPEED(4); +const float MenuStar::SPEED(4.0f); const int MenuStar::TEXTURE_SIZE(20); extern DataFile *g_pData; @@ -224,6 +224,7 @@ void MainMenu::Display() { OpenGL &opengl = OpenGL::GetInstance(); + for (StarListIt it = stars.begin(); it != stars.end(); ++it) { (*it).Display(); } @@ -286,17 +287,18 @@ MenuStar::MenuStar() const int screenWidth = opengl.GetWidth(); const int screenHeight = opengl.GetHeight(); - xpos = (float)(rand()%(screenWidth/2) + screenWidth/4); - ypos = (float)(rand()%(screenHeight/2) + screenHeight/4); + pos = Position((float)(rand()%(screenWidth/2) + screenWidth/4), + (float)(rand()%(screenHeight/2) + screenHeight/4)); - float ratio = (ypos - screenHeight/2) / (xpos - screenWidth/2); - angle = atanf(ratio); + float ratio = (pos.GetY() - screenHeight/2) / (pos.GetX() - screenWidth/2); + float angle = atanf(ratio); + vel = Velocity::Project(SPEED, angle); quad.uTexture = uStarTexture; quad.height = TEXTURE_SIZE; quad.width = TEXTURE_SIZE; - quad.x = (int)xpos; - quad.y = (int)ypos; + quad.x = (int)pos.GetX(); + quad.y = (int)pos.GetY(); } void MenuStar::Display(float fade) @@ -307,24 +309,18 @@ void MenuStar::Display(float fade) bool MenuStar::Move() { - const int screenWidth = OpenGL::GetInstance().GetWidth(); - const int screenHeight = OpenGL::GetInstance().GetHeight(); - - if (xpos > screenWidth / 2) { - xpos += SPEED * cosf(angle); - ypos += SPEED * sinf(angle); - } - else { - xpos -= SPEED * cosf(angle); - ypos -= SPEED * sinf(angle); - } - quad.x = (int)xpos; - quad.y = (int)ypos; + if (pos.GetX() > OpenGL::GetInstance().GetWidth() / 2) + pos += vel; + else + pos -= vel; + + quad.x = (int)pos.GetX(); + quad.y = (int)pos.GetY(); scale += ENLARGE_RATE; // Has it left the screen? - return (quad.x > screenWidth - || quad.y > screenHeight + return (quad.x > OpenGL::GetInstance().GetWidth() + || quad.y > OpenGL::GetInstance().GetHeight() || quad.x + quad.width < 0 || quad.y + quad.height < 0); } diff --git a/src/Menu.hpp b/src/Menu.hpp index a7f2725..df8128d 100644 --- a/src/Menu.hpp +++ b/src/Menu.hpp @@ -21,6 +21,7 @@ #define INC_MENU_HPP #include "ScreenManager.hpp" +#include "Mechanics.hpp" #define MENU_FADE_SPEED 0.1f #define HINT_DISPLAY_TIME 140 @@ -34,13 +35,14 @@ public: void Display(float fade=1.0f); private: - static const float ROTATE_SPEED, ENLARGE_RATE, INIT_SCALE; - static const int SPEED, TEXTURE_SIZE; + static const float ROTATE_SPEED, ENLARGE_RATE, INIT_SCALE, SPEED; + static const int TEXTURE_SIZE; TextureQuad quad; float scale; bool active; - float angle, xpos, ypos; + Position pos; + Velocity vel; static float starRotate; static bool hasLoaded; diff --git a/src/OpenGL.cpp b/src/OpenGL.cpp index 2a46c25..520d0ab 100644 --- a/src/OpenGL.cpp +++ b/src/OpenGL.cpp @@ -140,7 +140,7 @@ void OpenGL::DrawGLScene() fps_rate = fps_framesdrawn; fps_framesdrawn = 0; -#ifdef _DEBUG +#ifdef SHOW_FPS const int TITLE_BUF_LEN = 256; char buf[TITLE_BUF_LEN]; @@ -148,7 +148,7 @@ void OpenGL::DrawGLScene() snprintf(buf, TITLE_BUF_LEN, "%s {%dfps}", WINDOW_TITLE, fps_rate); SDL_WM_SetCaption(buf, NULL); } -#endif /* #ifdef _DEBUG */ +#endif /* #ifdef SHOW_FPS */ } fps_framesdrawn++; } -- 2.39.2