From 735a9dfceff7d01a2e6ed30a679f66b3aab9bde1 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 6 Jul 2011 14:52:45 -0400 Subject: [PATCH] Vertices can now render themselves - not sure what to do about edges, right now game is still doing them --- game.cpp | 43 +++-------------------------------- game.h | 4 ---- gamevertex.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ gamevertex.h | 13 ++++++++++- menubutton.cpp | 7 ++++-- 5 files changed, 81 insertions(+), 47 deletions(-) diff --git a/game.cpp b/game.cpp index 244ed5c..ad46961 100644 --- a/game.cpp +++ b/game.cpp @@ -11,10 +11,6 @@ Game::Game(stack* 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::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) { - GameVertex* v = dynamic_cast(*cursor); - draw_node(v); + dynamic_cast(*cursor)->render(display); } // draw the rest of the bottom menu draw_menu_bars(); + draw_player_info(); for (list::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(); diff --git a/game.h b/game.h index ce66f1a..99f00dc 100644 --- a/game.h +++ b/game.h @@ -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 buttons; diff --git a/gamevertex.cpp b/gamevertex.cpp index 0f6ee16..f690bf2 100644 --- a/gamevertex.cpp +++ b/gamevertex.cpp @@ -1,13 +1,26 @@ +#include "debug.h" #include "gamevertex.h" +#include "itos.h" +#include "drawutils.h" #include #include +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 +#include 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& visited); + static TTF_Font* font; + static SDL_Surface* attacker_icon; + static SDL_Surface* defender_icon; + static SDL_Surface* producer_icon; }; #endif diff --git a/menubutton.cpp b/menubutton.cpp index 47bf824..c09e2f9 100644 --- a/menubutton.cpp +++ b/menubutton.cpp @@ -95,10 +95,13 @@ 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 | - BUTTON_BUILD)) + if (action & (BUTTON_ATTACK | BUTTON_MOVE | + BUTTON_BUILD)) visible = true; // If we're one of the other three, and we're in MODE_BUILD, we're visible