2011-06-22 20:12:22 +00:00
|
|
|
#include "gamecore.h"
|
|
|
|
#include "drawutils.h"
|
2011-06-23 02:47:56 +00:00
|
|
|
#include "debug.h"
|
2011-06-22 20:12:22 +00:00
|
|
|
|
2011-06-23 02:32:33 +00:00
|
|
|
int GameCore::NODE_SIZE = 25;
|
|
|
|
|
2011-06-22 20:12:22 +00:00
|
|
|
GameCore::GameCore()
|
|
|
|
{
|
|
|
|
display = NULL;
|
2011-06-22 21:57:44 +00:00
|
|
|
background = NULL;
|
|
|
|
node = NULL;
|
2011-06-23 02:32:33 +00:00
|
|
|
move_template = NULL;
|
2011-06-22 20:12:22 +00:00
|
|
|
is_running = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GameCore::execute()
|
|
|
|
{
|
|
|
|
if (!init()) return 1;
|
|
|
|
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
while (is_running)
|
|
|
|
{
|
|
|
|
while(SDL_PollEvent(&event))
|
|
|
|
handle_event(&event);
|
|
|
|
iterate();
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GameCore::init()
|
|
|
|
{
|
|
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
|
|
|
|
|
|
|
|
display = SDL_SetVideoMode(800,600,32, SDL_HWSURFACE | SDL_DOUBLEBUF);
|
|
|
|
if (display == NULL)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
std::cerr << "GameCore::init(): error: Couldn't create main surface\n";
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-22 21:57:44 +00:00
|
|
|
background = DrawUtils::load("background.bmp");
|
|
|
|
node = DrawUtils::load("node.bmp");
|
2011-06-23 02:39:37 +00:00
|
|
|
move_template = DrawUtils::load("mvtemplate.bmp");
|
2011-06-23 02:32:33 +00:00
|
|
|
|
|
|
|
if (background == NULL || node == NULL || move_template == NULL)
|
2011-06-22 21:57:44 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
std::cerr << "GameCore::init(): error: Couldn't load an image file\n";
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-23 02:39:37 +00:00
|
|
|
DrawUtils::transpare(node, 255, 0, 255);
|
|
|
|
DrawUtils::transpare(move_template, 255, 0, 255);
|
|
|
|
SDL_SetAlpha(move_template, SDL_SRCALPHA, 128);
|
|
|
|
|
2011-06-22 20:12:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameCore::render()
|
|
|
|
{
|
2011-06-22 22:24:20 +00:00
|
|
|
DrawUtils::draw(display, background, 0, 0);
|
|
|
|
|
2011-06-23 01:54:35 +00:00
|
|
|
list<Vertex*> vertices = graph.get_vertices();
|
2011-06-22 21:57:44 +00:00
|
|
|
list<Edge> edges = graph.get_edges();
|
2011-06-22 20:12:22 +00:00
|
|
|
|
2011-06-23 01:54:35 +00:00
|
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
2011-06-22 21:57:44 +00:00
|
|
|
cursor != vertices.end(); cursor++)
|
|
|
|
{
|
2011-06-23 01:54:35 +00:00
|
|
|
Vertex v = *(*cursor);
|
2011-06-22 21:57:44 +00:00
|
|
|
DrawUtils::draw(display, node, v.x_min, v.y_min);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (list<Edge>::iterator cursor = edges.begin();
|
|
|
|
cursor != edges.end(); cursor++)
|
|
|
|
{
|
|
|
|
Edge e = *cursor;
|
2011-06-23 01:54:35 +00:00
|
|
|
DrawUtils::draw_line(display, e.a->x, e.a->y, e.b->x, e.b->y, 2,
|
|
|
|
0x000000);
|
2011-06-22 21:57:44 +00:00
|
|
|
}
|
|
|
|
|
2011-06-23 02:32:33 +00:00
|
|
|
if (graph.get_current_vertex() != NULL)
|
|
|
|
{
|
|
|
|
Vertex* v = graph.get_current_vertex();
|
2011-06-23 03:06:23 +00:00
|
|
|
DrawUtils::draw(display, move_template,
|
|
|
|
v->x - Graph::MAX_MOVE_DISTANCE,
|
|
|
|
v->y - Graph::MAX_MOVE_DISTANCE);
|
2011-06-23 02:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-22 21:57:44 +00:00
|
|
|
SDL_Flip(display);
|
2011-06-22 20:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameCore::iterate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameCore::cleanup()
|
|
|
|
{
|
2011-06-22 21:57:44 +00:00
|
|
|
SDL_FreeSurface(background);
|
|
|
|
SDL_FreeSurface(node);
|
2011-06-22 20:12:22 +00:00
|
|
|
SDL_FreeSurface(display);
|
|
|
|
SDL_Quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameCore::on_exit()
|
|
|
|
{
|
|
|
|
is_running = false;
|
|
|
|
}
|
2011-06-22 21:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
void GameCore::on_lbutton_down(int x, int y)
|
|
|
|
{
|
2011-06-23 02:32:33 +00:00
|
|
|
graph.do_vertex(x, y, NODE_SIZE);
|
2011-06-22 21:57:44 +00:00
|
|
|
}
|