97 lines
2.9 KiB
C++
Executable File
97 lines
2.9 KiB
C++
Executable File
#include <iostream>
|
|
#include <algorithm>
|
|
|
|
#include "renderer.hpp"
|
|
#include "game.hpp"
|
|
|
|
const SDL_Color BACKGROUND_COLOR = {0, 64, 88, SDL_ALPHA_OPAQUE};
|
|
const SDL_Color SNAKE_COLOR = {0, 184, 0, SDL_ALPHA_OPAQUE};
|
|
const SDL_Color FOOD_COLOR = {248, 56, 0, SDL_ALPHA_OPAQUE};
|
|
|
|
Renderer::Renderer(std::string name, int screen_width, int screen_height, int tiles_x, int tiles_y) :
|
|
screen_width(screen_width),
|
|
screen_height(screen_height),
|
|
tiles_x(tiles_x),
|
|
tiles_y(tiles_y)
|
|
{
|
|
tile_size = std::min(screen_width/tiles_x, screen_height/tiles_y);
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0){
|
|
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
|
|
window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
|
if(window == nullptr) {
|
|
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
|
|
renderer = SDL_CreateRenderer(window, -1, 0);
|
|
if(renderer == nullptr) {
|
|
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
}
|
|
|
|
Renderer::~Renderer()
|
|
{
|
|
SDL_DestroyWindow(window);
|
|
window = nullptr;
|
|
|
|
SDL_DestroyRenderer(renderer);
|
|
renderer = nullptr;
|
|
|
|
SDL_Quit();
|
|
}
|
|
|
|
void Renderer::updateSize()
|
|
{
|
|
SDL_GetWindowSize(window, &screen_width, &screen_height);
|
|
tile_size = std::min(screen_width/tiles_x, screen_height/tiles_y);
|
|
}
|
|
|
|
void Renderer::clear()
|
|
{
|
|
SDL_RenderClear(renderer);
|
|
}
|
|
|
|
void Renderer::renderSnake(Snake snake)
|
|
{
|
|
for(auto body: snake.body){
|
|
SDL_Rect snake_rect = getRectInScreen(body.x, body.y);
|
|
SDL_SetRenderDrawColor(renderer, SNAKE_COLOR.r, SNAKE_COLOR.g, SNAKE_COLOR.b, SNAKE_COLOR.a);
|
|
SDL_RenderFillRect(renderer, &snake_rect);
|
|
}
|
|
}
|
|
|
|
void Renderer::renderFood(Food food)
|
|
{
|
|
SDL_Rect food_rect = getRectInScreen(food.x, food.y);
|
|
SDL_SetRenderDrawColor(renderer, FOOD_COLOR.r, FOOD_COLOR.g, FOOD_COLOR.b, FOOD_COLOR.a);
|
|
SDL_RenderFillRect(renderer, &food_rect);
|
|
}
|
|
|
|
void Renderer::renderArena()
|
|
{
|
|
SDL_Rect background_rect = {0, 0, screen_width, screen_height};
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
|
|
SDL_RenderFillRect(renderer, &background_rect);
|
|
|
|
SDL_Rect arena_rect = getRectInScreen(0,0);
|
|
arena_rect.w = tile_size * tiles_x;
|
|
arena_rect.h = tile_size * tiles_y;
|
|
SDL_SetRenderDrawColor(renderer, BACKGROUND_COLOR.r, BACKGROUND_COLOR.g, BACKGROUND_COLOR.b, BACKGROUND_COLOR.a);
|
|
SDL_RenderFillRect(renderer, &arena_rect);
|
|
}
|
|
|
|
void Renderer::render()
|
|
{
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
SDL_Rect Renderer::getRectInScreen(int x, int y){
|
|
int rect_x = (x * tile_size) + ((screen_width / 2) - (tile_size * tiles_x / 2));
|
|
int rect_y = (y * tile_size) + ((screen_height / 2) - (tile_size * tiles_y / 2));
|
|
|
|
SDL_Rect rect = {rect_x, rect_y, tile_size, tile_size};
|
|
return rect;
|
|
}
|