mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-09 10:13:21 +08:00
Update Breakout source code to match updates Text Rendering chapter.
This commit is contained in:
@@ -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<PowerUp> &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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#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);
|
||||
};
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
125
src/7.in_practice/3.2d_game/0.full_source/progress/2.program.cpp
Normal file
125
src/7.in_practice/3.2d_game/0.full_source/progress/2.program.cpp
Normal file
@@ -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 <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "game.h"
|
||||
#include "resource_manager.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// 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);
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -49,7 +49,6 @@ public:
|
||||
// game state
|
||||
GameState State;
|
||||
bool Keys[1024];
|
||||
bool KeysProcessed[1024];
|
||||
unsigned int Width, Height;
|
||||
std::vector<GameLevel> Levels;
|
||||
unsigned int Level;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -50,7 +50,6 @@ public:
|
||||
// game state
|
||||
GameState State;
|
||||
bool Keys[1024];
|
||||
bool KeysProcessed[1024];
|
||||
unsigned int Width, Height;
|
||||
std::vector<GameLevel> Levels;
|
||||
std::vector<PowerUp> PowerUps;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user