From fc08adcade56e6ef2cb9309b20508041a35f2e4f Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 22 Jun 2011 17:57:44 -0400 Subject: [PATCH] First implementation of game logic and control --- drawutils.cpp | 1 + gamecore.cpp | 38 ++++++++++++++++++++++++++++++++++++++ gamecore.h | 6 +++++- graph.cpp | 18 +++++++++++++++--- graph.h | 10 +++++----- 5 files changed, 64 insertions(+), 9 deletions(-) diff --git a/drawutils.cpp b/drawutils.cpp index 2d28c9c..33d24f9 100644 --- a/drawutils.cpp +++ b/drawutils.cpp @@ -1,4 +1,5 @@ #include "drawutils.h" +#include SDL_Surface* DrawUtils::load(string file) { diff --git a/gamecore.cpp b/gamecore.cpp index 18966b0..2d68f59 100644 --- a/gamecore.cpp +++ b/gamecore.cpp @@ -8,6 +8,8 @@ GameCore::GameCore() { display = NULL; + background = NULL; + node = NULL; is_running = true; } @@ -44,13 +46,41 @@ bool GameCore::init() return false; } + background = DrawUtils::load("background.bmp"); + node = DrawUtils::load("node.bmp"); + + if (background == NULL || node == NULL) + { +#ifdef DEBUG + std::cerr << "GameCore::init(): error: Couldn't load an image file\n"; +#endif + return false; + } + return true; } void GameCore::render() { + list vertices = graph.get_vertices(); + list edges = graph.get_edges(); + for (list::iterator cursor = vertices.begin(); + cursor != vertices.end(); cursor++) + { + Vertex v = *cursor; + DrawUtils::draw(display, node, v.x_min, v.y_min); + } + + for (list::iterator cursor = edges.begin(); + cursor != edges.end(); cursor++) + { + Edge e = *cursor; + DrawUtils::draw_line(e.a.x, e.a.y, e.b.x, e.b.y, 2, 0xffffff, display); + } + + SDL_Flip(display); } @@ -62,6 +92,8 @@ void GameCore::iterate() void GameCore::cleanup() { + SDL_FreeSurface(background); + SDL_FreeSurface(node); SDL_FreeSurface(display); SDL_Quit(); } @@ -71,3 +103,9 @@ void GameCore::on_exit() { is_running = false; } + + +void GameCore::on_lbutton_down(int x, int y) +{ + graph.add_vertex(x, y, 25); +} diff --git a/gamecore.h b/gamecore.h index 25dc4b4..ab16baa 100644 --- a/gamecore.h +++ b/gamecore.h @@ -11,6 +11,7 @@ #include #include "mainevent.h" +#include "graph.h" class GameCore : public MainEvent { @@ -21,7 +22,7 @@ class GameCore : public MainEvent protected: // event handlers void on_exit(); - + void on_lbutton_down(int x, int y); private: bool init(); @@ -37,6 +38,9 @@ class GameCore : public MainEvent // textures to draw SDL_Surface* background; SDL_Surface* node; + + // data + Graph graph; }; #endif diff --git a/graph.cpp b/graph.cpp index 780f3bb..7c54d35 100644 --- a/graph.cpp +++ b/graph.cpp @@ -25,12 +25,24 @@ bool Graph::vertex_present(int x, int y, int size) return false; } -void Graph::add_vertex(Vertex v) +void Graph::add_vertex(int x, int y, int size) { - if (vertex_present(v.x, v.y)) return; + Vertex v; + v.x = x; + v.y = y; + + v.x_min = x - size/2; + v.x_max = x + size/2; + v.y_min = y - size/2; + v.y_max = y + size/2; + + if (vertex_present(v.x, v.y, 25)) return; vertices.push_back(v); - edges.push_back(Edge(last_vertex, v)); + Edge e; + e.a = last_vertex; + e.b = v; + edges.push_back(e); last_vertex = v; } diff --git a/graph.h b/graph.h index 960ce6b..f7fe32c 100644 --- a/graph.h +++ b/graph.h @@ -15,10 +15,10 @@ struct Vertex { int x; int y; - int min_x; - int min_y; - int max_x; - int max_y; + int x_min; + int x_max; + int y_min; + int y_max; }; struct Edge @@ -37,7 +37,7 @@ class Graph list get_vertices() { return vertices; } list get_edges() { return edges; } - void add_vertex(Vertex v); + void add_vertex(int x, int y, int size); private: Vertex last_vertex;