#include "gamecore.h" #include "drawutils.h" #include "mathutils.h" #include "debug.h" int GameCore::MAX_MOVE_DISTANCE = 100; int GameCore::NODE_RADIUS = 12; GameCore::GameCore() { display = NULL; background = 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(1024,768,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"); if (background == NULL) { #ifdef DEBUG std::cerr << "GameCore::init(): error: Couldn't load background image\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 vertices = graph.get_vertices(); list edges = graph.get_edges(); if (graph.get_current_vertex() != NULL) { Vertex* v = graph.get_current_vertex(); DrawUtils::draw_circle_filled(display, v->x, v->y, MAX_MOVE_DISTANCE, 0xcb1919); } for (list::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) { Vertex v = *(*cursor); DrawUtils::draw_circle_filled(display, v.x, v.y, v.r, 0x000000); } for (list::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); } SDL_Flip(display); } void GameCore::iterate() { } void GameCore::cleanup() { SDL_FreeSurface(background); SDL_FreeSurface(display); SDL_Quit(); } void GameCore::on_exit() { is_running = false; } void GameCore::on_lbutton_down(int x, int y) { Vertex* cv = graph.get_current_vertex(); if (cv != NULL && (MathUtils::distance(cv->x, cv->y, x, y) > MAX_MOVE_DISTANCE)) { graph.select_vertex(x, y); return; } graph.do_vertex(x, y, NODE_RADIUS); }