mirror of https://github.com/bjornbytes/lovr.git
Move all window code into graphics module;
This commit is contained in:
parent
58f00a0b4c
commit
9eb4d7b393
|
@ -1,23 +1,13 @@
|
|||
#include "event/event.h"
|
||||
#include "glfw.h"
|
||||
#include "lib/glfw.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static EventState state;
|
||||
|
||||
static void onClose(GLFWwindow* _window) {
|
||||
if (_window == window) {
|
||||
EventType type = EVENT_QUIT;
|
||||
EventData data = { .quit = { 0 } };
|
||||
Event event = { .type = type, .data = data };
|
||||
lovrEventPush(event);
|
||||
}
|
||||
}
|
||||
|
||||
void lovrEventInit() {
|
||||
vec_init(&state.pumps);
|
||||
vec_init(&state.events);
|
||||
lovrEventAddPump(glfwPollEvents);
|
||||
glfwSetWindowCloseCallback(window, onClose);
|
||||
atexit(lovrEventDestroy);
|
||||
}
|
||||
|
||||
|
|
41
src/glfw.c
41
src/glfw.c
|
@ -1,41 +0,0 @@
|
|||
#include "glfw.h"
|
||||
#include "util.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void onError(int code, const char* description) {
|
||||
error(description);
|
||||
}
|
||||
|
||||
void initGlfw() {
|
||||
glfwSetErrorCallback(onError);
|
||||
|
||||
if (!glfwInit()) {
|
||||
error("Error initializing glfw");
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||
|
||||
window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
|
||||
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
error("Could not create window");
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
|
||||
|
||||
glfwSetTime(0);
|
||||
glfwSwapInterval(0);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_LINE_SMOOTH);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
|
||||
}
|
|
@ -1,19 +1,53 @@
|
|||
#include "graphics/graphics.h"
|
||||
#include "loaders/texture.h"
|
||||
#include "loaders/font.h"
|
||||
#include "event/event.h"
|
||||
#include "math/mat4.h"
|
||||
#include "math/vec3.h"
|
||||
#include "util.h"
|
||||
#include "glfw.h"
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
static GraphicsState state;
|
||||
|
||||
static void onCloseWindow(GLFWwindow* window) {
|
||||
if (window == state.window) {
|
||||
EventType type = EVENT_QUIT;
|
||||
EventData data = { .quit = { 0 } };
|
||||
Event event = { .type = type, .data = data };
|
||||
lovrEventPush(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Base
|
||||
|
||||
void lovrGraphicsInit() {
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||
|
||||
state.window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
|
||||
|
||||
if (!state.window) {
|
||||
glfwTerminate();
|
||||
error("Could not create window");
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(state.window);
|
||||
glfwSetWindowCloseCallback(state.window, onCloseWindow);
|
||||
|
||||
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
|
||||
|
||||
glfwSetTime(0);
|
||||
glfwSwapInterval(0);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_LINE_SMOOTH);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
|
||||
|
||||
// Allocations
|
||||
state.activeFont = NULL;
|
||||
|
@ -106,7 +140,7 @@ void lovrGraphicsClear(int color, int depth) {
|
|||
}
|
||||
|
||||
void lovrGraphicsPresent() {
|
||||
glfwSwapBuffers(window);
|
||||
glfwSwapBuffers(state.window);
|
||||
}
|
||||
|
||||
void lovrGraphicsPrepare() {
|
||||
|
@ -232,7 +266,7 @@ void lovrGraphicsGetScissor(int* x, int* y, int* width, int* height) {
|
|||
|
||||
void lovrGraphicsSetScissor(int x, int y, int width, int height) {
|
||||
int windowWidth, windowHeight;
|
||||
glfwGetFramebufferSize(window, &windowWidth, &windowHeight);
|
||||
glfwGetFramebufferSize(state.window, &windowWidth, &windowHeight);
|
||||
state.scissor.x = x;
|
||||
state.scissor.x = y;
|
||||
state.scissor.width = width;
|
||||
|
@ -309,7 +343,7 @@ void lovrGraphicsSetProjection(mat4 projection) {
|
|||
|
||||
void lovrGraphicsSetPerspective(float near, float far, float fov) {
|
||||
int width, height;
|
||||
glfwGetWindowSize(window, &width, &height);
|
||||
glfwGetWindowSize(state.window, &width, &height);
|
||||
mat4_perspective(state.canvases[state.canvas]->projection, near, far, fov, (float) width / height);
|
||||
}
|
||||
|
||||
|
@ -382,13 +416,13 @@ void lovrGraphicsSetWireframe(int wireframe) {
|
|||
|
||||
int lovrGraphicsGetWidth() {
|
||||
int width;
|
||||
glfwGetFramebufferSize(window, &width, NULL);
|
||||
glfwGetFramebufferSize(state.window, &width, NULL);
|
||||
return width;
|
||||
}
|
||||
|
||||
int lovrGraphicsGetHeight() {
|
||||
int height;
|
||||
glfwGetFramebufferSize(window, NULL, &height);
|
||||
glfwGetFramebufferSize(state.window, NULL, &height);
|
||||
return height;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include "graphics/skybox.h"
|
||||
#include "graphics/texture.h"
|
||||
#include "math/math.h"
|
||||
#include "lib/glfw.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
|
@ -67,6 +68,7 @@ typedef struct CanvasState {
|
|||
} CanvasState;
|
||||
|
||||
typedef struct {
|
||||
GLFWwindow* window;
|
||||
Shader* activeShader;
|
||||
Shader* defaultShader;
|
||||
Shader* skyboxShader;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
#include "glfw.h"
|
||||
#include "util.h"
|
||||
#include "graphics/shader.h"
|
||||
#include "graphics/texture.h"
|
||||
#include "math/math.h"
|
||||
#include "lib/glfw.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include "graphics/mesh.h"
|
||||
#include "graphics/texture.h"
|
||||
#include "math/math.h"
|
||||
#include "glfw.h"
|
||||
#include "lib/glfw.h"
|
||||
#include "util.h"
|
||||
|
||||
#pragma once
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "glfw.h"
|
||||
#include "math/math.h"
|
||||
#include "lib/map/map.h"
|
||||
#include "lib/glfw.h"
|
||||
#include "util.h"
|
||||
|
||||
#pragma once
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "filesystem/blob.h"
|
||||
#include "glfw.h"
|
||||
#include "lib/glfw.h"
|
||||
#include "util.h"
|
||||
|
||||
#pragma once
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "loaders/texture.h"
|
||||
#include "glfw.h"
|
||||
#include "lib/glfw.h"
|
||||
#include "util.h"
|
||||
|
||||
#pragma once
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "headset/headset.h"
|
||||
#include "graphics/texture.h"
|
||||
#include "glfw.h"
|
||||
#include "lib/glfw.h"
|
||||
#include <stdbool.h>
|
||||
#ifndef _WIN32
|
||||
#define __stdcall
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#ifdef _WIN32
|
||||
#define APIENTRY __stdcall
|
||||
#endif
|
||||
|
||||
#include "lib/glad/glad.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#pragma once
|
||||
|
||||
GLFWwindow* window;
|
||||
|
||||
void initGlfw();
|
16
src/lovr.c
16
src/lovr.c
|
@ -1,11 +1,15 @@
|
|||
#include "lovr.h"
|
||||
#include "util.h"
|
||||
#include "api/lovr.h"
|
||||
#include "data/boot.lua.h"
|
||||
#include "data/logo.png.h"
|
||||
#include "glfw.h"
|
||||
#include "util.h"
|
||||
#include "lib/glfw.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static void onGlfwError(int code, const char* description) {
|
||||
error(description);
|
||||
}
|
||||
|
||||
static int getStackTrace(lua_State* L) {
|
||||
const char* message = luaL_checkstring(L, -1);
|
||||
lua_getglobal(L, "debug");
|
||||
|
@ -41,7 +45,13 @@ void lovrInit(lua_State* L, int argc, char** argv) {
|
|||
exit(0);
|
||||
}
|
||||
|
||||
initGlfw();
|
||||
glfwSetErrorCallback(onGlfwError);
|
||||
|
||||
if (!glfwInit()) {
|
||||
error("Error initializing glfw");
|
||||
}
|
||||
|
||||
glfwSetTime(0);
|
||||
|
||||
// arg global
|
||||
lua_newtable(L);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "timer/timer.h"
|
||||
#include "glfw.h"
|
||||
#include "lib/glfw.h"
|
||||
#include "util.h"
|
||||
|
||||
static TimerState timerState;
|
||||
|
|
Loading…
Reference in New Issue