#include "Game.hpp"

Game::Game(){

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		throw std::runtime_error("SDL could not initialize! SDL_Error: " + \
					std::string(SDL_GetError()));
	}

	_window = SDL_CreateWindow("Bomberman", \
				SDL_WINDOWPOS_CENTERED, \
				SDL_WINDOWPOS_CENTERED, \
				800, 600, \
				SDL_WINDOW_SHOWN);
	if (_window == nullptr) {
		throw std::runtime_error("Window could not be created! SDL_Error: " + \
					std::string(SDL_GetError()));
	}

	_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);
	if (_renderer == nullptr) {
		SDL_DestroyWindow(_window);
		throw std::runtime_error("Renderer could not be created! SDL_Error: " + \
					std::string(SDL_GetError()));
	}

	_isRunning = true;
}

Game::~Game() {
	if (_renderer) {
		SDL_DestroyRenderer(_renderer);
	}
	if (_window) {
		SDL_DestroyWindow(_window);
	}
	SDL_Quit();
}

void	Game::Run() {
	while (_isRunning) {
	//Handle events
		while (SDL_PollEvent(&_event)) {
			if (_event.type == SDL_QUIT) {
				_isRunning = false;
			}
		}

	//Clear the screen
		SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255);
		SDL_RenderClear(_renderer);

	//Render game objects here

	//Update the screen
		SDL_RenderPresent(_renderer);	
	}
}
