mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-30 20:13:22 +08:00
Update Breakout code for 'Setting up' chapter.
This commit is contained in:
@@ -34,7 +34,7 @@ TextRenderer *Text;
|
|||||||
|
|
||||||
|
|
||||||
Game::Game(unsigned int width, unsigned int height)
|
Game::Game(unsigned int width, unsigned int height)
|
||||||
: State(GAME_MENU), Keys(), Width(width), Height(height), Level(0), Lives(3)
|
: State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height), Level(0), Lives(3)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -548,4 +548,4 @@ Direction VectorDirection(glm::vec2 target)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (Direction)best_match;
|
return (Direction)best_match;
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// GLFW function declerations
|
// 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);
|
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
|
||||||
|
|
||||||
// The Width of the screen
|
// The Width of the screen
|
||||||
@@ -30,6 +31,9 @@ int main(int argc, char *argv[])
|
|||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
#ifdef __APPLE__
|
||||||
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
|
||||||
|
#endif
|
||||||
glfwWindowHint(GLFW_RESIZABLE, false);
|
glfwWindowHint(GLFW_RESIZABLE, false);
|
||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Breakout", nullptr, nullptr);
|
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Breakout", nullptr, nullptr);
|
||||||
@@ -44,11 +48,11 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
glfwSetKeyCallback(window, key_callback);
|
glfwSetKeyCallback(window, key_callback);
|
||||||
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
|
||||||
// OpenGL configuration
|
// OpenGL configuration
|
||||||
// --------------------
|
// --------------------
|
||||||
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||||
glEnable(GL_CULL_FACE);
|
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
||||||
@@ -114,4 +118,11 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
|
|||||||
Breakout.KeysProcessed[key] = false;
|
Breakout.KeysProcessed[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);
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
|
||||||
Game::Game(unsigned int width, unsigned int height)
|
Game::Game(unsigned int width, unsigned int height)
|
||||||
: State(GAME_MENU), Keys(), Width(width), Height(height)
|
: State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ public:
|
|||||||
// game state
|
// game state
|
||||||
GameState State;
|
GameState State;
|
||||||
bool Keys[1024];
|
bool Keys[1024];
|
||||||
|
bool KeysProcessed[1024];
|
||||||
unsigned int Width, Height;
|
unsigned int Width, Height;
|
||||||
// constructor/destructor
|
// constructor/destructor
|
||||||
Game(unsigned int width, unsigned int height);
|
Game(unsigned int width, unsigned int height);
|
||||||
|
|||||||
@@ -105,15 +105,11 @@ Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha)
|
|||||||
texture.Image_Format = GL_RGBA;
|
texture.Image_Format = GL_RGBA;
|
||||||
}
|
}
|
||||||
// load image
|
// load image
|
||||||
//int width, height;
|
|
||||||
//unsigned char* image = SOIL_load_image(file, &width, &height, 0, texture.Image_Format == GL_RGBA ? SOIL_LOAD_RGBA : SOIL_LOAD_RGB);
|
|
||||||
|
|
||||||
int width, height, nrChannels;
|
int width, height, nrChannels;
|
||||||
unsigned char* data = stbi_load(file, &width, &height, &nrChannels, 0);
|
unsigned char* data = stbi_load(file, &width, &height, &nrChannels, 0);
|
||||||
// now generate texture
|
// now generate texture
|
||||||
texture.Generate(width, height, data);
|
texture.Generate(width, height, data);
|
||||||
// and finally free image data
|
// and finally free image data
|
||||||
//SOIL_free_image_data(image);
|
|
||||||
stbi_image_free(data);
|
stbi_image_free(data);
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user