-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOGLWindow.cpp
96 lines (78 loc) · 2.33 KB
/
OGLWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "OGLWindow.h"
#include "Common.h"
OGLWindow::OGLWindow(int InWidth, int InHeight, const std::string& InTitle)
: Width(InWidth), Height(InHeight), Title(InTitle)
{
DeltaTime = 0.f;
LastTime = 0.f;
DIE_ON_ERROR(glfwInit(), "Failed to initialize GLFW");
// hints to init window
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
RawWindow = glfwCreateWindow(Width, Height, Title.c_str(), NULL, NULL);
DIE_ON_NULL(RawWindow, "Error: Opening Window");
// redirect all commands to window
glfwMakeContextCurrent(RawWindow);
//Disable VSYNC with glfw api
//glfwSwapInterval(0);
// load GLAD
DIE_ON_ERROR(gladLoadGL(), "Error: GLAD Wrapper");
}
OGLWindow::~OGLWindow()
{
glfwDestroyWindow(RawWindow);
glfwTerminate();
}
void OGLWindow::SetTitle(const std::string& InTitle)
{
glfwSetWindowTitle(RawWindow, InTitle.c_str());
}
float OGLWindow::GetDeltaTime() const
{
return DeltaTime;
}
bool OGLWindow::IsOpened() const
{
return !glfwWindowShouldClose(RawWindow);
}
void OGLWindow::Update()
{
float CurrentTime = glfwGetTime();
DeltaTime = CurrentTime - LastTime;
LastTime = CurrentTime;
// swap buffers with all draw operations
glfwSwapBuffers(RawWindow);
// poll events
glfwPollEvents();
}
void OGLWindow::PrintInfo() const
{
printf("GL VERSION: %s\n", glGetString(GL_VERSION));
printf("GL RENDERER: %s\n", glGetString(GL_RENDERER));
printf("GL VENDOR: %s\n", glGetString(GL_VENDOR));
printf("GLSL VERSION: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
}
glm::vec2 OGLWindow::MousePosition() const
{
//https://www.glfw.org/docs/3.1/group__input.html#ga01d37b6c40133676b9cea60ca1d7c0cc
//(0,0) e' l'angolo in alto a sinistra della finistra
//(w,h) e' l'angolo in basso a destra della finestra
//Fuori della finestra i valori x e y diventano negativi
double XPos, YPos;
glfwGetCursorPos(RawWindow, &XPos, &YPos);
return glm::vec2((int)XPos, (int)YPos);
}
glm::vec2 OGLWindow::Size() const
{
return glm::vec2(Width, Height);
}
bool OGLWindow::IsKeyPressed(Key InKey) const
{
int state = glfwGetKey(RawWindow, (int)InKey);
return state == GLFW_PRESS;
}