-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
145 lines (110 loc) · 2.84 KB
/
Game.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "Game.h"
#include "Shader.h"
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <GL/glew.h>
#include <GL/glfw3.h>
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#define GLFW_DLL
Game::Game(void)
{
}
Game::~Game(void)
{
}
void Game::start()
{
initGL();
float time1 = glfwGetTime();
camera = new Camera(this, window);
renderer = new GLRenderer(this, window, camera);
manager = new ChunkManager(renderer);
inputManager = new InputManager(window);
glfwSetMouseButtonCallback(window, &mouseCallBackWrapper);
glfwSetKeyCallback(window, &keyCallBackWrapper);
float time2 = glfwGetTime();
Chunk* chunk = new Chunk(manager, glm::vec3(0,0,0));
chunk->load();
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 16; j++)
{
for(int k = 0; k < 16; k++)
{
chunk->activateBlock(i,j,k,true);
}
}
}
glfwShowWindow(this->window);
this->run();
}
void Game::run()
{
printf("\nRunning");
glClearColor(0.0f, 0.0f, 0.8f, 0.0f);
double lastTime, curTime;
float delta;
lastTime = glfwGetTime();
do
{
curTime = glfwGetTime();
delta = float(curTime - lastTime);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
inputManager->update();
camera->move(delta);
glfwSetCursorPos(window, Game::screenWidth/2, Game::screenHeight/2);
//renderer->renderACube();
manager->update(delta, camera);
glfwSwapBuffers(this->window);
glfwPollEvents();
lastTime = curTime;
} while(glfwGetKey(this->window, GLFW_KEY_ESCAPE) != GLFW_PRESS && !glfwWindowShouldClose(this->window));
this->cleanup();
}
void Game::initGL()
{
if(!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW");
}
this->window = glfwCreateWindow(screenWidth, screenHeight, "ToggyCraft", NULL, NULL);
glfwHideWindow(this->window);
if(!this->window)
{
glfwTerminate();
fprintf(stderr, "Failed to create window");
}
glfwSetWindowUserPointer(window, this);
glfwMakeContextCurrent(this->window);
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
GLenum err = glewInit();
if(err != GLEW_OK)
{
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LESS);
}
void Game::cleanup()
{
delete camera;
delete renderer;
delete manager;
glfwDestroyWindow(this->window);
}
void Game::mouseCallBackWrapper(GLFWwindow* window, int button, int pressed, int modifier)
{
windowGetGame(window)->inputManager->mouseCallBack(window, button, pressed, modifier);
}
void Game::keyCallBackWrapper(GLFWwindow* window, int key, int scancode, int action, int modifier)
{
windowGetGame(window)->inputManager->keyCallBack(window, key, scancode, action, modifier);
}