game/src/main.cpp

224 lines
6.9 KiB
C++

// Include Config header generated by GNU autotools
#include <GL/glew.h>
// Make sure Glew is loaded first
#include <GL/gl.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_opengl.h>
// Not used yet
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
// File reader
#include <fstream>
#include <iostream>
#include <string>
// Shader
#include "ShaderLoader.h"
// Camera
#include "PlayerCamera.h"
// Include error class
#include "Error.h"
Error error("main");
int main(int argc, char **argv) {
// Initialise SDL2
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
error.crash("Unable to initialise SDL: ", SDL_GetError());
return 1;
}
// Make OpenGL use double buffering (Render game first then shove to output)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// TODO: Understand what a depth buffer is
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
// TODO: Discover if this is necessary for linux and what values they need be
// SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
// SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
// SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
// SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
// Create Window
SDL_Window *window =
SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (window == NULL) {
error.crash("Unable to initialise SDL Window: ", SDL_GetError());
return 1;
}
// Create glContext
SDL_GLContext glContext = SDL_GL_CreateContext(window);
// TODO: Test that glContext was created successfully
// Tell us the number of vertex attributes allowed in bug reports
//
int nrAttributes;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
error.log("Maximum num of vertex attributes supported: " +
std::to_string(nrAttributes));
// Initialise Glew
if (glewInit() != GLEW_OK) {
error.crash("Unable to initialise GLEW (OpenGL Extention Wrangler)");
}
// Create event handling struct
SDL_Event input;
ShaderLoader shader("./data/shaders"
"/vertex.glsl",
"./data/shaders"
"/fragment.glsl");
float vertices[] = {
// positions // texture Co-ords
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // First triangle
1, 2, 3 // Second triangle
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and
// then configure vertex attributes(s).
glBindVertexArray(VAO);
// Bind VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Bind EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// texture attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// You can unbind the VAO afterwards so other VAO calls won't accidentally
// modify this VAO, but this rarely happens. Modifying other VAOs requires a
// call to glBindVertexArray anyways so we generally don't unbind VAOs (nor
// VBOs) when it's not directly necessary. glBindVertexArray(0);
// Determine how we handle textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
// Determine border colour for incorrectly sized textures
float borderColor[] = {0.5f, 0.5f, 0.5f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
// Texture aliasing
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load texture image
SDL_Surface *image = IMG_Load("./data"
"/container.jpg");
if (image == nullptr) {
error.crash("SDL2_image was unable to load a texture", IMG_GetError());
}
// create and bind texture object
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// Handle different SDL Surface data types
int mode = GL_RGB;
if (image->format->BytesPerPixel == 4) {
mode = GL_RGBA;
}
// Generate texture and mipmap from image
glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, mode,
GL_UNSIGNED_BYTE, image->pixels);
glGenerateMipmap(GL_TEXTURE_2D);
// remove image surface now it's no longer needed to create texture
SDL_FreeSurface(image);
image = nullptr;
// Mess with perspective
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
// unit <-> 100 units
float width = 800;
float height = 600;
glm::mat4 Projection = glm::perspective(
glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
//// Camera matrix
// glm::mat4 View = glm::lookAt(
// glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space
// glm::vec3(0, 0, 0), // and looks at the origin
// glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
//);
//// Model matrix : an identity matrix (model will be at the origin)
// glm::mat4 Model = glm::mat4(1.0f);
//// Our ModelViewProjection : multiplication of our 3 matrices
// glm::mat4 mvp =
// Projection * View *
// Model; // Remember, matrix multiplication is the other way around
PlayerCamera camera;
// Game loop
bool running = true;
while (running) {
// SDL Event handling loop
while (SDL_PollEvent(&input) > 0) {
// Handle SDL quit event
if (input.type == SDL_QUIT) {
running = false;
}
// TODO: Do something with keys lol
};
camera.tick();
// Clear screen ready for next loop
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Make every shader/rendering call from this point on use our shader
// glUseProgram(shaderProgram);
// Send our glsl shader our mvp
shader.setMat4("MVP", camera.getMVP());
shader.use();
// I think this is meant to be here but it breaks...
// shove vertex array into buffer
glBindVertexArray(VAO);
// TODO: Run game here lol
// Draw triangle
// glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0);
SDL_GL_SwapWindow(window);
};
// Escaped Game loop
// On close also destroy window
SDL_DestroyWindow(window);
// Close all leftover SDL systems
SDL_Quit();
return 0;
}