From 48c45c21910bfe923e79108f1e32948b17e28dbe Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 22 Jun 2011 22:32:33 -0400 Subject: [PATCH] Code for some of the basic game design - selecting existing nodes, limiting edge distance --- gamecore.cpp | 27 +++++++++++++++++++++++++-- gamecore.h | 5 +++++ graph.cpp | 33 ++++++++++++++++++++++++++++----- graph.h | 8 ++++++-- 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/gamecore.cpp b/gamecore.cpp index 3291705..b27617a 100644 --- a/gamecore.cpp +++ b/gamecore.cpp @@ -5,11 +5,15 @@ #include #endif +int GameCore::NODE_SIZE = 25; +int GameCore::MAX_MOVE_DISTANCE = 200; + GameCore::GameCore() { display = NULL; background = NULL; node = NULL; + move_template = NULL; is_running = true; } @@ -51,7 +55,11 @@ bool GameCore::init() node = DrawUtils::load("node.bmp"); DrawUtils::transpare(node, 255, 0, 255); - if (background == NULL || node == NULL) + move_template = DrawUtils::load("move_template.bmp"); + DrawUtils::transpare(move_template, 255, 0, 255); + SDL_SetAlpha(move_template, SDL_SRCALPHA, 128); + + if (background == NULL || node == NULL || move_template == NULL) { #ifdef DEBUG std::cerr << "GameCore::init(): error: Couldn't load an image file\n"; @@ -85,6 +93,13 @@ void GameCore::render() 0x000000); } + if (graph.get_current_vertex() != NULL) + { + Vertex* v = graph.get_current_vertex(); + DrawUtils::draw(display, move_template, v->x, v->y); + } + + SDL_Flip(display); } @@ -112,5 +127,13 @@ void GameCore::on_exit() void GameCore::on_lbutton_down(int x, int y) { - graph.add_vertex(x, y, 25); + Vertex* v = graph.get_current_vertex(); + Vertex new_v = Vertex(); + new_v.x = x; + new_v.y = y; + + if (v != NULL && (Vertex::distance(*v, new_v) > MAX_MOVE_DISTANCE)) + return + + graph.do_vertex(x, y, NODE_SIZE); } diff --git a/gamecore.h b/gamecore.h index ab16baa..fae07fb 100644 --- a/gamecore.h +++ b/gamecore.h @@ -38,9 +38,14 @@ class GameCore : public MainEvent // textures to draw SDL_Surface* background; SDL_Surface* node; + SDL_Surface* move_template; // data Graph graph; + + // constants + static int NODE_SIZE; + static int MAX_MOVE_DISTANCE; }; #endif diff --git a/graph.cpp b/graph.cpp index 4481bfe..62ba6b4 100644 --- a/graph.cpp +++ b/graph.cpp @@ -11,7 +11,7 @@ using std::cerr; Graph::Graph() { - last_vertex = NULL; + current_vertex = NULL; } Graph::~Graph() @@ -54,6 +54,29 @@ bool Graph::vertex_present(int x, int y, int size) return false; } + +void Graph::do_vertex(int x, int y, int size) +{ + if (vertex_present(x, y, size)) select_vertex(x, y); + else add_vertex(x, y, size); +} + + +void Graph::select_vertex(int x, int y) +{ + for (list::iterator cursor = vertices.begin(); + cursor != vertices.end(); cursor++) + { + Vertex* v = *cursor; + if (x >= v->x_min && x <= v->x_max && y >= v->y_min && y <= v->y_max) + { + current_vertex = v; + return; + } + } +} + + void Graph::add_vertex(int x, int y, int size) { Vertex* v = new Vertex(); @@ -69,17 +92,17 @@ void Graph::add_vertex(int x, int y, int size) vertices.push_back(v); - if (last_vertex == NULL) + if (current_vertex == NULL) { - last_vertex = v; + current_vertex = v; return; } Edge e; - e.a = last_vertex; + e.a = current_vertex; e.b = v; edges.push_back(e); - last_vertex = v; + current_vertex = v; } diff --git a/graph.h b/graph.h index 1332868..de426a0 100644 --- a/graph.h +++ b/graph.h @@ -41,10 +41,14 @@ class Graph list get_vertices() { return vertices; } list get_edges() { return edges; } - void add_vertex(int x, int y, int size); + void do_vertex(int x, int y, int size); + Vertex* get_current_vertex() { return current_vertex; } private: - Vertex* last_vertex; + void add_vertex(int x, int y, int size); + void select_vertex(int x, int y); + + Vertex* current_vertex; list vertices; list edges; };