Moved Map Loader to XML. Changed const int return values to enumerators.

I need sleep. Night.
This commit is contained in:
Rtch90 2011-12-14 03:39:27 +00:00
parent 04ac0e839b
commit 6fc252a692
19 changed files with 13862 additions and 207 deletions

13611
Data/Media/Maps/map Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -4,7 +4,7 @@ Game::Game(void) {
m_player = new Player(&m_map); m_player = new Player(&m_map);
m_npc = new NPC(&m_map); m_npc = new NPC(&m_map);
m_runGameReturnValue = GAME_RETURN_TO_MMENU; m_runGameReturnValue = gameMainMenu;
} }
Game::~Game(void) { Game::~Game(void) {
@ -12,8 +12,8 @@ Game::~Game(void) {
delete m_npc; delete m_npc;
} }
int Game::Run(const string savegameIDArg) { gameNavVal_t Game::Run(const string savegameIDArg) {
m_player->SetXY(100, 50); m_player->SetXY(50, 50);
m_player->LoadSprites("../Data/Media/Images/Characters/template.png", 40, 45); m_player->LoadSprites("../Data/Media/Images/Characters/template.png", 40, 45);
m_npc->SetXY(300, 300); m_npc->SetXY(300, 300);
@ -35,14 +35,14 @@ int Game::Run(const string savegameIDArg) {
Timer updateTimer; Timer updateTimer;
m_gameRenderTime.SetXY(10, 10); m_gameRenderTime.SetXY(10, 10);
m_gameRenderTime.SetTextBlended("Render - XX", "vsmall", COLOUR_BLACK); m_gameRenderTime.SetTextBlended("Render - XX", vsmall, COLOUR_BLACK);
m_gameUpdateTime.SetXY(10, 30); m_gameUpdateTime.SetXY(10, 30);
m_gameUpdateTime.SetTextBlended("Update - XX", "vsmall", COLOUR_BLACK); m_gameUpdateTime.SetTextBlended("Update - XX", vsmall, COLOUR_BLACK);
stringstream playerXYString; stringstream playerXYString;
m_playerXY.SetXY(10, 50); m_playerXY.SetXY(10, 50);
m_playerXY.SetTextBlended("Player coords - XX XX", "vsmall", COLOUR_BLACK); m_playerXY.SetTextBlended("Player coords - XX XX", vsmall, COLOUR_BLACK);
m_gameRunning = true; m_gameRunning = true;
while(m_gameRunning) { while(m_gameRunning) {
@ -73,12 +73,12 @@ int Game::Run(const string savegameIDArg) {
// Check to see if we are allowed to display debug info. // Check to see if we are allowed to display debug info.
if(debugEnabled) { if(debugEnabled) {
m_gameUpdateTime.SetTextBlended("Update - " + updateTimer.GetTicksStr(), "vsmall", COLOUR_BLACK); m_gameUpdateTime.SetTextBlended("Update - " + updateTimer.GetTicksStr(), vsmall, COLOUR_BLACK);
m_gameRenderTime.SetTextBlended("Render - " + renderTimer.GetTicksStr(), "vsmall", COLOUR_BLACK); m_gameRenderTime.SetTextBlended("Render - " + renderTimer.GetTicksStr(), vsmall, COLOUR_BLACK);
playerXYString.str(""); playerXYString.str("");
playerXYString << "Player coords: x" << m_player->GetX() << ", y" << m_player->GetY(); playerXYString << "Player coords: x" << m_player->GetX() << ", y" << m_player->GetY();
m_playerXY.SetTextBlended(playerXYString.str(), "vsmall", COLOUR_BLACK); m_playerXY.SetTextBlended(playerXYString.str(), vsmall, COLOUR_BLACK);
} }
} }
// Restrict the fps. // Restrict the fps.
@ -107,24 +107,24 @@ void Game::HandleInput(void) {
} }
else if(event.type == SDL_QUIT) { else if(event.type == SDL_QUIT) {
m_gameRunning = false; m_gameRunning = false;
m_runGameReturnValue = GAME_QUIT_GAME; m_runGameReturnValue = gameQuitGame;
break; break;
} }
} }
} else { } else {
switch(m_ingameMenu.HandleInput()) { switch(m_ingameMenu.HandleInput()) {
case INGAME_MENU_NOTHING: case ingameMenuNothing:
break; break;
case INGAME_MENU_RESUME: case ingameMenuResume:
m_ingameMenu.SetStatus(false); m_ingameMenu.SetStatus(false);
break; break;
case INGAME_MENU_SAVE_GAME: case ingameMenuSaveGame:
break; break;
case INGAME_MENU_LOAD_GAME: case ingameMenuLoadGame:
break; break;
case INGAME_MENU_OPTIONS: case ingameMenuOptions:
break; break;
case INGAME_MENU_EXIT_TO_MMENU: case ingameMenuMainMenu:
m_gameRunning = false; m_gameRunning = false;
break; break;
} }
@ -132,7 +132,7 @@ void Game::HandleInput(void) {
if(event.type == SDL_QUIT) { if(event.type == SDL_QUIT) {
m_gameRunning = false; m_gameRunning = false;
m_ingameMenu.SetStatus(false); m_ingameMenu.SetStatus(false);
m_runGameReturnValue = GAME_QUIT_GAME; m_runGameReturnValue = gameQuitGame;
} }
} }
} }
@ -203,7 +203,6 @@ void Game::LoadSavegame(const string savegameIDArg) {
// <map> - Parse the map file. // <map> - Parse the map file.
dataElem = dataElem->NextSiblingElement("map"); dataElem = dataElem->NextSiblingElement("map");
assert(dataElem != NULL); assert(dataElem != NULL);
printf("%s\n", dataElem->GetText());
m_map.Load(dataElem->GetText()); m_map.Load(dataElem->GetText());
// </map> // </map>
} }

View File

@ -19,15 +19,14 @@
#include "../libUnuk/Text.h" #include "../libUnuk/Text.h"
using namespace std; using namespace std;
const int GAME_RETURN_TO_MMENU = 0; enum gameNavVal_t { gameMainMenu, gameQuitGame };
const int GAME_QUIT_GAME = 1;
class Game { class Game {
public: public:
Game(void); Game(void);
~Game(void); ~Game(void);
int Run(const string savegameIDArg); gameNavVal_t Run(const string savegameIDArg);
private: private:
void HandleInput(void); void HandleInput(void);
@ -37,9 +36,13 @@ private:
void LoadSavegame(const string savegameIDArg); void LoadSavegame(const string savegameIDArg);
void SaveSavegame(void); void SaveSavegame(void);
static const int MAX_FPS = 6000;
static const int GAME_UPDATES_PER_SECOND = 60;
static const int SKIP_TICKS = 1000 / GAME_UPDATES_PER_SECOND;
bool m_gameRunning; bool m_gameRunning;
int m_runGameReturnValue; gameNavVal_t m_runGameReturnValue;
string m_saveGameID; string m_saveGameID;
string m_mapID; string m_mapID;
@ -48,10 +51,6 @@ private:
Text m_gameRenderTime; Text m_gameRenderTime;
Text m_playerXY; Text m_playerXY;
static const int MAX_FPS = 6000;
static const int GAME_UPDATES_PER_SECOND = 60;
static const int SKIP_TICKS = 1000 / GAME_UPDATES_PER_SECOND;
IngameMenu m_ingameMenu; IngameMenu m_ingameMenu;
Map m_map; Map m_map;

View File

@ -57,7 +57,7 @@ void Player::HandleInput(void) {
void Player::Update(void) { void Player::Update(void) {
Move(); Move();
SetCamera(); //SetCamera();
} }
void Player::SetName(string nameArg) { void Player::SetName(string nameArg) {

View File

@ -50,33 +50,34 @@ int main() {
Timer fpsCalc; Timer fpsCalc;
fpsCalc.Start(); fpsCalc.Start();
int gameReturnVal;
bool menuRunning = true; bool menuRunning = true;
while(menuRunning) { while(menuRunning) {
menu->Render(); menu->Render();
SDL_Flip(screen); SDL_Flip(screen);
switch(menu->HandleInput()) { switch(menu->HandleInput()) {
case MAIN_MENU_NOTHING: case mainMenuNothing:
break; break;
case MAIN_MENU_NEW_GAME: case mainMenuNewGame:
delete menu; delete menu;
game = new Game; game = new Game;
gameReturnVal = game->Run("save.xml"); switch(game->Run("save")) {
case gameMainMenu:
if(gameReturnVal == GAME_RETURN_TO_MMENU)
menu = new MainMenu; menu = new MainMenu;
else if(gameReturnVal == GAME_QUIT_GAME) break;
case gameQuitGame:
menuRunning = false; menuRunning = false;
break;
}
delete game; delete game;
break; break;
case MAIN_MENU_LOAD_GAME: case mainMenuLoadGame:
break; break;
case MAIN_MENU_OPTIONS: case mainMenuOptions:
break; break;
case MAIN_MENU_EXIT: case mainMenuExitGame:
menuRunning = false; menuRunning = false;
delete menu; delete menu;
break; break;

View File

@ -43,16 +43,16 @@ void Button::SetTextRGB(Uint8 r, Uint8 g, Uint8 b) {
m_textColour.g = g; m_textColour.g = g;
m_textColour.b = b; m_textColour.b = b;
m_text.SetTextBlended(m_text.GetText(), "small", m_textColour); m_text.SetTextBlended(m_text.GetText(), small, m_textColour);
} }
void Button::SetTextRGB(SDL_Color colour) { void Button::SetTextRGB(SDL_Color colour) {
m_textColour = colour; m_textColour = colour;
m_text.SetTextBlended(m_text.GetText(), "small", colour); m_text.SetTextBlended(m_text.GetText(), small, colour);
} }
void Button::SetText(string textArg) { void Button::SetText(string textArg) {
m_text.SetTextBlended(textArg, "small", m_textColour); m_text.SetTextBlended(textArg, small, m_textColour);
w = m_text.GetWidth(); w = m_text.GetWidth();
h = m_text.GetHeight(); h = m_text.GetHeight();

View File

@ -54,7 +54,7 @@ void Character::LoadSprites(string filename, int wArg, int hArg) {
void Character::AddSpeachBubble(string text) { void Character::AddSpeachBubble(string text) {
m_speachBubble.push_back(text); m_speachBubble.push_back(text);
m_speachBubbleText.SetTextBlended(text, "small", 0, 0, 0); //m_speachBubbleText.SetTextBlended(text, "small", 0, 0, 0);
if(m_speachBubbleTimer.IsStarted() == false) if(m_speachBubbleTimer.IsStarted() == false)
m_speachBubbleTimer.Start(); m_speachBubbleTimer.Start();
@ -125,7 +125,7 @@ void Character::Update(void) {
} }
} else { } else {
if(m_speachBubble.front() != m_speachBubbleText.GetText()) { if(m_speachBubble.front() != m_speachBubbleText.GetText()) {
m_speachBubbleText.SetTextBlended(m_speachBubble.front(), "small", 0, 0, 0); //m_speachBubbleText.SetTextBlended(m_speachBubble.front(), "small", 0, 0, 0);
} }
} }
} }
@ -136,27 +136,19 @@ void Character::Move(void) {
tileX = ((x + (w / 2)) / TILE_WIDTH); tileX = ((x + (w / 2)) / TILE_WIDTH);
tileY = ((y + (h / 2)) / TILE_HEIGHT); tileY = ((y + (h / 2)) / TILE_HEIGHT);
if((x < 0) || (x + w) > levelWidth) if((x < 0) || (x + w) > levelWidth) x -= xVel;
x -= xVel; if(CheckTileCollisions()) x -= xVel;
if(CheckTileCollisions()) if(CheckEntityCollisions()) x -= xVel;
x -= xVel; if(CheckCharacterCollisions()) x -= xVel;
if(CheckEntityCollisions())
x -= xVel;
if(CheckCharacterCollisions())
x -= xVel;
y += yVel; y += yVel;
tileX = ((x + (w / 2)) / TILE_WIDTH); tileX = ((x + (w / 2)) / TILE_WIDTH);
tileY = ((y + (h / 2)) / TILE_HEIGHT); tileY = ((y + (h / 2)) / TILE_HEIGHT);
if((y < 0) || (y + h) > levelHeight) if((y < 0) || (y + h) > levelHeight) y -= yVel;
y -= yVel; if(CheckTileCollisions()) y -= yVel;
if(CheckTileCollisions()) if(CheckEntityCollisions()) y -= yVel;
y -= yVel; if(CheckCharacterCollisions()) y -= yVel;
if(CheckEntityCollisions())
y -= yVel;
if(CheckCharacterCollisions())
y -= yVel;
} }
bool Character::CheckTileCollisions(void) { bool Character::CheckTileCollisions(void) {

View File

@ -1,7 +1,7 @@
#include "IngameMenu.h" #include "IngameMenu.h"
IngameMenu::IngameMenu(void) { IngameMenu::IngameMenu(void) {
m_isActive = false; m_active = false;
btnResume.SetOutRGB(200, 200, 200); btnResume.SetOutRGB(200, 200, 200);
btnResume.SetOverRGB(255, 255, 255); btnResume.SetOverRGB(255, 255, 255);
@ -38,7 +38,7 @@ IngameMenu::~IngameMenu(void) {
} }
int IngameMenu::HandleInput(void) { ingameMenuNavVal_t IngameMenu::HandleInput(void) {
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
btnResume.CheckMouseOver(); btnResume.CheckMouseOver();
btnSaveGame.CheckMouseOver(); btnSaveGame.CheckMouseOver();
@ -48,24 +48,24 @@ int IngameMenu::HandleInput(void) {
if(event.key.type == SDL_KEYDOWN) { if(event.key.type == SDL_KEYDOWN) {
if(event.key.keysym.sym == SDLK_ESCAPE) if(event.key.keysym.sym == SDLK_ESCAPE)
return INGAME_MENU_RESUME; return ingameMenuResume;
} }
else if(event.type == SDL_MOUSEBUTTONUP) { else if(event.type == SDL_MOUSEBUTTONUP) {
if(event.button.button == SDL_BUTTON_LEFT) { if(event.button.button == SDL_BUTTON_LEFT) {
if(btnResume.CheckMouseOver()) if(btnResume.CheckMouseOver())
return INGAME_MENU_RESUME; return ingameMenuResume;
else if(btnSaveGame.CheckMouseOver()) else if(btnSaveGame.CheckMouseOver())
return INGAME_MENU_SAVE_GAME; return ingameMenuSaveGame;
else if(btnLoadGame.CheckMouseOver()) else if(btnLoadGame.CheckMouseOver())
return INGAME_MENU_LOAD_GAME; return ingameMenuLoadGame;
else if(btnOptions.CheckMouseOver()) else if(btnOptions.CheckMouseOver())
return INGAME_MENU_OPTIONS; return ingameMenuOptions;
else if(btnExitToMenu.CheckMouseOver()) else if(btnExitToMenu.CheckMouseOver())
return INGAME_MENU_EXIT_TO_MMENU; return ingameMenuMainMenu;
} }
} }
} }
return INGAME_MENU_NOTHING; return ingameMenuNothing;
} }
void IngameMenu::Render(void) { void IngameMenu::Render(void) {

View File

@ -1,27 +1,33 @@
#ifndef _INGAMEMENU_H_ #ifndef _INGAMEMENU_H_
#define _INGAMEMENU_H_ #define _INGAMEMENU_H_
#include "Menu.h"
const int INGAME_MENU_NOTHING = 0; #include "../Unuk/Globals.h"
const int INGAME_MENU_RESUME = 1; #include "../Unuk/Constants.h"
const int INGAME_MENU_SAVE_GAME = 2; #include "Button.h"
const int INGAME_MENU_LOAD_GAME = 3; #include "ButtonToggle.h"
const int INGAME_MENU_OPTIONS = 4;
const int INGAME_MENU_EXIT_TO_MMENU = 5;
class IngameMenu : public Menu { enum ingameMenuNavVal_t {
ingameMenuNothing,
ingameMenuResume,
ingameMenuSaveGame,
ingameMenuLoadGame,
ingameMenuOptions,
ingameMenuMainMenu
};
class IngameMenu {
public: public:
IngameMenu(void); IngameMenu(void);
~IngameMenu(void); ~IngameMenu(void);
int HandleInput(void); ingameMenuNavVal_t HandleInput(void);
void Render(void); void Render(void);
void SetStatus(bool arg) { m_isActive = arg; } void SetStatus(bool arg) { m_active = arg; }
bool GetStatus(void) { return m_isActive; } bool GetStatus(void) { return m_active; }
private: private:
bool m_isActive; bool m_active;
Button btnResume; Button btnResume;
Button btnSaveGame; Button btnSaveGame;

View File

@ -10,7 +10,7 @@ MainMenu::MainMenu(void) {
btnNewGameActive = false; btnNewGameActive = false;
lblNewGame.SetXY(275, 160); lblNewGame.SetXY(275, 160);
lblNewGame.SetTextBlended("This will delete your current game, are you sure?", "vsmall", 0, 0, 0); lblNewGame.SetTextBlended("This will delete your current game, are you sure?", vsmall, 0, 0, 0);
rectNewGame.SetRGB(200, 200, 200); rectNewGame.SetRGB(200, 200, 200);
rectNewGame.SetXY(250, 150); rectNewGame.SetXY(250, 150);
@ -47,9 +47,9 @@ MainMenu::MainMenu(void) {
btnExit.SetXY(100, 300); btnExit.SetXY(100, 300);
lblMenu.SetXY(100, 75); lblMenu.SetXY(100, 75);
lblMenu.SetTextBlended("Unuk", "vlarge", 0, 0, 0); lblMenu.SetTextBlended("Unuk", vlarge, 0, 0, 0);
m_background.Load("MainMenu"); //m_background.Load("MainMenu");
camera.x = 0; camera.x = 0;
camera.y = 0; camera.y = 0;
@ -59,7 +59,7 @@ MainMenu::~MainMenu(void) {
} }
int MainMenu::HandleInput(void) { mainMenuNavVal_t MainMenu::HandleInput(void) {
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
btnNewGame.CheckMouseOver(); btnNewGame.CheckMouseOver();
if(btnNewGameActive) { if(btnNewGameActive) {
@ -76,29 +76,29 @@ int MainMenu::HandleInput(void) {
if(btnNewGame.CheckMouseOver()) if(btnNewGame.CheckMouseOver())
btnNewGameActive = !btnNewGameActive; btnNewGameActive = !btnNewGameActive;
else if(btnLoadGame.CheckMouseOver()) else if(btnLoadGame.CheckMouseOver())
return MAIN_MENU_LOAD_GAME; return mainMenuLoadGame;
else if(btnOptions.CheckMouseOver()) else if(btnOptions.CheckMouseOver())
return MAIN_MENU_OPTIONS; return mainMenuOptions;
else if(btnExit.CheckMouseOver()) else if(btnExit.CheckMouseOver())
return MAIN_MENU_EXIT; return mainMenuOptions;
if(btnNewGameActive) { if(btnNewGameActive) {
if(btnNewGameYes.CheckMouseOver()) if(btnNewGameYes.CheckMouseOver())
return MAIN_MENU_NEW_GAME; return mainMenuNewGame;
else if(btnNewGameNo.CheckMouseOver()) else if(btnNewGameNo.CheckMouseOver())
btnNewGameActive = false; btnNewGameActive = false;
} }
} }
} }
else if(event.type == SDL_QUIT) { else if(event.type == SDL_QUIT) {
return MAIN_MENU_EXIT; return mainMenuExitGame;
} }
} }
return MAIN_MENU_NOTHING; return mainMenuNothing;
} }
void MainMenu::Render(void) { void MainMenu::Render(void) {
m_background.Render(); //m_background.Render();
lblMenu.Render(); lblMenu.Render();

View File

@ -1,23 +1,25 @@
#ifndef _MAINMENU_H_ #ifndef _MAINMENU_H_
#define _MAINMENU_H_ #define _MAINMENU_H_
#include "../Unuk/Constants.h" #include "../Unuk/Constants.h"
#include "Menu.h" #include "Button.h"
#include "Map.h" #include "Map.h"
#include "Rect.h" #include "Rect.h"
#include "Text.h" #include "Text.h"
const int MAIN_MENU_NOTHING = 0; enum mainMenuNavVal_t {
const int MAIN_MENU_NEW_GAME = 1; mainMenuNothing,
const int MAIN_MENU_LOAD_GAME = 2; mainMenuNewGame,
const int MAIN_MENU_OPTIONS = 3; mainMenuLoadGame,
const int MAIN_MENU_EXIT = 4; mainMenuOptions,
mainMenuExitGame
};
class MainMenu : public Menu { class MainMenu {
public: public:
MainMenu(void); MainMenu(void);
~MainMenu(void); ~MainMenu(void);
int HandleInput(void); mainMenuNavVal_t HandleInput(void);
void Render(void); void Render(void);
private: private:

View File

@ -1,107 +1,164 @@
#include "Map.h" #include "Map.h"
Map::Map(void) { Map::Map(void) {
//m_characters = CharacterManager;
} }
Map::~Map(void) { Map::~Map(void) {
//delete m_characters;
} }
void Map::Load(const string filename) { void Map::Load(const string filename) {
Unload();
m_currentMap = filename; m_currentMap = filename;
string fullMapPath = "../Data/Media/Maps/" + filename; string fullMapPath = "../Data/Media/Maps/" + filename;
ifstream mapFile(fullMapPath.c_str()); TiXmlDocument mapFile(fullMapPath);
assert(mapFile.is_open());
Unload(); assert(mapFile.LoadFile() == true);
// Read in from the map file, one line at a time. // Getting dirty with some XML. This seems like a nicer
string line; // approach to loading maps, rather than parsing tet files.
while(getline(mapFile, line)) { TiXmlElement* rootElem = NULL;
m_mapRows = 1; TiXmlElement* lineElem = NULL;
TiXmlElement* tileElem = NULL;
TiXmlElement* dataElem = NULL;
istringstream iss(line); // We will set x and y positions to zero for now, as we
// are going to set them withing the XML file.
x = 0;
y = 0;
string tileName; // <map> - Let's start parsing the map.
while(iss >> tileName) { rootElem = mapFile.FirstChildElement("map");
string fullTilePath = "../Data/Media/Images/Tiles/" + tileName + ".png"; assert(rootElem != NULL);
m_tile[m_mapRows][m_mapColumns].SetTileTexture(m_tileTextures.Add(fullTilePath)); if(rootElem) {
// <line> - We want to tile one line at a time. line represents
// the row we are tiling.
lineElem = rootElem->FirstChildElement("line");
assert(lineElem != NULL);
while(lineElem) {
y++;
x = 0;
// Read the tile solidity. // <tile> - Then we will select the tile. and increment x to keep tiling that row.
bool tileSolidity; tileElem = lineElem->FirstChildElement("tile");
iss >> tileSolidity; assert(tileElem != NULL);
m_tile[m_mapRows][m_mapColumns].SetTileSolidity(tileSolidity); while(tileElem) {
x++;
m_tile[x][y].SetTileXY((x - 1) * TILE_WIDTH, (y - 1) * TILE_HEIGHT);
// Set the tile x and y variable. // <tileTexture> - Apply a teture to the tile.
m_tile[m_mapRows][m_mapColumns].SetTileXY((m_mapRows - 1) * TILE_WIDTH, (m_mapColumns - 1) * TILE_HEIGHT); dataElem = tileElem->FirstChildElement("tileTexture");
assert(dataElem != NULL);
stringstream tilePath;
tilePath << "../Data/Media/Images/Tiles/" << dataElem->GetText() << ".png";
m_tile[x][y].SetTileTexture(m_tileTextures.Add(tilePath.str()));
// <tileTexture> - Finished applying the texture, move to the next sibling.
// Read the entity textures. // <solidTile> - Check to see if the tile is solid or not.
string entityName; dataElem = dataElem->NextSiblingElement("solidTile");
iss >> entityName; assert(dataElem != NULL);
string tileSolidity = dataElem->GetText();
assert(tileSolidity == "false" || tileSolidity == "true");
if(tileSolidity == "false")
m_tile[x][y].SetTileSolidity(false);
else
m_tile[x][y].SetTileSolidity(true);
// </solidTile>
if(entityName == "n") { // <entityTexture>
m_tile[m_mapRows][m_mapColumns].SetEntityTexture(NULL); dataElem = dataElem->NextSiblingElement("entityTexture");
} else { assert(dataElem != NULL);
string entityPath = "../Data/Media/Images/Entities/" + entityName + ".png"; string entityName = dataElem->GetText();
if(entityName == "null")
m_tile[x][y].SetEntityTexture(NULL);
else {
stringstream entityPath;
entityPath << "../Data/Media/Images/Entities/" << entityName << ".png";
m_tile[x][y].SetEntityTexture(m_entityTextures.AddAlpha(entityPath.str()));
m_tile[m_mapRows][m_mapColumns].SetEntityTexture(m_entityTextures.AddAlpha(entityPath)); m_tile[x][y].SetEntityXY(m_tile[x][y].GetTileX() - (m_tile[x][y].GetEntityWidth() / 2 + TILE_WIDTH / 2),
m_tile[x][y].GetTileY() - (m_tile[x][y].GetEntityHeight() / 2 + TILE_HEIGHT / 2));
// Set the entities x and y variables.
m_tile[m_mapRows][m_mapColumns].SetEntityXY(
m_tile[m_mapRows][m_mapColumns].GetTileX() - (m_tile[m_mapRows][m_mapColumns].GetEntityWidth() / 2 + TILE_WIDTH / 2),
m_tile[m_mapRows][m_mapColumns].GetTileY() - (m_tile[m_mapRows][m_mapColumns].GetEntityHeight() / 2 + TILE_HEIGHT / 2));
} }
// Read the entity solidity. // </entityTexture>
bool entitySolidity;
iss >> entitySolidity;
m_tile[m_mapRows][m_mapColumns].SetEntitySolidity(entitySolidity);
// Read the tile zlevel. // <SolidEntity>
int zLevel; dataElem = dataElem->NextSiblingElement("solidEntity");
iss >> zLevel; assert(dataElem != NULL);
m_tile[m_mapRows][m_mapColumns].SetZLevel(zLevel); string entitySolidity = dataElem->GetText();
assert(entitySolidity == "false" || entitySolidity == "true");
if(entitySolidity == "false")
m_tile[x][y].SetEntitySolidity(false);
else
m_tile[x][y].SetEntitySolidity(true);
// </solidEntity>
// Read the map transition value. // <zlevel>
string mapTransitionName; dataElem = dataElem->NextSiblingElement("zlevel");
iss >> mapTransitionName; assert(dataElem != NULL);
m_tile[m_mapRows][m_mapColumns].SetMapTransitionName(mapTransitionName); m_tile[x][y].SetZLevel(atoi(dataElem->GetText()));
// </zlevel>
// Read the transition x and y. // <mapTransition>
int mapTransitionX, mapTransitionY; dataElem = dataElem->NextSiblingElement("mapTransition");
iss >> mapTransitionX; assert(dataElem != NULL);
iss >> mapTransitionY; m_tile[x][y].SetMapTransitionName(dataElem->GetText());
m_tile[m_mapRows][m_mapColumns].SetMapTransitionXY(mapTransitionX, mapTransitionY); // </mapTransition>
m_mapRows++; // <mapTransX>
assert(m_mapRows < TILE_ARRAY_SIZE); dataElem = dataElem->NextSiblingElement("mapTransX");
assert(dataElem != NULL);
int mapTransX = atoi(dataElem->GetText());
// </mapTransX>
// <mapTransY>
dataElem = dataElem->NextSiblingElement("mapTransY");
assert(dataElem != NULL);
int mapTransY = atoi(dataElem->GetText());
// </mapTransY>
tileElem = tileElem->NextSiblingElement("tile");
} }
m_mapColumns++; //</tile>
assert(m_mapColumns < TILE_ARRAY_SIZE);
lineElem = lineElem->NextSiblingElement("line");
} }
levelWidth = (m_mapRows - 1) * TILE_WIDTH; // </line>
levelHeight = (m_mapColumns - 1) * TILE_HEIGHT; }
// </map>
levelWidth = (x - 1) * TILE_WIDTH;
levelHeight = (y - 1) * TILE_HEIGHT;
//character->Load(filename); //character->Load(filename);
} }
void Map::Render(void) { void Map::Render(void) {
for(int j = 1; j < x; j++)
for(int i = 1; i < y; i++) {
ApplySurface(m_tile[j][i].GetTileX(), m_tile[j][i].GetTileY(), m_tile[j][i].GetTileTexture(), screen);
if(m_tile[j][i].GetEntityTexture() != NULL)
ApplySurface(m_tile[j][i].GetEntityX(), m_tile[j][i].GetEntityY(), m_tile[j][i].GetEntityTexture(), screen);
}
return;
int xOrig = (camera.x / TILE_WIDTH) + 1; int xOrig = (camera.x / TILE_WIDTH) + 1;
int yOrig = (camera.y / TILE_HEIGHT) + 1; int yOrig = (camera.y / TILE_HEIGHT) + 1;
int xEnd = xOrig + (SCREEN_WIDTH / TILE_WIDTH); int xEnd = xOrig + (SCREEN_WIDTH / TILE_WIDTH);
int yEnd = yOrig + (SCREEN_HEIGHT / TILE_HEIGHT); int yEnd = yOrig + (SCREEN_HEIGHT / TILE_HEIGHT);
if(xEnd < m_mapRows) if(xEnd < x)
xEnd++; xEnd++;
if(yEnd < m_mapColumns) if(yEnd < y)
yEnd++; yEnd++;
for(int j = xOrig; j < xEnd; j++) for(int j = 1; j < xEnd; j++)
for(int i = yOrig; i < yEnd; i++) { for(int i = 1; i < yEnd; i++) {
ApplySurface(m_tile[j][i].GetTileX(), m_tile[j][i].GetTileY(), ApplySurface(m_tile[j][i].GetTileX(), m_tile[j][i].GetTileY(),
m_tile[j][i].GetTileTexture(), screen); m_tile[j][i].GetTileTexture(), screen);
if(m_tile[j][i].GetEntityTexture() != NULL) { if(m_tile[j][i].GetEntityTexture() != NULL) {
Debug::logger->message("Entity");
ApplySurface(m_tile[j][i].GetEntityX(), m_tile[j][i].GetEntityY(), ApplySurface(m_tile[j][i].GetEntityX(), m_tile[j][i].GetEntityY(),
m_tile[j][i].GetEntityTexture(), screen); m_tile[j][i].GetEntityTexture(), screen);
} }
@ -112,18 +169,13 @@ void Map::Unload(void) {
m_tileTextures.Unload(); m_tileTextures.Unload();
m_entityTextures.Unload(); m_entityTextures.Unload();
// Start at 1,1 so we do not have to be concerned about messy
// bounds checking when accessing the tile array within the game loop.
m_mapRows = 1;
m_mapColumns = 1;
// As we are not doing bounds checking inside the game loop
// we don't want there to be a solid entity with w,h of ($RAND)
// creating an invisible wall anywhere.
for(int i = 0; i < TILE_ARRAY_SIZE; i++) { for(int i = 0; i < TILE_ARRAY_SIZE; i++) {
for(int j = 0; j < TILE_ARRAY_SIZE; j++) { for(int j = 0; j < TILE_ARRAY_SIZE; j++) {
m_tile[i][j].SetTileTexture(NULL); m_tile[i][j].SetTileTexture(NULL);
m_tile[i][j].SetTileSolidity(false);
m_tile[i][j].SetEntityTexture(NULL); m_tile[i][j].SetEntityTexture(NULL);
m_tile[i][j].SetEntitySolidity(false);
m_tile[i][j].SetMapTransitionName("null");
} }
} }
} }

View File

@ -6,6 +6,7 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <assert.h> #include <assert.h>
#include <tinyxml.h>
#include "../Unuk/Globals.h" #include "../Unuk/Globals.h"
#include "../Unuk/Constants.h" #include "../Unuk/Constants.h"
@ -13,6 +14,7 @@
#include "ApplySurface.h" #include "ApplySurface.h"
#include "TextureManager.h" #include "TextureManager.h"
#include "MapTile.h" #include "MapTile.h"
#include "Debug.h"
using namespace std; using namespace std;
//class CharacterManager; //class CharacterManager;
@ -47,12 +49,11 @@ private:
void Unload(void); void Unload(void);
string m_currentMap; string m_currentMap;
int m_mapColumns; int x;
int m_mapRows; int y;
TextureManager m_tileTextures; TextureManager m_tileTextures;
TextureManager m_entityTextures; TextureManager m_entityTextures;
//CharacterManager* m_characters;
static const int TILE_ARRAY_SIZE = 150; static const int TILE_ARRAY_SIZE = 150;
MapTile m_tile[TILE_ARRAY_SIZE][TILE_ARRAY_SIZE]; MapTile m_tile[TILE_ARRAY_SIZE][TILE_ARRAY_SIZE];

View File

@ -62,10 +62,10 @@ private:
int m_entityH; int m_entityH;
// -1 is a 'special' tile, the next tile that the player walks // -1 is a 'special' tile, the next tile that the player walks
// on is the new player z-level. // on is the players new zlevel.
int m_zLevel; int m_zLevel;
//If not 'n', switch map when the player walks on this tile. // If not 'null', switch map when the player walks on this tile.
string m_mapTransitionName; string m_mapTransitionName;
int m_mapTransitionX; int m_mapTransitionX;
int m_mapTransitionY; int m_mapTransitionY;

View File

@ -50,78 +50,68 @@ void Text::SetXY(int xArg, int yArg) {
y = yArg; y = yArg;
} }
int Text::SetTextBlended(string textArg, string size, SDL_Color colour) { int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) {
m_textContents = textArg; m_textContents = textArg;
if(m_text != NULL) { if(m_text != NULL) {
SDL_FreeSurface(m_text); SDL_FreeSurface(m_text);
} }
if(size == "vsmall") { if(size == vsmall) {
m_text = TTF_RenderText_Blended(vSmallFont, textArg.c_str(), colour); m_text = TTF_RenderText_Blended(vSmallFont, textArg.c_str(), colour);
return 1; return 1;
} }
else if(size == "small") { else if(size == small) {
m_text = TTF_RenderText_Blended(smallFont, textArg.c_str(), colour); m_text = TTF_RenderText_Blended(smallFont, textArg.c_str(), colour);
return 1; return 1;
} }
else if(size == "medium") { else if(size == medium) {
m_text = TTF_RenderText_Blended(mediumFont, textArg.c_str(), colour); m_text = TTF_RenderText_Blended(mediumFont, textArg.c_str(), colour);
return 1; return 1;
} }
else if(size == "large") { else if(size == large) {
m_text = TTF_RenderText_Blended(largeFont, textArg.c_str(), colour); m_text = TTF_RenderText_Blended(largeFont, textArg.c_str(), colour);
return 1; return 1;
} } else {
else if(size == "vlarge") {
m_text = TTF_RenderText_Blended(vLargeFont, textArg.c_str(), colour); m_text = TTF_RenderText_Blended(vLargeFont, textArg.c_str(), colour);
return 1; return 1;
} else {
Debug::logger->message("Text::SetTextBlended(): Invalid size argument %s. Defaulted to small.", size.c_str());
m_text = TTF_RenderText_Blended(smallFont, textArg.c_str(), colour);
return 0;
} }
} }
int Text::SetTextBlended(string textArg, string size, Uint8 r, Uint8 g, Uint8 b) { int Text::SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b) {
SDL_Color f = { r, g, b }; SDL_Color f = { r, g, b };
return SetTextBlended(textArg, size, f); return SetTextBlended(textArg, size, f);
} }
int Text::SetTextShaded(string textArg, string size, SDL_Color colour, SDL_Color bgColour) { int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_Color bgColour) {
m_textContents = textArg; m_textContents = textArg;
if(m_text != NULL) { if(m_text != NULL) {
SDL_FreeSurface(m_text); SDL_FreeSurface(m_text);
} }
if(size == "vsmall") { if(size == vsmall) {
m_text = TTF_RenderText_Shaded(vSmallFont, textArg.c_str(), colour, bgColour); m_text = TTF_RenderText_Shaded(vSmallFont, textArg.c_str(), colour, bgColour);
return 1; return 1;
} }
else if(size == "small") { else if(size == small) {
m_text = TTF_RenderText_Shaded(smallFont, textArg.c_str(), colour, bgColour); m_text = TTF_RenderText_Shaded(smallFont, textArg.c_str(), colour, bgColour);
return 1; return 1;
} }
else if(size == "medium") { else if(size == medium) {
m_text = TTF_RenderText_Shaded(mediumFont, textArg.c_str(), colour, bgColour); m_text = TTF_RenderText_Shaded(mediumFont, textArg.c_str(), colour, bgColour);
return 1; return 1;
} }
else if(size == "large") { else if(size == large) {
m_text = TTF_RenderText_Shaded(largeFont, textArg.c_str(), colour, bgColour); m_text = TTF_RenderText_Shaded(largeFont, textArg.c_str(), colour, bgColour);
return 1; return 1;
} } else {
else if(size == "vlarge") {
m_text = TTF_RenderText_Shaded(vLargeFont, textArg.c_str(), colour, bgColour); m_text = TTF_RenderText_Shaded(vLargeFont, textArg.c_str(), colour, bgColour);
return 1; return 1;
} else {
Debug::logger->message("Text::SetTextBlended(): Invalid size argument %s. Defaulted to small.", size.c_str());
m_text = TTF_RenderText_Shaded(smallFont, textArg.c_str(), colour, bgColour);
return 0;
} }
} }
int Text::SetTextShaded(string textArg, string size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB) { int Text::SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB) {
SDL_Color f = { rF, gF, bF }; SDL_Color f = { rF, gF, bF };
SDL_Color b = { rB, gB, bB }; SDL_Color b = { rB, gB, bB };
return SetTextShaded(textArg, size, f, b); return SetTextShaded(textArg, size, f, b);

View File

@ -10,6 +10,8 @@
#include "Debug.h" #include "Debug.h"
using namespace std; using namespace std;
enum textSizes_t { vsmall, small, medium, large, vlarge };
class Text { class Text {
public: public:
Text(void); Text(void);
@ -28,10 +30,10 @@ public:
void SetXY(int xArg, int yArg); void SetXY(int xArg, int yArg);
int SetTextBlended(string textArg, string size, SDL_Color); int SetTextBlended(string textArg, textSizes_t size, SDL_Color);
int SetTextBlended(string textArg, string size, Uint8 r, Uint8 g, Uint8 b); int SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b);
int SetTextShaded(string textArg, string size, SDL_Color, SDL_Color); int SetTextShaded(string textArg, textSizes_t size, SDL_Color, SDL_Color);
int SetTextShaded(string textArg, string size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB); int SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB);
void Render(void); void Render(void);
void Render(int xArg, int yArg); void Render(int xArg, int yArg);

View File

@ -17,7 +17,7 @@ void TextureManager::Unload(void) {
} }
SDL_Surface* TextureManager::Add(string filename) { SDL_Surface* TextureManager::Add(string filename) {
assert(m_allocated < TEXTURE_NODE_SIZE); assert(m_allocated < TEXTURE_ARR_SIZE - 1);
// Has the texture been loaded already? // Has the texture been loaded already?
for(int i = 0; i < m_allocated; i++) { for(int i = 0; i < m_allocated; i++) {
@ -35,7 +35,7 @@ SDL_Surface* TextureManager::Add(string filename) {
} }
SDL_Surface* TextureManager::AddAlpha(string filename) { SDL_Surface* TextureManager::AddAlpha(string filename) {
assert(m_allocated < TEXTURE_NODE_SIZE); assert(m_allocated < TEXTURE_ARR_SIZE - 1);
// Has the texture been loaded already? // Has the texture been loaded already?
for(int i = 0; i < m_allocated; i++) { for(int i = 0; i < m_allocated; i++) {

View File

@ -10,8 +10,8 @@ using namespace std;
* The Texture Manager will keep a small "Database" * The Texture Manager will keep a small "Database"
* of the name of the texture that is loaded and the * of the name of the texture that is loaded and the
* actual texture so we can query it with the filename * actual texture so we can query it with the filename
* and it will return an index that we can use to retrieve * and it will return the teture if it is already in memory
* the texture. * or load the tture if it is not.
*/ */
class TextureManager { class TextureManager {
public: public:
@ -31,8 +31,8 @@ private:
}; };
// We should not need more than a hundred.. // We should not need more than a hundred..
static const int TEXTURE_NODE_SIZE = 100; static const int TEXTURE_ARR_SIZE = 100;
textureNode textures[TEXTURE_NODE_SIZE]; textureNode textures[TEXTURE_ARR_SIZE];
int m_allocated; int m_allocated;
}; };