Bladeren bron

feat: add platform specific IO functions on `platform.h`

Douglas A 1 jaar geleden
bovenliggende
commit
652a7abb87
5 gewijzigde bestanden met toevoegingen van 66 en 18 verwijderingen
  1. 1 1
      include/glad/glad.h
  2. 2 2
      src/CMakeLists.txt
  3. 11 13
      src/fio.c
  4. 50 0
      src/platform.h
  5. 2 2
      src/renderer.h

+ 1 - 1
include/glad/glad.h

@@ -86,7 +86,7 @@ GLAPI int gladLoadGL(void);
 
 GLAPI int gladLoadGLLoader(GLADloadproc);
 
-#include "KHR/khrplatform.h"
+#include <KHR/khrplatform.h>
 typedef unsigned int GLenum;
 typedef unsigned char GLboolean;
 typedef unsigned int GLbitfield;

+ 2 - 2
src/CMakeLists.txt

@@ -10,6 +10,6 @@ include_directories(${INCLUDE_DIRS})
 
 add_executable(${PROJECT_NAME} main.c fio.c glad.c renderer.c )
 
-find_library(GLFW_LIBRARY NAMES glfw3 HINTS "c:/usr/lib")
+find_library(GLFW_LIBRARY NAMES glfw)
 
-target_link_libraries(${PROJECT_NAME} ${GLFW_LIBRARY})
+target_link_libraries(${PROJECT_NAME} ${GLFW_LIBRARY})

+ 11 - 13
src/fio.c

@@ -1,38 +1,36 @@
 #include "fio.h"
 
+#include "platform.h"
+
 #include <assert.h>
 #include <inttypes.h>
-#include <io.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <errno.h>
 
 void read_shader_source(GLchar *source, const char *file_path) {
 
   GLchar buf[1024] = {0};
-  errno_t err = 0;
-  struct _stat st;
+  struct stat st;
   int result = 0;
 
-  if (_access(file_path, 4) == -1) {
+  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);
+  f = FOPEN(file_path, "r");
+  
+  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);
-}
+  STRCPY(source, buf);
+}

+ 50 - 0
src/platform.h

@@ -0,0 +1,50 @@
+#include <stdio.h>
+#ifdef _WIN32
+#include <io.h>
+#else 
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <string.h>
+#endif
+
+#define STAT(path, buf) platform_stat(path, buf)
+static int platform_stat(const char *path, struct stat *buf)
+{
+  #ifdef _WIN32
+    return _stat(path, buf);
+  #else 
+    return stat(path, buf);
+  #endif
+}
+
+
+#define FOPEN(path, method) platform_fopen(path, method)
+static FILE* platform_fopen(const char* path, const char* method)
+{
+  #ifdef _WIN32
+  #else
+
+    FILE* fp = fopen(path, method);
+  #endif
+
+  return fp;
+}
+
+#define ACCESS(path, mode) platform_access(path, mode)
+static int platform_access(const char* path, int mode)
+{
+  #ifdef _WIN32
+  #else 
+    return access(path, mode);
+  #endif
+}
+
+#define STRCPY(dst, src) platform_strcpy(dst, src)
+static char *platform_strcpy(char* dst, char* src)
+{
+  #ifdef _WIN32
+  #else 
+    return strcpy(dst, src);
+  #endif
+}

+ 2 - 2
src/renderer.h

@@ -6,8 +6,8 @@
 #include <stdbool.h>
 #include <stdlib.h>
 
-#include <GLFW/glfw3.h>
 #include <glad/glad.h>
+#include <GLFW/glfw3.h>
 
 #define UNIFORMS_COUNT 1
 #define PROGRAMS_COUNT 2
@@ -97,4 +97,4 @@ void render_aabb(Renderer *renderer, float *aabb, vec4 color);
 
 void read_shader_source(GLchar *source, const char *file_path);
 
-#endif // _RENDERER_H_
+#endif // _RENDERER_H_