mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-11 02:53:23 +08:00
Add full source code of the finished (and revised code) Breakout example. Not set up for cross-platform compilation as it has irrKlang and FreeType dependency, but at least source code is available online (and can be referenced from Breakout chapters).
This commit is contained in:
117
src/7.in_practice/3.2d_game/0.full_source/program.cpp
Normal file
117
src/7.in_practice/3.2d_game/0.full_source/program.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
/*******************************************************************
|
||||
** 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 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);
|
||||
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);
|
||||
|
||||
// OpenGL configuration
|
||||
// --------------------
|
||||
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
glEnable(GL_CULL_FACE);
|
||||
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;
|
||||
Breakout.KeysProcessed[key] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user