diff options
-rw-r--r-- | examples/resources/breast_black.png | bin | 0 -> 225 bytes | |||
-rw-r--r-- | examples/resources/dngn_blood_fountain.png (renamed from examples/simplegame/dngn_blood_fountain.png) | bin | 955 -> 955 bytes | |||
-rw-r--r-- | examples/resources/textureshader.frag (renamed from examples/simplegame/textureshader.frag) | 0 | ||||
-rw-r--r-- | examples/resources/textureshader.vert (renamed from examples/simplegame/textureshader.vert) | 0 | ||||
-rw-r--r-- | examples/simplegame/main.cpp | 32 | ||||
-rw-r--r-- | yage/core/input.h | 1 | ||||
-rw-r--r-- | yage/core/texture.h | 4 | ||||
-rw-r--r-- | yage/core/window.cpp | 31 | ||||
-rw-r--r-- | yage/core/window.h | 17 |
9 files changed, 51 insertions, 34 deletions
diff --git a/examples/resources/breast_black.png b/examples/resources/breast_black.png Binary files differnew file mode 100644 index 00000000..180b292f --- /dev/null +++ b/examples/resources/breast_black.png diff --git a/examples/simplegame/dngn_blood_fountain.png b/examples/resources/dngn_blood_fountain.png Binary files differindex 7214fd47..7214fd47 100644 --- a/examples/simplegame/dngn_blood_fountain.png +++ b/examples/resources/dngn_blood_fountain.png diff --git a/examples/simplegame/textureshader.frag b/examples/resources/textureshader.frag index ef728b04..ef728b04 100644 --- a/examples/simplegame/textureshader.frag +++ b/examples/resources/textureshader.frag diff --git a/examples/simplegame/textureshader.vert b/examples/resources/textureshader.vert index 3277d8b0..3277d8b0 100644 --- a/examples/simplegame/textureshader.vert +++ b/examples/resources/textureshader.vert diff --git a/examples/simplegame/main.cpp b/examples/simplegame/main.cpp index a631e45b..06200661 100644 --- a/examples/simplegame/main.cpp +++ b/examples/simplegame/main.cpp @@ -17,17 +17,19 @@ using namespace yage; int main() { Window window; - GlslProgram program; + GlslProgram textureProgram; window.create("Simple Game", 800, 640); SpriteBatch sp; - program.compileShadersFromFile("examples/simplegame/textureshader.vert", - "examples/simplegame/textureshader.frag"); - program.linkShaders(); + textureProgram.compileShadersFromFile("examples/resources/textureshader.vert", + "examples/resources/textureshader.frag"); + textureProgram.linkShaders(); Texture fountain = ResourceManager::getTexture( - "examples/simplegame/dngn_blood_fountain.png"); + "examples/resources/dngn_blood_fountain.png"); + Texture breast_plate = + ResourceManager::getTexture("examples/resources/breast_black.png"); cout << "texture: " << fountain.width << ", " << fountain.height << '\n'; @@ -35,23 +37,31 @@ int main() while (!window.shouldClose()) { window.clearBuffer(); + Texture texture = fountain; - program.use(); - camera.update(program); + window.pollEvents(); + if (window.keyPressed(yage::key::SPACE)) { + cout << "Pressed A" << '\n'; + } + if (window.keyPressed(yage::key::E)) { + texture = breast_plate; + } + + textureProgram.use(); + camera.update(textureProgram); glActiveTexture(GL_TEXTURE0); - GLint texture_location = program.getUniformLocation("texture_sampler"); + GLint texture_location = textureProgram.getUniformLocation("texture_sampler"); glUniform1i(texture_location, 0); - sp.draw({0.f, 0.f, 64.f, 64.f}, {0, 0, 1, 1}, fountain.id, + sp.draw({0.f, 0.f, 64.f, 64.f}, {0, 0, 1, 1}, texture.id, Colour(255, 0, 255, 255), 0); sp.render(); glBindTexture(GL_TEXTURE_2D, 0); - program.unuse(); + textureProgram.unuse(); window.swapBuffer(); - window.pollEvents(); } } diff --git a/yage/core/input.h b/yage/core/input.h index 125f4296..866793d8 100644 --- a/yage/core/input.h +++ b/yage/core/input.h @@ -9,6 +9,7 @@ #ifndef YAGE_CORE_INPUT_H #define YAGE_CORE_INPUT_H +#define GLFW_INCLUDE_NONE #include <GLFW/glfw3.h> namespace yage diff --git a/yage/core/texture.h b/yage/core/texture.h index 2eae8bf4..aec7b906 100644 --- a/yage/core/texture.h +++ b/yage/core/texture.h @@ -6,8 +6,8 @@ * ---------------------------------------------------------------------------- */ -#ifndef GL_TEXTURE_H -#define GL_TEXTURE_H +#ifndef YAGE_CORE_TEXTURE_H +#define YAGE_CORE_TEXTURE_H #include <glad/glad.h> diff --git a/yage/core/window.cpp b/yage/core/window.cpp index 70e8d8e3..f2f22e3b 100644 --- a/yage/core/window.cpp +++ b/yage/core/window.cpp @@ -7,23 +7,21 @@ */ #include "window.h" +#include "input.h" + +#include <glad/glad.h> +#include <GLFW/glfw3.h> #include <stdexcept> +using std::runtime_error; + namespace yage { void key_callback(GLFWwindow *window, int key, int scanCode, int action, int mods) { - if (key == GLFW_KEY_E && (action == GLFW_REPEAT || action == GLFW_PRESS)) { - glClearColor(0.5f, 0.f, 0.f, 1.f); - } else if (key == GLFW_KEY_Q && - (action == GLFW_REPEAT || action == GLFW_PRESS)) { - glClearColor(0.f, 0.f, 0.5f, 1.f); - } else { - glClearColor(0.f, .5f, 0.f, 1.f); - } } Window::Window() = default; @@ -37,7 +35,7 @@ Window::~Window() void Window::create(std::string window_name, int width, int height) { if (glfwInit() == GLFW_FALSE) { - throw std::runtime_error("GLFW Initialisation failed"); + throw runtime_error("GLFW Initialisation failed"); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); @@ -47,7 +45,7 @@ void Window::create(std::string window_name, int width, int height) window_ = glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr); if (window_ == nullptr) { - throw std::runtime_error("GLFW Window creation failed"); + throw runtime_error("GLFW Window creation failed"); } // initialize the gl context @@ -101,10 +99,19 @@ bool Window::shouldClose() void Window::pollEvents() const { glfwPollEvents(); +} - if (glfwGetKey(window_, GLFW_KEY_W) == GLFW_PRESS) { - glClearColor(0.f, 0.5f, 0.5f, 1.f); +bool Window::keyPressed(key k) +{ + if (window_ == nullptr) { + throw runtime_error("Window is not initialized"); + } + + if (glfwGetKey(window_, static_cast<int>(k)) == GLFW_PRESS) { + return true; } + + return false; } } // namespace yage diff --git a/yage/core/window.h b/yage/core/window.h index 5c16c172..93ed6c07 100644 --- a/yage/core/window.h +++ b/yage/core/window.h @@ -6,21 +6,18 @@ * ---------------------------------------------------------------------------- */ -/** @file - */ - -#ifndef WINDOW_H -#define WINDOW_H - -#include <glad/glad.h> - -#include <GLFW/glfw3.h> +#ifndef YAGE_CORE_WINDOW_H +#define YAGE_CORE_WINDOW_H #include <string> +class GLFWwindow; + namespace yage { +enum class key; + // window flags that can change it's appearance enum WindowFlags : unsigned { SHOWN = 0x1, @@ -58,6 +55,8 @@ public: void show(); bool shouldClose(); void pollEvents() const; + + bool keyPressed(key k); }; } // namespace yage |