First implementation of game logic and control

This commit is contained in:
Anna Rose 2011-06-22 17:57:44 -04:00
parent 2aadb9ad7e
commit fc08adcade
5 changed files with 64 additions and 9 deletions

View File

@ -1,4 +1,5 @@
#include "drawutils.h" #include "drawutils.h"
#include <cmath>
SDL_Surface* DrawUtils::load(string file) SDL_Surface* DrawUtils::load(string file)
{ {

View File

@ -8,6 +8,8 @@
GameCore::GameCore() GameCore::GameCore()
{ {
display = NULL; display = NULL;
background = NULL;
node = NULL;
is_running = true; is_running = true;
} }
@ -44,13 +46,41 @@ bool GameCore::init()
return false; 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; return true;
} }
void GameCore::render() void GameCore::render()
{ {
list<Vertex> vertices = graph.get_vertices();
list<Edge> edges = graph.get_edges();
for (list<Vertex>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++)
{
Vertex v = *cursor;
DrawUtils::draw(display, node, v.x_min, v.y_min);
}
for (list<Edge>::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() void GameCore::cleanup()
{ {
SDL_FreeSurface(background);
SDL_FreeSurface(node);
SDL_FreeSurface(display); SDL_FreeSurface(display);
SDL_Quit(); SDL_Quit();
} }
@ -71,3 +103,9 @@ void GameCore::on_exit()
{ {
is_running = false; is_running = false;
} }
void GameCore::on_lbutton_down(int x, int y)
{
graph.add_vertex(x, y, 25);
}

View File

@ -11,6 +11,7 @@
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include "mainevent.h" #include "mainevent.h"
#include "graph.h"
class GameCore : public MainEvent class GameCore : public MainEvent
{ {
@ -21,7 +22,7 @@ class GameCore : public MainEvent
protected: protected:
// event handlers // event handlers
void on_exit(); void on_exit();
void on_lbutton_down(int x, int y);
private: private:
bool init(); bool init();
@ -37,6 +38,9 @@ class GameCore : public MainEvent
// textures to draw // textures to draw
SDL_Surface* background; SDL_Surface* background;
SDL_Surface* node; SDL_Surface* node;
// data
Graph graph;
}; };
#endif #endif

View File

@ -25,12 +25,24 @@ bool Graph::vertex_present(int x, int y, int size)
return false; 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); 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; last_vertex = v;
} }

10
graph.h
View File

@ -15,10 +15,10 @@ struct Vertex
{ {
int x; int x;
int y; int y;
int min_x; int x_min;
int min_y; int x_max;
int max_x; int y_min;
int max_y; int y_max;
}; };
struct Edge struct Edge
@ -37,7 +37,7 @@ class Graph
list<Vertex> get_vertices() { return vertices; } list<Vertex> get_vertices() { return vertices; }
list<Edge> get_edges() { return edges; } list<Edge> get_edges() { return edges; }
void add_vertex(Vertex v); void add_vertex(int x, int y, int size);
private: private:
Vertex last_vertex; Vertex last_vertex;