MinequackCPP/main.cpp

152 lines
4 KiB
C++
Raw Normal View History

2022-04-07 13:50:51 +00:00
#include <GLES3/gl3.h>
#include <GL/glew.h>
#include "./classes/renderer.cpp"
#include "defs.h"
#include <GL/gl.h>
#include <iostream>
#include <string>
#include <GLFW/glfw3.h>
#include <GL/glut.h>
double windowWidth = VARS::WINDOW_WIDTH;
double windowHeight = VARS::WINDOW_HEIGHT;
class pressedKey {
public:
int action;
int key;
int scancode;
int mods;
bool x = false;
};
pressedKey lastPressedKey;
bool textTestEnabled = true;
void handleResize(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
windowWidth = width;
windowHeight = height;
return;
}
void cursorTrack(GLFWwindow *window, double posx, double posy) {
std::cout << "Cursor at: " << posx << " " << posy << std::endl;
return;
}
void keyTrack(GLFWwindow *window, int key, int scancode, int action, int mods) {
pressedKey pk;
pk.action = action;
pk.key = key;
pk.scancode = scancode;
pk.mods = mods;
lastPressedKey = pk;
lastPressedKey.x = true;
return;
}
void charTrack(GLFWwindow *window, unsigned int key) {
// std::cout << "Key: " << key << std::endl;
return;
}
int main() {
glfwInit();
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(VARS::WINDOW_WIDTH, VARS::WINDOW_HEIGHT, VARS::WINDOW_NAME, NULL, NULL);
glfwMakeContextCurrent(window);
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return EXIT_FAILURE;
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
glfwSetWindowSizeCallback(window, handleResize);
glfwSetCursorPosCallback(window, cursorTrack);
glfwSetKeyCallback(window, keyTrack);
glfwSetCharCallback(window, charTrack);
RENDERER renderer; // construct renderer
// Shader handling
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
unsigned int vertexShader = loadShader("./shaders/vertex.glsl");
unsigned int fragmentShader = loadShader("./shaders/fragment.glsl");
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
std::string text;
while(!glfwWindowShouldClose(window)) {
renderer.render(window);
glfwSwapBuffers(window);
glfwPollEvents();
// ignore this trash code, this will get a rewrite with much better code, this was just a little test
if(lastPressedKey.x && textTestEnabled) {
switch(lastPressedKey.key) {
case GLFW_KEY_ESCAPE:
if(lastPressedKey.action == GLFW_PRESS) {
lastPressedKey.x = false;
glfwSetWindowShouldClose(window, GL_TRUE);
exit(EXIT_SUCCESS);
}
break;
default:
if(lastPressedKey.action == GLFW_PRESS && lastPressedKey.key == GLFW_KEY_ENTER) {
std::cout << text << std::endl;
lastPressedKey.x = false;
text = "";
break;
} else if (lastPressedKey.action == GLFW_PRESS && lastPressedKey.key == GLFW_KEY_BACKSPACE && !text.empty()) {
text.pop_back();
lastPressedKey.x = false;
break;
}
if(!(lastPressedKey.key >= 32 && lastPressedKey.key <= 126)) break;
if(lastPressedKey.action == GLFW_PRESS) {
if(lastPressedKey.key == 32) {
text.append(" ");
lastPressedKey.x = false;
break;
}
text.append(glfwGetKeyName(lastPressedKey.key, lastPressedKey.scancode));
lastPressedKey.x = false;
}
break;
}
}
}
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS;
}