terrain

Real-time terrain generation using marching cubes
git clone git://git.christianermann.dev/terrain
Log | Files | Refs | README | LICENSE

commit bf86e319f576516bd074fda2ee13d23cd73c53e7
parent c63216862550a584b6dcb1b0969efed01383ed4f
Author: Christian Ermann <christianermann@gmail.com>
Date:   Wed, 17 Nov 2021 12:01:00 -0800

Moved SDL and OpenGL setup into App

Diffstat:
Ainclude/app.h | 26++++++++++++++++++++++++++
Asrc/app.c | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.c | 59++++++++++++++++++-----------------------------------------
3 files changed, 134 insertions(+), 41 deletions(-)

diff --git a/include/app.h b/include/app.h @@ -0,0 +1,25 @@ +#ifndef APP_H +#define APP_H + +#include <SDL2/SDL.h> + +typedef struct { + const char *title; + int width; + int height; + int gl_major_version; + int gl_minor_version; +} AppInfo; + +typedef struct { + SDL_Window *window; + SDL_GLContext context; +} App; + +App *App_create(AppInfo *app_info); +void App_destroy(App *app); + +void App_hideCursor(App *app); +void App_showCursor(App *app); + +#endif +\ No newline at end of file diff --git a/src/app.c b/src/app.c @@ -0,0 +1,89 @@ +#include "app.h" + +#include "glad/glad.h" + +#include <stdbool.h> + +static void App_createWindow(App *app, AppInfo *app_info) +{ + app->window = SDL_CreateWindow( + app_info->title, + 100, + 100, + app_info->width, + app_info->height, + SDL_WINDOW_OPENGL + ); + if (app->window == NULL) + { + fprintf(stderr, "ERROR (App_createWindow): %s\n", SDL_GetError()); + App_destroy(app); + exit(EXIT_FAILURE); + } +} + +static void App_createContext(App *app, AppInfo *app_info) +{ + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, app_info->gl_major_version); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, app_info->gl_minor_version); + + app->context = SDL_GL_CreateContext(app->window); + if (app->context == NULL) + { + fprintf(stderr, "ERROR (App_createContext): %s\n", SDL_GetError()); + App_destroy(app); + exit(EXIT_FAILURE); + } + + gladLoadGLLoader(SDL_GL_GetProcAddress); + glViewport(0, 0, app_info->width, app_info->height); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); +} + +App *App_create(AppInfo *app_info) +{ + App *app = malloc(sizeof *app); + + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + fprintf(stderr, "ERROR (App_create): %s\n", SDL_GetError()); + App_destroy(app); + exit(EXIT_FAILURE); + } + + App_createWindow(app, app_info); + App_createContext(app, app_info); + + return app; +} + +void App_destroy(App *app) +{ + if (app->context) + { + SDL_GL_DeleteContext(app->context); + } + if (app->window) + { + SDL_DestroyWindow(app->window); + } + SDL_Quit(); + + if (app) + { + free(app); + } +} + +void App_hideCursor(App *app) +{ + SDL_ShowCursor(SDL_DISABLE); + SDL_SetRelativeMouseMode(SDL_TRUE); +} + +void App_showCursor(App *app) +{ + SDL_ShowCursor(SDL_ENABLE); + SDL_SetRelativeMouseMode(SDL_FALSE); +} +\ No newline at end of file diff --git a/src/main.c b/src/main.c @@ -1,3 +1,4 @@ +#include "app.h" #include "camera.h" #include "chunk_manager.h" #include "glh/shader.h" @@ -42,48 +43,24 @@ float sphereSDF(const float p[3]) int main(int argc, char** argv) { - if (SDL_Init(SDL_INIT_VIDEO) < 0) - { - fprintf(stderr, "SDL INIT ERROR: %s\n", SDL_GetError()); - exit(EXIT_FAILURE); - } - - SDL_Window* window = SDL_CreateWindow("Marching Cubes", 100, 100, WIDTH, - HEIGHT, SDL_WINDOW_OPENGL); - if (window == NULL) - { - fprintf(stderr, "SDL WINDOW ERROR: %s\n", SDL_GetError()); - exit(EXIT_FAILURE); - } - - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, - SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); - - SDL_GLContext context = SDL_GL_CreateContext(window); - if (context == NULL) - { - fprintf(stderr, "SDL GL CONTEXT ERROR: %s\n", SDL_GetError()); - exit(EXIT_FAILURE); - } - - SDL_ShowCursor(SDL_DISABLE); - SDL_SetRelativeMouseMode(SDL_TRUE); - - Camera camera = Camera_create(0, 0, -3, 0, 0); - - gladLoadGLLoader(SDL_GL_GetProcAddress); - - glViewport(0, 0, WIDTH, HEIGHT); + AppInfo app_info = { + .title = "Marching Cubes Terrain", + .width = 512, + .height = 512, + .gl_major_version = 3, + .gl_minor_version = 3 + }; + App *app = App_create(&app_info); + App_hideCursor(app); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + Camera camera = Camera_create(0, 0, -3, 0, 0); ChunkManager chunk_manager = ChunkManager_create(camera.position, 3, perlinSDF, 0.0f); + ChunkManager_drawChunks(&chunk_manager); Shader shader = Shader_create("shaders/basic.vs", "shaders/basic.fs"); glUseProgram(shader.program); @@ -107,8 +84,10 @@ int main(int argc, char** argv) switch (window_event.key.keysym.sym) { case SDLK_ESCAPE: - SDL_ShowCursor(SDL_ENABLE); - SDL_SetRelativeMouseMode(SDL_FALSE); + App_showCursor(app); + break; + case SDLK_RETURN: + App_hideCursor(app); break; case SDLK_r: Shader_reload(&shader); @@ -129,11 +108,9 @@ int main(int argc, char** argv) ChunkManager_recenter(&chunk_manager, camera.position); ChunkManager_drawChunks(&chunk_manager); - SDL_GL_SwapWindow(window); + SDL_GL_SwapWindow(app->window); } ChunkManager_free(&chunk_manager); - - SDL_DestroyWindow(window); - SDL_Quit(); + App_destroy(app); }