Added basic save file loading. Time to move this bitching map stuff to XML.

- Also added some speach bubble stuff, it is a little messy however.
This commit is contained in:
Rtch90 2011-12-14 01:10:27 +00:00
parent 5e51ef0fd1
commit 04ac0e839b
19 changed files with 198 additions and 69 deletions

1
Save/1
View File

@ -1 +0,0 @@
55

0
Save/2
View File

6
Save/save Normal file
View File

@ -0,0 +1,6 @@
<save>
<name>Allanis</name>
<x>150</x>
<y>150</y>
<map>map</map>
</save>

View File

@ -1,6 +1,6 @@
############################################################################# #############################################################################
# Makefile for building: Unuk-QT # Makefile for building: Unuk-QT
# Generated by qmake (2.01a) (Qt 4.7.3) on: Tue Dec 13 22:04:29 2011 # Generated by qmake (2.01a) (Qt 4.7.3) on: Wed Dec 14 00:58:57 2011
# Project: Unuk-QT.pro # Project: Unuk-QT.pro
# Template: app # Template: app
# Command: /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ CONFIG+=debug -o Makefile Unuk-QT.pro # Command: /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ CONFIG+=debug -o Makefile Unuk-QT.pro
@ -16,7 +16,7 @@ CXXFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I.
LINK = g++ LINK = g++
LFLAGS = LFLAGS =
LIBS = $(SUBLIBS) -L/usr/lib -lGL -lSDL -lSDL_ttf -lSDL_image -lGLU -lQtGui -lQtCore -lpthread LIBS = $(SUBLIBS) -L/usr/lib -lGL -lSDL -lSDL_ttf -lSDL_image -lSDL_gfx -ltinyxml -lGLU -lQtGui -lQtCore -lpthread
AR = ar cqs AR = ar cqs
RANLIB = RANLIB =
QMAKE = /usr/bin/qmake-qt4 QMAKE = /usr/bin/qmake-qt4
@ -396,7 +396,9 @@ Character.o: ../src/libUnuk/Character.cpp ../src/libUnuk/Character.h \
../src/libUnuk/TextureManager.h \ ../src/libUnuk/TextureManager.h \
../src/libUnuk/MapTile.h \ ../src/libUnuk/MapTile.h \
../src/libUnuk/Entity.h \ ../src/libUnuk/Entity.h \
../src/libUnuk/Timer.h ../src/libUnuk/Timer.h \
../src/libUnuk/Text.h \
../src/libUnuk/Font.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o Character.o ../src/libUnuk/Character.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o Character.o ../src/libUnuk/Character.cpp
NPC.o: ../src/libUnuk/NPC.cpp ../src/libUnuk/NPC.h \ NPC.o: ../src/libUnuk/NPC.cpp ../src/libUnuk/NPC.h \
@ -411,7 +413,9 @@ NPC.o: ../src/libUnuk/NPC.cpp ../src/libUnuk/NPC.h \
../src/libUnuk/TextureManager.h \ ../src/libUnuk/TextureManager.h \
../src/libUnuk/MapTile.h \ ../src/libUnuk/MapTile.h \
../src/libUnuk/Entity.h \ ../src/libUnuk/Entity.h \
../src/libUnuk/Timer.h ../src/libUnuk/Timer.h \
../src/libUnuk/Text.h \
../src/libUnuk/Font.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o NPC.o ../src/libUnuk/NPC.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o NPC.o ../src/libUnuk/NPC.cpp
Player.o: ../src/Unuk/Player.cpp ../src/Unuk/Player.h \ Player.o: ../src/Unuk/Player.cpp ../src/Unuk/Player.h \
@ -426,7 +430,9 @@ Player.o: ../src/Unuk/Player.cpp ../src/Unuk/Player.h \
../src/libUnuk/TextureManager.h \ ../src/libUnuk/TextureManager.h \
../src/libUnuk/MapTile.h \ ../src/libUnuk/MapTile.h \
../src/libUnuk/Entity.h \ ../src/libUnuk/Entity.h \
../src/libUnuk/Timer.h ../src/libUnuk/Timer.h \
../src/libUnuk/Text.h \
../src/libUnuk/Font.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o Player.o ../src/Unuk/Player.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o Player.o ../src/Unuk/Player.cpp
Game.o: ../src/Unuk/Game.cpp ../src/Unuk/Game.h \ Game.o: ../src/Unuk/Game.cpp ../src/Unuk/Game.h \
@ -443,12 +449,12 @@ Game.o: ../src/Unuk/Game.cpp ../src/Unuk/Game.h \
../src/libUnuk/MapTile.h \ ../src/libUnuk/MapTile.h \
../src/libUnuk/Entity.h \ ../src/libUnuk/Entity.h \
../src/libUnuk/Timer.h \ ../src/libUnuk/Timer.h \
../src/libUnuk/Text.h \
../src/libUnuk/Font.h \
../src/libUnuk/IngameMenu.h \ ../src/libUnuk/IngameMenu.h \
../src/libUnuk/Menu.h \ ../src/libUnuk/Menu.h \
../src/libUnuk/Button.h \ ../src/libUnuk/Button.h \
../src/libUnuk/Input.h \ ../src/libUnuk/Input.h \
../src/libUnuk/Text.h \
../src/libUnuk/Font.h \
../src/libUnuk/Rect.h \ ../src/libUnuk/Rect.h \
../src/libUnuk/ButtonToggle.h \ ../src/libUnuk/ButtonToggle.h \
../src/libUnuk/NPC.h ../src/libUnuk/NPC.h

BIN
Unuk-QT/Unuk-QT Executable file

Binary file not shown.

View File

@ -2,6 +2,8 @@ LIBS += -lGL \
-lSDL \ -lSDL \
-lSDL_ttf \ -lSDL_ttf \
-lSDL_image \ -lSDL_image \
-lSDL_gfx \
-ltinyxml \
-lGLU -lGLU
RESOURCES += RESOURCES +=
OTHER_FILES += ../src/libUnuk/Debug.o OTHER_FILES += ../src/libUnuk/Debug.o

View File

@ -162,19 +162,54 @@ void Game::Render(void) {
m_ingameMenu.Render(); m_ingameMenu.Render();
} }
SDL_Flip(screen); SDL_Flip(screen);
SDL_Flip(screen); //SDL_Flip(screen);
} }
void Game::LoadSavegame(const string savegameIDArg) { void Game::LoadSavegame(const string savegameIDArg) {
m_saveGameID = savegameIDArg; m_saveGameID = savegameIDArg;
string saveFilename = "../Save/" + m_saveGameID; string saveFilename = "../Save/" + m_saveGameID;
ifstream saveFile(saveFilename.c_str()); // Converting to XML ftw!
assert(saveFile.is_open()); TiXmlDocument mapFile(saveFilename.c_str());
assert(mapFile.LoadFile() == true);
// Read stuff. TiXmlElement* rootElem = NULL;
m_map.Load("TestLevel"); TiXmlElement* dataElem = NULL;
// <save> - Grab a save file.
rootElem = mapFile.FirstChildElement("save");
assert(rootElem != NULL);
if(rootElem) {
// <name> - Parse the player name.
dataElem = rootElem->FirstChildElement("name");
assert(dataElem != NULL);
m_player->SetName(dataElem->GetText());
// </name>
// <x> - Parse the player x coord.
dataElem = dataElem->NextSiblingElement("x");
assert(dataElem != NULL);
int playerX = atoi(dataElem->GetText());
// </x>
// <y> - Parse the player y coord.
dataElem = dataElem->NextSiblingElement("y");
assert(dataElem != NULL);
int playerY = atoi(dataElem->GetText());
// </y>
m_player->SetXY(playerX, playerY);
// <map> - Parse the map file.
dataElem = dataElem->NextSiblingElement("map");
assert(dataElem != NULL);
printf("%s\n", dataElem->GetText());
m_map.Load(dataElem->GetText());
// </map>
}
// <save>
// </save>
} }
void Game::SaveSavegame(void) { void Game::SaveSavegame(void) {

View File

@ -6,6 +6,7 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <tinyxml.h>
#include "Constants.h" #include "Constants.h"
#include "Globals.h" #include "Globals.h"

View File

@ -2,12 +2,17 @@
SDL_Surface* screen = NULL; SDL_Surface* screen = NULL;
SDL_Surface* errorTexture = NULL; SDL_Surface* errorTexture = NULL;
SDL_Rect camera; SDL_Rect camera;
SDL_Event event; SDL_Event event;
int levelWidth; int levelWidth;
int levelHeight; int levelHeight;
bool debugEnabled = true; bool debugEnabled = true;
int plrKeyUp;
int plrKeyDown;
int plrKeyeft;
int plrKeyRight;
int plrBtnSpell;

View File

@ -1,13 +1,10 @@
#ifndef _GLOBALS_H_ #ifndef _GLOBALS_H_
#define _GLOBALS_H_ #define _GLOBALS_H_
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
extern SDL_Surface* screen; extern SDL_Surface* screen;
extern SDL_Surface* errorTexture; extern SDL_Surface* errorTexture;
extern SDL_Rect camera; extern SDL_Rect camera;
extern SDL_Event event; extern SDL_Event event;
extern int levelWidth; extern int levelWidth;

View File

@ -60,6 +60,10 @@ void Player::Update(void) {
SetCamera(); SetCamera();
} }
void Player::SetName(string nameArg) {
m_name = nameArg;
}
void Player::SetCamera(void) { void Player::SetCamera(void) {
camera.x = (x + w / 2) - SCREEN_WIDTH / 2; camera.x = (x + w / 2) - SCREEN_WIDTH / 2;
camera.y = (y + h / 2) - SCREEN_HEIGHT / 2; camera.y = (y + h / 2) - SCREEN_HEIGHT / 2;

View File

@ -14,6 +14,9 @@ public:
void HandleInput(void); void HandleInput(void);
void Update(void); void Update(void);
void SetName(string nameArg);
string GetName(void) { return m_name; }
protected: protected:
void Move(void); void Move(void);
void CheckTileCollisions(void); void CheckTileCollisions(void);
@ -21,6 +24,8 @@ protected:
private: private:
void SetCamera(void); void SetCamera(void);
static const float PLAYER_SPEED; static const float PLAYER_SPEED;
string m_name;
}; };
#endif #endif

View File

@ -63,7 +63,7 @@ int main() {
delete menu; delete menu;
game = new Game; game = new Game;
gameReturnVal = game->Run("1"); gameReturnVal = game->Run("save.xml");
if(gameReturnVal == GAME_RETURN_TO_MMENU) if(gameReturnVal == GAME_RETURN_TO_MMENU)
menu = new MainMenu; menu = new MainMenu;

View File

@ -4,7 +4,7 @@
const float Character::CHARACTER_SPEED = 3.5f; const float Character::CHARACTER_SPEED = 3.5f;
static list<Character*>collisionList; static list<Character*>collisionList;
static list<Character*>::iterator collisionIterator; static list<Character*>::iterator collisionIter;
Character::Character(Map* mapArg) { Character::Character(Map* mapArg) {
map = mapArg; map = mapArg;
@ -24,15 +24,64 @@ Character::Character(Map* mapArg) {
Character::~Character(void) { Character::~Character(void) {
SDL_FreeSurface(m_texture); SDL_FreeSurface(m_texture);
for(collisionIterator = collisionList.begin(); collisionIterator != collisionList.end(); collisionIterator++) { for(collisionIter = collisionList.begin(); collisionIter != collisionList.end(); collisionIter++) {
if((*collisionIterator) == this) { if((*collisionIter) == this) {
collisionList.erase(collisionIterator); collisionList.erase(collisionIter);
break; break;
} }
} }
} }
void Character::LoadSprites(string filename, int wArg, int hArg) {
if(m_texture != NULL)
SDL_FreeSurface(m_texture);
m_texture = LoadImageAlpha(filename.c_str());
w = wArg;
h = hArg;
for(int m_direction = 0; m_direction < 4; m_direction++) {
for(int m_action = 0; m_action < 4; m_action++) {
sprites[m_direction][m_action].x = w * m_action;
sprites[m_direction][m_action].y = h * m_direction;
sprites[m_direction][m_action].w = w;
sprites[m_direction][m_action].h = h;
}
}
}
void Character::AddSpeachBubble(string text) {
m_speachBubble.push_back(text);
m_speachBubbleText.SetTextBlended(text, "small", 0, 0, 0);
if(m_speachBubbleTimer.IsStarted() == false)
m_speachBubbleTimer.Start();
}
void Character::Render(void) { void Character::Render(void) {
// Draw some fancy speach bubbles. It is a bit of a mess, I am playing.
if(m_speachBubble.size() != 0) {
if(m_speachBubbleTimer.GetTicks() < SPEACH_BUBBLE_DISPLAY_LENGTH) {
roundedBoxRGBA(screen, (x + w / 2) - 100,
y - 100,
(x + w / 2) + 100,
y - 35,
5, 255, 255, 255, 255);
filledTrigonRGBA(screen, (x + w / 2) - 100,
y - 100,
(x + w / 2) - 10,
y - 40,
(x + w / 2) + 10,
y - 40,
255, 255, 255, 255);
m_speachBubbleText.Render((x + w / 2) - 90, y - 90);
}
}
if(m_attacking && m_attackTimer.GetTicks() < ATTACKING_DISPLAY_LEN) { if(m_attacking && m_attackTimer.GetTicks() < ATTACKING_DISPLAY_LEN) {
ApplySurface(x, y, m_texture, screen, &sprites[m_directionFacing][ANIM_ATTACK]); ApplySurface(x, y, m_texture, screen, &sprites[m_directionFacing][ANIM_ATTACK]);
return; return;
@ -64,27 +113,22 @@ void Character::Render(void) {
} }
} }
void Character::LoadSprites(string filename, int wArg, int hArg) {
if(m_texture != NULL)
SDL_FreeSurface(m_texture);
m_texture = LoadImageAlpha(filename.c_str());
w = wArg;
h = hArg;
for(int m_direction = 0; m_direction < 4; m_direction++) {
for(int m_action = 0; m_action < 4; m_action++) {
sprites[m_direction][m_action].x = w * m_action;
sprites[m_direction][m_action].y = h * m_direction;
sprites[m_direction][m_action].w = w;
sprites[m_direction][m_action].h = h;
}
}
}
void Character::Update(void) { void Character::Update(void) {
Move(); Move();
if(m_speachBubble.size() != 0) {
if(m_speachBubbleTimer.GetTicks() > SPEACH_BUBBLE_DISPLAY_LENGTH) {
m_speachBubble.pop_front();
if(m_speachBubble.size() != 0) {
m_speachBubbleTimer.Start();
}
} else {
if(m_speachBubble.front() != m_speachBubbleText.GetText()) {
m_speachBubbleText.SetTextBlended(m_speachBubble.front(), "small", 0, 0, 0);
}
}
}
} }
void Character::Move(void) { void Character::Move(void) {
@ -143,15 +187,15 @@ bool Character::CheckEntityCollisions(void) {
} }
bool Character::CheckCharacterCollisions(void) { bool Character::CheckCharacterCollisions(void) {
for(collisionIterator = collisionList.begin(); for(collisionIter = collisionList.begin();
collisionIterator != collisionList.end(); collisionIter != collisionList.end();
collisionIterator++) { collisionIter++) {
if((*collisionIterator) != this) { if((*collisionIter) != this) {
if(CheckCollisionXY(x, y, w, h, if(CheckCollisionXY(x, y, w, h,
(*collisionIterator)->GetX(), (*collisionIter)->GetX(),
(*collisionIterator)->GetY(), (*collisionIter)->GetY(),
(*collisionIterator)->GetWidth(), (*collisionIter)->GetWidth(),
(*collisionIterator)->GetHeight())) { (*collisionIter)->GetHeight())) {
return true; return true;
} }
} }

View File

@ -1,6 +1,7 @@
#ifndef _CHARACTER_H_ #ifndef _CHARACTER_H_
#define _CHARACTER_H_ #define _CHARACTER_H_
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>
#include <list> #include <list>
#include "../Unuk/Globals.h" #include "../Unuk/Globals.h"
@ -10,6 +11,7 @@
#include "Collision.h" #include "Collision.h"
#include "Map.h" #include "Map.h"
#include "Timer.h" #include "Timer.h"
#include "Text.h"
#include "Debug.h" #include "Debug.h"
using namespace std; using namespace std;
@ -20,6 +22,8 @@ public:
Character(Map* mapArg); Character(Map* mapArg);
~Character(void); ~Character(void);
void LoadSprites(string filename, int wArg, int hArg);
float GetX(void) { return x; } float GetX(void) { return x; }
float GetY(void) { return y; } float GetY(void) { return y; }
float GetWidth(void) { return w; } float GetWidth(void) { return w; }
@ -29,9 +33,9 @@ public:
void SetXVelocity(float arg) { xVel = arg; } void SetXVelocity(float arg) { xVel = arg; }
void SetYVelocity(float arg) { yVel = arg; } void SetYVelocity(float arg) { yVel = arg; }
void Render(void); void AddSpeachBubble(string text);
void LoadSprites(string filename, int wArg, int hArg);
void Render(void);
void Update(void); void Update(void);
protected: protected:
@ -74,6 +78,8 @@ private:
static const int ANIMATION_SPEED = 200; static const int ANIMATION_SPEED = 200;
static const int ATTACKING_DISPLAY_LEN = 150; static const int ATTACKING_DISPLAY_LEN = 150;
static const int SPEACH_BUBBLE_DISPLAY_LENGTH = 6000;
SDL_Surface* m_texture; SDL_Surface* m_texture;
// [direction][action] // [direction][action]
@ -82,6 +88,11 @@ private:
Timer m_animationTimer; Timer m_animationTimer;
int m_animationStage; int m_animationStage;
bool m_leftFoot; bool m_leftFoot;
list<string> m_speachBubble;
list<string>::iterator m_speachBubbleIter;
Timer m_speachBubbleTimer;
Text m_speachBubbleText;
}; };
#endif #endif

View File

@ -4,6 +4,7 @@ Timer::Timer(void) {
m_startTicks = 0; m_startTicks = 0;
m_pausedTicks = 0; m_pausedTicks = 0;
m_paused = false; m_paused = false;
m_started = false;
} }
Timer::~Timer(void) { Timer::~Timer(void) {
@ -11,30 +12,38 @@ Timer::~Timer(void) {
void Timer::Start(void) { void Timer::Start(void) {
m_paused = false; m_paused = false;
m_started = true;
m_startTicks = SDL_GetTicks(); m_startTicks = SDL_GetTicks();
} }
void Timer::Pause(void) { void Timer::Stop(void) {
if(m_paused == false) { m_paused = false;
m_paused = true; m_started = true;
m_pausedTicks = SDL_GetTicks() - m_startTicks;
} }
void Timer::Pause(void) {
assert(m_paused == false);
m_paused = true;
m_pausedTicks = SDL_GetTicks() - m_startTicks;
} }
void Timer::Unpause(void) { void Timer::Unpause(void) {
if(m_paused == true) { assert(m_paused == true);
m_paused = false; m_paused = false;
m_startTicks = SDL_GetTicks() - m_pausedTicks; m_startTicks = SDL_GetTicks() - m_pausedTicks;
m_pausedTicks = 0; m_pausedTicks = 0;
} }
}
int Timer::GetTicks(void) { int Timer::GetTicks(void) {
if(m_paused == true) { if(m_paused == true)
return m_pausedTicks; return m_pausedTicks;
} else { else if(m_started == true)
return SDL_GetTicks() - m_startTicks; return SDL_GetTicks() - m_startTicks;
} else
return 0;
} }
string Timer::GetTicksStr(void) { string Timer::GetTicksStr(void) {

View File

@ -2,6 +2,7 @@
#define _TIMER_H_ #define _TIMER_H_
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <string> #include <string>
#include <assert.h>
#include <sstream> #include <sstream>
using namespace std; using namespace std;
@ -13,8 +14,10 @@ public:
void Pause(void); void Pause(void);
void Unpause(void); void Unpause(void);
void Start(void); void Start(void);
void Stop(void);
bool IsPaused(void) { return m_paused; } bool IsPaused(void) { return m_paused; }
bool IsStarted(void) { return m_started; }
int GetTicks(void); int GetTicks(void);
@ -22,6 +25,8 @@ public:
private: private:
bool m_paused; bool m_paused;
bool m_started;
int m_startTicks; int m_startTicks;
int m_pausedTicks; int m_pausedTicks;
}; };