diff options
Diffstat (limited to 'engine-ocean/Engine')
-rw-r--r-- | engine-ocean/Engine/core.cpp | 46 | ||||
-rw-r--r-- | engine-ocean/Engine/core.h | 32 | ||||
-rw-r--r-- | engine-ocean/Engine/window.cpp | 129 | ||||
-rw-r--r-- | engine-ocean/Engine/window.h | 26 |
4 files changed, 233 insertions, 0 deletions
diff --git a/engine-ocean/Engine/core.cpp b/engine-ocean/Engine/core.cpp new file mode 100644 index 0000000..a0f8557 --- /dev/null +++ b/engine-ocean/Engine/core.cpp @@ -0,0 +1,46 @@ +#include "core.h" + +Core::Core() +{ +} + +Core::~Core(){ + +} + +void Core::update(double deltaTime){ + m_app.update(deltaTime); + +} + +void Core::draw(){ + m_app.draw(); +} + +void Core::keyEvent(int key, int action){ + m_app.keyEvent(key, action); +} + +void Core::mousePosEvent(double xpos, double ypos){ + + m_app.mousePosEvent(xpos,ypos); + +} + +void Core::mouseButtonEvent(int button, int action){ + m_app.mouseButtonEvent(button, action); + +} + +void Core::scrollEvent(double distance){ + m_app.scrollEvent(distance); + +} + +void Core::framebufferResizeEvent(int width, int height){ + m_app.framebufferResizeEvent(width, height); +} + +void Core::windowResizeEvent(int width, int height){ + m_app.windowResizeEvent(width, height); +} diff --git a/engine-ocean/Engine/core.h b/engine-ocean/Engine/core.h new file mode 100644 index 0000000..e6f0672 --- /dev/null +++ b/engine-ocean/Engine/core.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Game/Application.h" +#include "Graphics/global.h" + +#include <GLFW/glfw3.h> + +class Core +{ +public: + Core(); + ~Core(); + void update(double deltaTime); + void draw(); + void keyEvent(int key, int action); + void mousePosEvent(double xpos, double ypos); + void mouseButtonEvent(int button, int action); + void scrollEvent(double distance); + void windowResizeEvent(int width, int height); + void framebufferResizeEvent(int width, int height); + +private: + std::shared_ptr<Camera> camera; + std::shared_ptr<Shape> shape; + std::shared_ptr<ModelTransform> modelTransform; + + bool w_down, a_down, s_down, d_down; + bool right_mouse_down = false; + glm::vec2 prev_mouse_pos; + float m_velocity = .05f; + Application m_app; +}; diff --git a/engine-ocean/Engine/window.cpp b/engine-ocean/Engine/window.cpp new file mode 100644 index 0000000..f1d7637 --- /dev/null +++ b/engine-ocean/Engine/window.cpp @@ -0,0 +1,129 @@ +#include "window.h" + +Window::Window(){ + std::cout<<"Start"<<std::endl; + start(); + std::cout<<"Loop"<<std::endl; + loop(); + std::cout<<"End"<<std::endl; + end(); +} + +Window::~Window(){ + +} + +void Window::start(){ + srand(time(NULL)); + // Testing glfw + if(!glfwInit()){ + std::cout<<"GLFW init failed :("<<std::endl; + exit(EXIT_FAILURE); + } + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); + + m_GLFWwindow = glfwCreateWindow(640, 480, "Ninja Mouse", NULL, NULL); + if (!m_GLFWwindow) + { + std::cout<<"Window Creation Failed :("<<std::endl; + glfwTerminate(); + exit(EXIT_FAILURE); + } + + glfwMakeContextCurrent(m_GLFWwindow); + + Global::graphics.initializeGLEW(); // IMPORTANT: Can't make ANY OpenGL calls before this occurs! + Global::graphics.initialize(); + int width, height; + glfwGetWindowSize(m_GLFWwindow, &width, &height); + Global::graphics.setWindowSize(glm::ivec2(width, height)); + glfwGetFramebufferSize(m_GLFWwindow, &width, &height); + Global::graphics.setFramebufferSize(glm::ivec2(width, height)); + + glfwSwapInterval(1); + + // Set up Core now that windowing and opengl are set up + m_core = new Core(); + + // Stores variable in glfw to reference our m_core object. This allows it to be accessed + // even in static methods such as keyCallback and windowSizeCallback + glfwSetWindowUserPointer(m_GLFWwindow, m_core); + + glfwSetKeyCallback(m_GLFWwindow, keyCallback); + + glfwSetMouseButtonCallback(m_GLFWwindow, mouseButtonCallback); + + // glfwSetInputMode(m_GLFWwindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + // if (glfwRawMouseMotionSupported()){ + // glfwSetInputMode(m_GLFWwindow, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE); + // } + + glfwSetCursorPosCallback(m_GLFWwindow, cursorPosCallback); + + glfwSetScrollCallback(m_GLFWwindow, scrollCallback); + + glfwSetWindowSizeCallback(m_GLFWwindow, windowSizeCallback); + + glfwSetFramebufferSizeCallback(m_GLFWwindow, framebufferSizeCallback); + + glfwSetInputMode(m_GLFWwindow, GLFW_STICKY_KEYS, GLFW_TRUE); +} + +void Window::loop(){ + double previous = glfwGetTime(); + while (!glfwWindowShouldClose(m_GLFWwindow)) + { + + double current = glfwGetTime(); + double difference = current-previous; + + glfwPollEvents(); + m_core->update(difference); + m_core->draw(); + glfwSwapBuffers(m_GLFWwindow); + + } +} + +void Window::end(){ + glfwDestroyWindow(m_GLFWwindow); + glfwTerminate(); + exit(EXIT_SUCCESS); +} + +void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods){ + if(key == GLFW_KEY_ESCAPE){ + glfwSetWindowShouldClose(window, true); + } + Core* ptr = (Core*)glfwGetWindowUserPointer(window); + ptr->keyEvent(key, action); +} + +void Window::cursorPosCallback(GLFWwindow* window, double xpos, double ypos){ + Core* ptr = (Core*)glfwGetWindowUserPointer(window); + ptr->mousePosEvent(xpos, ypos); +} + +void Window::mouseButtonCallback(GLFWwindow* window, int button, int action, int mods){ + Core* ptr = (Core*)glfwGetWindowUserPointer(window); + ptr->mouseButtonEvent(button, action); +} + +void Window::scrollCallback(GLFWwindow* window, double xoffset, double yoffset){ + Core* ptr = (Core*)glfwGetWindowUserPointer(window); + ptr->scrollEvent(yoffset); +} + +void Window::windowSizeCallback(GLFWwindow* window, int width, int height){ + Core* ptr = (Core*)glfwGetWindowUserPointer(window); + ptr->windowResizeEvent(width, height); +} + +void Window::framebufferSizeCallback(GLFWwindow* window, int width, int height){ + Core* ptr = (Core*)glfwGetWindowUserPointer(window); + ptr->framebufferResizeEvent(width, height); +} diff --git a/engine-ocean/Engine/window.h b/engine-ocean/Engine/window.h new file mode 100644 index 0000000..743cf0d --- /dev/null +++ b/engine-ocean/Engine/window.h @@ -0,0 +1,26 @@ +#pragma once + +#include "core.h" + +class Window +{ +public: + Window(); + ~Window(); + +private: + void start(); + void loop(); + void end(); + static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); + static void cursorPosCallback(GLFWwindow* window, double xpos, double ypos); + static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods); + static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset); + static void windowSizeCallback(GLFWwindow* window, int width, int height); + static void framebufferSizeCallback(GLFWwindow* window, int width, int height); + + GLFWwindow* m_GLFWwindow; + Core* m_core; + //Application* m_app; + const double m_secPerUpdate = 1.0/60; +}; |