diff options
| author | realtradam <[email protected]> | 2022-11-25 02:55:55 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2022-11-25 02:55:55 -0500 |
| commit | 4e7226ad1b081982e5cbed349edca1c5b0e1b779 (patch) | |
| tree | 6d497896f979e6bbcb0841d33d3a2815333cfef9 | |
| parent | 6e1d0c6a3f3699e44000f411cd93261c460f75a9 (diff) | |
| download | Ogle-4e7226ad1b081982e5cbed349edca1c5b0e1b779.tar.gz Ogle-4e7226ad1b081982e5cbed349edca1c5b0e1b779.zip | |
fixed Makefile, implemented bunnymark
| -rw-r--r-- | Makefile | 21 | ||||
| -rw-r--r-- | assets/wabbit_alpha.png | bin | 496 -> 1211 bytes | |||
| -rw-r--r-- | src/batch.cpp | 22 | ||||
| -rw-r--r-- | src/input.cpp | 29 | ||||
| -rw-r--r-- | src/input.hpp | 3 | ||||
| -rw-r--r-- | src/main.cpp | 97 | ||||
| -rw-r--r-- | src/random.cpp | 9 | ||||
| -rw-r--r-- | src/random.hpp | 3 | ||||
| -rw-r--r-- | src/shader.cpp | 14 | ||||
| -rw-r--r-- | src/shader.hpp | 5 | ||||
| -rw-r--r-- | src/shaders/default.frag | 17 | ||||
| -rw-r--r-- | src/shaders/default.vert | 5 | ||||
| -rw-r--r-- | src/texture.cpp | 2 | ||||
| -rw-r--r-- | src/window.cpp | 5 |
14 files changed, 189 insertions, 43 deletions
@@ -3,37 +3,40 @@ EXE := game BUILD_DIR := build INC_FLAGS := -Isrc -Iglfw/build/include -Iglad/include -Istb -Iglm LINK_FLAGS := -lGL -lX11 -lpthread -lXrandr -lXi -ldl -OBJ_NAMES := glad.o input.o shader.o batch.o texture.o window.o +OBJ_NAMES := glad.o input.o shader.o batch.o texture.o window.o random.o OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(OBJ_NAMES)) -default: src/main.cpp $(OBJ_NAMES) +$(BUILD_DIR)/$(EXE): src/main.cpp $(OBJ_FILES) @mkdir -p $(BUILD_DIR) g++ -o $(BUILD_DIR)/$(EXE) src/main.cpp $(OBJ_FILES) $(INC_FLAGS) glfw/build/lib/libglfw3.a $(LINK_FLAGS) -glad.o: glad/src/glad.c +$(BUILD_DIR)/glad.o: glad/src/glad.c @mkdir -p $(BUILD_DIR) g++ -o $(BUILD_DIR)/glad.o -c glad/src/glad.c $(INC_FLAGS) $(LINK_FLAGS) -input.o: src/input.cpp src/input.hpp +$(BUILD_DIR)/input.o: src/input.cpp src/input.hpp @mkdir -p $(BUILD_DIR) g++ -o $(BUILD_DIR)/input.o -c src/input.cpp $(INC_FLAGS) $(LINK_FLAGS) -shader.o: src/shader.cpp src/shader.hpp +$(BUILD_DIR)/shader.o: src/shader.cpp src/shader.hpp @mkdir -p $(BUILD_DIR) g++ -o $(BUILD_DIR)/shader.o -c src/shader.cpp $(INC_FLAGS) $(LINK_FLAGS) -batch.o: src/batch.cpp src/batch.hpp +$(BUILD_DIR)/batch.o: src/batch.cpp src/batch.hpp @mkdir -p $(BUILD_DIR) g++ -o $(BUILD_DIR)/batch.o -c src/batch.cpp $(INC_FLAGS) $(LINK_FLAGS) -texture.o: src/texture.cpp src/texture.hpp +$(BUILD_DIR)/texture.o: src/texture.cpp src/texture.hpp @mkdir -p $(BUILD_DIR) g++ -o $(BUILD_DIR)/texture.o -c src/texture.cpp $(INC_FLAGS) $(LINK_FLAGS) -window.o: src/window.cpp src/window.hpp +$(BUILD_DIR)/window.o: src/window.cpp src/window.hpp @mkdir -p $(BUILD_DIR) g++ -o $(BUILD_DIR)/window.o -c src/window.cpp $(INC_FLAGS) $(LINK_FLAGS) +$(BUILD_DIR)/random.o: src/random.cpp src/random.hpp + @mkdir -p $(BUILD_DIR) + g++ -o $(BUILD_DIR)/random.o -c src/random.cpp $(INC_FLAGS) $(LINK_FLAGS) .PHONY: clean clean: rm -r $(BUILD_DIR) .PHONY: run -run: default +run: $(BUILD_DIR)/$(EXE) $(BUILD_DIR)/$(EXE) diff --git a/assets/wabbit_alpha.png b/assets/wabbit_alpha.png Binary files differindex db4081f..0f25b90 100644 --- a/assets/wabbit_alpha.png +++ b/assets/wabbit_alpha.png diff --git a/src/batch.cpp b/src/batch.cpp index d380e53..b36e6d6 100644 --- a/src/batch.cpp +++ b/src/batch.cpp @@ -74,10 +74,10 @@ int Batch::drawTexture( float h_width = width / 2.0; return this->primativeDrawQuad( texture.id, - glm::vec3(x+h_height,y+h_width,0.0f), // top right - glm::vec3(x+h_height, y-h_width, 0.0f), // bottom right - glm::vec3(x-h_height, y-h_width, 0.0f), // bottom left - glm::vec3(x-h_height, y+h_height, 0.0f), // top left + glm::vec3(x+h_height,y+h_width, -1.0f), // top right + glm::vec3(x+h_height, y-h_width, -1.0f), // bottom right + glm::vec3(x-h_height, y-h_width, -1.0f), // bottom left + glm::vec3(x-h_height, y+h_height, -1.0f), // top left color ); } @@ -95,10 +95,10 @@ int Batch::drawRectangle( float h_width = width / 2.0; return this->primativeDrawQuad( 0, - glm::vec3(x+h_height,y+h_width,0.0f), // top right - glm::vec3(x+h_height, y-h_width, 0.0f), // bottom right - glm::vec3(x-h_height, y-h_width, 0.0f), // bottom left - glm::vec3(x-h_height, y+h_height, 0.0f), // top left + glm::vec3(x+h_height,y+h_width, -1.0f), // top right + glm::vec3(x+h_height, y-h_width, -1.0f), // bottom right + glm::vec3(x-h_height, y-h_width, -1.0f), // bottom left + glm::vec3(x-h_height, y+h_height, -1.0f), // top left color ); } @@ -170,7 +170,7 @@ void Batch::flush_batch() GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), - GL_STATIC_DRAW + GL_DYNAMIC_DRAW ); // we already binded this in the VAO during initialization @@ -180,7 +180,7 @@ void Batch::flush_batch() GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), - GL_STATIC_DRAW + GL_DYNAMIC_DRAW ); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, last_texture); @@ -188,7 +188,7 @@ void Batch::flush_batch() glBindVertexArray(0); // unbind the VAO - last_texture = -1; + last_texture = 0; size = 0; vertices.clear(); indices.clear(); diff --git a/src/input.cpp b/src/input.cpp index bad2ce8..5622afd 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -3,9 +3,38 @@ namespace Input { + namespace { + int mouse_x; + int mouse_y; + bool mouse_click; + } void process(GLFWwindow *window) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); + else + { + double _mouse_x, _mouse_y; + glfwGetCursorPos(window, &_mouse_x, &_mouse_y); + mouse_x = (int)_mouse_x; + mouse_y = (int)_mouse_y; + if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)) + mouse_click = true; + else + mouse_click = false; + } + } + + int get_mouse_x() + { + return mouse_x; + } + int get_mouse_y() + { + return mouse_y; + } + bool get_mouse_click() + { + return mouse_click; } } diff --git a/src/input.hpp b/src/input.hpp index 955ed95..20bba64 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -3,4 +3,7 @@ namespace Input { void process(GLFWwindow *window); + int get_mouse_x(); + int get_mouse_y(); + bool get_mouse_click(); } diff --git a/src/main.cpp b/src/main.cpp index 38022bf..5cca382 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,21 +5,47 @@ #include "shader.hpp" #include "batch.hpp" #include "window.hpp" +#include "random.hpp" #include <iostream> #include <cmath> +#include <chrono> + +#include <glm/glm.hpp> +#include <glm/gtc/matrix_transform.hpp> +#include <glm/gtc/type_ptr.hpp> + +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() { - Window::init(800, 600, "Ogle"); + Window::init(SCREEN_WIDTH, SCREEN_HEIGHT, "Ogle"); Shader shader = Shader("src/shaders/default.vert", "src/shaders/default.frag"); - Batch batch = Batch(); - Texture texture = Texture("assets/awesomeface.png"); + Batch batch = Batch(1024 * 8); + Texture texture = Texture("assets/wabbit_alpha.png"); + + 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(); + + std::vector<Bunny> bunnies = std::vector<Bunny>{}; + const int MAX_BUNNIES = 50000; // game loop while(!glfwWindowShouldClose(Window::get())) { + start = std::chrono::high_resolution_clock::now(); Input::process(Window::get()); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); @@ -27,21 +53,68 @@ int main() { shader.use(); - batch.drawRectangle(-0.5,-0.5,1.0,1.0,glm::vec4(0.3,0.3,1.0,1.0)); - batch.drawRectangle(0.5,0.5,1.0,1.0,glm::vec4(1.0,0.3,0.3,1.0)); - batch.drawRectangle(-0.5,0.5,1.0,1.0,glm::vec4(1.0,0.3,1.0,1.0)); - batch.drawRectangle(0.5,-0.5,1.0,1.0,glm::vec4(1.0,1.0,0.3,1.0)); - batch.drawTexture(texture,-0.5,-0.5,1.0,1.0,glm::vec4(1.0,1.0,1.0,0.3)); - batch.drawTexture(texture,0.5,0.5,1.0,1.0,glm::vec4(1.0,1.0,1.0,0.3)); - batch.drawTexture(texture,-0.5,0.5,1.0,1.0,glm::vec4(1.0,1.0,1.0,0.3)); - batch.drawTexture(texture,0.5,-0.5,1.0,1.0,glm::vec4(1.0,1.0,1.0,0.3)); - batch.drawTexture(texture,0.0,0.0,1.0,1.0,glm::vec4(1.0,1.0,1.0,0.5)); + if(Input::get_mouse_click()) + { + Bunny new_bunny; + for (int i =0; i < 100; i++) + { + if (bunnies.size() < MAX_BUNNIES) + { + new_bunny = (Bunny){ + glm::vec2(Input::get_mouse_x(), Input::get_mouse_y()), + glm::vec2((float)Random::get_value(-250, 250)/60.0f, (float)Random::get_value(-250, 250)/60.0f), + glm::vec4( + Random::get_value(50, 240) / 255.0, + Random::get_value(50, 240) / 255.0, + Random::get_value(100, 240) / 255.0, + 1.0f + ) + }; + 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 + for (int i = 0; i < bunnies.size(); i++) + { + bunnies.at(i).position.x += bunnies.at(i).speed.x; + bunnies.at(i).position.y += bunnies.at(i).speed.y; + + if (((bunnies.at(i).position.x) > SCREEN_WIDTH) || ((bunnies.at(i).position.x) < 0)) + bunnies.at(i).speed.x *= -1; + if (((bunnies.at(i).position.y) > SCREEN_HEIGHT) || ((bunnies.at(i).position.y) < 0)) + bunnies.at(i).speed.y *= -1; + } + + for (int i = 0; i < bunnies.size(); i++) + { + batch.drawTexture( + texture, + bunnies.at(i).position.x, + bunnies.at(i).position.y, + 32, + 32, + 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 glfwSwapBuffers(Window::get()); glfwPollEvents(); + stop = std::chrono::high_resolution_clock::now(); + + auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start); + + 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; } glfwTerminate(); diff --git a/src/random.cpp b/src/random.cpp new file mode 100644 index 0000000..ec49e36 --- /dev/null +++ b/src/random.cpp @@ -0,0 +1,9 @@ +#include "random.hpp" +#include <cstdlib> +namespace Random +{ + int get_value(int min, int max) + { + return (std::rand()%(abs(max - min) + 1) + min); + } +} diff --git a/src/random.hpp b/src/random.hpp new file mode 100644 index 0000000..897b31f --- /dev/null +++ b/src/random.hpp @@ -0,0 +1,3 @@ +namespace Random { + int get_value(int min, int max); +} diff --git a/src/shader.cpp b/src/shader.cpp index 08da2a8..7542337 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -7,6 +7,10 @@ #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 @@ -108,3 +112,13 @@ void Shader::set4f(const std::string &name, float value0, float value1, float va { glUniform4f(glGetUniformLocation(ID, name.c_str()), value0, value1, value2, value3); } + +void Shader::setMatrix4fv(const std::string &name, glm::mat4 matrix) +{ + glUniformMatrix4fv( + glGetUniformLocation(ID, name.c_str()), + 1, + GL_FALSE, + glm::value_ptr(matrix) + ); +} diff --git a/src/shader.hpp b/src/shader.hpp index 7e4f25d..2434a21 100644 --- a/src/shader.hpp +++ b/src/shader.hpp @@ -4,6 +4,10 @@ #include "glad/glad.h" +#include <glm/glm.hpp> +#include <glm/gtc/matrix_transform.hpp> +#include <glm/gtc/type_ptr.hpp> + class Shader { public: @@ -26,4 +30,5 @@ class Shader float value2, float value3 ) const; + void setMatrix4fv(const std::string &name, glm::mat4 matrix); }; diff --git a/src/shaders/default.frag b/src/shaders/default.frag index 2ad6732..2176a7d 100644 --- a/src/shaders/default.frag +++ b/src/shaders/default.frag @@ -9,13 +9,14 @@ uniform sampler2D texture_1; void main() { - if(v_TexId > 0) - { - FragColor = texture(texture_1, v_TexCoord) * v_Color; - } - else - { - FragColor = v_Color; - } + FragColor = texture(texture_1, v_TexCoord) * 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 38e5343..404e583 100644 --- a/src/shaders/default.vert +++ b/src/shaders/default.vert @@ -10,9 +10,12 @@ out vec4 v_Color; out vec2 v_TexCoord; out float v_TexId; +uniform mat4 transform; + void main() { - gl_Position = vec4(a_Pos, 1.0); + 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 e9a49ce..0697e28 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -15,7 +15,7 @@ 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); + //stbi_set_flip_vertically_on_load(true); unsigned char *data = stbi_load(texture_path, &width, &height, &nrChannels, 0); if (data) { diff --git a/src/window.cpp b/src/window.cpp index adc51c0..98a7a60 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -28,6 +28,9 @@ namespace Window { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 4.6 is highest glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + glfwSwapInterval(1); window = glfwCreateWindow(width, height, title, NULL, NULL); if (window == NULL) @@ -44,7 +47,7 @@ namespace Window { return -1; } - glViewport(0, 0, 800, 600); + glViewport(0, 0, width, height); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); |
