-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
144 lines (119 loc) · 3.39 KB
/
main.cpp
File metadata and controls
144 lines (119 loc) · 3.39 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
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
#include "stdafx.h"
#include "chip8.h"
#include "nuklear.h"
#include "nuklear_glfw_gl4.h"
#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
const std::string GAME = "PONG";
GLFWwindow* g_pWindow = nullptr;
const int WIDTH = 640 * 2;
const int HEIGHT = 320 * 2;
std::shared_ptr<IEmulator> s_current_emulator;
/* Error callback */
void error_callback(int code, const char* descr)
{
std::cout << "Error " << code << ": " << descr << std::endl;
}
/* initializing glfw glew and creating the window */
bool init()
{
glfwSetErrorCallback(error_callback);
/* Start initialization */
if (!glfwInit())
return false;
/* Set minimum required version */
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
/* Create the window */
g_pWindow = glfwCreateWindow(WIDTH, HEIGHT, "Chip 8", nullptr, nullptr);
if (!g_pWindow)
{
glfwTerminate();
return false;
}
glfwMakeContextCurrent(g_pWindow);
glewExperimental = 1;
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to setup GLEW " << std::endl;
return false;
}
return true;
}
static void on_file_drop_callback(GLFWwindow* window, int count, const char** paths)
{
if (count > 0)
{
if (s_current_emulator != nullptr)
s_current_emulator->load_rom(paths[0]);
}
}
static void on_set_key_callback(GLFWwindow* window, int key, int scannode, int action, int mods)
{
if (s_current_emulator)
{
s_current_emulator->set_keys(window, key, scannode, action, mods);
}
}
void nk_gui_update(nk_context* nk_ctx)
{
}
void Draw(const IDrawable& d)
{
d.on_draw(g_pWindow);
}
/* Main entry point */
int main(const int* argc, const char** argv)
{
// Setup the basic window features
init();
// Create the Chip8 emulator by default
s_current_emulator = std::make_shared<Chip8>();
s_current_emulator->init();
// Setup the games path
std::string games = "res/Games/";
s_current_emulator->load_rom(games + GAME);
// #todo : Make the callback find out what emulator is running
glfwSetKeyCallback(g_pWindow, on_set_key_callback);
glfwSetDropCallback(g_pWindow, on_file_drop_callback);
/* Init NK GUI */
nk_context* nk_ctx = nk_glfw3_init(g_pWindow, NK_GLFW3_INSTALL_CALLBACKS, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
float alpha = 0.8f;
nk_ctx->style.window.fixed_background = nk_style_item_color(nk_rgba_f(0.1f,0.12f,0.1f, alpha));
nk_ctx->style.window.background = nk_rgba_f(0.1f, 0.12f, 0.1f, alpha);
{
nk_font_atlas* atlas;
nk_glfw3_font_stash_begin(&atlas);
//nk_font *default = nk_font_atlas_add_from_file(atlas, "res/arial.ttf", 13.0f, 0);
nk_glfw3_font_stash_end();
}
//
glfwSwapInterval(0);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
float currTime = (float)glfwGetTime();
while (!glfwWindowShouldClose(g_pWindow))
{
/* deltaTime calculation */
float prevTime = currTime;
currTime = (float)glfwGetTime();
float dt = currTime - prevTime;
glfwPollEvents();
nk_glfw3_new_frame();
Logger::NewFrame();
/* Emulator updating*/
s_current_emulator->update(dt);
/* NK gui updating */
s_current_emulator->on_gui(nk_ctx);
glClear(GL_COLOR_BUFFER_BIT);
Draw(*s_current_emulator);
// Don't forget state reset
nk_glfw3_render(NK_ANTI_ALIASING_ON);
glfwSwapBuffers(g_pWindow);
}
/* Cleanup font atlas*/
s_current_emulator->deinit();
nk_glfw3_shutdown();
glfwTerminate();
}