1
0
Files
BreakOut/src/game.cpp
2022-10-19 20:16:07 +02:00

97 lines
2.9 KiB
C++

#include "game.h"
#include <iostream>
SpriteRenderer* Renderer;
Game::~Game()
{
delete Renderer;
}
Game::Game(unsigned int width, unsigned int height)
: State(GAME_ACTIVE), Keys(), Width(width), Height(height)
{
}
// Initial size of the player paddle
const glm::vec2 PLAYER_SIZE (100.0f, 20.0f);
// Initial velocity of the player paddle
const float PLAYER_VELOCITY(500.0f);
GameObject* player;
void Game::Init()
{
// Load shaders
ResourceManager::LoadShader("shader.vs", "shader.fs", nullptr, "sprite");
// configure shaders
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(this->Width), static_cast<float>(this->Height), 0.0f, -1.0f, 1.0f);
ResourceManager::GetShader("sprite").Use().SetInteger("image", 0);
ResourceManager::GetShader("sprite").SetMatrix4("projection", projection);
// set render-specific controls
Renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
// load textures
ResourceManager::LoadTexture("textures/awesomeface.png", true, "face");
ResourceManager::LoadTexture("textures/background.jpg", false, "background");
ResourceManager::LoadTexture("textures/block.png", false, "block");
ResourceManager::LoadTexture("textures/block_solid.png", false, "block_solid");
ResourceManager::LoadTexture("textures/paddle.png", true, "paddle");
// Load levels
GameLevel one; one.Load("levels/one.lvl", this->Width, this->Height / 2);
GameLevel two; two.Load("levels/two.lvl", this->Width, this->Height / 2);
GameLevel three; three.Load("levels/three.lvl", this->Width, this->Height / 2);
GameLevel four; four.Load("levels/four.lvl", this->Width, this->Height / 2);
this->Levels.push_back(one);
this->Levels.push_back(two);
this->Levels.push_back(three);
this->Levels.push_back(four);
this->Level = 0;
glm::vec2 playerPos = glm::vec2(this->Width /2 - PLAYER_SIZE.x /2.0f,
this->Height - PLAYER_SIZE.y);
player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle"));
}
void Game::ProcessInput(float dt)
{
if(this->State == GAME_ACTIVE)
{
float velocity = PLAYER_VELOCITY * dt;
if( this->Keys[GLFW_KEY_A])
{
if(player->Position.x >= 0.0f)
player->Position.x -= velocity;
}
if( this->Keys[GLFW_KEY_D])
{
if(player->Position.x <= this->Width - player->Size.x)
player->Position.x += velocity;
}
}
}
void Game::Update(float dt)
{
}
void Game::Render()
{
if(this->State == GAME_ACTIVE)
{
// draw background
Renderer->DrawSprite(ResourceManager::GetTexture("background"),
glm::vec2(0.0f, 0.0f), glm::vec2(this->Width, this->Height), 0.0f);
// draw level
this->Levels[this->Level].Draw(*Renderer);
player->Draw(*Renderer);
}
}