We now have a triangle :D
This commit is contained in:
parent
3291732dd3
commit
3652cd82d4
2 changed files with 81 additions and 53 deletions
4
Makefile
4
Makefile
|
|
@ -9,6 +9,8 @@ BUILDDIR = build/
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
||||||
$(TARGET): $(SRCDIR)main.cpp
|
$(TARGET): $(SRCDIR)main.cpp $(BUILDDIR)
|
||||||
$(CC) $(CFLAGS) -o $(BUILDDIR)$(TARGET) $(SRCDIR)main.cpp
|
$(CC) $(CFLAGS) -o $(BUILDDIR)$(TARGET) $(SRCDIR)main.cpp
|
||||||
|
|
||||||
|
$(BUILDDIR):
|
||||||
|
mkdir -p $(BUILDDIR)
|
||||||
|
|
|
||||||
130
src/main.cpp
130
src/main.cpp
|
|
@ -1,75 +1,101 @@
|
||||||
//#include <iostream>
|
//#include <iostream>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
// Make sure Glew is loaded first
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_opengl.h>
|
#include <SDL2/SDL_opengl.h>
|
||||||
|
|
||||||
//TODO: Create Error handling class
|
// TODO: Create Error handling class
|
||||||
|
|
||||||
int main(int argc, char ** argv){
|
int main(int argc, char **argv) {
|
||||||
// Initialise SDL2
|
// Initialise SDL2
|
||||||
//TODO: Check if sdl initialised
|
// TODO: Check if sdl initialised
|
||||||
SDL_Init(SDL_INIT_EVERYTHING);
|
SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
|
|
||||||
//Make OpenGL use double buffering (Render game first then shove to output)
|
// Make OpenGL use double buffering (Render game first then shove to output)
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
|
||||||
//TODO: Understand what a depth buffer is
|
// TODO: Understand what a depth buffer is
|
||||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
||||||
|
|
||||||
//TODO: Discover if thid is necessary for linux and what values they need be
|
// TODO: Discover if thid is necessary for linux and what values they need be
|
||||||
//SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
// SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||||
//SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
// SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||||
//SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
// SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||||
//SDL_GL_SetAttribute(SDL_GL_ALPHA_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);
|
|
||||||
//TODO: Test that window was created successfully
|
|
||||||
|
|
||||||
//Create glContext
|
// Create Window
|
||||||
SDL_GLContext glContext = SDL_GL_CreateContext(window);
|
SDL_Window *window =
|
||||||
//TODO: Test that glContext was created successfully
|
SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||||
|
800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
|
||||||
|
// TODO: Test that window was created successfully
|
||||||
|
|
||||||
//Initialise Glew
|
// Create glContext
|
||||||
if (glewInit() != GLEW_OK){
|
SDL_GLContext glContext = SDL_GL_CreateContext(window);
|
||||||
//TODO: Throw an error lol
|
// TODO: Test that glContext was created successfully
|
||||||
}
|
|
||||||
|
|
||||||
|
// Initialise Glew
|
||||||
|
if (glewInit() != GLEW_OK) {
|
||||||
|
// TODO: Throw an error lol
|
||||||
|
}
|
||||||
|
|
||||||
//Create event handling struct
|
// Create event handling struct
|
||||||
SDL_Event input;
|
SDL_Event input;
|
||||||
|
|
||||||
// Game loop
|
// test triangle
|
||||||
bool running = true;
|
float vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f};
|
||||||
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
|
|
||||||
|
|
||||||
};
|
// Vertex Buffer Object
|
||||||
//Clear screen ready for next loop
|
unsigned int VBO;
|
||||||
glClearColor(0.0f,0.0f,0.0f,0.0f);
|
glGenBuffers(1, &VBO);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
|
|
||||||
// TODO: Run game here lol
|
// Bind the buffer and size it to the data provided
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
SDL_GL_SwapWindow(window);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// Create vertex array that will populate buffer
|
||||||
|
unsigned int VAO;
|
||||||
|
glCreateVertexArrays(1, &VAO);
|
||||||
|
glEnableVertexArrayAttrib(VAO, 0);
|
||||||
|
|
||||||
|
// set up vertex array atributes
|
||||||
|
glVertexArrayAttribFormat(VAO, 0, 3, GL_FLOAT, GL_FALSE, 0);
|
||||||
|
|
||||||
|
// sut up vbo for vao
|
||||||
|
glVertexArrayVertexBuffer(VAO, 0, VBO, 0, sizeof(float) * 3);
|
||||||
|
|
||||||
|
// shove vertex array into buffer
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
|
// 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
|
||||||
};
|
};
|
||||||
|
// Clear screen ready for next loop
|
||||||
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// Escaped Game loop
|
// TODO: Run game here lol
|
||||||
|
// Draw triangle
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
// On close also destroy window
|
SDL_GL_SwapWindow(window);
|
||||||
SDL_DestroyWindow(window);
|
};
|
||||||
|
|
||||||
// Close all leftover SDL systems
|
// Escaped Game loop
|
||||||
SDL_Quit();
|
|
||||||
|
|
||||||
return 0;
|
// On close also destroy window
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
|
||||||
|
// Close all leftover SDL systems
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue