This commit is contained in:
2025-09-13 18:29:56 +02:00
parent db2645a1b5
commit d2c18668eb
6 changed files with 65 additions and 15 deletions
+4 -3
View File
@@ -5,6 +5,7 @@
#include "args.h"
#include "config.h"
#include "logs.h"
void print_help(int status_code) {
puts(PACKAGE " " VERSION "\n\n"
@@ -26,12 +27,12 @@ void print_help(int status_code) {
}
void invalid_arg(char *arg) {
fprintf(stderr, "invalid argument: '%s'\n\n", arg);
log_error("invalid argument: '%s'", arg);
print_help(EXIT_FAILURE);
}
void invalid_value(char *arg, char *value) {
fprintf(stderr, "invalid value for argument '%s': '%s'\n\n", arg, value);
log_error("invalid value for argument '%s': '%s'", arg, value);
print_help(EXIT_FAILURE);
}
@@ -95,7 +96,7 @@ Parameters parse_args(int argc, char **argv) {
}
if (params.frag_path == 0) {
fprintf(stderr, "required argument -f/--frag\n\n");
log_error("required argument -f/--frag");
exit(EXIT_FAILURE);
}
+4
View File
@@ -4,6 +4,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "logs.h"
#include "types.h"
time_t get_file_time(File file) {
@@ -23,6 +24,7 @@ void update_file(File *file) {
if (file->content != 0) {
free(file->content);
}
log_info("Reading '%s'...", file->path);
// init empty file
file->content = 0;
file->error = false;
@@ -31,6 +33,7 @@ void update_file(File *file) {
FILE *file_pointer = fopen(file->path, "rb");
if (!file_pointer) {
file->error = true;
log_error("Cannot open file '%s'", file->path);
return;
}
// read file length
@@ -42,6 +45,7 @@ void update_file(File *file) {
if (!file->content) {
file->error = true;
fclose(file_pointer);
log_error("Cannot read file '%s'", file->path);
return;
}
// read file
+2 -4
View File
@@ -1,15 +1,15 @@
#include <linmath.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "file.h"
#include "logs.h"
#include "shaders.h"
#include "types.h"
#include "window.h"
void error_callback(int error, const char *description) {
fprintf(stderr, "Error %d: %s\n", error, description);
log_error("[GLFW] %d: %s", error, description);
close_window(0, true);
exit(EXIT_FAILURE);
}
@@ -45,7 +45,6 @@ void forge_run(Parameters params) {
File fragment_shader = read_file(params.frag_path);
if (fragment_shader.error) {
fprintf(stderr, "Cannot read file\n");
exit(EXIT_FAILURE);
}
@@ -54,7 +53,6 @@ void forge_run(Parameters params) {
ShaderProgram program = init_program(fragment_shader);
if (program.error) {
fprintf(stderr, "Failed to compile shaders\n");
close_window(window, true);
exit(EXIT_FAILURE);
}
+29
View File
@@ -0,0 +1,29 @@
#include <stdio.h>
#ifndef LOG_H
#define LOG_H
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define log_debug(format, ...) \
fprintf(stderr, ANSI_COLOR_MAGENTA "[DEBUG] " format ANSI_COLOR_RESET \
"\n" __VA_OPT__(, ) __VA_ARGS__)
#define log_success(format, ...) \
fprintf(stdout, ANSI_COLOR_GREEN "[SUCCESS] " format ANSI_COLOR_RESET \
"\n" __VA_OPT__(, ) __VA_ARGS__)
#define log_info(format, ...) \
fprintf(stdout, "[INFO] " format "\n" __VA_OPT__(, ) __VA_ARGS__)
#define log_warn(format, ...) \
fprintf(stderr, ANSI_COLOR_YELLOW "[WARN] " format ANSI_COLOR_RESET \
"\n" __VA_OPT__(, ) __VA_ARGS__)
#define log_error(format, ...) \
fprintf(stderr, ANSI_COLOR_RED "[ERROR] " format ANSI_COLOR_RESET \
"\n" __VA_OPT__(, ) __VA_ARGS__)
#endif
+9 -3
View File
@@ -1,15 +1,17 @@
#include <glad/gl.h>
#include <linmath.h>
#include <stddef.h>
#include <stdio.h>
#include "constants.h"
#include "logs.h"
#include "types.h"
bool compile_shader(GLuint shader_id, char *name, char *source_code) {
GLint status_params;
char log[1024];
log_info("Compiling '%s'...", name);
// update shader source code
glShaderSource(shader_id, 1, (const GLchar **)&source_code, NULL);
@@ -21,9 +23,9 @@ bool compile_shader(GLuint shader_id, char *name, char *source_code) {
glGetShaderInfoLog(shader_id, 1024, NULL, (GLchar *)&log);
if (status_params == GL_FALSE) {
fprintf(stderr, "Failed to compile shader '%s'\n%s\n", name, log);
log_error("Failed to compile\n%s", log);
} else {
fprintf(stdout, "Compiled shader '%s'\n", name);
log_success("Compilation successful");
}
return status_params == GL_TRUE;
@@ -72,6 +74,8 @@ ShaderProgram init_program(File fragment_shader) {
glVertexAttribPointer(program.vpos_location, 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (void *)offsetof(Vertex, pos));
log_success("Program initialized");
return program;
}
@@ -84,6 +88,8 @@ void update_program(ShaderProgram program, File fragment_shader) {
if (result) {
// re-link program
glLinkProgram(program.program);
log_success("Program updated");
}
}
+17 -5
View File
@@ -5,21 +5,24 @@
#include <stdlib.h>
#include "config.h"
#include "logs.h"
#include "types.h"
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
void init_glfw(void (*error_callback)(int, const char *)) {
log_info("[GLFW] Initializing...");
// set errors handler
glfwSetErrorCallback(error_callback);
// print current GLFW version
fprintf(stdout, "[GLFW] %s\n", glfwGetVersionString());
log_info("[GLFW] %s", glfwGetVersionString());
// init GLFW
if (!glfwInit()) {
fprintf(stderr, "[GLFW] Initialization failed\n");
log_error("[GLFW] Initialization failed");
exit(EXIT_FAILURE);
}
@@ -27,6 +30,8 @@ void init_glfw(void (*error_callback)(int, const char *)) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
log_success("[GLFS] Initialized...");
}
GLFWmonitor *get_monitor(unsigned char screen_index) {
@@ -36,8 +41,8 @@ GLFWmonitor *get_monitor(unsigned char screen_index) {
// check selected monitor availability
if (screen_index >= count) {
fprintf(stderr, "Screen %d is out of range [0-%d]\n", screen_index,
count - 1);
log_error("[GLFW] Screen %d is out of range [0-%d]", screen_index,
count - 1);
glfwTerminate();
exit(EXIT_FAILURE);
}
@@ -47,6 +52,9 @@ GLFWmonitor *get_monitor(unsigned char screen_index) {
GLFWwindow *create_window(GLFWmonitor *monitor,
void (*key_callback)(Window *, int, int, int, int)) {
log_info("[GLFW] Creating window...");
// Window related hints
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
glfwWindowHint(GLFW_FOCUSED, GLFW_FALSE);
@@ -59,7 +67,7 @@ GLFWwindow *create_window(GLFWmonitor *monitor,
// handle window creation fail
if (!window) {
fprintf(stderr, "[GLFW] Window or context creation failed\n");
log_error("[GLFW] Window or context creation failed");
glfwTerminate();
exit(EXIT_FAILURE);
}
@@ -69,6 +77,8 @@ GLFWwindow *create_window(GLFWmonitor *monitor,
// hide cursor
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
log_success("[GLFW] Window created");
return window;
}
@@ -117,8 +127,10 @@ Context get_window_context(Window *window) {
void close_window(Window *window, bool hard) {
if (hard) {
log_info("[GLFW] Terminating library...");
glfwTerminate();
} else {
log_info("[GLFW] Closing window...");
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}