Encapsulate GLFW windows
This commit is contained in:
parent
19e5e1b959
commit
45d564bccf
@ -51,6 +51,8 @@ target_sources(couchlib PUBLIC
|
|||||||
Util.cpp
|
Util.cpp
|
||||||
Vertex.h
|
Vertex.h
|
||||||
Vertex.cpp
|
Vertex.cpp
|
||||||
|
Window.h
|
||||||
|
Window.cpp
|
||||||
World.h
|
World.h
|
||||||
World.cpp
|
World.cpp
|
||||||
Scripting/ScriptingLanguage.h
|
Scripting/ScriptingLanguage.h
|
||||||
|
@ -13,19 +13,20 @@ Input *Input::GetInstance() {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Input::Use(Window *window){
|
|
||||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
void Input::Use(Window window){
|
||||||
glfwSetKeyCallback(window, (GLFWkeyfun)HandleKeys);
|
glfwSetInputMode(window.glfwWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
glfwSetCursorPosCallback(window, (GLFWcursorposfun)HandleMousePosition);
|
glfwSetKeyCallback(window.glfwWindow, (GLFWkeyfun)HandleKeys);
|
||||||
|
glfwSetCursorPosCallback(window.glfwWindow, (GLFWcursorposfun)HandleMousePosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Input::HandleKeys(Window *window, int keys, int code, int action, int mods) {
|
void Input::HandleKeys(GLFWwindow *_, int keys, int code, int action, int mods) {
|
||||||
for (KeyHandler keyHandler : instance->keyHandlers) {
|
for (KeyHandler keyHandler : instance->keyHandlers) {
|
||||||
keyHandler(window, keys, code, action, mods);
|
keyHandler(keys, code, action, mods);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Input::HandleMousePosition(Window *window, double xpos, double ypos) {
|
void Input::HandleMousePosition(GLFWwindow *_, double xpos, double ypos) {
|
||||||
double relx, rely;
|
double relx, rely;
|
||||||
if (instance->firstMousePositionUpdate) {
|
if (instance->firstMousePositionUpdate) {
|
||||||
relx = 0.0;
|
relx = 0.0;
|
||||||
@ -36,7 +37,7 @@ void Input::HandleMousePosition(Window *window, double xpos, double ypos) {
|
|||||||
rely = ypos - instance->lasty;
|
rely = ypos - instance->lasty;
|
||||||
}
|
}
|
||||||
for (MousePositionHandler mousePositionHandler : instance->mousePositionHandlers) {
|
for (MousePositionHandler mousePositionHandler : instance->mousePositionHandlers) {
|
||||||
mousePositionHandler(window, xpos, ypos, relx, rely);
|
mousePositionHandler(xpos, ypos, relx, rely);
|
||||||
}
|
}
|
||||||
instance->lastx = xpos;
|
instance->lastx = xpos;
|
||||||
instance->lasty = ypos;
|
instance->lasty = ypos;
|
||||||
|
11
core/Input.h
11
core/Input.h
@ -6,22 +6,23 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
#include "Window.h"
|
||||||
|
|
||||||
typedef void (*KeyHandler)(Window *window, int key, int code, int action, int mods);
|
typedef void (*KeyHandler)(int key, int code, int action, int mods);
|
||||||
typedef void (*MousePositionHandler)(Window *window, double xpos, double ypos, double xrel, double yrel);
|
typedef void (*MousePositionHandler)(double xpos, double ypos, double xrel, double yrel);
|
||||||
|
|
||||||
class Input {
|
class Input {
|
||||||
public:
|
public:
|
||||||
static Input *GetInstance();
|
static Input *GetInstance();
|
||||||
void Use(Window *window);
|
void Use(Window window);
|
||||||
std::vector<KeyHandler> keyHandlers;
|
std::vector<KeyHandler> keyHandlers;
|
||||||
std::vector<MousePositionHandler> mousePositionHandlers;
|
std::vector<MousePositionHandler> mousePositionHandlers;
|
||||||
private:
|
private:
|
||||||
bool firstMousePositionUpdate = true;
|
bool firstMousePositionUpdate = true;
|
||||||
double lastx, lasty;
|
double lastx, lasty;
|
||||||
Input();
|
Input();
|
||||||
static void HandleKeys(Window *window, int key, int code, int action, int mods);
|
static void HandleKeys(GLFWwindow *_, int key, int code, int action, int mods);
|
||||||
static void HandleMousePosition(Window *window, double xpos, double ypos);
|
static void HandleMousePosition(GLFWwindow *_, double xpos, double ypos);
|
||||||
static Input *instance;
|
static Input *instance;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ bool Lua::HasHook(const char *name) {
|
|||||||
|
|
||||||
#ifdef LUA_SCRIPTING
|
#ifdef LUA_SCRIPTING
|
||||||
|
|
||||||
void Lua::LuaKeyHandler(Window *window, int key, int code, int action, int mods) {
|
void Lua::LuaKeyHandler(int key, int code, int action, int mods) {
|
||||||
lua_getglobal(L, "onkey");
|
lua_getglobal(L, "onkey");
|
||||||
lua_pushinteger(L, key);
|
lua_pushinteger(L, key);
|
||||||
lua_pushinteger(L, code);
|
lua_pushinteger(L, code);
|
||||||
@ -92,8 +92,7 @@ void Lua::LuaKeyHandler(Window *window, int key, int code, int action, int mods)
|
|||||||
lua_call(L, 4, 0);
|
lua_call(L, 4, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lua::LuaMousePositionHandler(Window *window, double xpos, double ypos, double relx, double rely) {
|
void Lua::LuaMousePositionHandler(double xpos, double ypos, double relx, double rely) {
|
||||||
// lua_State *L = (lua_State*) glfwGetWindowUserPointer(window);
|
|
||||||
lua_getglobal(L, "onmousemotion");
|
lua_getglobal(L, "onmousemotion");
|
||||||
lua_pushnumber(L, xpos);
|
lua_pushnumber(L, xpos);
|
||||||
lua_pushnumber(L, ypos);
|
lua_pushnumber(L, ypos);
|
||||||
|
@ -25,8 +25,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
#ifdef LUA_SCRIPTING
|
#ifdef LUA_SCRIPTING
|
||||||
static lua_State *L;
|
static lua_State *L;
|
||||||
static void LuaKeyHandler(Window *window, int key, int code, int action, int mods);
|
static void LuaKeyHandler(int key, int code, int action, int mods);
|
||||||
static void LuaMousePositionHandler(Window *window, double xpos, double ypos, double xrel, double yrel);
|
static void LuaMousePositionHandler(double xpos, double ypos, double xrel, double yrel);
|
||||||
static int LuaExceptionHandler(lua_State *L1);
|
static int LuaExceptionHandler(lua_State *L1);
|
||||||
#endif // LUA_SCRIPTING
|
#endif // LUA_SCRIPTING
|
||||||
};
|
};
|
||||||
|
43
core/Window.cpp
Normal file
43
core/Window.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include "Window.h"
|
||||||
|
|
||||||
|
const int width = 800;
|
||||||
|
const int height = 600;
|
||||||
|
|
||||||
|
void Window::Init() {
|
||||||
|
int err;
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
|
||||||
|
glfwWindowHintString(GLFW_X11_CLASS_NAME, "Couch");
|
||||||
|
|
||||||
|
glfwWindow = glfwCreateWindow(width, height, "Couch", NULL, NULL);
|
||||||
|
|
||||||
|
if (!glfwWindow) {
|
||||||
|
throw "Error creating window.";
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwMakeContextCurrent(glfwWindow);
|
||||||
|
|
||||||
|
err = glewInit();
|
||||||
|
if (err != GLEW_OK) {
|
||||||
|
throw "Error initiailizing GLEW.";
|
||||||
|
}
|
||||||
|
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Window::ShouldClose() {
|
||||||
|
return glfwWindowShouldClose(glfwWindow);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::Update() {
|
||||||
|
glfwSwapBuffers(glfwWindow);
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::Close() {
|
||||||
|
glfwDestroyWindow(glfwWindow);
|
||||||
|
glfwTerminate();
|
||||||
|
}
|
53
core/Window.h
Normal file
53
core/Window.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
@file
|
||||||
|
@author Dane Johnson <dane@danejohnson.org>
|
||||||
|
|
||||||
|
@section LICENSE
|
||||||
|
|
||||||
|
Couch Copyright (C) 2021 Dane Johnson
|
||||||
|
|
||||||
|
This program comes with ABSOLUTELY NO WARRANTY; without event the
|
||||||
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
See the GNU General Public License for details at
|
||||||
|
https://www.gnu.org/licenses/gpl-3.0.html
|
||||||
|
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under the terms of the GNU General Public License as published
|
||||||
|
by the Free Software Foundation; either version 3 of the License,
|
||||||
|
or (at your option) any later version.
|
||||||
|
|
||||||
|
@section DESCRIPTION
|
||||||
|
|
||||||
|
The window controls interfacing with the operating system and creating the
|
||||||
|
interface.
|
||||||
|
*/
|
||||||
|
#ifndef WINDOW_H
|
||||||
|
#define WINDOW_H
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
class Window {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Initializes the window.
|
||||||
|
*/
|
||||||
|
void Init();
|
||||||
|
/**
|
||||||
|
@returns whether or not the user tried to close the window
|
||||||
|
*/
|
||||||
|
bool ShouldClose();
|
||||||
|
/**
|
||||||
|
Close the window.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
Swap the buffers and poll for events
|
||||||
|
*/
|
||||||
|
void Update();
|
||||||
|
void Close();
|
||||||
|
private:
|
||||||
|
friend class Input;
|
||||||
|
GLFWwindow *glfwWindow;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* WINDOW_H */
|
@ -7,6 +7,7 @@
|
|||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
#include "Window.h"
|
||||||
|
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
|
||||||
@ -29,11 +30,6 @@
|
|||||||
|
|
||||||
#include "Scripting/Lua.h"
|
#include "Scripting/Lua.h"
|
||||||
|
|
||||||
Window *window;
|
|
||||||
|
|
||||||
const int width = 800;
|
|
||||||
const int height = 600;
|
|
||||||
|
|
||||||
Node *root;
|
Node *root;
|
||||||
|
|
||||||
void render(Node *curr, Shader *shader, Matrix model) {
|
void render(Node *curr, Shader *shader, Matrix model) {
|
||||||
@ -59,31 +55,9 @@ void render(Node *curr, Shader *shader, Matrix model) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
Window window;
|
||||||
int err;
|
window.Init();
|
||||||
glfwInit();
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
|
|
||||||
glfwWindowHintString(GLFW_X11_CLASS_NAME, "Couch");
|
|
||||||
|
|
||||||
window = glfwCreateWindow(width, height, "Couch", NULL, NULL);
|
|
||||||
|
|
||||||
if (!window) {
|
|
||||||
std::cerr << "Error creating window" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwMakeContextCurrent(window);
|
|
||||||
|
|
||||||
err = glewInit();
|
|
||||||
if (err != GLEW_OK) {
|
|
||||||
std::cerr << "Error initializing glew" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
glViewport(0, 0, width, height);
|
|
||||||
|
|
||||||
root = Node::GetRoot();
|
root = Node::GetRoot();
|
||||||
|
|
||||||
Input *input = Input::GetInstance();
|
Input *input = Input::GetInstance();
|
||||||
@ -109,7 +83,7 @@ int main() {
|
|||||||
double lastTime = glfwGetTime();
|
double lastTime = glfwGetTime();
|
||||||
double delta = 0.0;
|
double delta = 0.0;
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!window.ShouldClose()) {
|
||||||
// Physics update()
|
// Physics update()
|
||||||
world->Step(delta);
|
world->Step(delta);
|
||||||
|
|
||||||
@ -164,11 +138,11 @@ int main() {
|
|||||||
// // Render the screen
|
// // Render the screen
|
||||||
screenShader->Use();
|
screenShader->Use();
|
||||||
screenShader->UpdateTex(screen.tex);
|
screenShader->UpdateTex(screen.tex);
|
||||||
glViewport(0, 0, width, height);
|
// Ummm?
|
||||||
|
// glViewport(0, 0, width, height);
|
||||||
screen.Draw();
|
screen.Draw();
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
window.Update();
|
||||||
glfwPollEvents();
|
|
||||||
|
|
||||||
double curTime = glfwGetTime();
|
double curTime = glfwGetTime();
|
||||||
delta = curTime - lastTime;
|
delta = curTime - lastTime;
|
||||||
@ -181,7 +155,6 @@ int main() {
|
|||||||
lua->Close();
|
lua->Close();
|
||||||
delete lua;
|
delete lua;
|
||||||
|
|
||||||
glfwDestroyWindow(window);
|
window.Close();
|
||||||
glfwTerminate();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
typedef GLFWwindow Window;
|
|
||||||
typedef glm::vec3 Vector3;
|
typedef glm::vec3 Vector3;
|
||||||
typedef glm::mat4 Matrix;
|
typedef glm::mat4 Matrix;
|
||||||
typedef std::string Name;
|
typedef std::string Name;
|
||||||
|
Loading…
Reference in New Issue
Block a user