aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2023-06-10 21:13:02 -0500
committerjvech <jmvalenciae@unal.edu.co>2023-06-10 21:13:02 -0500
commit756490a02d84288ee635a8aa33811e043ca10b44 (patch)
tree7088759369e55d8ec26fc1aea74c6655df150e48 /src
parent0a245e329a9af7df792981f1f35a70bbfaded8b9 (diff)
add: style code improvments done
Diffstat (limited to 'src')
-rw-r--r--src/linear.h2
-rw-r--r--src/main.c102
-rw-r--r--src/obj.c10
3 files changed, 69 insertions, 45 deletions
diff --git a/src/linear.h b/src/linear.h
index deec495..3733c70 100644
--- a/src/linear.h
+++ b/src/linear.h
@@ -47,7 +47,7 @@ Mat4 linearMat4Mul(Mat4 x1, Mat4 x2);
Mat4 linearMat4Muln(int n, ...);
Mat4 linearMat4Transpose(Mat4 x);
Mat4 linearMat4Inv(Mat4 x); //TODO
-Mat4 linearMat4Add(Mat4 x1, Mat4 x2); //TODO
+Mat4 linearMat4Add(Mat4 x1, Mat4 x2);
float linearMat4Det(Mat4 x); //TODO
Vec3 linearVec3(float x, float y, float z);
diff --git a/src/main.c b/src/main.c
index f58fa28..d0ce19b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -42,6 +42,9 @@ struct Camera {
Vec3 up;
};
+static void loadCLI(int argc, char *argv[], char **vertexPath, char **fragmentPath);
+static void initOpengl(void);
+static void initGlfw(void);
static void userError(const char *msg, const char *detail);
static void glfw_size_callback(GLFWwindow *window, int width, int height);
static void processInput(GLFWwindow *window);
@@ -56,6 +59,53 @@ static void usage(int status);
static float cameraSpeed = 2.0;
void
+loadCLI(int argc, char *argv[], char **vertexPath, char **fragmentPath)
+{
+ int opt;
+ while ((opt = getopt(argc, argv, "hv:f:")) != -1) {
+ switch (opt) {
+ case 'h':
+ usage(0);
+ break;
+ case 'v':
+ *vertexPath = optarg;
+ break;
+ case 'f':
+ *fragmentPath = optarg;
+ break;
+ default:
+ usage(2);
+ }
+ }
+
+ if (optind >= argc) userError("cli Error", "expected argument after options\n");
+ else if (!vertexPath) userError("environment Error", "MVERSE_VERTEX not defined");
+ else if (!fragmentPath) userError("environment Error", "MVERSE_FRAGMENT not defined");
+
+}
+
+void
+initGlfw(void)
+{
+ const char *glfwErrno;
+ if (!glfwInit()) {
+ glfwGetError(&glfwErrno);
+ glfwTerminate();
+ userError("glfwInit() Error", glfwErrno);
+ }
+}
+
+void
+initOpengl(void)
+{
+ GLubyte glewErrno = glewInit();
+ if (glewErrno != GLEW_OK) {
+ glfwTerminate();
+ userError("initGlfw() Error", (const char *)glewGetErrorString(glewErrno));
+ }
+}
+
+void
userError(const char *msg, const char *detail)
{
fprintf(stderr, "%s: %s\n", msg, detail);
@@ -271,48 +321,20 @@ int main(int argc, char *argv[])
{
Obj obj;
GLFWwindow *window;
- const char *glfwErrno;
-
- int opt;
char *vertexFile, *fragmentFile;
+ unsigned int shader;
+
vertexFile = getenv("MVERSE_VERTEX");
fragmentFile = getenv("MVERSE_FRAGMENT");
- while ((opt = getopt(argc, argv, "hv:f:")) != -1) {
- switch (opt) {
- case 'h':
- usage(0);
- break;
- case 'v':
- vertexFile = optarg;
- break;
- case 'f':
- fragmentFile = optarg;
- break;
- default:
- usage(2);
- }
- }
-
- if (optind >= argc) userError("cli Error", "expected argument after options\n");
- else if (!vertexFile) userError("environment Error", "MVERSE_VERTEX not defined");
- else if (!fragmentFile) userError("environment Error", "MVERSE_FRAGMENT not defined");
-
+ loadCLI(argc, argv, &vertexFile, &fragmentFile);
argv += optind;
argc -= optind;
obj = objCreate(argv[0]);
- if (obj.mesh == NULL) {
- glfwTerminate();
- userError("objCreateMesh Error", "NULL mesh returned");
- }
-
- if (!glfwInit()) {
- glfwGetError(&glfwErrno);
- glfwTerminate();
- userError("glfwInit() Error", glfwErrno);
- }
+ // glfw Init
+ initGlfw();
window = glfwCreateWindow(640, 480, "Mverse", NULL, NULL);
if (!window) {
@@ -320,18 +342,14 @@ int main(int argc, char *argv[])
userError("glfwCreateWindow() Error", "Can't create window");
}
+ // Window Setup
glfwSetFramebufferSizeCallback(window, glfw_size_callback);
- glfwMakeContextCurrent(window);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
+ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
- GLubyte glewErrno = glewInit();
- if (glewErrno != GLEW_OK) {
- glfwTerminate();
- userError("glewInit Error", (const char *)glewGetErrorString(glewErrno));
- }
-
- unsigned int shader = shaderCreateProgram(vertexFile, fragmentFile);
+ glfwMakeContextCurrent(window);
+ initOpengl();
+ shader = shaderCreateProgram(vertexFile, fragmentFile);
objSetUp(obj);
diff --git a/src/obj.c b/src/obj.c
index d832604..ea69f18 100644
--- a/src/obj.c
+++ b/src/obj.c
@@ -77,7 +77,7 @@ objCreate(const char *filename)
// On error return NULL
if (fi == NULL || mesh == NULL) {
- fprintf(stderr, "objCreateMesh() Error: %s\n", strerror(errno));
+ perror("objCreateMesh() Error");
fclose(fi);
exit(1);
}
@@ -133,7 +133,13 @@ objCreate(const char *filename)
}
void
-readF(char *line, Mesh *mesh, int meshIndex, struct Setv3 *v, struct Setv2 *vt, struct Setv3 *vn)
+readF(char *line,
+ Mesh *mesh,
+ int meshIndex,
+ struct Setv3 *v,
+ struct Setv2 *vt,
+ struct Setv3 *vn
+ )
{
Vertex vertexBuffer;
struct Seti *f;
Feel free to download, copy and edit any repo