/
Asteroids.cpp
106 lines (97 loc) · 3.39 KB
/
Asteroids.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
// Asteroids.cpp : Defines the entry point for the application.
//
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
// Other includes
#include "AsteroidsGame/AsteroidsGame.h"
#include "Engine/IO/IO.h"
#include "Engine/Window/Window.h"
#include "Engine/Timing/Timing.h"
#include "Engine/Texture/Texture.h"
#include "Engine/HUD/HUDElement/Text/Text.h"
#include "Engine/HUD/HUDElement/Text/Menu/Menu.h"
#include "Engine/Audio/Audio.h"
#include "Engine/Options/Options.h"
#include <string>
void Game(GLFWwindow* window);
void GLAPIENTRY
MessageCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam)
{
if (type == 0x8251) { return; }
fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
type, severity, message);
}
// During init, enable debug output
#ifdef _WIN32
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow) {
#ifdef DEBUG
AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#endif // DEBUG
#else
int main() {
#endif // _WIN32
glfwInit();
Engine::Audio::LoadDeviceAndContext();
GLFWwindow* window = Engine::CreateGameWindow(Engine::Options::screenSize.first, Engine::Options::screenSize.second, "Asteroids", Engine::key_callback, Engine::mouse_callback, Engine::character_callback);
Engine::Options::LoadConfig();
Engine::Menu menu(
{
{ "New Game", [window]() { Game(window); } },
{ "Options", Engine::Options::GetOptionsMenu() },
{ "Quit", [window]() { glfwSetWindowShouldClose(window, true); } }
}, false);
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, 0);
while (!glfwWindowShouldClose(window)) {
menu.Show(window);
}
glfwTerminate();
Engine::Audio::UnloadDeviceAndContext();
return 0;
}
void Game(GLFWwindow * window) {
Engine::InputState inputState = {};
void* oldInputState = glfwGetWindowUserPointer(window);
glfwSetWindowUserPointer(window, (void*)(&inputState));
Asteroids::AsteroidsGame game(Engine::Options::screenSize.first, Engine::Options::screenSize.second);
Engine::GameTimer timer(0.01666667f);
Engine::Shader textShader("./text");
Engine::Shader graphicsShader("./default");
Engine::Texture font("./font.dds");
timer.Start();
int i = 0;
while (true) {
timer.Tick();
while (timer.ConsumeTick() && !glfwWindowShouldClose(window)) {
glfwPollEvents();
game.HandleEvents(inputState);
game.Update(inputState);
}
if (glfwWindowShouldClose(window)) {
glfwSetWindowUserPointer(window, oldInputState);
glfwSetWindowShouldClose(window, false);
break;
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
game.DrawFrame();
Engine::Text::DrawString(font, textShader, Engine::Text::Anchor::TOP_LEFT, -1.0f, 1.0f, 24.0f, "Score: " + std::to_string(game.GetScore()) + "\nStage: " + std::to_string(game.GetStage()));
glfwSwapBuffers(window);
}
}