Moved most of the window management to a seperate class
This commit is contained in:
parent
c1aa2130af
commit
5a78f43e55
3 changed files with 70 additions and 53 deletions
61
src/main.c
61
src/main.c
|
|
@ -6,56 +6,22 @@
|
|||
#include "SDL3/SDL_events.h"
|
||||
#include "SDL3/SDL_keycode.h"
|
||||
#include "camera.h"
|
||||
#include "window.h"
|
||||
|
||||
// settings
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_GLContext glcontext;
|
||||
SDL_Event event;
|
||||
|
||||
wn_window wn_window = {0};
|
||||
if (!wn_window_init(&wn_window)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize wn_window");
|
||||
}
|
||||
|
||||
const char *vertexShaderSource = SDL_LoadFile("shaders/vert.glsl", NULL);
|
||||
const char *fragmentShaderSource = SDL_LoadFile("shaders/frag.glsl", NULL);
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s",
|
||||
SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||
|
||||
window = SDL_CreateWindow("Hello SDL3", 320, 240,
|
||||
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||
if (!window) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s",
|
||||
SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
glcontext = SDL_GL_CreateContext(window);
|
||||
if (!glcontext) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"Couldn't create OpenGL Context: %s", SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
GLenum glewError = glewInit();
|
||||
if (glewError != GLEW_OK) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error initializing GLEW! %s",
|
||||
glewGetErrorString(glewError));
|
||||
}
|
||||
|
||||
if (!SDL_GL_MakeCurrent(window, glcontext)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"Couldn't make glcontext current: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
// vertex shader
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
|
||||
|
|
@ -146,20 +112,20 @@ int main(int argc, char *argv[]) {
|
|||
Camera camera = Camera_default;
|
||||
|
||||
// Lock and hide mouse
|
||||
SDL_SetWindowRelativeMouseMode(window, true);
|
||||
SDL_SetWindowRelativeMouseMode(wn_window.window, true);
|
||||
SDL_GetRelativeMouseState(NULL, NULL);
|
||||
|
||||
bool wants_running = true;
|
||||
while (wants_running) {
|
||||
|
||||
// handle input events
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
while (SDL_PollEvent(&wn_window.event)) {
|
||||
switch (wn_window.event.type) {
|
||||
case SDL_EVENT_QUIT:
|
||||
wants_running = false;
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
if (event.key.key == SDLK_ESCAPE) {
|
||||
if (wn_window.event.key.key == SDLK_ESCAPE) {
|
||||
wants_running = false;
|
||||
}
|
||||
break;
|
||||
|
|
@ -189,7 +155,7 @@ int main(int argc, char *argv[]) {
|
|||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
// glBindVertexArray(0); // no need to unbind it every time
|
||||
|
||||
SDL_GL_SwapWindow(window);
|
||||
SDL_GL_SwapWindow(wn_window.window);
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
|
|
@ -197,10 +163,7 @@ int main(int argc, char *argv[]) {
|
|||
glDeleteBuffers(1, &EBO);
|
||||
glDeleteProgram(shaderProgram);
|
||||
|
||||
SDL_GL_DestroyContext(glcontext);
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
SDL_Quit();
|
||||
wn_window_deinit(&wn_window);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
|||
53
src/window.c
53
src/window.c
|
|
@ -0,0 +1,53 @@
|
|||
#include "window.h"
|
||||
#include "SDL3/SDL_init.h"
|
||||
#include <GL/glew.h>
|
||||
#include <SDL3/SDL_log.h>
|
||||
#include <SDL3/SDL_opengl.h>
|
||||
|
||||
bool wn_window_init(wn_window *window) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||
|
||||
if (!window->window) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s",
|
||||
SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
GLenum glewError = glewInit();
|
||||
if (glewError != GLEW_OK) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error initializing GLEW! %s",
|
||||
glewGetErrorString(glewError));
|
||||
}
|
||||
|
||||
if (!SDL_GL_MakeCurrent(window->window, window->glcontext)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"Couldn't make glcontext current: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void wn_window_deinit(wn_window *window) {
|
||||
SDL_GL_DestroyContext(window->glcontext);
|
||||
SDL_DestroyWindow(window->window);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
|
@ -3,16 +3,17 @@
|
|||
#include "SDL3/SDL_events.h"
|
||||
#include "SDL3/SDL_render.h"
|
||||
#include "SDL3/SDL_video.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
struct wn_window_s {
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_GLContext *glcontext;
|
||||
SDL_Event *event;
|
||||
SDL_GLContext glcontext;
|
||||
SDL_Event event;
|
||||
};
|
||||
typedef struct wn_window_s wn_window;
|
||||
|
||||
wn_window *wn_window_init(wn_window *window);
|
||||
bool wn_window_init(wn_window *window);
|
||||
void wn_window_deinit(wn_window *window);
|
||||
|
||||
void wn_swapwindow(wn_window*);
|
||||
void wn_swapwindow(wn_window *);
|
||||
|
|
|
|||
Loading…
Reference in a new issue