From 82b066067f3d05e3d8a76bee262884e522766de8 Mon Sep 17 00:00:00 2001 From: Joey de Vries Date: Tue, 28 Apr 2020 14:46:15 +0200 Subject: [PATCH] Update Breakout source code to match updates Text Rendering chapter. --- .../3.2d_game/0.full_source/game.cpp | 24 ++-- .../3.2d_game/0.full_source/game.h | 3 +- .../0.full_source/progress/2.game.cpp | 2 +- .../3.2d_game/0.full_source/progress/2.game.h | 1 - .../0.full_source/progress/2.program.cpp | 125 ++++++++++++++++++ .../0.full_source/progress/3.game.cpp | 2 +- .../0.full_source/progress/4.game.cpp | 2 +- .../3.2d_game/0.full_source/progress/4.game.h | 1 - .../0.full_source/progress/5.game.cpp | 2 +- .../3.2d_game/0.full_source/progress/5.game.h | 1 - .../0.full_source/progress/6.game.cpp | 2 +- .../0.full_source/progress/7.game.cpp | 2 +- .../0.full_source/progress/8.game.cpp | 2 +- .../3.2d_game/0.full_source/progress/8.game.h | 1 - .../0.full_source/progress/9.game.cpp | 2 +- .../3.2d_game/0.full_source/text_renderer.cpp | 12 +- 16 files changed, 152 insertions(+), 32 deletions(-) create mode 100644 src/7.in_practice/3.2d_game/0.full_source/progress/2.program.cpp 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 9f9c7a9..8e5e385 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 @@ -29,9 +29,10 @@ BallObject *Ball; ParticleGenerator *Particles; PostProcessor *Effects; ISoundEngine *SoundEngine = createIrrKlangDevice(); -float ShakeTime = 0.0f; TextRenderer *Text; +float ShakeTime = 0.0f; + Game::Game(unsigned int width, unsigned int height) : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height), Level(0), Lives(3) @@ -204,7 +205,7 @@ void Game::Render() { if (this->State == GAME_ACTIVE || this->State == GAME_MENU || this->State == GAME_WIN) { - // begin rendering to postprocessing quad + // begin rendering to postprocessing framebuffer Effects->BeginRender(); // draw background Renderer->DrawSprite(ResourceManager::GetTexture("background"), glm::vec2(0.0f, 0.0f), glm::vec2(this->Width, this->Height), 0.0f); @@ -220,7 +221,7 @@ void Game::Render() Particles->Draw(); // draw ball Ball->Draw(*Renderer); - // end rendering to postprocessing quad + // end rendering to postprocessing framebuffer Effects->EndRender(); // render postprocessing quad Effects->Render(glfwGetTime()); @@ -269,7 +270,7 @@ void Game::ResetPlayer() } -// power-ups +// powerups bool IsOtherPowerUpActive(std::vector &powerUps, std::string type); void Game::UpdatePowerUps(float dt) @@ -349,7 +350,6 @@ void Game::SpawnPowerUps(GameObject &block) void ActivatePowerUp(PowerUp &powerUp) { - // initiate a powerup based type of powerup if (powerUp.Type == "speed") { Ball->Velocity *= 1.2; @@ -424,7 +424,7 @@ void Game::DoCollisions() // collision resolution Direction dir = std::get<1>(collision); glm::vec2 diff_vector = std::get<2>(collision); - if (!(Ball->PassThrough && !box.IsSolid)) // don't do collision resolution on non-solid bricks if pass-through activated + if (!(Ball->PassThrough && !box.IsSolid)) // don't do collision resolution on non-solid bricks if pass-through is activated { if (dir == LEFT || dir == RIGHT) // horizontal collision { @@ -524,17 +524,17 @@ Collision CheckCollision(BallObject &one, GameObject &two) // AABB - Circle coll if (glm::length(difference) < one.Radius) // not <= since in that case a collision also occurs when object one exactly touches object two, which they are at the end of each collision resolution stage. return std::make_tuple(true, VectorDirection(difference), difference); else - return std::make_tuple(false, UP, glm::vec2(0, 0)); + return std::make_tuple(false, UP, glm::vec2(0.0f, 0.0f)); } // calculates which direction a vector is facing (N,E,S or W) Direction VectorDirection(glm::vec2 target) { glm::vec2 compass[] = { - glm::vec2(0.0f, 1.0f), // north - glm::vec2(1.0f, 0.0f), // east - glm::vec2(0.0f, -1.0f), // south - glm::vec2(-1.0f, 0.0f) // west + glm::vec2(0.0f, 1.0f), // up + glm::vec2(1.0f, 0.0f), // right + glm::vec2(0.0f, -1.0f), // down + glm::vec2(-1.0f, 0.0f) // left }; float max = 0.0f; unsigned int best_match = -1; @@ -548,4 +548,4 @@ Direction VectorDirection(glm::vec2 target) } } return (Direction)best_match; -} \ No newline at end of file +} diff --git a/src/7.in_practice/3.2d_game/0.full_source/game.h b/src/7.in_practice/3.2d_game/0.full_source/game.h index a953872..c795048 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/game.h +++ b/src/7.in_practice/3.2d_game/0.full_source/game.h @@ -14,7 +14,6 @@ #include #include -#include "game_object.h" #include "game_level.h" #include "power_up.h" @@ -72,7 +71,7 @@ public: // reset void ResetLevel(); void ResetPlayer(); - // power-ups + // powerups void SpawnPowerUps(GameObject &block); void UpdatePowerUps(float dt); }; diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/2.game.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/2.game.cpp index aaa9499..82542d1 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/2.game.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/2.game.cpp @@ -9,7 +9,7 @@ #include "game.h" Game::Game(unsigned int width, unsigned int height) - : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) + : State(GAME_MENU), Keys(), Width(width), Height(height) { } diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/2.game.h b/src/7.in_practice/3.2d_game/0.full_source/progress/2.game.h index d043dab..369027d 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/2.game.h +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/2.game.h @@ -28,7 +28,6 @@ public: // game state GameState State; bool Keys[1024]; - bool KeysProcessed[1024]; unsigned int Width, Height; // constructor/destructor Game(unsigned int width, unsigned int height); diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/2.program.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/2.program.cpp new file mode 100644 index 0000000..ff66ef0 --- /dev/null +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/2.program.cpp @@ -0,0 +1,125 @@ +/******************************************************************* +** 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 +#include + +#include "game.h" +#include "resource_manager.h" + +#include + +// GLFW function declerations +void framebuffer_size_callback(GLFWwindow* window, int width, int height); +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); + +// The Width of the screen +const unsigned int SCREEN_WIDTH = 800; +// The height of the screen +const unsigned int SCREEN_HEIGHT = 600; + +Game Breakout(SCREEN_WIDTH, SCREEN_HEIGHT); + +int main(int argc, char *argv[]) +{ + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + glfwWindowHint(GLFW_RESIZABLE, false); + + GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Breakout", nullptr, nullptr); + glfwMakeContextCurrent(window); + + // glad: load all OpenGL function pointers + // --------------------------------------- + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + std::cout << "Failed to initialize GLAD" << std::endl; + return -1; + } + + glfwSetKeyCallback(window, key_callback); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + // OpenGL configuration + // -------------------- + glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + // initialize game + // --------------- + Breakout.Init(); + + // deltaTime variables + // ------------------- + float deltaTime = 0.0f; + float lastFrame = 0.0f; + + // start game within menu state + // ---------------------------- + Breakout.State = GAME_MENU; + + while (!glfwWindowShouldClose(window)) + { + // calculate delta time + // -------------------- + float currentFrame = glfwGetTime(); + deltaTime = currentFrame - lastFrame; + lastFrame = currentFrame; + glfwPollEvents(); + + // manage user input + // ----------------- + Breakout.ProcessInput(deltaTime); + + // update game state + // ----------------- + Breakout.Update(deltaTime); + + // render + // ------ + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + Breakout.Render(); + + glfwSwapBuffers(window); + } + + // delete all resources as loaded using the resource manager + // --------------------------------------------------------- + ResourceManager::Clear(); + + glfwTerminate(); + return 0; +} + +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) +{ + // When a user presses the escape key, we set the WindowShouldClose property to true, closing the application + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); + if (key >= 0 && key < 1024) + { + if (action == GLFW_PRESS) + Breakout.Keys[key] = true; + else if (action == GLFW_RELEASE) + Breakout.Keys[key] = false; + } +} + +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + // make sure the viewport matches the new window dimensions; note that width and + // height will be significantly larger than specified on retina displays. + glViewport(0, 0, width, height); +} \ No newline at end of file diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/3.game.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/3.game.cpp index 6d78d49..81c4562 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/3.game.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/3.game.cpp @@ -16,7 +16,7 @@ SpriteRenderer *Renderer; Game::Game(unsigned int width, unsigned int height) - : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) + : State(GAME_MENU), Keys(), Width(width), Height(height) { } 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 index 4d11bbb..fe49cc5 100644 --- 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 @@ -16,7 +16,7 @@ SpriteRenderer *Renderer; GameObject *Player; Game::Game(unsigned int width, unsigned int height) - : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) + : State(GAME_MENU), Keys(), Width(width), Height(height) { } 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 index 2ead5f7..1c24672 100644 --- 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 @@ -33,7 +33,6 @@ public: // game state GameState State; bool Keys[1024]; - bool KeysProcessed[1024]; unsigned int Width, Height; // constructor/destructor Game(unsigned int width, unsigned int height); diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/5.game.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/5.game.cpp index 4009f5b..a1a9d2a 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/5.game.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/5.game.cpp @@ -18,7 +18,7 @@ GameObject *Player; BallObject *Ball; Game::Game(unsigned int width, unsigned int height) - : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) + : State(GAME_MENU), Keys(), Width(width), Height(height) { } diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/5.game.h b/src/7.in_practice/3.2d_game/0.full_source/progress/5.game.h index d46d230..fccea74 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/5.game.h +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/5.game.h @@ -49,7 +49,6 @@ public: // game state GameState State; bool Keys[1024]; - bool KeysProcessed[1024]; unsigned int Width, Height; std::vector Levels; unsigned int Level; diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/6.game.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/6.game.cpp index 5f3381f..394fd58 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/6.game.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/6.game.cpp @@ -20,7 +20,7 @@ BallObject *Ball; ParticleGenerator *Particles; Game::Game(unsigned int width, unsigned int height) - : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) + : State(GAME_MENU), Keys(), Width(width), Height(height) { } diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/7.game.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/7.game.cpp index 3c09f37..1cc89e9 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/7.game.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/7.game.cpp @@ -24,7 +24,7 @@ PostProcessor *Effects; float ShakeTime = 0.0f; Game::Game(unsigned int width, unsigned int height) - : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) + : State(GAME_MENU), Keys(), Width(width), Height(height) { } diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/8.game.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/8.game.cpp index 0412320..061335e 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/8.game.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/8.game.cpp @@ -26,7 +26,7 @@ PostProcessor *Effects; float ShakeTime = 0.0f; Game::Game(unsigned int width, unsigned int height) - : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) + : State(GAME_MENU), Keys(), Width(width), Height(height) { } diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/8.game.h b/src/7.in_practice/3.2d_game/0.full_source/progress/8.game.h index 8c5c880..ac9d249 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/8.game.h +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/8.game.h @@ -50,7 +50,6 @@ public: // game state GameState State; bool Keys[1024]; - bool KeysProcessed[1024]; unsigned int Width, Height; std::vector Levels; std::vector PowerUps; diff --git a/src/7.in_practice/3.2d_game/0.full_source/progress/9.game.cpp b/src/7.in_practice/3.2d_game/0.full_source/progress/9.game.cpp index ee8fb07..98e3127 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/progress/9.game.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/progress/9.game.cpp @@ -30,7 +30,7 @@ ISoundEngine *SoundEngine = createIrrKlangDevice(); float ShakeTime = 0.0f; Game::Game(unsigned int width, unsigned int height) - : State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height) + : State(GAME_MENU), Keys(), Width(width), Height(height) { } diff --git a/src/7.in_practice/3.2d_game/0.full_source/text_renderer.cpp b/src/7.in_practice/3.2d_game/0.full_source/text_renderer.cpp index 9af5917..d9c2e0a 100644 --- a/src/7.in_practice/3.2d_game/0.full_source/text_renderer.cpp +++ b/src/7.in_practice/3.2d_game/0.full_source/text_renderer.cpp @@ -116,13 +116,13 @@ void TextRenderer::RenderText(std::string text, float x, float y, float scale, g float h = ch.Size.y * scale; // update VBO for each character float vertices[6][4] = { - { xpos, ypos + h, 0.0, 1.0 }, - { xpos + w, ypos, 1.0, 0.0 }, - { xpos, ypos, 0.0, 0.0 }, + { xpos, ypos + h, 0.0f, 1.0f }, + { xpos + w, ypos, 1.0f, 0.0f }, + { xpos, ypos, 0.0f, 0.0f }, - { xpos, ypos + h, 0.0, 1.0 }, - { xpos + w, ypos + h, 1.0, 1.0 }, - { xpos + w, ypos, 1.0, 0.0 } + { xpos, ypos + h, 0.0f, 1.0f }, + { xpos + w, ypos + h, 1.0f, 1.0f }, + { xpos + w, ypos, 1.0f, 0.0f } }; // render glyph texture over quad glBindTexture(GL_TEXTURE_2D, ch.TextureID);