Implemented font framework, print info about each vertex
This commit is contained in:
parent
36414a6996
commit
938e75716c
4
Makefile
4
Makefile
|
@ -3,10 +3,10 @@
|
||||||
PROJECT=treewars
|
PROJECT=treewars
|
||||||
CXX=g++
|
CXX=g++
|
||||||
CXXFLAGS=-DDEBUG -g `sdl-config --cflags`
|
CXXFLAGS=-DDEBUG -g `sdl-config --cflags`
|
||||||
LDFLAGS=`sdl-config --libs`
|
LDFLAGS=`sdl-config --libs` -lSDL_ttf
|
||||||
OBJECTS=\
|
OBJECTS=\
|
||||||
main.o \
|
main.o \
|
||||||
drawutils.o mathutils.o timer.o \
|
drawutils.o mathutils.o timer.o itos.o \
|
||||||
graph.o gamedata.o \
|
graph.o gamedata.o \
|
||||||
mainevent.o gamestate.o game.o titlescreen.o
|
mainevent.o gamestate.o game.o titlescreen.o
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "drawutils.h"
|
#include "drawutils.h"
|
||||||
#include <cmath>
|
|
||||||
#include "mathutils.h"
|
#include "mathutils.h"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
SDL_Surface* DrawUtils::load(string file)
|
SDL_Surface* DrawUtils::load(string file)
|
||||||
{
|
{
|
||||||
|
@ -147,3 +147,40 @@ bool DrawUtils::transpare(SDL_Surface* surface, int r, int g, int b)
|
||||||
|
|
||||||
return true;
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -3,8 +3,9 @@
|
||||||
#ifndef _DRAWUTILS_H_
|
#ifndef _DRAWUTILS_H_
|
||||||
#define _DRAWUTILS_H_
|
#define _DRAWUTILS_H_
|
||||||
|
|
||||||
#include <SDL.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <SDL_ttf.h>
|
||||||
|
|
||||||
using std::string;
|
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_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_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
|
// transpare (v) - to make transparent
|
||||||
// this function makes a particular color key on a surface 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
|
// the traditional color key is 255, 0, 255 (bright pink), so we default
|
||||||
|
|
19
game.cpp
19
game.cpp
|
@ -2,6 +2,7 @@
|
||||||
#include "mathutils.h"
|
#include "mathutils.h"
|
||||||
#include "drawutils.h"
|
#include "drawutils.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
#include "itos.h"
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
int Game::NODE_RADIUS = 10;
|
int Game::NODE_RADIUS = 10;
|
||||||
|
@ -11,6 +12,7 @@ Game::Game(stack<GameState*>* state_stack, SDL_Surface* display)
|
||||||
: GameState(state_stack, display)
|
: GameState(state_stack, display)
|
||||||
{
|
{
|
||||||
background = NULL;
|
background = NULL;
|
||||||
|
font = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,6 +20,8 @@ Game::~Game()
|
||||||
{
|
{
|
||||||
if (background != NULL)
|
if (background != NULL)
|
||||||
SDL_FreeSurface(background);
|
SDL_FreeSurface(background);
|
||||||
|
if (font != NULL)
|
||||||
|
TTF_CloseFont(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,6 +35,14 @@ bool Game::init()
|
||||||
return false;
|
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();
|
return GameState::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,6 +82,13 @@ void Game::render()
|
||||||
Vertex* v = *cursor;
|
Vertex* v = *cursor;
|
||||||
DrawUtils::draw_circle_filled(display, v->x, v->y, v->r,
|
DrawUtils::draw_circle_filled(display, v->x, v->y, v->r,
|
||||||
v->colour);
|
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();
|
for (list<Vertex*>::iterator subcursor = v->neighbors.begin();
|
||||||
subcursor != v->neighbors.end(); subcursor++)
|
subcursor != v->neighbors.end(); subcursor++)
|
||||||
{
|
{
|
||||||
|
|
2
game.h
2
game.h
|
@ -10,6 +10,7 @@
|
||||||
#include "gamedata.h"
|
#include "gamedata.h"
|
||||||
#include "gamestate.h"
|
#include "gamestate.h"
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
#include <SDL_ttf.h>
|
||||||
|
|
||||||
using std::stack;
|
using std::stack;
|
||||||
|
|
||||||
|
@ -40,6 +41,7 @@ class Game : public GameState
|
||||||
|
|
||||||
// surfaces containing textures to draw
|
// surfaces containing textures to draw
|
||||||
SDL_Surface* background;
|
SDL_Surface* background;
|
||||||
|
TTF_Font* font;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
10
gamedata.h
10
gamedata.h
|
@ -10,6 +10,14 @@
|
||||||
|
|
||||||
enum Turn {PLAYER1, PLAYER2, WIN1, WIN2};
|
enum Turn {PLAYER1, PLAYER2, WIN1, WIN2};
|
||||||
enum Mode {MODE_MOVE, MODE_ATTACK};
|
enum Mode {MODE_MOVE, MODE_ATTACK};
|
||||||
|
enum VertexType {ATTACKER, DEFENDER, PRODUCER};
|
||||||
|
|
||||||
|
class GameVertex : public Vertex
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VertexType type;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class GameData : public Graph
|
class GameData : public Graph
|
||||||
{
|
{
|
||||||
|
@ -39,9 +47,9 @@ class GameData : public Graph
|
||||||
// check for (and set, if needed) winner
|
// check for (and set, if needed) winner
|
||||||
bool endgame();
|
bool endgame();
|
||||||
Turn get_turn() const { return player; }
|
Turn get_turn() const { return player; }
|
||||||
|
float calculate_strength(Vertex* node);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float calculate_strength(Vertex* node);
|
|
||||||
float calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited);
|
float calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited);
|
||||||
|
|
||||||
Vertex* current;
|
Vertex* current;
|
||||||
|
|
4
main.cpp
4
main.cpp
|
@ -4,11 +4,14 @@
|
||||||
#include "titlescreen.h"
|
#include "titlescreen.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
#include <SDL_ttf.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
// Barebones setup for our game
|
// Barebones setup for our game
|
||||||
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
|
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
|
||||||
|
if (TTF_Init() < 0) return false;
|
||||||
|
|
||||||
SDL_WM_SetCaption("TreeWars","TreeWars");
|
SDL_WM_SetCaption("TreeWars","TreeWars");
|
||||||
|
|
||||||
SDL_Surface* display = SDL_SetVideoMode(800, 600, 32,
|
SDL_Surface* display = SDL_SetVideoMode(800, 600, 32,
|
||||||
|
@ -50,6 +53,7 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_FreeSurface(display);
|
SDL_FreeSurface(display);
|
||||||
|
TTF_Quit();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user