70 lines
1.4 KiB
C++
70 lines
1.4 KiB
C++
#include "game.hpp"
|
|
|
|
#include <chrono>
|
|
|
|
uint64_t time_milli() {
|
|
using namespace std::chrono;
|
|
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
|
|
}
|
|
|
|
Game::Game(SDL_Window* window, SDL_Surface* surface)
|
|
{
|
|
this->window = window;
|
|
this->surface = surface;
|
|
running = false;
|
|
}
|
|
|
|
Game::Game(SDL_Window* window, SDL_Surface* surface, std::string board_fen)
|
|
{
|
|
this->window = window;
|
|
this->surface = surface;
|
|
board = Board(board_fen);
|
|
running = false;
|
|
}
|
|
|
|
void Game::run()
|
|
{
|
|
uint64_t last_update_time = time_milli(), delta_time;
|
|
uint64_t time_quantum = 1000/60;
|
|
running = true;
|
|
while(running)
|
|
{
|
|
delta_time = time_milli()-last_update_time;
|
|
while(delta_time > time_quantum && running)
|
|
{
|
|
tick();
|
|
|
|
delta_time -= time_quantum;
|
|
last_update_time += time_quantum;
|
|
}
|
|
draw();
|
|
}
|
|
}
|
|
|
|
void Game::tick()
|
|
{
|
|
SDL_Event e;
|
|
while(SDL_PollEvent(&e) != 0)
|
|
{
|
|
if (e.type == SDL_QUIT || (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_q))
|
|
{
|
|
running = false;
|
|
}
|
|
|
|
if (e.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
int x,y;
|
|
SDL_GetMouseState(&x, &y);
|
|
x/=SPRITE_SIZE;
|
|
y/=SPRITE_SIZE;
|
|
board.set_selected_space(x,y);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Game::draw()
|
|
{
|
|
board.draw_board(surface);
|
|
|
|
SDL_UpdateWindowSurface(window);
|
|
} |