From e96183e1002ebc2b3e1209ae85a7ad5311ee47b9 Mon Sep 17 00:00:00 2001 From: nick Date: Sun, 20 Apr 2008 18:43:39 +0000 Subject: [PATCH] Change ship implementation a bit git-svn-id: http://svn.nickg.me.uk/work/lander@358 a97b1542-0b21-0410-a459-e47997c36f34 --- trunk/data/images/ship.png | Bin 586 -> 586 bytes trunk/data/images/star.png | Bin 4026 -> 4044 bytes trunk/data/images/start_option.png | Bin 1075 -> 1075 bytes trunk/po/en_GB.po | 6 +- trunk/po/lander.pot | 6 +- trunk/src/Game.cpp | 30 +++---- trunk/src/Lander.hpp | 12 +-- trunk/src/Ship.cpp | 126 +++++++++++++---------------- trunk/src/Ship.hpp | 38 ++++----- 9 files changed, 98 insertions(+), 120 deletions(-) diff --git a/trunk/data/images/ship.png b/trunk/data/images/ship.png index b89c9798a33ae6c98d829f8a2d9c44401bddd9da..26a988a250c7dc4088736c93c0d614a0dcd07fcf 100644 GIT binary patch delta 19 bcmX@ba*Aa_0*8o@A$M5*?{gb79x(y{M1Ti@ delta 19 acmX@ba*Aa_0*A1muy~bky5q)-M~nbHq6SU? diff --git a/trunk/data/images/star.png b/trunk/data/images/star.png index dac1a1a3ed40db8d5c6a9b5ec6f2540c38bcb740..11f2c874b889bdde42af2a0b91da09889d8f6168 100644 GIT binary patch delta 38 scmdlbe@1?SAwOG^x4R3&e-K=-clqRrdI}sOLYj;w3%fcuhNbWW02aay00000 delta 22 ecmX>jze|3C;Y4Ry4q+ia%jk_ePH)Ue;RgU(S_lRJ diff --git a/trunk/data/images/start_option.png b/trunk/data/images/start_option.png index 57ea6fef4b4d530b8eb53081b2fbe7251e3265f6..105366c19e098a1369c278ffd090ec3882748b26 100644 GIT binary patch delta 18 ZcmdnYv6*9nKfAcOZ2n(i?~P%1nE^Wn26+Gg delta 18 acmdnYv6*9nKf4IC%))7T#T&!!G6MiQeFl#J diff --git a/trunk/po/en_GB.po b/trunk/po/en_GB.po index 85a1f76..3275296 100644 --- a/trunk/po/en_GB.po +++ b/trunk/po/en_GB.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: lander 0.9\n" "Report-Msgid-Bugs-To: nick@cakesniffer.co.uk\n" -"POT-Creation-Date: 2008-04-19 17:46+0100\n" +"POT-Creation-Date: 2008-04-20 00:45+0100\n" "PO-Revision-Date: 2008-04-19 01:09+0100\n" "Last-Translator: Nick Gasson \n" "Language-Team: English (British)\n" @@ -33,7 +33,7 @@ msgstr "Press ENTER or FIRE to continue" msgid "Name? %s" msgstr "Name? %s" -#: src/Menu.cpp:241 +#: src/Menu.cpp:229 msgid "LUNAR LANDER" msgstr "LUNAR LANDER" @@ -63,7 +63,7 @@ msgstr "Game Over" msgid "Paused" msgstr "Paused" -#: src/Main.cpp:103 +#: src/Main.cpp:108 #, c-format msgid "Runtime Error: %s\n" msgstr "Runtime Error: %s\n" diff --git a/trunk/po/lander.pot b/trunk/po/lander.pot index fc1d5c1..b49f72a 100644 --- a/trunk/po/lander.pot +++ b/trunk/po/lander.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: lander 0.9\n" "Report-Msgid-Bugs-To: nick@cakesniffer.co.uk\n" -"POT-Creation-Date: 2008-04-19 17:46+0100\n" +"POT-Creation-Date: 2008-04-20 00:45+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -33,7 +33,7 @@ msgstr "" msgid "Name? %s" msgstr "" -#: src/Menu.cpp:241 +#: src/Menu.cpp:229 msgid "LUNAR LANDER" msgstr "" @@ -63,7 +63,7 @@ msgstr "" msgid "Paused" msgstr "" -#: src/Main.cpp:103 +#: src/Main.cpp:108 #, c-format msgid "Runtime Error: %s\n" msgstr "" diff --git a/trunk/src/Game.cpp b/trunk/src/Game.cpp index 00d2e47..544cb28 100644 --- a/trunk/src/Game.cpp +++ b/trunk/src/Game.cpp @@ -1,5 +1,5 @@ /* - * Game.cpp - Implementation of core game logic. + * Game.cpp -- Implementation of core game logic. * Copyright (C) 2006 Nick Gasson * * This program is free software; you can redistribute it and/or modify @@ -56,11 +56,12 @@ extern DataFile *g_pData; Game::Game() - : hasloaded(false), - state(gsNone), - ship(&viewport), + : ship(&viewport), surface(&viewport), - speedmeter(&ship) + speedmeter(&ship), + hasloaded(false), + state(gsNone), + starImage("images/star.png") { } @@ -70,7 +71,6 @@ void Game::Load() OpenGL &opengl = OpenGL::GetInstance(); if (!hasloaded) { - uStarTexture = opengl.LoadTextureAlpha(g_pData, "Star.bmp"); uFadeTexture = opengl.LoadTexture(g_pData, "Fade.bmp"); uLevComTexture = opengl.LoadTextureAlpha(g_pData, "LevelComplete.bmp"); uSurf2Texture[0] = opengl.LoadTexture(g_pData, "GrassSurface2.bmp"); @@ -80,7 +80,6 @@ void Game::Load() uSurf2Texture[4] = opengl.LoadTexture(g_pData, "RockSurface2.bmp"); uShipSmallTexture = opengl.LoadTextureAlpha(g_pData, "ShipSmall.bmp"); - Ship::Load(); LandingPad::Load(); Surface::Load(); Mine::Load(); @@ -436,8 +435,7 @@ void Game::StartLevel(int level) for (int i = 0; i < nStarCount; i++) { stars[i].xpos = (int)(rand()%(viewport.GetLevelWidth()/20))*20; stars[i].ypos = (int)(rand()%(viewport.GetLevelHeight()/20))*20; - stars[i].quad.uTexture = uStarTexture; - stars[i].quad.width = stars[i].quad.height = rand()%15; + stars[i].scale = (double)rand()/(double)RAND_MAX/8.0; } // Generate landing pads @@ -502,18 +500,14 @@ void Game::StartLevel(int level) } // Create gateways - int gatewaycount = level/2 + rand()%level - 1; + int gatewaycount = level/2 + rand()%level - 2; gateways.clear(); if (gatewaycount > MAX_GATEWAYS) gatewaycount = MAX_GATEWAYS; for (int i = 0; i < gatewaycount; i++) { // Allocate space for gateway int length = rand()%(MAX_GATEWAY_LENGTH-3) + 3; - bool vertical; - switch(rand() % 2) { - case 0: vertical = true; break; - case 1: vertical = false; break; - } + bool vertical = rand() % 2 == 0; bool result; int xpos, ypos; @@ -578,9 +572,9 @@ void Game::Display() // Draw the stars for (int i = 0; i < nStarCount; i++) { - stars[i].quad.x = stars[i].xpos - viewport.GetXAdjust(); - stars[i].quad.y = stars[i].ypos - viewport.GetYAdjust(); - opengl.DrawRotate(&stars[i].quad, starrotate); + int x = stars[i].xpos - viewport.GetXAdjust(); + int y = stars[i].ypos - viewport.GetYAdjust(); + starImage.Draw(x, y, starrotate, stars[i].scale); starrotate += 0.005f; } diff --git a/trunk/src/Lander.hpp b/trunk/src/Lander.hpp index ce6768e..d6fa310 100644 --- a/trunk/src/Lander.hpp +++ b/trunk/src/Lander.hpp @@ -122,19 +122,21 @@ private: int countdown_timeout, leveltext_timeout, levelcomp_timeout; enum GameState { gsNone, gsInGame, gsExplode, gsGameOver, gsDeathWait, - gsFadeIn, gsFadeToDeath, gsFadeToRestart, gsLevelComplete, gsPaused }; + gsFadeIn, gsFadeToDeath, gsFadeToRestart, gsLevelComplete, + gsPaused }; GameState state; // Textures - GLuint uStarTexture, uSurf2Texture[Surface::NUM_SURF_TEX], uFadeTexture; + GLuint uSurf2Texture[Surface::NUM_SURF_TEX], uFadeTexture; GLuint uLevComTexture; - GLuint uShipSmallTexture; + GLuint uShipSmallTexture; + + Image starImage; // Stars static const int MAX_GAME_STARS = 2048; struct Star { - TextureQuad quad; - float scale; + double scale; int xpos, ypos; } stars[MAX_GAME_STARS]; int nStarCount; diff --git a/trunk/src/Ship.cpp b/trunk/src/Ship.cpp index 5a30a40..e99033f 100644 --- a/trunk/src/Ship.cpp +++ b/trunk/src/Ship.cpp @@ -16,11 +16,6 @@ */ #include "Ship.hpp" -#include "DataFile.hpp" - -extern DataFile *g_pData; - -GLuint Ship::uShipTexture = 0; /* * Defines a simplified polygon representing the ship. @@ -31,32 +26,24 @@ const Point Ship::hotspots[] = { Ship::Ship(Viewport *v) - : xpos(0), ypos(0), speedX(0), speedY(0), angle(0), viewport(v), + : shipImage("images/ship.png"), + xpos(0), ypos(0), speedX(0), speedY(0), angle(0), viewport(v), thrusting(false) { - tq.width = SHIP_TEX_WIDTH; - tq.height = SHIP_TEX_HEIGHT; -} - -void Ship::Load() -{ - OpenGL &opengl = OpenGL::GetInstance(); - uShipTexture = opengl.LoadTextureAlpha(g_pData, "Ship.bmp"); } void Ship::Display() { - tq.x = (int)xpos - viewport->GetXAdjust(); - tq.y = (int)ypos - viewport->GetYAdjust(); - tq.uTexture = uShipTexture; - - OpenGL::GetInstance().DrawRotate(&tq, angle); + int dx = (int)xpos - viewport->GetXAdjust(); + int dy = (int)ypos - viewport->GetYAdjust(); + + shipImage.Draw(dx, dy, angle); } void Ship::DrawExhaust(bool paused) { - static float xlast, ylast; + static double xlast, ylast; if (thrusting) { if (sqrt(speedX*speedX + speedY*speedY) > 2.0f) { @@ -64,15 +51,15 @@ void Ship::DrawExhaust(bool paused) ((int)(exhaust.xpos + (exhaust.xpos - xlast)/2), (int)(exhaust.ypos + (exhaust.ypos - ylast)/2)); } - exhaust.Draw((float)viewport->GetXAdjust(), - (float)viewport->GetYAdjust(), true); + exhaust.Draw((double)viewport->GetXAdjust(), + (double)viewport->GetYAdjust(), true); } else if (paused) - exhaust.Draw((float)viewport->GetXAdjust(), - (float)viewport->GetYAdjust(), false, false); + exhaust.Draw((double)viewport->GetXAdjust(), + (double)viewport->GetYAdjust(), false, false); else - exhaust.Draw((float)viewport->GetXAdjust(), - (float)viewport->GetYAdjust(), false); + exhaust.Draw((double)viewport->GetXAdjust(), + (double)viewport->GetYAdjust(), false); xlast = exhaust.xpos; ylast = exhaust.ypos; @@ -80,8 +67,8 @@ void Ship::DrawExhaust(bool paused) void Ship::DrawExplosion(bool createNew) { - explosion.Draw((float)viewport->GetXAdjust(), - (float)viewport->GetYAdjust(), createNew); + explosion.Draw((double)viewport->GetXAdjust(), + (double)viewport->GetYAdjust(), createNew); } void Ship::Move() @@ -92,31 +79,31 @@ void Ship::Move() ypos += speedY; // Check bounds - if (xpos <= 0.0f) { - xpos = 0.0f; - speedX *= -0.5f; + if (xpos <= 0.0) { + xpos = 0.0; + speedX *= -0.5; } - else if (xpos + tq.width > viewport->GetLevelWidth()) { - xpos = (float)(viewport->GetLevelWidth() - tq.width); - speedX *= -0.5f; + else if (xpos + shipImage.GetWidth() > viewport->GetLevelWidth()) { + xpos = (double)(viewport->GetLevelWidth() - shipImage.GetWidth()); + speedX *= -0.5; } - if (ypos <= 0.0f) { - ypos = 0.0f; - speedY *= -0.5f; + if (ypos <= 0.0) { + ypos = 0.0; + speedY *= -0.5; } - else if (ypos + tq.height > viewport->GetLevelHeight()) { - ypos = (float)(viewport->GetLevelHeight() - tq.height); - speedY *= -0.5f; + else if (ypos + shipImage.GetHeight() > viewport->GetLevelHeight()) { + ypos = (double)(viewport->GetLevelHeight() - shipImage.GetHeight()); + speedY *= -0.5; } - exhaust.xpos = xpos + tq.width/2 - - (tq.width/2)*(float)sin(angle*(PI/180)); - exhaust.ypos = ypos + tq.height/2 - + (tq.height/2)*(float)cos(angle*(PI/180)); + exhaust.xpos = xpos + shipImage.GetWidth()/2 + - (shipImage.GetWidth()/2)*(double)sin(angle*(PI/180)); + exhaust.ypos = ypos + shipImage.GetHeight()/2 + + (shipImage.GetHeight()/2)*(double)cos(angle*(PI/180)); exhaust.yg = speedY; //+ (flGravity * 10); exhaust.xg = speedX; - explosion.xpos = xpos + tq.width/2; - explosion.ypos = ypos + tq.height/2; + explosion.xpos = xpos + shipImage.GetWidth()/2; + explosion.ypos = ypos + shipImage.GetHeight()/2; } void Ship::ThrustOn() @@ -129,18 +116,18 @@ void Ship::ThrustOff() thrusting = false; } -void Ship::Thrust(float speed) +void Ship::Thrust(double speed) { speedX += speed * sinf(angle*(PI/180)); speedY -= speed * cosf(angle*(PI/180)); } -void Ship::Turn(float delta) +void Ship::Turn(double delta) { angle += delta; } -void Ship::ApplyGravity(float gravity) +void Ship::ApplyGravity(double gravity) { speedY += gravity; } @@ -155,8 +142,8 @@ void Ship::Bounce() void Ship::CentreInViewport() { - int centrex = (int)xpos + (tq.width/2); - int centrey = (int)ypos + (tq.height/2); + int centrex = (int)xpos + (shipImage.GetWidth()/2); + int centrey = (int)ypos + (shipImage.GetHeight()/2); OpenGL &opengl = OpenGL::GetInstance(); viewport->SetXAdjust(centrex - (opengl.GetWidth()/2)); viewport->SetYAdjust(centrey - (opengl.GetHeight()/2)); @@ -170,7 +157,7 @@ void Ship::Reset() exhaust.Reset(); explosion.Reset(); - xpos = (float)viewport->GetLevelWidth()/2; + xpos = (double)viewport->GetLevelWidth()/2; ypos = SHIP_START_Y - 40; angle = 0.0f; @@ -179,7 +166,7 @@ void Ship::Reset() } void Ship::RotatePoints(const Point *pPoints, Point *pDest, int nCount, - float angle, int adjustx, int adjusty) + double angle, int adjustx, int adjusty) { for (int i = 0; i < nCount; i++) { int x = pPoints[i].x + adjustx; @@ -195,7 +182,7 @@ void Ship::RotatePoints(const Point *pPoints, Point *pDest, int nCount, /* * Check for collision between the ship and a polygon. */ -bool Ship::HotSpotCollision(LineSegment &l, float dx, float dy) +bool Ship::HotSpotCollision(LineSegment &l, double dx, double dy) { for (int i = 0; i < NUM_HOTSPOTS; i++) { if (CheckCollision(l, dx + points[i].x, dy + points[i].y)) @@ -225,36 +212,37 @@ bool Ship::BoxCollision(int x, int y, int w, int h) /* * Checks for collision between the ship and a line segment. */ -bool Ship::CheckCollision(LineSegment &l, float dx, float dy) +bool Ship::CheckCollision(LineSegment &l, double dx, double dy) { - float xpos = this->xpos + dx; - float ypos = this->ypos + dy; + double xpos = this->xpos + dx; + double ypos = this->ypos + dy; - if (!viewport->PointInScreen((int)xpos, (int)ypos, SHIP_TEX_WIDTH, SHIP_TEX_HEIGHT)) + if (!viewport->PointInScreen + ((int)xpos, (int)ypos, shipImage.GetWidth(), shipImage.GetHeight())) return false; // Get position after next move - float cX = xpos + speedX; - float cY = ypos + speedY; + double cX = xpos + speedX; + double cY = ypos + speedY; // Get displacement - float vecX = cX - xpos; - float vecY = cY - ypos; + double vecX = cX - xpos; + double vecY = cY - ypos; // Get line position - float wallX = (float)(l.p2.x - l.p1.x); - float wallY = (float)(l.p2.y - l.p1.y); + double wallX = (double)(l.p2.x - l.p1.x); + double wallY = (double)(l.p2.y - l.p1.y); // Work out numerator and denominator (used parametric equations) - float numT = wallX * (ypos - l.p1.y) - wallY * (xpos - l.p1.x); - float numU = vecX * (ypos - l.p1.y) - vecY * (xpos - l.p1.x); + double numT = wallX * (ypos - l.p1.y) - wallY * (xpos - l.p1.x); + double numU = vecX * (ypos - l.p1.y) - vecY * (xpos - l.p1.x); // Work out denominator - float denom = wallY * (cX - xpos) - wallX * (cY - ypos); + double denom = wallY * (cX - xpos) - wallX * (cY - ypos); // Work out u and t - float u = numU / denom; - float t = numT / denom; + double u = numU / denom; + double t = numT / denom; // Collision occured if (0 < t < 1) and (0 < u < 1) return (t > 0.0f) && (t < 1.0f) && (u > 0.0f) && (u < 1.0f); diff --git a/trunk/src/Ship.hpp b/trunk/src/Ship.hpp index 972a669..53a69d0 100644 --- a/trunk/src/Ship.hpp +++ b/trunk/src/Ship.hpp @@ -19,17 +19,15 @@ #define INC_SHIP_HPP #include "Platform.hpp" -#include "OpenGL.hpp" #include "Geometry.hpp" #include "Viewport.hpp" #include "Emitter.hpp" +#include "Image.hpp" class Ship { public: Ship(Viewport *v); - static void Load(); - void Reset(); void Display(); @@ -38,36 +36,32 @@ public: void Move(); void ThrustOn(); void ThrustOff(); - void Thrust(float speed); - void Turn(float delta); + void Thrust(double speed); + void Turn(double delta); void Bounce(); - void ApplyGravity(float gravity); + void ApplyGravity(double gravity); void CentreInViewport(); - bool CheckCollision(LineSegment &l, float dx=0, float dy=0); - bool HotSpotCollision(LineSegment &l, float dx=0, float dy=0); + bool CheckCollision(LineSegment &l, double dx=0, double dy=0); + bool HotSpotCollision(LineSegment &l, double dx=0, double dy=0); bool BoxCollision(int x, int y, int w, int h); - float GetX() const { return xpos; } - float GetY() const { return ypos; } - float GetXSpeed() const { return speedX; } - float GetYSpeed() const { return speedY; } - float GetAngle() const { return angle; } + double GetX() const { return xpos; } + double GetY() const { return ypos; } + double GetXSpeed() const { return speedX; } + double GetYSpeed() const { return speedY; } + double GetAngle() const { return angle; } static const int SHIP_START_Y = 100; private: void RotatePoints(const Point *pPoints, Point *pDest, int nCount, - float angle, int adjustx=0, int adjusty=0); - - static const int SHIP_TEX_WIDTH = 32; - static const int SHIP_TEX_HEIGHT = 32; - - static GLuint uShipTexture; + double angle, int adjustx=0, int adjusty=0); + + Image shipImage; - float xpos, ypos; - float speedX, speedY, angle; - TextureQuad tq; + double xpos, ypos; + double speedX, speedY, angle; Viewport *viewport; Explosion explosion; -- 2.39.2