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::MAX_MOVE_DISTANCE = 100;
int GameCore::NODE_RADIUS = 12; int GameCore::NODE_RADIUS = 12;
int GameCore::PLAYER1_COLOUR = 0x4a483f;
int GameCore::PLAYER2_COLOUR = 0x090c7a;
GameCore::GameCore() GameCore::GameCore()
{ {
display = NULL; display = NULL;
background = NULL; background = NULL;
is_running = true; is_running = true;
who = PLAYER1;
} }
int GameCore::execute() int GameCore::execute()
@ -83,7 +88,7 @@ void GameCore::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,
0x000000); v.colour);
} }
for (list<Edge>::iterator cursor = edges.begin(); for (list<Edge>::iterator cursor = edges.begin();
@ -91,7 +96,7 @@ void GameCore::render()
{ {
Edge e = *cursor; Edge e = *cursor;
DrawUtils::draw_line(display, e.a->x, e.a->y, e.b->x, e.b->y, 2, DrawUtils::draw_line(display, e.a->x, e.a->y, e.b->x, e.b->y, 2,
0x000000); e.a->colour);
} }
SDL_Flip(display); SDL_Flip(display);
@ -125,9 +130,21 @@ void GameCore::on_lbutton_down(int x, int y)
(MathUtils::distance(cv->x, cv->y, x, y) (MathUtils::distance(cv->x, cv->y, x, y)
> MAX_MOVE_DISTANCE)) > 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); graph.select_vertex(x, y);
return; 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 "mainevent.h"
#include "graph.h" #include "graph.h"
enum turn {PLAYER1, PLAYER2, WIN1, WIN2};
class GameCore : public MainEvent class GameCore : public MainEvent
{ {
public: public:
@ -23,6 +25,7 @@ class GameCore : public MainEvent
// event handlers // event handlers
void on_exit(); void on_exit();
void on_lbutton_down(int x, int y); void on_lbutton_down(int x, int y);
void on_rbutton_down(int mX, int mY);
private: private:
bool init(); bool init();
@ -40,9 +43,13 @@ class GameCore : public MainEvent
// data // data
Graph graph; Graph graph;
turn who;
static int NODE_RADIUS; static int NODE_RADIUS;
static int MAX_MOVE_DISTANCE; static int MAX_MOVE_DISTANCE;
static int PLAYER1_COLOUR;
static int PLAYER2_COLOUR;
}; };
#endif #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); 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(); for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++) cursor != vertices.end(); cursor++)
@ -73,16 +73,17 @@ void Graph::select_vertex(int x, int y)
Vertex* v = *cursor; Vertex* v = *cursor;
if (MathUtils::distance(v->x, v->y, x, y) <= v->r) if (MathUtils::distance(v->x, v->y, x, y) <= v->r)
{ {
current_vertex = v; current_vertex = v
return; 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 // Make sure the nodes won't overlap
if (vertex_would_overlap(v->x, v->y, v->r)) 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->x = x;
this->y = y; this->y = y;
this->r = r; this->r = r;
this->colour = colour;
} }

11
graph.h
View File

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