diff options
| author | realtradam <[email protected]> | 2022-11-25 17:36:37 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2022-11-25 17:36:37 -0500 |
| commit | 62de63a0d26ece5c0dc2cd48c0a41d35de1e38d4 (patch) | |
| tree | b91117aabf53eb1dec0b8dc0d34c7656872f71e8 | |
| parent | 4e7226ad1b081982e5cbed349edca1c5b0e1b779 (diff) | |
| download | Ogle-62de63a0d26ece5c0dc2cd48c0a41d35de1e38d4.tar.gz Ogle-62de63a0d26ece5c0dc2cd48c0a41d35de1e38d4.zip | |
cleanup code
| -rw-r--r-- | src/batch.cpp | 14 | ||||
| -rw-r--r-- | src/batch.hpp | 11 | ||||
| -rw-r--r-- | src/input.cpp | 7 | ||||
| -rw-r--r-- | src/input.hpp | 2 | ||||
| -rw-r--r-- | src/main.cpp | 63 | ||||
| -rw-r--r-- | src/random.cpp | 5 | ||||
| -rw-r--r-- | src/random.hpp | 1 | ||||
| -rw-r--r-- | src/shader.cpp | 28 | ||||
| -rw-r--r-- | src/shader.hpp | 13 | ||||
| -rw-r--r-- | src/shaders/default.frag | 18 | ||||
| -rw-r--r-- | src/shaders/default.vert | 4 | ||||
| -rw-r--r-- | src/texture.cpp | 10 | ||||
| -rw-r--r-- | src/texture.hpp | 2 | ||||
| -rw-r--r-- | src/window.cpp | 9 | ||||
| -rw-r--r-- | src/window.hpp | 4 |
15 files changed, 123 insertions, 68 deletions
diff --git a/src/batch.cpp b/src/batch.cpp index b36e6d6..36c8a19 100644 --- a/src/batch.cpp +++ b/src/batch.cpp @@ -1,11 +1,15 @@ -#include "batch.hpp" -#include <vector> -#include <iostream> +// external libs #include "glad/glad.h" +// project headers +#include "batch.hpp" #include "texture.hpp" +// std libs +#include <vector> +#include <iostream> + Batch::Batch(unsigned int batch_limit) { @@ -118,7 +122,7 @@ int Batch::primativeDrawQuad( // if surpassing batch limit // or if texture is different if((size >= batch_limit) || ((last_texture != texture_id) && (texture_id != 0))) - this->flush_batch(); last_texture = texture_id; + this->flush(); last_texture = texture_id; float verts[] = { @@ -159,7 +163,7 @@ int Batch::primativeDrawQuad( return 0; } -void Batch::flush_batch() +void Batch::flush() { glBindVertexArray(VAO); diff --git a/src/batch.hpp b/src/batch.hpp index 444a547..39a2a25 100644 --- a/src/batch.hpp +++ b/src/batch.hpp @@ -1,12 +1,15 @@ #pragma once -#include <vector> - +// external libs #include "glad/glad.h" -#include <glm/glm.hpp> +#include "glm/glm.hpp" +// project headers #include "texture.hpp" +// std libs +#include <vector> + class Batch { public: @@ -32,7 +35,7 @@ class Batch float height, glm::vec4 color ); - void flush_batch(); + void flush(); private: int primativeDrawQuad( unsigned int texture_id, diff --git a/src/input.cpp b/src/input.cpp index 5622afd..674b274 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,6 +1,11 @@ -#include "input.hpp" + +// external libs #include "GLFW/glfw3.h" +// project headers +#include "input.hpp" + + namespace Input { namespace { diff --git a/src/input.hpp b/src/input.hpp index 20bba64..abaa6fc 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -1,4 +1,6 @@ #pragma once + +// external libs #include "GLFW/glfw3.h" namespace Input { diff --git a/src/main.cpp b/src/main.cpp index 5cca382..81ee360 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,58 +1,70 @@ + +// external libs #include "glad/glad.h" #include "GLFW/glfw3.h" +#include "glm/glm.hpp" +#include "glm/gtc/matrix_transform.hpp" +#include "glm/gtc/type_ptr.hpp" +// project headers #include "input.hpp" #include "shader.hpp" #include "batch.hpp" #include "window.hpp" #include "random.hpp" +// std libs #include <iostream> #include <cmath> #include <chrono> -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> +const int SCREEN_WIDTH = 800; +const int SCREEN_HEIGHT = 450; +const int MAX_BUNNIES = 15000; typedef struct Bunny { glm::vec2 position; glm::vec2 speed; glm::vec4 color; } Bunny; -const int SCREEN_WIDTH = 800; -const int SCREEN_HEIGHT = 450; - - int main() { + + // init glfw window Window::init(SCREEN_WIDTH, SCREEN_HEIGHT, "Ogle"); + // init various opengl components Shader shader = Shader("src/shaders/default.vert", "src/shaders/default.frag"); Batch batch = Batch(1024 * 8); Texture texture = Texture("assets/wabbit_alpha.png"); + // setup projection matrix + // Here we swap the height-begin and + // height-end values to flip it as opengl is "upsidedown" glm::mat4 transform = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f, 0.1f, 100.0f); shader.use(); shader.setMatrix4fv("transform", transform); - auto start = std::chrono::high_resolution_clock::now(); - auto stop = std::chrono::high_resolution_clock::now(); - + // setup bunnies std::vector<Bunny> bunnies = std::vector<Bunny>{}; - const int MAX_BUNNIES = 50000; + bunnies.reserve(MAX_BUNNIES * sizeof(Bunny)); + + // setup fps measuring + std::vector<int> fps_smoothing = std::vector<int>{}; // game loop while(!glfwWindowShouldClose(Window::get())) { - start = std::chrono::high_resolution_clock::now(); + auto start_frametime = std::chrono::high_resolution_clock::now(); + Input::process(Window::get()); - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.4f, 0.2f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); shader.use(); + // create bunnies when clicking if(Input::get_mouse_click()) { Bunny new_bunny; @@ -73,7 +85,6 @@ int main() { bunnies.push_back(new_bunny); } } - //batch.drawRectangle(100,100,50.0,50.0,glm::vec4(0.3,0.3,1.0,1.0)); } // move bunnies @@ -88,6 +99,7 @@ int main() { bunnies.at(i).speed.y *= -1; } + // render bunnies for (int i = 0; i < bunnies.size(); i++) { batch.drawTexture( @@ -99,22 +111,25 @@ int main() { bunnies.at(i).color ); } - //batch.drawTexture(texture,0.0,0.0,1.0,1.0,glm::vec4(1.0,1.0,1.0,0.5)); - - batch.flush_batch(); // does the drawing + batch.flush(); // forces remaining batched verts to be rendered glfwSwapBuffers(Window::get()); glfwPollEvents(); - stop = std::chrono::high_resolution_clock::now(); - auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start); + auto end_frametime = std::chrono::high_resolution_clock::now(); + auto frametime = std::chrono::duration_cast<std::chrono::microseconds>(end_frametime - start_frametime); - std::cout << bunnies.size(); - std::cout << " Bunnies at "; - std::cout << round(1.0f/((float)duration.count() / 10000000.0f))/10.0; - std::cout << " FPS"; - std::cout << std::endl; + if(fps_smoothing.size() >= 30) + fps_smoothing.erase(fps_smoothing.begin()); + fps_smoothing.push_back(round(1.0f/((float)frametime.count() / 1000000.0f))); + int average = 0; + for(int i = 0; i < fps_smoothing.size(); i++) + { + average += fps_smoothing.at(i); + } + average /= fps_smoothing.size(); + std::cout << bunnies.size() << " Bunnies at " << average << " FPS" << std::endl; } glfwTerminate(); diff --git a/src/random.cpp b/src/random.cpp index ec49e36..8ff600f 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -1,5 +1,10 @@ + +// project headers #include "random.hpp" + +// std libs #include <cstdlib> + namespace Random { int get_value(int min, int max) diff --git a/src/random.hpp b/src/random.hpp index 897b31f..326cc3e 100644 --- a/src/random.hpp +++ b/src/random.hpp @@ -1,3 +1,4 @@ + namespace Random { int get_value(int min, int max); } diff --git a/src/shader.cpp b/src/shader.cpp index 7542337..5491d1f 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -1,19 +1,22 @@ + +// external libs +#include "glad/glad.h" +#include <glm/glm.hpp> +#include <glm/gtc/matrix_transform.hpp> +#include <glm/gtc/type_ptr.hpp> + +// project headers #include "shader.hpp" +// std libs #include <string> #include <fstream> #include <sstream> #include <iostream> -#include "glad/glad.h" - -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> - Shader::Shader(const char* vertexPath, const char* fragmentPath) { - // 1. retrieve the vertex/fragment source code from filePath + // -- read shaders from file -- std::string vertexCode; std::string fragmentCode; std::ifstream vShaderFile; @@ -44,7 +47,7 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) const char* vShaderCode = vertexCode.c_str(); const char* fShaderCode = fragmentCode.c_str(); - // 2. compile shaders + // -- compile shaders -- unsigned int vertex, fragment; int success; char infoLog[512]; @@ -53,7 +56,6 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vShaderCode, NULL); glCompileShader(vertex); - // print compile errors if any glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); if(!success) { @@ -65,18 +67,18 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fShaderCode, NULL); glCompileShader(fragment); - // print compile errors if any glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); if(!success) { glGetShaderInfoLog(fragment, 512, NULL, infoLog); std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; }; + + // linking shaders this->ID = glCreateProgram(); glAttachShader(ID, vertex); glAttachShader(ID, fragment); glLinkProgram(ID); - // print linking errors if any glGetProgramiv(ID, GL_LINK_STATUS, &success); if(!success) { @@ -84,6 +86,7 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; } + // cleanup uneeded pre-linked shaders glDeleteShader(vertex); glDeleteShader(fragment); } @@ -93,6 +96,9 @@ void Shader::use() glUseProgram(ID); } + +// -- various setters for shader uniforms -- + void Shader::setBool(const std::string &name, bool value) const { glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value); diff --git a/src/shader.hpp b/src/shader.hpp index 2434a21..88ac64a 100644 --- a/src/shader.hpp +++ b/src/shader.hpp @@ -1,12 +1,13 @@ #pragma once -#include <string> - +// external libs #include "glad/glad.h" +#include "glm/glm.hpp" +#include "glm/gtc/matrix_transform.hpp" +#include "glm/gtc/type_ptr.hpp" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> +// std libs +#include <string> class Shader { @@ -19,7 +20,7 @@ class Shader // activate the shader void use(); - // utility functions for uniforms + // utility setter functions for uniforms void setBool(const std::string &name, bool value) const; void setInt(const std::string &name, int value) const; void set1f(const std::string &name, float value) const; diff --git a/src/shaders/default.frag b/src/shaders/default.frag index 2176a7d..6038bb1 100644 --- a/src/shaders/default.frag +++ b/src/shaders/default.frag @@ -1,4 +1,5 @@ #version 330 core + out vec4 FragColor; in vec4 v_Color; @@ -9,14 +10,13 @@ uniform sampler2D texture_1; void main() { - FragColor = texture(texture_1, v_TexCoord) * v_Color; - //if(v_TexId > 0) - //{ - // FragColor = texture(texture_1, v_TexCoord) * v_Color; - //} - //else - //{ - // FragColor = v_Color; - //} + if(v_TexId > 0) + { + FragColor = texture(texture_1, v_TexCoord) * v_Color; + } + else + { + FragColor = v_Color; + } } diff --git a/src/shaders/default.vert b/src/shaders/default.vert index 404e583..6cab025 100644 --- a/src/shaders/default.vert +++ b/src/shaders/default.vert @@ -1,11 +1,10 @@ #version 330 core + layout (location = 0) in vec3 a_Pos; layout (location = 1) in vec4 a_Color; layout (location = 2) in vec2 a_TexCoord; layout (location = 3) in float a_TexId; -//uniform float offset_x; - out vec4 v_Color; out vec2 v_TexCoord; out float v_TexId; @@ -15,7 +14,6 @@ uniform mat4 transform; void main() { gl_Position = transform * vec4(a_Pos, 1.0); - //gl_Position = vec4(a_Pos, 1.0); v_Color = a_Color; v_TexCoord = a_TexCoord; v_TexId = a_TexId; diff --git a/src/texture.cpp b/src/texture.cpp index 0697e28..6322ce7 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,10 +1,15 @@ -#include "texture.hpp" -#include <iostream> +//external libs #include "glad/glad.h" #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" +// project headers +#include "texture.hpp" + +// std libs +#include <iostream> + Texture::Texture(const char* texture_path) { glGenTextures(1, &id); @@ -15,7 +20,6 @@ Texture::Texture(const char* texture_path) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int width, height, nrChannels; - //stbi_set_flip_vertically_on_load(true); unsigned char *data = stbi_load(texture_path, &width, &height, &nrChannels, 0); if (data) { diff --git a/src/texture.hpp b/src/texture.hpp index f8ceaed..5309bd0 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -1,4 +1,6 @@ #pragma once + +// external libs #include "glad/glad.h" class Texture diff --git a/src/window.cpp b/src/window.cpp index 98a7a60..f1a6c46 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,6 +1,12 @@ + +// external libs #include "glad/glad.h" #include "GLFW/glfw3.h" +// project headers +#include "window.hpp" + +// std libs #include <iostream> namespace Window { @@ -25,7 +31,7 @@ namespace Window { ) { glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 4.6 is highest + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 4.6 is highest, but lets use 3.3 for compatability glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); @@ -53,6 +59,7 @@ namespace Window { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + return 0; } } diff --git a/src/window.hpp b/src/window.hpp index 0613da6..568fd2a 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -1,9 +1,11 @@ + +// external libs #include "glad/glad.h" #include "GLFW/glfw3.h" namespace Window { GLFWwindow* get(); - void init( + int init( unsigned int width, unsigned int height, const char* title |
