SimpleEngine/src/game.cpp

94 lines
2.2 KiB
C++
Raw Normal View History

//
// Created by SeanOMik on 7/2/2020.
// Github: https://github.com/SeanOMik
//
#include "game.h"
2021-11-20 05:48:47 +00:00
#include <iostream>
2021-11-20 05:48:47 +00:00
#include <gl/glew.h>
#include <GLFW/glfw3.h>
2021-11-20 05:48:47 +00:00
#include <gl/gl.h>
2021-11-20 05:48:47 +00:00
simpleengine::Game::Game(int w, int h, const std::string& window_name, const bool& resizeable) {
// Create a window
glfwInit();
2021-11-20 05:48:47 +00:00
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_RESIZABLE, resizeable);
2021-11-20 05:48:47 +00:00
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
window = std::shared_ptr<GLFWwindow>(glfwCreateWindow(w, h, window_name.c_str(), NULL, NULL));
// If we're not resizeable, we need to set the viewport size.
if (!resizeable) {
int fbWidth;
int fbHeight;
glfwGetFramebufferSize(window.get(), &fbWidth, &fbHeight);
glViewport(0, 0, fbWidth, fbHeight);
} else {
glfwSetFramebufferSizeCallback(window.get(), simpleengine::Game::framebuffer_resize_callback);
}
2021-11-20 05:48:47 +00:00
glfwMakeContextCurrent(window.get());
2021-11-20 05:48:47 +00:00
glewExperimental = GL_TRUE;
2021-11-20 05:48:47 +00:00
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize glew!" << std::endl;
glfwTerminate();
}
}
2021-11-20 05:48:47 +00:00
simpleengine::Game::~Game() {
}
2021-11-20 05:48:47 +00:00
void simpleengine::Game::update() {
}
2021-11-20 05:48:47 +00:00
void simpleengine::Game::render_window() {
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
render_items();
}
void simpleengine::Game::render_items() {
}
int simpleengine::Game::run() {
while (!glfwWindowShouldClose(window.get())) {
// Update input
glfwPollEvents();
update();
2021-11-20 05:48:47 +00:00
render_window();
// End draw
glfwSwapBuffers(window.get());
glFlush();
}
return 0;
}
2021-11-20 05:48:47 +00:00
std::shared_ptr<GLFWwindow> simpleengine::Game::get_window() {
return window;
}
2021-11-20 05:48:47 +00:00
void simpleengine::Game::exit() {
glfwSetWindowShouldClose(window.get(), true);
glfwTerminate();
}
2021-11-20 05:48:47 +00:00
void simpleengine::Game::framebuffer_resize_callback(GLFWwindow*, int fbW, int fbH) {
glViewport(0, 0, fbW, fbH);
}