Support different colours for different vertices

This commit is contained in:
Anna Rose 2011-06-23 15:10:40 -04:00
parent 9d36120f6d
commit b967542157
4 changed files with 43 additions and 16 deletions

View File

@ -5,12 +5,17 @@
int GameCore::MAX_MOVE_DISTANCE = 100;
int GameCore::NODE_RADIUS = 12;
int GameCore::PLAYER1_COLOUR = 0x4a483f;
int GameCore::PLAYER2_COLOUR = 0x090c7a;
GameCore::GameCore()
{
display = NULL;
background = NULL;
is_running = true;
who = PLAYER1;
}
int GameCore::execute()
@ -83,7 +88,7 @@ void GameCore::render()
{
Vertex v = *(*cursor);
DrawUtils::draw_circle_filled(display, v.x, v.y, v.r,
0x000000);
v.colour);
}
for (list<Edge>::iterator cursor = edges.begin();
@ -91,7 +96,7 @@ void GameCore::render()
{
Edge e = *cursor;
DrawUtils::draw_line(display, e.a->x, e.a->y, e.b->x, e.b->y, 2,
0x000000);
e.a->colour);
}
SDL_Flip(display);
@ -125,9 +130,21 @@ void GameCore::on_lbutton_down(int x, int y)
(MathUtils::distance(cv->x, cv->y, x, y)
> MAX_MOVE_DISTANCE))
{
Vertex* v = graph.vertex_at(x, y);
if (v->colour == PLAYER1_COLOUR && turn == PLAYER1 ||
v->colour == PLAYER2_COLOUR && turn == PLAYER2)
graph.select_vertex(x, y);
return;
}
graph.do_vertex(x, y, NODE_RADIUS);
graph.do_vertex(x, y, NODE_RADIUS, PLAYER1_COLOUR);
if (turn == PLAYER1) turn = PLAYER2;
else turn = PLAYER_1;
}
void GameCore::on_rbutton_down(int mX, int mY)
{
graph.clear_current_vertex();
}

View File

@ -13,6 +13,8 @@
#include "mainevent.h"
#include "graph.h"
enum turn {PLAYER1, PLAYER2, WIN1, WIN2};
class GameCore : public MainEvent
{
public:
@ -23,6 +25,7 @@ class GameCore : public MainEvent
// event handlers
void on_exit();
void on_lbutton_down(int x, int y);
void on_rbutton_down(int mX, int mY);
private:
bool init();
@ -40,9 +43,13 @@ class GameCore : public MainEvent
// data
Graph graph;
turn who;
static int NODE_RADIUS;
static int MAX_MOVE_DISTANCE;
static int PLAYER1_COLOUR;
static int PLAYER2_COLOUR;
};
#endif

View File

@ -58,14 +58,14 @@ bool Graph::crosses_edge(Edge e)
}
void Graph::do_vertex(int x, int y, int r)
void Graph::do_vertex(int x, int y, int r, int colour)
{
if (vertex_present(x, y, r)) select_vertex(x, y);
else add_vertex(x, y, r);
else add_vertex(x, y, r, colour);
}
void Graph::select_vertex(int x, int y)
Vertex* Graph::select_vertex(int x, int y)
{
for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++)
@ -73,16 +73,17 @@ void Graph::select_vertex(int x, int y)
Vertex* v = *cursor;
if (MathUtils::distance(v->x, v->y, x, y) <= v->r)
{
current_vertex = v;
current_vertex = v
return;
}
}
return NULL;
}
void Graph::add_vertex(int x, int y, int r)
void Graph::add_vertex(int x, int y, int r, int colour)
{
Vertex* v = new Vertex(x, y, r);
Vertex* v = new Vertex(x, y, r, colour);
// Make sure the nodes won't overlap
if (vertex_would_overlap(v->x, v->y, v->r))
@ -124,9 +125,10 @@ void Graph::add_vertex(int x, int y, int r)
}
Vertex::Vertex(int x, int y, int r)
Vertex::Vertex(int x, int y, int r, int colour)
{
this->x = x;
this->y = y;
this->r = r;
this->colour = colour;
}

11
graph.h
View File

@ -14,11 +14,12 @@ using std::list;
class Vertex
{
public:
Vertex(int x, int y, int r);
Vertex(int x, int y, int r, int colour = 0x000000);
int x;
int y;
int r;
int colour;
};
struct Edge
@ -39,15 +40,15 @@ class Graph
list<Edge> get_edges() const { return edges; }
void select_vertex(int x, int y);
void do_vertex(int x, int y, int r);
void do_vertex(int x, int y, int r, int colour);
Vertex* get_current_vertex() const { return current_vertex; }
void clear_current_vertex() { current_vertex = NULL; }
private:
bool vertex_would_overlap(int x, int y, int r) const;
bool crosses_edge(Edge e) const;
void add_vertex(int x, int y, int r);
void add_vertex(int x, int y, int r, int colour);
bool vertex_would_overlap(int x, int y, int r);
bool crosses_edge(Edge e);
Vertex* current_vertex;
list<Vertex*> vertices;