From 0c2e850c05fe118a216926a846b6a1903e74263d Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Thu, 7 Jul 2011 18:10:05 -0400 Subject: [PATCH] Added labels that display energy costs when building --- Makefile | 2 +- TODO | 3 +-- game.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ game.h | 8 ++++++-- label.cpp | 16 ++++++++++++++++ label.h | 38 ++++++++++++++++++++++++++++++++++++++ player.cpp | 1 + 7 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 label.cpp create mode 100644 label.h diff --git a/Makefile b/Makefile index 52591e3..fa86b73 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ OBJECTS=\ main.o \ drawutils.o mathutils.o timer.o itos.o \ graph.o gamedata.o player.o vertex.o gamevertex.o \ -entity.o menubutton.o \ +entity.o menubutton.o label.o \ mainevent.o gamestate.o game.o titlescreen.o all: $(PROJECT) diff --git a/TODO b/TODO index 78bf6f3..26ae6d6 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ Some things that need doing: -* Defenders should add to armor score, attackers to attack score, producers boost each other's energy production... all scaled by distance on the graph -* Display energy costs... +* Defenders should add to armor score, attackers to attack score, producers boost each other's energy production... all scaled by distance on the graph? * OpenGL, of course... can we start with a rendering in the corner? * Better art! diff --git a/game.cpp b/game.cpp index dda9c52..e892332 100644 --- a/game.cpp +++ b/game.cpp @@ -25,6 +25,13 @@ Game::~Game() { delete *cursor; } + + for (map::iterator cursor = costs.begin(); + cursor != costs.end(); cursor++) + { + delete (*cursor).second; + } + } @@ -80,6 +87,10 @@ bool Game::init() } } + costs[VERTEX_ATTACKER] = new Label("", col3 - 10, row1 + 2, font); + costs[VERTEX_DEFENDER] = new Label("", col3 - 10, row2 + 2, font); + costs[VERTEX_PRODUCER] = new Label("", col3 - 10, row3 + 2, font); + mode_changed(); data.set_first_turn(); @@ -95,6 +106,37 @@ void Game::iterate() { (*cursor)->iterate(); } + + // set the costs + Label* tmp_label; + + if (data.get_mode() & MODE_BUILD && data.get_turn()->has_played()) + { + tmp_label = costs[VERTEX_ATTACKER]; + tmp_label->set_text(itos(data.get_energy_cost(VERTEX_ATTACKER)) + + " energy"); + tmp_label = costs[VERTEX_DEFENDER]; + tmp_label->set_text(itos(data.get_energy_cost(VERTEX_DEFENDER)) + + " energy"); + tmp_label = costs[VERTEX_PRODUCER]; + tmp_label->set_text(itos(data.get_energy_cost(VERTEX_PRODUCER)) + + " energy"); + } + else + { + tmp_label = costs[VERTEX_ATTACKER]; + tmp_label->set_text(""); + tmp_label = costs[VERTEX_DEFENDER]; + tmp_label->set_text(""); + tmp_label = costs[VERTEX_PRODUCER]; + tmp_label->set_text(""); + } + + for (map::iterator cursor = costs.begin(); + cursor != costs.end(); cursor++) + { + (*cursor).second->iterate(); + } } @@ -190,6 +232,13 @@ void Game::render() (*cursor)->render(display); } + for (map::iterator cursor = costs.begin(); + cursor != costs.end(); cursor++) + { + (*cursor).second->render(display); + } + + SDL_Flip(display); } diff --git a/game.h b/game.h index 99f00dc..ba66108 100644 --- a/game.h +++ b/game.h @@ -10,12 +10,15 @@ #include "gamedata.h" #include "gamestate.h" #include "menubutton.h" +#include "label.h" #include #include #include +#include using std::list; using std::stack; +using std::map; class Game : public GameState @@ -55,8 +58,9 @@ class Game : public GameState SDL_Surface* background; TTF_Font* font; - // menu buttons - list buttons; + list buttons; // the buttons! + + map costs; // simple renderables static int NODE_RADIUS; diff --git a/label.cpp b/label.cpp new file mode 100644 index 0000000..a23b62b --- /dev/null +++ b/label.cpp @@ -0,0 +1,16 @@ +#include "label.h" +#include "drawutils.h" + +Label::Label(string text, int x, int y, TTF_Font* font, int colour) +{ + this->text = text; + this->font = font; + this->colour = colour; + this->x = x; + this->y = y; +} + +void Label::render(SDL_Surface* display) +{ + if (text != "") DrawUtils::draw_text(display, text, x, y, font, colour); +} diff --git a/label.h b/label.h new file mode 100644 index 0000000..9b0c45f --- /dev/null +++ b/label.h @@ -0,0 +1,38 @@ +/* This widget roughly mimics a "Label" from GTK. + * It lets us put a writable text area at a particular part of + * the screen, and then write to it / change its text as we like + */ + +#ifndef _LABEL_H_ +#define _LABEL_H_ + +#include "entity.h" +#include +#include + +using std::string; + +class Label : public Entity +{ + public: + Label(string text, int x, int y, TTF_Font* font = NULL, + int colour = 0x000000); + + bool init() {} + void render(SDL_Surface* display); + void iterate() {} + + string get_text() const { return text; } + + void set_text(string new_text) { text = new_text; } + void set_colour(int new_colour) { colour = new_colour; } + + private: + string text; + TTF_Font* font; + int x; + int y; + int colour; +}; + +#endif diff --git a/player.cpp b/player.cpp index 620c04f..09b5f26 100644 --- a/player.cpp +++ b/player.cpp @@ -5,6 +5,7 @@ Player::Player(string name, unsigned int colour) this->name = name; this->colour = colour; energy = 0; + played = false; }