summaryrefslogtreecommitdiffstats
path: root/graphics/Display.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-05-10 15:44:51 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-05-10 15:44:51 +0200
commit469a84c10ba60fba4a513c1b1a0eea600cd7b22f (patch)
tree4de3514353341f68fd63d3c12fa0a162008d3209 /graphics/Display.cpp
parent2017-04-23 (diff)
downloadAltCraft-469a84c10ba60fba4a513c1b1a0eea600cd7b22f.tar
AltCraft-469a84c10ba60fba4a513c1b1a0eea600cd7b22f.tar.gz
AltCraft-469a84c10ba60fba4a513c1b1a0eea600cd7b22f.tar.bz2
AltCraft-469a84c10ba60fba4a513c1b1a0eea600cd7b22f.tar.lz
AltCraft-469a84c10ba60fba4a513c1b1a0eea600cd7b22f.tar.xz
AltCraft-469a84c10ba60fba4a513c1b1a0eea600cd7b22f.tar.zst
AltCraft-469a84c10ba60fba4a513c1b1a0eea600cd7b22f.zip
Diffstat (limited to '')
-rw-r--r--graphics/Display.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/graphics/Display.cpp b/graphics/Display.cpp
new file mode 100644
index 0000000..01dd903
--- /dev/null
+++ b/graphics/Display.cpp
@@ -0,0 +1,103 @@
+#include <iostream>
+#include "Display.hpp"
+#include "Shader.hpp"
+
+Display *Display::instance = nullptr;
+
+Display::Display(int w, int h, std::string title, World *worldPtr) {
+ if (instance != nullptr)
+ throw 516;
+ instance = this;
+ world = worldPtr;
+ //GLFW
+ glfwInit();
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+ glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
+ window = glfwCreateWindow(w, h, title.c_str(), nullptr, nullptr);
+ if (window == nullptr) {
+ std::cerr << "Can't create GLFW window" << std::endl;
+ glfwTerminate();
+ throw 517;
+ }
+ glfwMakeContextCurrent(window);
+ glfwSetKeyCallback(window, &Display::callback_key);
+ //GLEW
+ glewExperimental = GL_TRUE;
+ GLenum glewStatus = glewInit();
+ if (glewStatus != GLEW_OK) {
+ std::cerr << "Can't initialize GLEW: " << glewGetErrorString(glewStatus) << std::endl;
+ throw 518;
+ }
+ int width, height;
+ glfwGetFramebufferSize(window, &width, &height);
+ glViewport(0, 0, width, height);
+}
+
+Display::~Display() {
+ instance = nullptr;
+ glfwTerminate();
+}
+
+bool Display::IsClosed() {
+ return false;
+}
+
+void Display::SetPlayerPos(float x, float y) {
+
+}
+
+void Display::MainLoop() {
+ Shader vertexShader("./graphics/simpleVS.vs");
+ Shader fragmentShader("./graphics/simpleFS.fs", false);
+ ShaderProgram program;
+ program.Attach(vertexShader);
+ program.Attach(fragmentShader);
+ program.Link();
+
+ GLfloat vertices[] = {
+ -0.5f, -0.5f, 0.0f,
+ 0.5f, -0.5f, 0.0f,
+ 0.0f, 0.5f, 0.0f
+ };
+ GLuint VBO;
+ glGenBuffers(1, &VBO);
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
+ glBufferData(VBO, sizeof(vertices), vertices, GL_STATIC_DRAW);
+
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *) 0);
+ glEnableVertexAttribArray(0);
+
+ GLuint VAO;
+ glGenVertexArrays(1, &VAO);
+
+ glBindVertexArray(VAO);
+ // 2. Копируем наш массив вершин в буфер для OpenGL
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+ // 3. Устанавливаем указатели на вершинные атрибуты
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *) 0);
+ glEnableVertexAttribArray(0);
+ //4. Отвязываем VAO
+ glBindVertexArray(0);
+
+ while (!glfwWindowShouldClose(window)) {
+ glfwPollEvents();
+
+ glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ glUseProgram((GLuint) program);
+ glBindVertexArray(VAO);
+ glDrawArrays(GL_TRIANGLES, 0, 3);
+ glBindVertexArray(0);
+
+ glfwSwapBuffers(window);
+ }
+}
+
+void Display::callback_key(GLFWwindow *window, int key, int scancode, int action, int mode) {
+ if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
+ glfwSetWindowShouldClose(instance->window, GL_TRUE);
+}