Implemented font framework, print info about each vertex

This commit is contained in:
Anna Rose 2011-07-01 13:15:30 -04:00
parent 36414a6996
commit 938e75716c
7 changed files with 79 additions and 5 deletions

View File

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

View File

@ -1,6 +1,6 @@
#include "drawutils.h"
#include <cmath>
#include "mathutils.h"
#include <cmath>
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);
}

View File

@ -3,8 +3,9 @@
#ifndef _DRAWUTILS_H_
#define _DRAWUTILS_H_
#include <SDL.h>
#include <string>
#include <SDL.h>
#include <SDL_ttf.h>
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

View File

@ -2,6 +2,7 @@
#include "mathutils.h"
#include "drawutils.h"
#include "debug.h"
#include "itos.h"
#include <SDL.h>
int Game::NODE_RADIUS = 10;
@ -11,6 +12,7 @@ Game::Game(stack<GameState*>* 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<Vertex*>::iterator subcursor = v->neighbors.begin();
subcursor != v->neighbors.end(); subcursor++)
{

2
game.h
View File

@ -10,6 +10,7 @@
#include "gamedata.h"
#include "gamestate.h"
#include <stack>
#include <SDL_ttf.h>
using std::stack;
@ -40,6 +41,7 @@ class Game : public GameState
// surfaces containing textures to draw
SDL_Surface* background;
TTF_Font* font;
};
#endif

View File

@ -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<Vertex*>& visited);
Vertex* current;

View File

@ -4,11 +4,14 @@
#include "titlescreen.h"
#include "debug.h"
#include <SDL.h>
#include <SDL_ttf.h>
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;