app.c (1558B)
1 #include "app.h" 2 #include "logger.h" 3 4 #include <stdlib.h> 5 6 void error_callback(int error, const char* description) 7 { 8 LOGE("%s", description); 9 } 10 11 App *App_make(const AppInfo *app_info) 12 { 13 if (!glfwInit()) 14 { 15 LOGF("Failed to initialize GLFW."); 16 return NULL; 17 } 18 19 glfwSetErrorCallback(error_callback); 20 21 App *app = malloc(sizeof *app); 22 if (!app) return NULL; 23 24 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); 25 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, app_info->gl_major_version); 26 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, app_info->gl_minor_version); 27 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 28 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); 29 glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); 30 31 app->window = glfwCreateWindow( 32 app_info->width, 33 app_info->height, 34 app_info->title, 35 NULL, 36 NULL 37 ); 38 if (!app->window) 39 { 40 LOGF("Failed to create window."); 41 App_free(app); 42 return NULL; 43 } 44 45 glfwSetInputMode(app->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); 46 47 glfwMakeContextCurrent(app->window); 48 49 if (!gladLoadGLLoader(glfwGetProcAddress)) 50 { 51 LOGF("Failed to initialized GLAD."); 52 App_free(app); 53 return NULL; 54 } 55 56 i32 width, height; 57 glfwGetFramebufferSize(app->window, &width, &height); 58 glViewport(0, 0, width, height); 59 60 return app; 61 } 62 63 void App_free(App *app) 64 { 65 if (app) 66 { 67 free(app); 68 } 69 glfwTerminate(); 70 } 71