Vertices can now render themselves - not sure what to do about edges, right now game is still doing them

This commit is contained in:
Anna Rose 2011-07-06 14:52:45 -04:00
parent 42e2e43ead
commit 735a9dfcef
5 changed files with 81 additions and 47 deletions

View File

@ -11,10 +11,6 @@ Game::Game(stack<GameState*>* state_stack, SDL_Surface* display)
{
background = NULL;
font = NULL;
attacker_icon = NULL;
defender_icon = NULL;
producer_icon = NULL;
}
@ -37,21 +33,13 @@ bool Game::init()
{
background = DrawUtils::load("background.bmp");
font = TTF_OpenFont("res/LiberationSans-Regular.ttf", 12);
attacker_icon = DrawUtils::load("attacker_icon.bmp");
defender_icon = DrawUtils::load("defender_icon.bmp");
producer_icon = DrawUtils::load("producer_icon.bmp");
if (background == NULL || font == NULL || attacker_icon == NULL ||
defender_icon == NULL || producer_icon == NULL)
if (background == NULL || font == NULL)
{
debug("Game::init(): error: Couldn't load some resource(s)");
return false;
}
DrawUtils::transpare(attacker_icon);
DrawUtils::transpare(defender_icon);
DrawUtils::transpare(producer_icon);
int row1 = display->h - 95;
int row2 = display->h - 65;
int row3 = display->h - 35;
@ -161,12 +149,12 @@ void Game::render()
for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++)
{
GameVertex* v = dynamic_cast<GameVertex*>(*cursor);
draw_node(v);
dynamic_cast<GameVertex*>(*cursor)->render(display);
}
// draw the rest of the bottom menu
draw_menu_bars();
draw_player_info();
for (list<MenuButton*>::iterator cursor = buttons.begin();
cursor != buttons.end(); cursor++)
@ -174,8 +162,6 @@ void Game::render()
(*cursor)->render(display);
}
draw_player_info();
SDL_Flip(display);
}
@ -197,29 +183,6 @@ void Game::draw_menu_bars()
}
void Game::draw_node(GameVertex* v)
{
DrawUtils::draw_circle_filled(display, v->x, v->y, v->r,
v->colour);
DrawUtils::draw_text(display, itos(v->score), v->x, v->y, font,
0x00ff00, true, true);
switch (v->type)
{
case VERTEX_ATTACKER:
DrawUtils::draw(display, attacker_icon, v->x + 5, v-> y + 5);
break;
case VERTEX_DEFENDER:
DrawUtils::draw(display, defender_icon, v->x + 5, v-> y + 5);
break;
case VERTEX_PRODUCER:
DrawUtils::draw(display, producer_icon, v->x + 5, v-> y + 5);
break;
}
}
void Game::draw_player_info()
{
Player* player = data.get_turn();

4
game.h
View File

@ -39,7 +39,6 @@ class Game : public GameState
private:
void draw_stats(GameVertex* v);
void draw_node(GameVertex* v);
void draw_player_info();
void draw_menu_bars();
@ -55,9 +54,6 @@ class Game : public GameState
// surfaces containing textures to draw
SDL_Surface* background;
TTF_Font* font;
SDL_Surface* attacker_icon;
SDL_Surface* defender_icon;
SDL_Surface* producer_icon;
// menu buttons
list<MenuButton*> buttons;

View File

@ -1,13 +1,26 @@
#include "debug.h"
#include "gamevertex.h"
#include "itos.h"
#include "drawutils.h"
#include <algorithm>
#include <cassert>
TTF_Font* GameVertex::font = NULL;
SDL_Surface* GameVertex::attacker_icon = NULL;
SDL_Surface* GameVertex::defender_icon = NULL;
SDL_Surface* GameVertex::producer_icon = NULL;
GameVertex::GameVertex(int x, int y, int z, int r, int colour, int score,
VertexType type, Player* player)
: Vertex(x, y, z, r, colour, score)
{
this->type = type;
this->player = player;
if (font == NULL || attacker_icon == NULL || defender_icon == NULL ||
producer_icon == NULL)
init();
}
@ -108,3 +121,51 @@ float GameVertex::calculate_strength_r(Vertex* node, unsigned int depth, list<Ve
return modscore;
}
bool GameVertex::init()
{
font = TTF_OpenFont("res/LiberationSans-Regular.ttf", 12);
attacker_icon = DrawUtils::load("attacker_icon.bmp");
defender_icon = DrawUtils::load("defender_icon.bmp");
producer_icon = DrawUtils::load("producer_icon.bmp");
if (font == NULL || attacker_icon == NULL ||
defender_icon == NULL || producer_icon == NULL)
{
debug("GameVertex::init(): error: Couldn't load some resource(s)");
return false;
}
DrawUtils::transpare(attacker_icon);
DrawUtils::transpare(defender_icon);
DrawUtils::transpare(producer_icon);
return true;
}
void GameVertex::render(SDL_Surface* display)
{
SDL_Surface* icon;
switch (type)
{
case VERTEX_ATTACKER:
icon = attacker_icon;
break;
case VERTEX_DEFENDER:
icon = defender_icon;
break;
case VERTEX_PRODUCER:
icon = producer_icon;
break;
}
DrawUtils::draw_circle_filled(display, x, y, r, colour);
DrawUtils::draw_text(display, itos(score), x, y, font,
0x00ff00, true, true);
DrawUtils::draw(display, icon, x + 5, y + 5);
}

View File

@ -11,16 +11,23 @@
#include "vertex.h"
#include "player.h"
#include "entity.h"
#include <SDL.h>
#include <SDL_ttf.h>
enum VertexType {VERTEX_NONE=0x1, VERTEX_ATTACKER=0x2, VERTEX_DEFENDER=0x4,
VERTEX_PRODUCER=0x8};
class GameVertex : public Vertex
class GameVertex : public Vertex, public Entity
{
public:
GameVertex(int x, int y, int z, int r, int colour = 0, int score = 0,
VertexType type = VERTEX_NONE, Player* player = NULL);
bool init();
void iterate() {}
void render(SDL_Surface* display);
VertexType type;
Player* player;
bool attacked; // only applicable for a VERTEX_ATTACKER
@ -32,6 +39,10 @@ class GameVertex : public Vertex
float calculate_strength();
float calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited);
static TTF_Font* font;
static SDL_Surface* attacker_icon;
static SDL_Surface* defender_icon;
static SDL_Surface* producer_icon;
};
#endif

View File

@ -95,9 +95,12 @@ void MenuButton::set_state(Mode mode, VertexType type, GameVertex* current)
)
selected = true;
// no optional buttons if current == NULL, period
if (current == NULL) return;
// If we have selected one of our vertices, and we're one of the three
// main buttons, be visible
if (current != NULL && action & (BUTTON_ATTACK | BUTTON_MOVE |
if (action & (BUTTON_ATTACK | BUTTON_MOVE |
BUTTON_BUILD))
visible = true;