clarify code: local variables in start of function
This commit is contained in:
+9
-4
@@ -47,11 +47,12 @@ static char *split_arg_value(char *arg) {
|
|||||||
static bool is_digit(char c) { return c >= '0' && c <= '9'; }
|
static bool is_digit(char c) { return c >= '0' && c <= '9'; }
|
||||||
|
|
||||||
static bool is_number(char *value) {
|
static bool is_number(char *value) {
|
||||||
|
unsigned long value_len;
|
||||||
|
unsigned int i;
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
unsigned long value_len = strlen(value);
|
value_len = strlen(value);
|
||||||
unsigned int i;
|
|
||||||
for (i = 0; i < value_len; i++) {
|
for (i = 0; i < value_len; i++) {
|
||||||
if (!is_digit(value[i])) {
|
if (!is_digit(value[i])) {
|
||||||
return false;
|
return false;
|
||||||
@@ -72,11 +73,15 @@ static unsigned char parse_uchar(char *arg, char *value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Parameters args_parse(int argc, char **argv) {
|
Parameters args_parse(int argc, char **argv) {
|
||||||
Parameters params = {0, 0, false};
|
Parameters params;
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
char *arg;
|
char *arg;
|
||||||
char *value;
|
char *value;
|
||||||
|
|
||||||
|
params.screen = 0;
|
||||||
|
params.frag_path = 0;
|
||||||
|
params.hot_reload = false;
|
||||||
|
|
||||||
for (i = 1; i < argc; i++) {
|
for (i = 1; i < argc; i++) {
|
||||||
arg = argv[i];
|
arg = argv[i];
|
||||||
value = split_arg_value(arg);
|
value = split_arg_value(arg);
|
||||||
|
|||||||
+19
-5
@@ -1,3 +1,4 @@
|
|||||||
|
#include <GLFW/glfw3.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -22,6 +23,9 @@ bool file_should_update(File file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void file_update(File *file) {
|
void file_update(File *file) {
|
||||||
|
long length;
|
||||||
|
FILE *file_pointer;
|
||||||
|
|
||||||
// free remaining data
|
// free remaining data
|
||||||
if (file->content != 0) {
|
if (file->content != 0) {
|
||||||
free(file->content);
|
free(file->content);
|
||||||
@@ -30,9 +34,9 @@ void file_update(File *file) {
|
|||||||
// init empty file
|
// init empty file
|
||||||
file->content = 0;
|
file->content = 0;
|
||||||
file->error = false;
|
file->error = false;
|
||||||
long length;
|
|
||||||
// open file
|
// open file
|
||||||
FILE *file_pointer = fopen(file->path, "rb");
|
file_pointer = fopen(file->path, "rb");
|
||||||
if (file_pointer == NULL) {
|
if (file_pointer == NULL) {
|
||||||
file->error = true;
|
file->error = true;
|
||||||
log_error("Cannot open file '%s'", file->path);
|
log_error("Cannot open file '%s'", file->path);
|
||||||
@@ -61,18 +65,28 @@ void file_update(File *file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
File file_read(char *path) {
|
File file_read(char *path) {
|
||||||
File file = {path, 0, false, 0};
|
File file;
|
||||||
|
|
||||||
|
file.path = path;
|
||||||
|
file.content = NULL;
|
||||||
|
file.error = false;
|
||||||
|
file.last_write = 0;
|
||||||
|
|
||||||
file_update(&file);
|
file_update(&file);
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_prepend(File *src, File extra) {
|
void file_prepend(File *src, File extra) {
|
||||||
char *old_src_content = src->content;
|
char *old_src_content;
|
||||||
|
|
||||||
|
old_src_content = src->content;
|
||||||
src->content = strings_concat(extra.content, src->content);
|
src->content = strings_concat(extra.content, src->content);
|
||||||
free(old_src_content);
|
free(old_src_content);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_free(File *file) {
|
void file_free(File *file, bool free_path) {
|
||||||
free(file->content);
|
free(file->content);
|
||||||
|
if (free_path) {
|
||||||
free(file->path);
|
free(file->path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -11,6 +11,6 @@ void file_update(File *file);
|
|||||||
|
|
||||||
void file_prepend(File *src, File extra);
|
void file_prepend(File *src, File extra);
|
||||||
|
|
||||||
void file_free(File *file);
|
void file_free(File *file, bool free_path);
|
||||||
|
|
||||||
#endif /* FILE_H */
|
#endif /* FILE_H */
|
||||||
+8
-4
@@ -42,7 +42,9 @@ static int compute_fps(Window *window, Timer *timer) {
|
|||||||
static void hot_reload(ShaderProgram program, File *common_shader_code,
|
static void hot_reload(ShaderProgram program, File *common_shader_code,
|
||||||
File *fragment_shaders) {
|
File *fragment_shaders) {
|
||||||
int i;
|
int i;
|
||||||
bool force_update = false;
|
bool force_update;
|
||||||
|
|
||||||
|
force_update = false;
|
||||||
|
|
||||||
if (file_should_update(*common_shader_code)) {
|
if (file_should_update(*common_shader_code)) {
|
||||||
file_update(common_shader_code);
|
file_update(common_shader_code);
|
||||||
@@ -78,7 +80,9 @@ static void loop(Window *window, ShaderProgram program, bool hr,
|
|||||||
|
|
||||||
File read_fragment_shader_file(char *frag_path, int i) {
|
File read_fragment_shader_file(char *frag_path, int i) {
|
||||||
File fragment_shader;
|
File fragment_shader;
|
||||||
char *file_path = malloc(sizeof(char) * 1024);
|
char *file_path;
|
||||||
|
|
||||||
|
file_path = malloc(sizeof(char) * 1024);
|
||||||
|
|
||||||
sprintf(file_path, "%s/frag%d.glsl", frag_path, i);
|
sprintf(file_path, "%s/frag%d.glsl", frag_path, i);
|
||||||
fragment_shader = file_read(file_path);
|
fragment_shader = file_read(file_path);
|
||||||
@@ -108,10 +112,10 @@ static void free_files(File *common_shader_code, File *fragment_shaders) {
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < FRAG_COUNT; i++) {
|
for (i = 0; i < FRAG_COUNT; i++) {
|
||||||
file_free(&fragment_shaders[i]);
|
file_free(&fragment_shaders[i], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_free(common_shader_code);
|
file_free(common_shader_code, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void forge_run(Parameters params) {
|
void forge_run(Parameters params) {
|
||||||
|
|||||||
@@ -8,8 +8,12 @@
|
|||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
Parameters params;
|
Parameters params;
|
||||||
|
|
||||||
params = args_parse(argc, argv);
|
params = args_parse(argc, argv);
|
||||||
|
|
||||||
puts(PACKAGE " " VERSION);
|
puts(PACKAGE " " VERSION);
|
||||||
|
|
||||||
forge_run(params);
|
forge_run(params);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-4
@@ -188,9 +188,11 @@ static void init_single_program(ShaderProgram *program, int i, bool output) {
|
|||||||
|
|
||||||
ShaderProgram shaders_init(File *fragment_shaders, Context context) {
|
ShaderProgram shaders_init(File *fragment_shaders, Context context) {
|
||||||
int i;
|
int i;
|
||||||
ShaderProgram program = {.error = false,
|
ShaderProgram program;
|
||||||
.last_width = context.width,
|
|
||||||
.last_height = context.height};
|
program.error = false;
|
||||||
|
program.last_width = context.width;
|
||||||
|
program.last_height = context.height;
|
||||||
|
|
||||||
init_textures(&program, context);
|
init_textures(&program, context);
|
||||||
|
|
||||||
@@ -233,6 +235,7 @@ void shaders_update(ShaderProgram program, File *fragment_shaders, int i) {
|
|||||||
void shaders_apply(ShaderProgram program, Context context) {
|
void shaders_apply(ShaderProgram program, Context context) {
|
||||||
int i, j;
|
int i, j;
|
||||||
GLuint subroutines[3];
|
GLuint subroutines[3];
|
||||||
|
vec2 resolution;
|
||||||
|
|
||||||
// viewport changed
|
// viewport changed
|
||||||
if (context.width != program.last_width ||
|
if (context.width != program.last_width ||
|
||||||
@@ -248,7 +251,8 @@ void shaders_apply(ShaderProgram program, Context context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vec2 resolution = {(float)context.width, (float)context.height};
|
resolution[0] = (float)context.width;
|
||||||
|
resolution[1] = (float)context.height;
|
||||||
|
|
||||||
for (i = 0; i < FRAG_COUNT + 1; i++) {
|
for (i = 0; i < FRAG_COUNT + 1; i++) {
|
||||||
// use specific shader program
|
// use specific shader program
|
||||||
|
|||||||
+5
-3
@@ -2,10 +2,12 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
char *strings_concat(const char *s1, const char *s2) {
|
char *strings_concat(const char *s1, const char *s2) {
|
||||||
char *result =
|
char *result;
|
||||||
malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
|
|
||||||
// in real code you would check for errors in malloc here
|
result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
|
||||||
|
|
||||||
strcpy(result, s1);
|
strcpy(result, s1);
|
||||||
strcat(result, s2);
|
strcat(result, s2);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
+8
-4
@@ -3,10 +3,10 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
Timer timer_init(const unsigned int target) {
|
Timer timer_init(const unsigned int target) {
|
||||||
Timer output = {
|
Timer output;
|
||||||
.counter = 0,
|
|
||||||
.target = target,
|
output.counter = 0;
|
||||||
};
|
output.target = target;
|
||||||
|
|
||||||
gettimeofday(&output.start, NULL);
|
gettimeofday(&output.start, NULL);
|
||||||
|
|
||||||
@@ -21,11 +21,15 @@ bool timer_inc(Timer *timer) {
|
|||||||
double timer_reset(Timer *timer) {
|
double timer_reset(Timer *timer) {
|
||||||
struct timeval stop;
|
struct timeval stop;
|
||||||
double secs, per_secs;
|
double secs, per_secs;
|
||||||
|
|
||||||
gettimeofday(&stop, NULL);
|
gettimeofday(&stop, NULL);
|
||||||
|
|
||||||
secs = (double)(stop.tv_usec - timer->start.tv_usec) / 1000000 +
|
secs = (double)(stop.tv_usec - timer->start.tv_usec) / 1000000 +
|
||||||
(double)(stop.tv_sec - timer->start.tv_sec);
|
(double)(stop.tv_sec - timer->start.tv_sec);
|
||||||
per_secs = ((float)timer->counter) / secs;
|
per_secs = ((float)timer->counter) / secs;
|
||||||
|
|
||||||
timer->start = stop;
|
timer->start = stop;
|
||||||
timer->counter = 0;
|
timer->counter = 0;
|
||||||
|
|
||||||
return per_secs;
|
return per_secs;
|
||||||
}
|
}
|
||||||
+6
-3
@@ -33,9 +33,11 @@ static void init_glfw(void (*error_callback)(int, const char *)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static GLFWmonitor *get_monitor(unsigned char monitor_index) {
|
static GLFWmonitor *get_monitor(unsigned char monitor_index) {
|
||||||
// detect monitors
|
|
||||||
int count;
|
int count;
|
||||||
GLFWmonitor **monitors = glfwGetMonitors(&count);
|
GLFWmonitor **monitors;
|
||||||
|
|
||||||
|
// detect monitors
|
||||||
|
monitors = glfwGetMonitors(&count);
|
||||||
|
|
||||||
// check selected monitor availability
|
// check selected monitor availability
|
||||||
if (monitor_index >= count) {
|
if (monitor_index >= count) {
|
||||||
@@ -51,6 +53,7 @@ static GLFWmonitor *get_monitor(unsigned char monitor_index) {
|
|||||||
static GLFWwindow *create_window(GLFWmonitor *monitor, char *title,
|
static GLFWwindow *create_window(GLFWmonitor *monitor, char *title,
|
||||||
void (*key_callback)(Window *, int, int, int,
|
void (*key_callback)(Window *, int, int, int,
|
||||||
int)) {
|
int)) {
|
||||||
|
GLFWwindow *window;
|
||||||
|
|
||||||
log_info("[GLFW] Creating window...");
|
log_info("[GLFW] Creating window...");
|
||||||
|
|
||||||
@@ -61,7 +64,7 @@ static GLFWwindow *create_window(GLFWmonitor *monitor, char *title,
|
|||||||
glfwWindowHint(GLFW_FOCUS_ON_SHOW, GLFW_FALSE);
|
glfwWindowHint(GLFW_FOCUS_ON_SHOW, GLFW_FALSE);
|
||||||
|
|
||||||
// create fullscreen window in selected monitor
|
// create fullscreen window in selected monitor
|
||||||
GLFWwindow *window = glfwCreateWindow(1, 1, title, monitor, NULL);
|
window = glfwCreateWindow(1, 1, title, monitor, NULL);
|
||||||
|
|
||||||
// handle window creation fail
|
// handle window creation fail
|
||||||
if (window == NULL) {
|
if (window == NULL) {
|
||||||
|
|||||||
Reference in New Issue
Block a user