Added labels that display energy costs when building

This commit is contained in:
Anna Rose 2011-07-07 18:10:05 -04:00
parent 4e4cb561fc
commit 0c2e850c05
7 changed files with 112 additions and 5 deletions

View File

@ -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)

3
TODO
View File

@ -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!

View File

@ -25,6 +25,13 @@ Game::~Game()
{
delete *cursor;
}
for (map<VertexType,Label*>::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<VertexType,Label*>::iterator cursor = costs.begin();
cursor != costs.end(); cursor++)
{
(*cursor).second->iterate();
}
}
@ -190,6 +232,13 @@ void Game::render()
(*cursor)->render(display);
}
for (map<VertexType,Label*>::iterator cursor = costs.begin();
cursor != costs.end(); cursor++)
{
(*cursor).second->render(display);
}
SDL_Flip(display);
}

8
game.h
View File

@ -10,12 +10,15 @@
#include "gamedata.h"
#include "gamestate.h"
#include "menubutton.h"
#include "label.h"
#include <SDL_ttf.h>
#include <list>
#include <stack>
#include <map>
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<MenuButton*> buttons;
list<MenuButton*> buttons; // the buttons!
map<VertexType,Label*> costs; // simple renderables
static int NODE_RADIUS;

16
label.cpp Normal file
View File

@ -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);
}

38
label.h Normal file
View File

@ -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 <SDL_ttf.h>
#include <string>
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

View File

@ -5,6 +5,7 @@ Player::Player(string name, unsigned int colour)
this->name = name;
this->colour = colour;
energy = 0;
played = false;
}