-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (74 loc) · 1.74 KB
/
main.cpp
File metadata and controls
95 lines (74 loc) · 1.74 KB
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/gl.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "game.hpp"
#include "input_handler.hpp"
const int SCREEN_WIDTH = 960;
const int SCREEN_HEIGHT = 720;
using namespace pathogen;
static void resizeCallback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
static GLFWwindow* initGLFWAndWindow()
{
if (!glfwInit())
{
std::cerr << "GLFW failed to initialize\n";
return nullptr;
}
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Pathogen", nullptr, nullptr);
if (!window)
{
std::cerr << "Failed to create GLFW window\n";
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
glfwSetWindowSizeCallback(window, resizeCallback);
return window;
}
static bool initOpenGL()
{
if (!gladLoadGL(glfwGetProcAddress))
{
std::cerr << "Failed to initialize GLAD\n";
return false;
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return true;
}
static void initGame(Game& game)
{
game.init(SCREEN_WIDTH, SCREEN_HEIGHT);
}
int main()
{
GLFWwindow* window = initGLFWAndWindow();
if (!window) return -1;
if (!initOpenGL()) return -1;
InputHandler::init(window);
Game game;
initGame(game);
float deltaTime = 0.0f;
float lastFrame = 0.0f;
while (!glfwWindowShouldClose(window))
{
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glClearColor(0.2f, 0.15f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
game.setScreenDim(width, height);
game.tick(deltaTime);
game.draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}