2020-07-03 03:48:58 +00:00
|
|
|
#include "game.h"
|
2021-11-21 06:23:53 +00:00
|
|
|
#include "event/event.h"
|
2020-07-03 03:48:58 +00:00
|
|
|
|
2021-11-20 05:48:47 +00:00
|
|
|
#include <iostream>
|
2020-07-03 03:48:58 +00:00
|
|
|
|
2021-12-02 21:58:15 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <GL/gl.h>
|
2021-12-13 03:17:59 +00:00
|
|
|
#else
|
2021-11-20 05:48:47 +00:00
|
|
|
#include <gl/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <gl/gl.h>
|
2021-12-02 21:58:15 +00:00
|
|
|
#endif
|
2021-03-02 04:08:10 +00:00
|
|
|
|
2021-11-21 06:23:53 +00:00
|
|
|
simpleengine::Game::Game(int w, int h, const std::string& window_name, const int& gl_profile, const int& major_version,
|
|
|
|
const int& minor_version, const bool& resizeable, const int& forward_compat) : window_resizeable(resizeable) {
|
|
|
|
initialize(gl_profile, major_version, minor_version, window_resizeable, forward_compat);
|
2021-11-20 05:48:47 +00:00
|
|
|
|
2021-11-21 06:23:53 +00:00
|
|
|
// Create a window
|
2021-12-02 21:58:15 +00:00
|
|
|
window = glfwCreateWindow(w, h, window_name.c_str(), NULL, NULL);
|
2021-11-20 05:48:47 +00:00
|
|
|
|
|
|
|
// If we're not resizeable, we need to set the viewport size.
|
|
|
|
if (!resizeable) {
|
|
|
|
int fbWidth;
|
|
|
|
int fbHeight;
|
2021-12-02 21:58:15 +00:00
|
|
|
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
|
2021-11-20 05:48:47 +00:00
|
|
|
glViewport(0, 0, fbWidth, fbHeight);
|
|
|
|
} else {
|
2021-12-02 21:58:15 +00:00
|
|
|
glfwSetFramebufferSizeCallback(window, simpleengine::Game::framebuffer_resize_callback);
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 21:58:15 +00:00
|
|
|
glfwMakeContextCurrent(window);
|
2020-07-03 03:48:58 +00:00
|
|
|
|
2021-11-20 05:48:47 +00:00
|
|
|
glewExperimental = GL_TRUE;
|
2020-07-03 03:48:58 +00:00
|
|
|
|
2021-11-20 05:48:47 +00:00
|
|
|
if (glewInit() != GLEW_OK) {
|
|
|
|
std::cout << "Failed to initialize glew!" << std::endl;
|
|
|
|
glfwTerminate();
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
2021-12-07 03:44:47 +00:00
|
|
|
|
|
|
|
enable_default_gl_options();
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::enable_default_gl_options() const {
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
2021-12-09 21:32:15 +00:00
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glCullFace(GL_BACK);
|
2021-12-07 03:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::enable_gl_option(GLenum option) const {
|
|
|
|
glEnable(option);
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 06:23:53 +00:00
|
|
|
void simpleengine::Game::initialize(const int& gl_profile, const int& major_version, const int& minor_version,
|
|
|
|
const bool& resizeable, const int& forward_compat) {
|
|
|
|
glfwInit();
|
|
|
|
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, gl_profile);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major_version);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor_version);
|
|
|
|
glfwWindowHint(GLFW_RESIZABLE, window_resizeable);
|
|
|
|
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, forward_compat);
|
|
|
|
}
|
|
|
|
|
2021-11-20 05:48:47 +00:00
|
|
|
simpleengine::Game::~Game() {
|
2021-12-02 21:58:15 +00:00
|
|
|
glfwDestroyWindow(window);
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 06:23:53 +00:00
|
|
|
void simpleengine::Game::add_event(std::shared_ptr<simpleengine::Event> event) {
|
|
|
|
events.push_back(event);
|
|
|
|
}
|
|
|
|
|
2021-11-20 05:48:47 +00:00
|
|
|
void simpleengine::Game::update() {
|
2021-11-21 06:23:53 +00:00
|
|
|
handle_input();
|
|
|
|
|
|
|
|
// Update items
|
|
|
|
for (const std::shared_ptr<Event>& event : events) {
|
|
|
|
event->update(0.f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::handle_input() {
|
|
|
|
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 05:48:47 +00:00
|
|
|
void simpleengine::Game::render_window() {
|
2021-11-21 06:23:53 +00:00
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
2021-11-20 05:48:47 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
render_items();
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::render_items() {
|
2021-11-21 06:23:53 +00:00
|
|
|
for (const std::shared_ptr<Event>& event : events) {
|
|
|
|
event->render(window);
|
|
|
|
}
|
2021-11-20 05:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int simpleengine::Game::run() {
|
2021-12-02 21:58:15 +00:00
|
|
|
while (!glfwWindowShouldClose(window)) {
|
2021-11-20 05:48:47 +00:00
|
|
|
// Update input
|
|
|
|
glfwPollEvents();
|
|
|
|
|
|
|
|
update();
|
2020-07-03 03:48:58 +00:00
|
|
|
|
2021-11-20 05:48:47 +00:00
|
|
|
render_window();
|
|
|
|
|
|
|
|
// End draw
|
2021-12-02 21:58:15 +00:00
|
|
|
glfwSwapBuffers(window);
|
2021-11-20 05:48:47 +00:00
|
|
|
glFlush();
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-02 21:58:15 +00:00
|
|
|
GLFWwindow* simpleengine::Game::get_window() {
|
2020-07-03 03:48:58 +00:00
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
2021-11-20 05:48:47 +00:00
|
|
|
void simpleengine::Game::exit() {
|
2021-12-02 21:58:15 +00:00
|
|
|
glfwSetWindowShouldClose(window, true);
|
2021-11-20 05:48:47 +00:00
|
|
|
glfwTerminate();
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
2021-11-20 05:48:47 +00:00
|
|
|
|
|
|
|
void simpleengine::Game::framebuffer_resize_callback(GLFWwindow*, int fbW, int fbH) {
|
|
|
|
glViewport(0, 0, fbW, fbH);
|
|
|
|
}
|