145 lines
2.8 KiB
C++
145 lines
2.8 KiB
C++
#include "gamecore.h"
|
|
#include "drawutils.h"
|
|
#include "debug.h"
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
background = DrawUtils::load("background.bmp");
|
|
node = DrawUtils::load("node.bmp");
|
|
move_template = DrawUtils::load("mvtemplate.bmp");
|
|
|
|
if (background == NULL || node == NULL || move_template == NULL)
|
|
{
|
|
#ifdef DEBUG
|
|
std::cerr << "GameCore::init(): error: Couldn't load an image file\n";
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
DrawUtils::transpare(node, 255, 0, 255);
|
|
DrawUtils::transpare(move_template, 255, 0, 255);
|
|
SDL_SetAlpha(move_template, SDL_SRCALPHA, 128);
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
void GameCore::render()
|
|
{
|
|
DrawUtils::draw(display, background, 0, 0);
|
|
|
|
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(display, e.a->x, e.a->y, e.b->x, e.b->y, 2,
|
|
0x000000);
|
|
}
|
|
|
|
if (graph.get_current_vertex() != NULL)
|
|
{
|
|
Vertex* v = graph.get_current_vertex();
|
|
DrawUtils::draw(display, move_template, v->x - MAX_MOVE_DISTANCE / 2,
|
|
v->y - MAX_MOVE_DISTANCE / 2);
|
|
}
|
|
|
|
|
|
SDL_Flip(display);
|
|
}
|
|
|
|
|
|
void GameCore::iterate()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
void GameCore::cleanup()
|
|
{
|
|
SDL_FreeSurface(background);
|
|
SDL_FreeSurface(node);
|
|
SDL_FreeSurface(display);
|
|
SDL_Quit();
|
|
}
|
|
|
|
|
|
void GameCore::on_exit()
|
|
{
|
|
is_running = false;
|
|
}
|
|
|
|
|
|
void GameCore::on_lbutton_down(int x, int y)
|
|
{
|
|
Vertex* v = graph.get_current_vertex();
|
|
Vertex new_v = Vertex();
|
|
new_v.x = x;
|
|
new_v.y = y;
|
|
|
|
#ifdef DEBUG
|
|
if (v != NULL)
|
|
cerr << "debug: GameCore::on_lbutton_down(): distance="
|
|
<< Vertex::distance(*v, new_v) << "\n";
|
|
else
|
|
cerr << "debug: GameCore::on_lbutton_down(): distance=0, current_vertex is NULL\n";
|
|
#endif
|
|
|
|
if (v != NULL && (Vertex::distance(*v, new_v) > MAX_MOVE_DISTANCE))
|
|
return;
|
|
|
|
graph.do_vertex(x, y, NODE_SIZE);
|
|
}
|