2011-06-22 20:12:22 +00:00
|
|
|
#include "gamecore.h"
|
|
|
|
#include "drawutils.h"
|
2011-06-23 16:36:40 +00:00
|
|
|
#include "mathutils.h"
|
2011-06-23 02:47:56 +00:00
|
|
|
#include "debug.h"
|
2011-06-22 20:12:22 +00:00
|
|
|
|
2011-06-23 16:36:40 +00:00
|
|
|
int GameCore::MAX_MOVE_DISTANCE = 100;
|
|
|
|
int GameCore::NODE_RADIUS = 12;
|
2011-06-23 02:32:33 +00:00
|
|
|
|
2011-06-22 20:12:22 +00:00
|
|
|
GameCore::GameCore()
|
|
|
|
{
|
|
|
|
display = NULL;
|
2011-06-22 21:57:44 +00:00
|
|
|
background = 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;
|
|
|
|
|
2011-06-23 16:46:32 +00:00
|
|
|
display = SDL_SetVideoMode(1024,768,32, SDL_HWSURFACE | SDL_DOUBLEBUF);
|
2011-06-22 20:12:22 +00:00
|
|
|
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");
|
2011-06-23 02:32:33 +00:00
|
|
|
|
2011-06-23 14:10:07 +00:00
|
|
|
if (background == NULL)
|
2011-06-22 21:57:44 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2011-06-23 14:10:07 +00:00
|
|
|
std::cerr << "GameCore::init(): error: Couldn't load background image\n";
|
2011-06-22 21:57:44 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-23 14:10:07 +00:00
|
|
|
// DrawUtils::transpare(node, 255, 0, 255);
|
2011-06-23 14:04:14 +00:00
|
|
|
// DrawUtils::transpare(move_template, 255, 0, 255);
|
|
|
|
// SDL_SetAlpha(move_template, SDL_SRCALPHA, 128);
|
2011-06-23 02:39:37 +00:00
|
|
|
|
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 21:28:49 +00:00
|
|
|
list<Vertex*> vertices = data.get_vertices();
|
|
|
|
list<Edge> edges = data.get_edges();
|
2011-06-22 20:12:22 +00:00
|
|
|
|
2011-06-23 21:28:49 +00:00
|
|
|
if (data.get_current_vertex() != NULL)
|
2011-06-23 14:04:14 +00:00
|
|
|
{
|
2011-06-23 21:28:49 +00:00
|
|
|
Vertex* v = data.get_current_vertex();
|
2011-06-23 14:04:14 +00:00
|
|
|
DrawUtils::draw_circle_filled(display, v->x, v->y,
|
2011-06-23 18:34:53 +00:00
|
|
|
MAX_MOVE_DISTANCE, 0xcb1919);
|
2011-06-23 14:04:14 +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-23 16:46:32 +00:00
|
|
|
DrawUtils::draw_circle_filled(display, v.x, v.y, v.r,
|
2011-06-23 19:10:40 +00:00
|
|
|
v.colour);
|
2011-06-22 21:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2011-06-23 19:10:40 +00:00
|
|
|
e.a->colour);
|
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);
|
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 21:28:49 +00:00
|
|
|
Vertex* cv = data.get_current_vertex();
|
2011-06-23 16:36:40 +00:00
|
|
|
if (cv != NULL &&
|
|
|
|
(MathUtils::distance(cv->x, cv->y, x, y)
|
|
|
|
> MAX_MOVE_DISTANCE))
|
2011-06-23 16:46:32 +00:00
|
|
|
{
|
2011-06-23 21:28:49 +00:00
|
|
|
data.select_vertex(x, y);
|
2011-06-23 16:36:40 +00:00
|
|
|
return;
|
2011-06-23 16:46:32 +00:00
|
|
|
}
|
2011-06-23 16:36:40 +00:00
|
|
|
|
2011-06-23 21:28:49 +00:00
|
|
|
data.do_vertex(x, y, NODE_RADIUS);
|
2011-06-23 19:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameCore::on_rbutton_down(int mX, int mY)
|
|
|
|
{
|
2011-06-23 21:28:49 +00:00
|
|
|
data.clear_current_vertex();
|
2011-06-22 21:57:44 +00:00
|
|
|
}
|