From 351c9683735d61783368b52a7c7f121811239a16 Mon Sep 17 00:00:00 2001 From: Joey de Vries Date: Thu, 23 Apr 2020 12:00:15 +0200 Subject: [PATCH] Update breakout code to match revised changes 'Levels' chapter. --- .../3.2d_game/0.full_source/game.cpp | 2 +- .../3.2d_game/0.full_source/game_object.cpp | 2 +- .../0.full_source/progress/4.game.cpp | 96 +++++++++++++++++++ .../3.2d_game/0.full_source/progress/4.game.h | 49 ++++++++++ 4 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 src/7.in_practice/3.2d_game/0.full_source/progress/4.game.cpp create mode 100644 src/7.in_practice/3.2d_game/0.full_source/progress/4.game.h diff --git a/src/7.in_practice/3.2d_game/0.full_source/game.cpp b/src/7.in_practice/3.2d_game/0.full_source/game.cpp index 0a92e03..9f9c7a9 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/game.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/game.cpp @@ -179,7 +179,7 @@ void Game::ProcessInput(float dt) // move playerboard if (this->Keys[GLFW_KEY_A]) { - if (Player->Position.x >= 0) + if (Player->Position.x >= 0.0f) { Player->Position.x -= velocity; if (Ball->Stuck) diff --git a/src/7.in_practice/3.2d_game/0.full_source/game_object.cpp b/src/7.in_practice/3.2d_game/0.full_source/game_object.cpp index 33718d6..5459b3c 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/game_object.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/game_object.cpp @@ -10,7 +10,7 @@ GameObject::GameObject() - : Position(0, 0), Size(1, 1), Velocity(0.0f), Color(1.0f), Rotation(0.0f), Sprite(), IsSolid(false), Destroyed(false) { } + : Position(0.0f, 0.0f), Size(1.0f, 1.0f), Velocity(0.0f), Color(1.0f), Rotation(0.0f), Sprite(), IsSolid(false), Destroyed(false) { } GameObject::GameObject(glm::vec2 pos, glm::vec2 size, Texture2D sprite, glm::vec3 color, glm::vec2 velocity) : Position(pos), Size(size), Velocity(velocity), Color(color), Rotation(0.0f), Sprite(sprite), IsSolid(false), Destroyed(false) { } diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/4.game.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/4.game.cpp new file mode 100644 index 0000000..e9576b1 --- /dev/null +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/4.game.cpp @@ -0,0 +1,96 @@ +/******************************************************************* +** This code is part of Breakout. +** +** Breakout is free software: you can redistribute it and/or modify +** it under the terms of the CC BY 4.0 license as published by +** Creative Commons, either version 4 of the License, or (at your +** option) any later version. +******************************************************************/ +#include "game.h" +#include "resource_manager.h" +#include "sprite_renderer.h" + + +// Game-related State data +SpriteRenderer *Renderer; +GameObject *Player; + +Game::Game(unsigned int width, unsigned int height) + : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) +{ + +} + +Game::~Game() +{ + +} + +void Game::Init() +{ + // load shaders + ResourceManager::LoadShader("shaders/sprite.vs", "shaders/sprite.frag", nullptr, "sprite"); + // configure shaders + glm::mat4 projection = glm::ortho(0.0f, static_cast(this->Width), + static_cast(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/background.jpg", false, "background"); + ResourceManager::LoadTexture("textures/awesomeface.png", true, "face"); + 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; + // configure game objects + glm::vec2 playerPos = glm::vec2(this->Width / 2.0f - PLAYER_SIZE.x / 2.0f, this->Height - PLAYER_SIZE.y); + Player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle")); +} + +void Game::Update(float dt) +{ + +} + +void Game::ProcessInput(float dt) +{ + if (this->State == GAME_ACTIVE) + { + float velocity = PLAYER_VELOCITY * dt; + // move playerboard + 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::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); + // draw player + Player->Draw(*Renderer); + } +} \ No newline at end of file diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/4.game.h b/src/7.in_practice/3.2d_game/0.full_source/progress/4.game.h new file mode 100644 index 0000000..2ead5f7 --- /dev/null +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/4.game.h @@ -0,0 +1,49 @@ +/******************************************************************* +** This code is part of Breakout. +** +** Breakout is free software: you can redistribute it and/or modify +** it under the terms of the CC BY 4.0 license as published by +** Creative Commons, either version 4 of the License, or (at your +** option) any later version. +******************************************************************/ +#ifndef GAME_H +#define GAME_H + +#include +#include + +// Represents the current state of the game +enum GameState { + GAME_ACTIVE, + GAME_MENU, + GAME_WIN +}; + +// 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); + +// Game holds all game-related state and functionality. +// Combines all game-related data into a single class for +// easy access to each of the components and manageability. +class Game +{ +public: + // game state + GameState State; + bool Keys[1024]; + bool KeysProcessed[1024]; + unsigned int Width, Height; + // constructor/destructor + Game(unsigned int width, unsigned int height); + ~Game(); + // initialize game state (load all shaders/textures/levels) + void Init(); + // game loop + void ProcessInput(float dt); + void Update(float dt); + void Render(); +}; + +#endif \ No newline at end of file