summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--assets/wabbit_alpha.pngbin0 -> 496 bytes
-rw-r--r--src/input.cpp9
-rw-r--r--src/input.hpp4
-rw-r--r--src/main.cpp123
-rw-r--r--src/window.cpp55
-rw-r--r--src/window.hpp11
7 files changed, 93 insertions, 114 deletions
diff --git a/Makefile b/Makefile
index 6ea02e6..2100233 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ 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
+OBJ_NAMES := glad.o input.o shader.o batch.o texture.o window.o
OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(OBJ_NAMES))
@@ -26,6 +26,9 @@ batch.o: src/batch.cpp src/batch.hpp
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
+ @mkdir -p $(BUILD_DIR)
+ g++ -o $(BUILD_DIR)/window.o -c src/window.cpp $(INC_FLAGS) $(LINK_FLAGS)
.PHONY: clean
clean:
diff --git a/assets/wabbit_alpha.png b/assets/wabbit_alpha.png
new file mode 100644
index 0000000..db4081f
--- /dev/null
+++ b/assets/wabbit_alpha.png
Binary files differ
diff --git a/src/input.cpp b/src/input.cpp
index 827581c..bad2ce8 100644
--- a/src/input.cpp
+++ b/src/input.cpp
@@ -1,8 +1,11 @@
#include "input.hpp"
#include "GLFW/glfw3.h"
-void processInput(GLFWwindow *window)
+namespace Input
{
- if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, true);
+ void process(GLFWwindow *window)
+ {
+ if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
+ glfwSetWindowShouldClose(window, true);
+ }
}
diff --git a/src/input.hpp b/src/input.hpp
index 39d4e9e..955ed95 100644
--- a/src/input.hpp
+++ b/src/input.hpp
@@ -1,4 +1,6 @@
#pragma once
#include "GLFW/glfw3.h"
-void processInput(GLFWwindow *window);
+namespace Input {
+ void process(GLFWwindow *window);
+}
diff --git a/src/main.cpp b/src/main.cpp
index c634ab5..38022bf 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,138 +4,43 @@
#include "input.hpp"
#include "shader.hpp"
#include "batch.hpp"
+#include "window.hpp"
#include <iostream>
#include <cmath>
-void framebuffer_size_callback(GLFWwindow* window, int width, int height)
-{
- glViewport(0, 0, width, height);
-}
int main() {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 4.6 is highest
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-
- GLFWwindow* window = glfwCreateWindow(800, 600, "Ogle", NULL, NULL);
- if (window == NULL)
- {
- std::cout << "Failed to create GLFW window" << std::endl;
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
-
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
- {
- std::cout << "Failed to init GLAD" << std::endl;
- return -1;
- }
-
- glViewport(0, 0, 800, 600);
-
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
+ Window::init(800, 600, "Ogle");
Shader shader = Shader("src/shaders/default.vert", "src/shaders/default.frag");
-
-
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // wireframe mode
-
Batch batch = Batch();
Texture texture = Texture("assets/awesomeface.png");
- shader.use();
-
- float pi = 2.0f * acos(0.0f);
-
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// game loop
- while(!glfwWindowShouldClose(window))
+ while(!glfwWindowShouldClose(Window::get()))
{
- processInput(window);
+ Input::process(Window::get());
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
-
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)
- );
+ 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));
batch.flush_batch(); // does the drawing
- glfwSwapBuffers(window);
+ glfwSwapBuffers(Window::get());
glfwPollEvents();
}
diff --git a/src/window.cpp b/src/window.cpp
new file mode 100644
index 0000000..adc51c0
--- /dev/null
+++ b/src/window.cpp
@@ -0,0 +1,55 @@
+#include "glad/glad.h"
+#include "GLFW/glfw3.h"
+
+#include <iostream>
+
+namespace Window {
+ namespace { // private
+ GLFWwindow* window;
+
+ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
+ {
+ glViewport(0, 0, width, height);
+ }
+ }
+
+ GLFWwindow* get()
+ {
+ return window;
+ }
+
+ int init(
+ unsigned int width,
+ unsigned int height,
+ const char* title
+ )
+ {
+ glfwInit();
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 4.6 is highest
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+
+ window = glfwCreateWindow(width, height, title, NULL, NULL);
+ if (window == NULL)
+ {
+ std::cout << "Failed to create GLFW window" << std::endl;
+ glfwTerminate();
+ return -1;
+ }
+ glfwMakeContextCurrent(window);
+
+ if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
+ {
+ std::cout << "Failed to init GLAD" << std::endl;
+ return -1;
+ }
+
+ glViewport(0, 0, 800, 600);
+
+ glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
+
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ return 0;
+ }
+}
diff --git a/src/window.hpp b/src/window.hpp
new file mode 100644
index 0000000..0613da6
--- /dev/null
+++ b/src/window.hpp
@@ -0,0 +1,11 @@
+#include "glad/glad.h"
+#include "GLFW/glfw3.h"
+
+namespace Window {
+ GLFWwindow* get();
+ void init(
+ unsigned int width,
+ unsigned int height,
+ const char* title
+ );
+}