diff --git a/Makefile b/Makefile index 17c743f..a79e1a3 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ PROJECT=treewars CXX=g++ CXXFLAGS=-DDEBUG -g `sdl-config --cflags` -LDFLAGS=`sdl-config --libs` +LDFLAGS=`sdl-config --libs` -lSDL_ttf OBJECTS=\ main.o \ -drawutils.o mathutils.o timer.o \ +drawutils.o mathutils.o timer.o itos.o \ graph.o gamedata.o \ mainevent.o gamestate.o game.o titlescreen.o diff --git a/drawutils.cpp b/drawutils.cpp index c9418f9..64ab53b 100644 --- a/drawutils.cpp +++ b/drawutils.cpp @@ -1,6 +1,6 @@ #include "drawutils.h" -#include #include "mathutils.h" +#include SDL_Surface* DrawUtils::load(string file) { @@ -147,3 +147,40 @@ bool DrawUtils::transpare(SDL_Surface* surface, int r, int g, int b) return true; } + + +// Modified from +// http://www.parallelrealities.co.uk/tutorials/basic/tutorial7.php +void DrawUtils::draw_text(SDL_Surface* display, string text, int x, int y, + TTF_Font *font, bool center_x, bool center_y) +{ + SDL_Rect dest; + SDL_Surface *surface; + SDL_Color color; + + color.r = 0; + color.g = 0; + color.b = 0; + + surface = TTF_RenderUTF8_Blended(font, text.c_str(), color); + + if (surface == NULL) + { +#ifdef DEBUG + fprintf(stderr, "Couldn't create String '%s': %s\n", text.c_str(), + SDL_GetError()); +#endif + return; + } + + transpare(surface); + + /* Blit the surface */ + dest.x = (center_x ? (display->w - surface->w) / 2 : x); + dest.y = (center_y ? (display->h - surface->h) / 2 : y); + dest.w = surface->w; + dest.h = surface->h; + + SDL_BlitSurface(surface, NULL, display, &dest); + SDL_FreeSurface(surface); +} diff --git a/drawutils.h b/drawutils.h index 9a1557b..7fa48c9 100644 --- a/drawutils.h +++ b/drawutils.h @@ -3,8 +3,9 @@ #ifndef _DRAWUTILS_H_ #define _DRAWUTILS_H_ -#include #include +#include +#include using std::string; @@ -22,6 +23,9 @@ class DrawUtils static void draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour); static void draw_circle_filled(SDL_Surface* dest, Sint16 x, Sint16 y, Uint16 r, Uint32 colour); + static void draw_text(SDL_Surface* display, string text, int x, int y, + TTF_Font *font, bool center_x, bool center_y); + // transpare (v) - to make transparent // this function makes a particular color key on a surface transparent // the traditional color key is 255, 0, 255 (bright pink), so we default diff --git a/game.cpp b/game.cpp index 6198a54..8380752 100644 --- a/game.cpp +++ b/game.cpp @@ -2,6 +2,7 @@ #include "mathutils.h" #include "drawutils.h" #include "debug.h" +#include "itos.h" #include int Game::NODE_RADIUS = 10; @@ -11,6 +12,7 @@ Game::Game(stack* state_stack, SDL_Surface* display) : GameState(state_stack, display) { background = NULL; + font = NULL; } @@ -18,6 +20,8 @@ Game::~Game() { if (background != NULL) SDL_FreeSurface(background); + if (font != NULL) + TTF_CloseFont(font); } @@ -31,6 +35,14 @@ bool Game::init() return false; } + font = TTF_OpenFont("LiberationSans-Regular.ttf", 12); + + if (font == NULL) + { + debug("Game::init(): error: Couldn't load font"); + return false; + } + return GameState::init(); } @@ -70,6 +82,13 @@ void Game::render() Vertex* v = *cursor; DrawUtils::draw_circle_filled(display, v->x, v->y, v->r, v->colour); + DrawUtils::draw_text(display, + "str " + itos(data.calculate_strength(v)), + v->x, v->y, font, 0, 0); + DrawUtils::draw_text(display, "hp " + itos(v->score), + v->x, v->y + 13, font, 0, 0); + + for (list::iterator subcursor = v->neighbors.begin(); subcursor != v->neighbors.end(); subcursor++) { diff --git a/game.h b/game.h index 120f2d3..8b749d1 100644 --- a/game.h +++ b/game.h @@ -10,6 +10,7 @@ #include "gamedata.h" #include "gamestate.h" #include +#include using std::stack; @@ -40,6 +41,7 @@ class Game : public GameState // surfaces containing textures to draw SDL_Surface* background; + TTF_Font* font; }; #endif diff --git a/gamedata.h b/gamedata.h index 77c38e4..4de4ded 100644 --- a/gamedata.h +++ b/gamedata.h @@ -10,6 +10,14 @@ enum Turn {PLAYER1, PLAYER2, WIN1, WIN2}; enum Mode {MODE_MOVE, MODE_ATTACK}; +enum VertexType {ATTACKER, DEFENDER, PRODUCER}; + +class GameVertex : public Vertex +{ + public: + VertexType type; +}; + class GameData : public Graph { @@ -39,9 +47,9 @@ class GameData : public Graph // check for (and set, if needed) winner bool endgame(); Turn get_turn() const { return player; } + float calculate_strength(Vertex* node); private: - float calculate_strength(Vertex* node); float calculate_strength_r(Vertex* node, unsigned int depth, list& visited); Vertex* current; diff --git a/main.cpp b/main.cpp index 5595e8b..5d2ed0b 100644 --- a/main.cpp +++ b/main.cpp @@ -4,11 +4,14 @@ #include "titlescreen.h" #include "debug.h" #include +#include int main(int argc, char** argv) { // Barebones setup for our game if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false; + if (TTF_Init() < 0) return false; + SDL_WM_SetCaption("TreeWars","TreeWars"); SDL_Surface* display = SDL_SetVideoMode(800, 600, 32, @@ -50,6 +53,7 @@ int main(int argc, char** argv) } SDL_FreeSurface(display); + TTF_Quit(); SDL_Quit(); return 0;