clarify code
This commit is contained in:
+35
-34
@@ -11,9 +11,9 @@
|
||||
#include "types.h"
|
||||
#include "window.h"
|
||||
|
||||
void error_callback(int error, const char *description) {
|
||||
static void error_callback(int error, const char *description) {
|
||||
log_error("[GLFW] %d: %s", error, description);
|
||||
close_window(0, true);
|
||||
window_close(0, true);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -21,58 +21,59 @@ static void key_callback(Window *window, int key,
|
||||
__attribute__((unused)) int scancode, int action,
|
||||
__attribute__((unused)) int mods) {
|
||||
// close window on escape key
|
||||
if (escape_key(key, action)) {
|
||||
close_window(window, false);
|
||||
if (window_escape_key(key, action)) {
|
||||
window_close(window, false);
|
||||
}
|
||||
}
|
||||
|
||||
int compute_fps(Window *window, Timer *timer) {
|
||||
static int compute_fps(Window *window, Timer *timer) {
|
||||
static double fps;
|
||||
char title[100];
|
||||
|
||||
if (inc_timer(timer)) {
|
||||
fps = reset_and_count(timer);
|
||||
if (timer_inc(timer)) {
|
||||
fps = timer_reset(timer);
|
||||
sprintf(title, PACKAGE " " VERSION " - %.0ffps", fps);
|
||||
update_window_title(window, title);
|
||||
window_update_title(window, title);
|
||||
}
|
||||
|
||||
return (int)round(fps);
|
||||
}
|
||||
|
||||
void hot_reload(ShaderProgram program, File *common_shader_code,
|
||||
File *fragment_shaders) {
|
||||
static void hot_reload(ShaderProgram program, File *common_shader_code,
|
||||
File *fragment_shaders) {
|
||||
int i;
|
||||
bool force_update = false;
|
||||
|
||||
if (should_update_file(*common_shader_code)) {
|
||||
update_file(common_shader_code);
|
||||
if (file_should_update(*common_shader_code)) {
|
||||
file_update(common_shader_code);
|
||||
force_update = true;
|
||||
}
|
||||
|
||||
for (i = 0; i < FRAG_COUNT; i++) {
|
||||
if (force_update || should_update_file(fragment_shaders[i])) {
|
||||
update_file(&fragment_shaders[i]);
|
||||
prepend_file(&fragment_shaders[i], *common_shader_code);
|
||||
update_program(program, fragment_shaders, i);
|
||||
if (force_update || file_should_update(fragment_shaders[i])) {
|
||||
file_update(&fragment_shaders[i]);
|
||||
file_prepend(&fragment_shaders[i], *common_shader_code);
|
||||
shaders_update(program, fragment_shaders, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void loop(Window *window, ShaderProgram program, bool hr,
|
||||
File *common_shader_code, File *fragment_shaders, Timer *timer) {
|
||||
static void loop(Window *window, ShaderProgram program, bool hr,
|
||||
File *common_shader_code, File *fragment_shaders,
|
||||
Timer *timer) {
|
||||
Context context;
|
||||
|
||||
if (hr) {
|
||||
hot_reload(program, common_shader_code, fragment_shaders);
|
||||
}
|
||||
|
||||
context = get_window_context(window);
|
||||
context = window_get_context(window);
|
||||
|
||||
context.fps = compute_fps(window, timer);
|
||||
|
||||
apply_program(program, context);
|
||||
shaders_apply(program, context);
|
||||
|
||||
refresh_window(window);
|
||||
window_refresh(window);
|
||||
}
|
||||
|
||||
File read_fragment_shader_file(char *frag_path, int i) {
|
||||
@@ -80,7 +81,7 @@ File read_fragment_shader_file(char *frag_path, int i) {
|
||||
char *file_path = malloc(sizeof(char) * 1024);
|
||||
|
||||
sprintf(file_path, "%s/frag%d.glsl", frag_path, i);
|
||||
fragment_shader = read_file(file_path);
|
||||
fragment_shader = file_read(file_path);
|
||||
if (fragment_shader.error) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -88,8 +89,8 @@ File read_fragment_shader_file(char *frag_path, int i) {
|
||||
return fragment_shader;
|
||||
}
|
||||
|
||||
void init_files(char *frag_path, File *common_shader_code,
|
||||
File *fragment_shaders) {
|
||||
static void init_files(char *frag_path, File *common_shader_code,
|
||||
File *fragment_shaders) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FRAG_COUNT + 1; i++) {
|
||||
@@ -98,19 +99,19 @@ void init_files(char *frag_path, File *common_shader_code,
|
||||
} else {
|
||||
fragment_shaders[i - 1] = read_fragment_shader_file(frag_path, i);
|
||||
|
||||
prepend_file(&fragment_shaders[i - 1], *common_shader_code);
|
||||
file_prepend(&fragment_shaders[i - 1], *common_shader_code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void free_files(File *common_shader_code, File *fragment_shaders) {
|
||||
static void free_files(File *common_shader_code, File *fragment_shaders) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FRAG_COUNT; i++) {
|
||||
free_file(&fragment_shaders[i]);
|
||||
file_free(&fragment_shaders[i]);
|
||||
}
|
||||
|
||||
free_file(common_shader_code);
|
||||
file_free(common_shader_code);
|
||||
}
|
||||
|
||||
void forge_run(Parameters params) {
|
||||
@@ -123,26 +124,26 @@ void forge_run(Parameters params) {
|
||||
|
||||
init_files(params.frag_path, &common_shader_code, fragment_shaders);
|
||||
|
||||
window = init_window(PACKAGE " " VERSION, params.screen, error_callback,
|
||||
window = window_init(PACKAGE " " VERSION, params.screen, error_callback,
|
||||
key_callback);
|
||||
|
||||
context = get_window_context(window);
|
||||
context = window_get_context(window);
|
||||
|
||||
program = init_program(fragment_shaders, context);
|
||||
program = shaders_init(fragment_shaders, context);
|
||||
|
||||
if (program.error) {
|
||||
close_window(window, true);
|
||||
window_close(window, true);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
timer = create_timer(30);
|
||||
timer = timer_init(30);
|
||||
|
||||
while (!window_should_close(window)) {
|
||||
loop(window, program, params.hot_reload, &common_shader_code,
|
||||
fragment_shaders, &timer);
|
||||
}
|
||||
|
||||
close_window(window, true);
|
||||
window_close(window, true);
|
||||
|
||||
free_files(&common_shader_code, fragment_shaders);
|
||||
}
|
||||
Reference in New Issue
Block a user