Compare commits

..

11 commits

8 changed files with 126 additions and 45 deletions

BIN
data/testxure.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

View file

@ -1,7 +1,11 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
FragColor = texture(ourTexture, TexCoord);
}

View file

@ -1,10 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vec4(aPos.x, aPos.y, aPos.z, 1.0);
TexCoord = aTexCoord;
}

View file

@ -1,16 +1,19 @@
#include <GL/glew.h>
#include <SDL3/SDL.h>
#include <stdio.h>
#include <SDL3/SDL_surface.h>
#include <stdlib.h>
#include "SDL3/SDL_events.h"
#include "SDL3/SDL_keycode.h"
#include "SDL3/SDL_surface.h"
#include "SDL3_image/SDL_image.h"
#include "camera.h"
#include "shader.h"
#include "window.h"
#include "arena_allocator.h"
/*
void test_task(void *data) {
printf("\ttest task has been run: %s\n", (char *)data);
}
@ -54,11 +57,13 @@ int main(int argc, char *argv[]) {
printf("tests complete\n");
}
*/
/*
int main(int argc, char *argv[]) {
wn_window window = {0};
if (!wn_window_init(&window)) {
Arena *global_arena = arena_init(NULL, 0);
wn_window *window = wn_window_init(global_arena);
if (window == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize wn_window");
return EXIT_FAILURE;
}
@ -66,23 +71,24 @@ int main(int argc, char *argv[]) {
.vertex_shader_source_path = "shaders/vert.glsl",
.fragment_shader_source_path = "shaders/frag.glsl"};
wn_shader shader = wn_shader_init(shader_code_location);
if (shader.success == false) {
wn_shader *shader = wn_shader_init(global_arena, shader_code_location);
if (shader->success == false) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize shader");
return EXIT_FAILURE;
}
// set up vertex data (and buffer(s)) and configure vertex attributes
float vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
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);
@ -98,13 +104,16 @@ int main(int argc, char *argv[]) {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// note that this is allowed, the call to glVertexAttribPointer registered VBO
// as the vertex attribute's bound vertex buffer object so afterwards we can
// safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
// remember: do NOT unbind the EBO while a VAO is active as the bound element
// buffer object IS stored in the VAO; keep the EBO bound.
@ -114,21 +123,59 @@ int main(int argc, char *argv[]) {
// 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);
// glBindVertexArray(0);
// Texture Load
SDL_Surface *image = IMG_Load("./data/testxure.png");
if (image == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load texture");
return EXIT_FAILURE;
}
SDL_FlipSurface(image, SDL_FLIP_VERTICAL);
GLuint texture;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
// Handle different SDL Surface data types
int mode = GL_RGBA;
// if (image->format->BytesPerPixel == 4) {
// mode = GL_RGBA;
// }
glBindTexture(GL_TEXTURE_2D, texture);
// set the texture wrapping parameters
glTexParameteri(
GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, mode,
GL_UNSIGNED_BYTE, image->pixels);
glGenerateMipmap(GL_TEXTURE_2D);
SDL_DestroySurface(image);
// Initialise camera
Camera camera = Camera_default;
// set our texture uniform
glUseProgram(shader->shaderProgram);
glUniform1i(glGetUniformLocation(shader->shaderProgram, "ourTexture"), 0);
bool wants_running = true;
while (wants_running) {
// TODO: Create event handler files
while (SDL_PollEvent(&window.event)) {
switch (window.event.type) {
while (SDL_PollEvent(&window->event)) {
switch (window->event.type) {
case SDL_EVENT_QUIT:
wants_running = false;
break;
case SDL_EVENT_KEY_DOWN:
if (window.event.key.key == SDLK_ESCAPE) {
if (window->event.key.key == SDLK_ESCAPE) {
wants_running = false;
}
break;
@ -142,7 +189,7 @@ int main(int argc, char *argv[]) {
mat4 mvp;
camera_calc_mvp(&mvp, &camera);
int mvp_uniform_location =
glGetUniformLocation(shader.shaderProgram, "MVP");
glGetUniformLocation(shader->shaderProgram, "MVP");
glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]);
// render
@ -150,24 +197,23 @@ int main(int argc, char *argv[]) {
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shader.shaderProgram);
glBindVertexArray(
VAO); // seeing as we only have a single VAO there's no need to bind it
glUseProgram(shader->shaderProgram);
// seeing as we only have a single VAO there's no need to bind it
// every time, but we'll do so to keep things a bit more organized
glBindVertexArray(VAO);
// glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0); // no need to unbind it every time
wn_swapwindow(&window);
wn_swapwindow(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
wn_shader_deinit(shader);
wn_window_deinit(&window);
arena_deinit(global_arena);
return EXIT_SUCCESS;
}
*/

View file

@ -1,10 +1,14 @@
#include "shader.h"
#include "SDL3/SDL_iostream.h"
#include "SDL3/SDL_log.h"
#include "arena_allocator.h"
#include <GLES2/gl2.h>
wn_shader wn_shader_init(wn_shader_code_location shader_code_location) {
wn_shader shader = {.shaderProgram = 0, .success = false};
wn_shader *wn_shader_init(Arena *arena,
wn_shader_code_location shader_code_location) {
wn_shader *shader = arena_alloc(arena, sizeof(wn_shader));
*shader = (wn_shader){.shaderProgram = 0, .success = false};
const char *vertexShaderSource =
SDL_LoadFile(shader_code_location.vertex_shader_source_path, NULL);
@ -60,11 +64,16 @@ wn_shader wn_shader_init(wn_shader_code_location shader_code_location) {
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
shader.shaderProgram = shaderProgram;
shader.success = true;
shader->shaderProgram = shaderProgram;
shader->success = true;
arena_deinit_task_push(arena, (ArenaDeinitTask){.func_ptr = *wn_shader_deinit,
.func_param = shader,
.next = NULL});
return shader;
}
void wn_shader_deinit(wn_shader shader) {
glDeleteProgram(shader.shaderProgram);
void wn_shader_deinit(void *shader) {
wn_shader *cast_shader = (wn_shader *)shader;
glDeleteProgram(cast_shader->shaderProgram);
}

View file

@ -1,5 +1,6 @@
#pragma once
#include "arena_allocator.h"
#include <stdbool.h>
typedef struct {
@ -13,6 +14,7 @@ struct wn_shader_s {
};
typedef struct wn_shader_s wn_shader;
wn_shader wn_shader_init(wn_shader_code_location shader_code_location);
wn_shader *wn_shader_init(Arena *arena,
wn_shader_code_location shader_code_location);
void wn_shader_deinit(wn_shader shader);
void wn_shader_deinit(void* shader);

View file

@ -1,17 +1,24 @@
#include "window.h"
#include "SDL3/SDL_hints.h"
#include "SDL3/SDL_init.h"
#include "arena_allocator.h"
#include <GL/glew.h>
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_opengl.h>
bool wn_window_init(wn_window *window) {
wn_window *wn_window_init(Arena *arena) {
// Try to use X11 till I fix glew may require xwayland
SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "x11");
wn_window *window = arena_alloc(arena, sizeof(wn_window));
*window = (wn_window){0};
window->window = SDL_CreateWindow("Hello SDL3", 320, 240,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s",
SDL_GetError());
return false;
return NULL;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
@ -22,37 +29,45 @@ bool wn_window_init(wn_window *window) {
if (!window->window) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s",
SDL_GetError());
return false;
return NULL;
}
window->glcontext = SDL_GL_CreateContext(window->window);
if (!window->glcontext) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't create OpenGL Context: %s", SDL_GetError());
return false;
return NULL;
}
GLenum glewError = glewInit();
if (glewError != GLEW_OK) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error initializing GLEW! %s",
glewGetErrorString(glewError));
return NULL;
}
if (!SDL_GL_MakeCurrent(window->window, window->glcontext)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't make glcontext current: %s", SDL_GetError());
return NULL;
}
// hide and lock mouse
SDL_SetWindowRelativeMouseMode(window->window, true);
SDL_GetRelativeMouseState(NULL, NULL);
return true;
// add deinit function to arena deinit stack
arena_deinit_task_push(arena, (ArenaDeinitTask){.func_param = (void *)window,
.func_ptr = *wn_window_deinit,
.next = NULL});
return window;
}
void wn_window_deinit(wn_window *window) {
SDL_GL_DestroyContext(window->glcontext);
SDL_DestroyWindow(window->window);
void wn_window_deinit(void *window) {
wn_window *cast_window = (wn_window *)window;
SDL_GL_DestroyContext(cast_window->glcontext);
SDL_DestroyWindow(cast_window->window);
SDL_Quit();
}

View file

@ -3,6 +3,7 @@
#include "SDL3/SDL_events.h"
#include "SDL3/SDL_render.h"
#include "SDL3/SDL_video.h"
#include "arena_allocator.h"
#include <stdbool.h>
struct wn_window_s {
@ -13,7 +14,7 @@ struct wn_window_s {
};
typedef struct wn_window_s wn_window;
bool wn_window_init(wn_window *window);
void wn_window_deinit(wn_window *window);
wn_window *wn_window_init(Arena *arena);
void wn_window_deinit(void *window);
void wn_swapwindow(wn_window *window);