treewars/gamecore.cpp

80 lines
1.2 KiB
C++

#include "gamecore.h"
#include "mathutils.h"
#include "debug.h"
#include <SDL.h>
int GameCore::MAX_MOVE_DISTANCE = 100;
int GameCore::NODE_RADIUS = 12;
GameCore::GameCore()
{
is_running = true;
}
int GameCore::execute()
{
if (!init()) return 1;
SDL_Event event;
while (is_running)
{
while(SDL_PollEvent(&event))
handle_event(&event);
// iterate();
renderer.render(data, MAX_MOVE_DISTANCE);
}
cleanup();
return 0;
}
bool GameCore::init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
SDL_WM_SetCaption("TreeWars","TreeWars");
return renderer.init();
}
void GameCore::cleanup()
{
renderer.cleanup();
SDL_Quit();
}
void GameCore::on_exit()
{
is_running = false;
}
void GameCore::on_lbutton_down(int x, int y)
{
Vertex* cv = data.get_current_vertex();
if (cv != NULL &&
(MathUtils::distance(cv->x, cv->y, x, y)
> MAX_MOVE_DISTANCE))
{
data.select_vertex(x, y);
return;
}
data.do_vertex(x, y, NODE_RADIUS);
}
void GameCore::on_rbutton_down(int mX, int mY)
{
data.clear_current_vertex();
}
void GameCore::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
{
if (sym == SDLK_q && mod & KMOD_CTRL) is_running = false;
}