Quellcode durchsuchen

feat: refactor file operations to separate module

Douglas Andreani vor 1 Jahr
Ursprung
Commit
3a1a8c794c
5 geänderte Dateien mit 70 neuen und 40 gelöschten Zeilen
  1. 1 1
      src/CMakeLists.txt
  2. 41 0
      src/fio.c
  3. 9 0
      src/fio.h
  4. 12 2
      src/main.c
  5. 7 37
      src/renderer.c

+ 1 - 1
src/CMakeLists.txt

@@ -6,7 +6,7 @@ set(PROJECT_NAME gl-windows)
 project(${PROJECT_NAME})
 include_directories(${INCLUDE_DIRS})
 
-add_executable(${PROJECT_NAME} main.c glad.c renderer.c)
+add_executable(${PROJECT_NAME} main.c fio.c glad.c renderer.c )
 
 find_library(GLFW_LIBRARY NAMES glfw3 HINTS "c:/temp/usr/lib")
 

+ 41 - 0
src/fio.c

@@ -0,0 +1,41 @@
+#include "fio.h"
+
+#include <inttypes.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <io.h>
+
+void read_shader_source(GLchar* source, const char* file_path) {
+    
+    GLchar buf[1024] = { 0 };
+    errno_t err = 0;
+    struct _stat st;
+    int result = 0;
+    
+
+    if (_access(file_path, 4) == -1) {
+        fprintf(stderr, "file not found %s\n", file_path);
+        return;
+    }
+    FILE* f = NULL;
+
+
+    err = fopen_s(&f, file_path, "r");
+    if (err != 0) {
+        fprintf(stderr, "could not open file %s\n", file_path);
+        return;
+    }
+    
+   
+    result = _stat(file_path, &st);
+    assert(result == 0 && "could not stat file");
+    assert(st.st_size <= 1024 && "shader source too long");
+    
+    fread(buf, st.st_size, 1, f);
+    assert(buf[0] != 0);
+
+    strcpy_s(source, 1024, buf);
+}

+ 9 - 0
src/fio.h

@@ -0,0 +1,9 @@
+#ifndef _FIO_H_
+#define _FIO_H_
+
+#include <glad/glad.h>
+
+void read_shader_source(GLchar* source, const char* file_path);
+
+
+#endif // _IO_H_

+ 12 - 2
src/main.c

@@ -22,6 +22,14 @@ void resize_window_callback(GLFWwindow* window, int width, int height) {
 	glViewport(0, 0, width, height);
 }
 
+
+void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
+{
+	if(key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
+		glfwSetWindowShouldClose(window, GLFW_TRUE);
+	}
+}
+
 int main() {
 
 	Renderer renderer = {0};
@@ -30,6 +38,8 @@ int main() {
 	//assert(renderer != NULL && "failed to initialize renderer");
 	
 	glfwSetErrorCallback(error_callback);
+	glfwSetKeyCallback(renderer.window, key_callback);
+
 
 	while (!glfwWindowShouldClose(renderer.window)) {
 		glfwPollEvents();
@@ -49,7 +59,7 @@ int main() {
 
 	}
 
-	
-	glfwTerminate();
+	renderer_destroy(&renderer);
+
 	return 0;
 }

+ 7 - 37
src/renderer.c

@@ -1,50 +1,17 @@
 
 #include "renderer.h"
+#include "fio.h"
+
 #include <linmath.h>
 
 #include <glad/glad.h>
-#include <inttypes.h>
 #include <assert.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <stdlib.h>
-#include <io.h>
-#include <sys/stat.h>
 #include <GLFW/glfw3.h>
 
 
-void read_shader_source(GLchar* source, const char* file_path) {
-    
-    GLchar buf[1024] = { 0 };
-    errno_t err = 0;
-    struct _stat st;
-    int result = 0;
-    
-
-    if (_access(file_path, 4) == -1) {
-        fprintf(stderr, "file not found %s\n", file_path);
-        return;
-    }
-    FILE* f = NULL;
-
-
-    err = fopen_s(&f, file_path, "r");
-    if (err != 0) {
-        fprintf(stderr, "could not open file %s\n", file_path);
-        return;
-    }
-    
-   
-    result = _stat(file_path, &st);
-    assert(result == 0 && "could not stat file");
-    assert(st.st_size <= 1024 && "shader source too long");
-    
-    fread(buf, st.st_size, 1, f);
-    assert(buf[0] != 0);
-
-    strcpy_s(source, 1024, buf);
-}
-
 
 void renderer_init_nk(Renderer* renderer) {
 #if 0
@@ -196,13 +163,16 @@ void render_quad(Renderer* renderer, vec2 pos, vec2 size, vec4 color)
 
     glBindVertexArray(0);
 
-
 }
 
 void renderer_destroy(Renderer* renderer) {
-    //glDeleteTextures(1, &renderer->font_tex);
+
+    glDeleteTextures(1, &renderer->texture_color);
     glDeleteBuffers(1, &renderer->vbo);
+    glDeleteBuffers(1, &renderer->ebo);
     glDeleteProgram(renderer->programs[0]);
+
+    glfwTerminate();
     //nk_buffer_free(&renderer->cmds);
 }