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_number(char *value) {
|
||||
unsigned long value_len;
|
||||
unsigned int i;
|
||||
if (value == NULL) {
|
||||
return false;
|
||||
}
|
||||
unsigned long value_len = strlen(value);
|
||||
unsigned int i;
|
||||
value_len = strlen(value);
|
||||
for (i = 0; i < value_len; i++) {
|
||||
if (!is_digit(value[i])) {
|
||||
return false;
|
||||
@@ -72,11 +73,15 @@ static unsigned char parse_uchar(char *arg, char *value) {
|
||||
}
|
||||
|
||||
Parameters args_parse(int argc, char **argv) {
|
||||
Parameters params = {0, 0, false};
|
||||
|
||||
Parameters params;
|
||||
int i;
|
||||
char *arg;
|
||||
char *value;
|
||||
|
||||
params.screen = 0;
|
||||
params.frag_path = 0;
|
||||
params.hot_reload = false;
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
arg = argv[i];
|
||||
value = split_arg_value(arg);
|
||||
|
||||
+20
-6
@@ -1,3 +1,4 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -22,6 +23,9 @@ bool file_should_update(File file) {
|
||||
}
|
||||
|
||||
void file_update(File *file) {
|
||||
long length;
|
||||
FILE *file_pointer;
|
||||
|
||||
// free remaining data
|
||||
if (file->content != 0) {
|
||||
free(file->content);
|
||||
@@ -30,9 +34,9 @@ void file_update(File *file) {
|
||||
// init empty file
|
||||
file->content = 0;
|
||||
file->error = false;
|
||||
long length;
|
||||
|
||||
// open file
|
||||
FILE *file_pointer = fopen(file->path, "rb");
|
||||
file_pointer = fopen(file->path, "rb");
|
||||
if (file_pointer == NULL) {
|
||||
file->error = true;
|
||||
log_error("Cannot open file '%s'", file->path);
|
||||
@@ -61,18 +65,28 @@ void file_update(File *file) {
|
||||
}
|
||||
|
||||
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);
|
||||
return file;
|
||||
}
|
||||
|
||||
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);
|
||||
free(old_src_content);
|
||||
}
|
||||
|
||||
void file_free(File *file) {
|
||||
void file_free(File *file, bool free_path) {
|
||||
free(file->content);
|
||||
free(file->path);
|
||||
if (free_path) {
|
||||
free(file->path);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -11,6 +11,6 @@ void file_update(File *file);
|
||||
|
||||
void file_prepend(File *src, File extra);
|
||||
|
||||
void file_free(File *file);
|
||||
void file_free(File *file, bool free_path);
|
||||
|
||||
#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,
|
||||
File *fragment_shaders) {
|
||||
int i;
|
||||
bool force_update = false;
|
||||
bool force_update;
|
||||
|
||||
force_update = false;
|
||||
|
||||
if (file_should_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 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);
|
||||
fragment_shader = file_read(file_path);
|
||||
@@ -108,10 +112,10 @@ static void free_files(File *common_shader_code, File *fragment_shaders) {
|
||||
int 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) {
|
||||
|
||||
@@ -8,8 +8,12 @@
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Parameters params;
|
||||
|
||||
params = args_parse(argc, argv);
|
||||
|
||||
puts(PACKAGE " " VERSION);
|
||||
|
||||
forge_run(params);
|
||||
|
||||
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) {
|
||||
int i;
|
||||
ShaderProgram program = {.error = false,
|
||||
.last_width = context.width,
|
||||
.last_height = context.height};
|
||||
ShaderProgram program;
|
||||
|
||||
program.error = false;
|
||||
program.last_width = context.width;
|
||||
program.last_height = context.height;
|
||||
|
||||
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) {
|
||||
int i, j;
|
||||
GLuint subroutines[3];
|
||||
vec2 resolution;
|
||||
|
||||
// viewport changed
|
||||
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++) {
|
||||
// use specific shader program
|
||||
|
||||
+5
-3
@@ -2,10 +2,12 @@
|
||||
#include <string.h>
|
||||
|
||||
char *strings_concat(const char *s1, const char *s2) {
|
||||
char *result =
|
||||
malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
|
||||
// in real code you would check for errors in malloc here
|
||||
char *result;
|
||||
|
||||
result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
|
||||
|
||||
strcpy(result, s1);
|
||||
strcat(result, s2);
|
||||
|
||||
return result;
|
||||
}
|
||||
+8
-4
@@ -3,10 +3,10 @@
|
||||
#include "types.h"
|
||||
|
||||
Timer timer_init(const unsigned int target) {
|
||||
Timer output = {
|
||||
.counter = 0,
|
||||
.target = target,
|
||||
};
|
||||
Timer output;
|
||||
|
||||
output.counter = 0;
|
||||
output.target = target;
|
||||
|
||||
gettimeofday(&output.start, NULL);
|
||||
|
||||
@@ -21,11 +21,15 @@ bool timer_inc(Timer *timer) {
|
||||
double timer_reset(Timer *timer) {
|
||||
struct timeval stop;
|
||||
double secs, per_secs;
|
||||
|
||||
gettimeofday(&stop, NULL);
|
||||
|
||||
secs = (double)(stop.tv_usec - timer->start.tv_usec) / 1000000 +
|
||||
(double)(stop.tv_sec - timer->start.tv_sec);
|
||||
per_secs = ((float)timer->counter) / secs;
|
||||
|
||||
timer->start = stop;
|
||||
timer->counter = 0;
|
||||
|
||||
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) {
|
||||
// detect monitors
|
||||
int count;
|
||||
GLFWmonitor **monitors = glfwGetMonitors(&count);
|
||||
GLFWmonitor **monitors;
|
||||
|
||||
// detect monitors
|
||||
monitors = glfwGetMonitors(&count);
|
||||
|
||||
// check selected monitor availability
|
||||
if (monitor_index >= count) {
|
||||
@@ -51,6 +53,7 @@ static GLFWmonitor *get_monitor(unsigned char monitor_index) {
|
||||
static GLFWwindow *create_window(GLFWmonitor *monitor, char *title,
|
||||
void (*key_callback)(Window *, int, int, int,
|
||||
int)) {
|
||||
GLFWwindow *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);
|
||||
|
||||
// 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
|
||||
if (window == NULL) {
|
||||
|
||||
Reference in New Issue
Block a user