summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/rodeo.c101
-rw-r--r--src/rodeo_internal.h2
-rw-r--r--src/rodeo_log.c62
-rw-r--r--src/rodeo_string.c59
4 files changed, 173 insertions, 51 deletions
diff --git a/src/rodeo.c b/src/rodeo.c
index b513bdf..37ba49c 100644
--- a/src/rodeo.c
+++ b/src/rodeo.c
@@ -32,15 +32,28 @@ rodeo_window_init(
state.screen_height = screen_height;
state.screen_width = screen_width;
- printf("SDL_Init...\n");
+ rodeo_log(
+ rodeo_loglevel_info,
+ "Initializing SDL..."
+ );
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
- printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
+ rodeo_log(
+ rodeo_loglevel_error,
+ "Failed to initialize SDL. SDL_Error: %s",
+ SDL_GetError()
+ );
exit(EXIT_FAILURE);
}
- printf("done\n");
+ rodeo_log(
+ rodeo_loglevel_info,
+ "Success initializing SDL"
+ );
- printf("SDL_CreateWindow...\n");
+ rodeo_log(
+ rodeo_loglevel_info,
+ "Initializing SDL window..."
+ );
state.window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
@@ -49,15 +62,21 @@ rodeo_window_init(
screen_height,
SDL_WINDOW_SHOWN
);
- printf("done\n");
-
if(state.window == NULL)
{
- printf("Window could not be created! SDL_Error %s\n", SDL_GetError());
+ rodeo_log(
+ rodeo_loglevel_error,
+ "Failed creating SDL window. SDL_Error %s",
+ SDL_GetError()
+ );
exit(EXIT_FAILURE);
}
+
#if !__EMSCRIPTEN__
- printf("SDL_VERSION...\n");
+ rodeo_log(
+ rodeo_loglevel_info,
+ "SDL setting up driver specific information..."
+ );
SDL_VERSION(&state.wmi.version);
if(
!SDL_GetWindowWMInfo(
@@ -66,10 +85,16 @@ rodeo_window_init(
)
)
{
- printf("SDL_Error %s\n", SDL_GetError());
+ rodeo_log(
+ rodeo_loglevel_error,
+ "SDL failed to get driver specific information. SDL Error: %s", SDL_GetError()
+ );
exit(EXIT_FAILURE);
}
- printf("done\n");
+ rodeo_log(
+ rodeo_loglevel_info,
+ "Success getting driver specific information"
+ );
bgfx_render_frame(-1);
#endif
@@ -119,36 +144,42 @@ rodeo_window_init(
// load shaders
//const char* shader_path = "???";
- rodeo_string_p shader_path = rodeo_string_create("???");
+ rodeo_string_t shader_path = rodeo_string_create("???");
switch(bgfx_get_renderer_type()) {
case BGFX_RENDERER_TYPE_NOOP:
- printf("Noop renderer error");
+ rodeo_log(
+ rodeo_loglevel_error,
+ "BGFX failed to get determine an appropriate renderer."
+ );
exit(EXIT_FAILURE);
case BGFX_RENDERER_TYPE_OPENGLES:
rodeo_string_set(
- shader_path,
+ &shader_path,
"shaders/100_es/"
);
break;
case BGFX_RENDERER_TYPE_VULKAN:
rodeo_string_set(
- shader_path,
+ &shader_path,
"shaders/spirv/"
);
break;
default:
- printf("No shaders for selected renderer. Exiting...");
+ rodeo_log(
+ rodeo_loglevel_error,
+ "No shaders compiled for BGFX renderer chosen."
+ );
exit(EXIT_FAILURE);
}
- rodeo_string_p vertex_shader_path = rodeo_string_create("simple.vertex.bin");
- rodeo_string_p fragment_shader_path = rodeo_string_create("simple.fragment.bin");
+ rodeo_string_t vertex_shader_path = rodeo_string_create("simple.vertex.bin");
+ rodeo_string_t fragment_shader_path = rodeo_string_create("simple.fragment.bin");
rodeo_string_prepend(
- vertex_shader_path,
+ &vertex_shader_path,
shader_path
);
rodeo_string_prepend(
- fragment_shader_path,
+ &fragment_shader_path,
shader_path
);
@@ -221,15 +252,15 @@ rodeo_frame_end(void)
void
rodeo_mainloop_run(
- rodeo_mainloop_func main_loop_function
+ rodeo_mainloop_function mainloop_func
)
{
#if __EMSCRIPTEN__
- emscripten_set_main_loop(main_loop_function, 0, 1);
+ emscripten_set_main_loop(main_loop_func, 0, 1);
#else
while(!rodeo_window_check_quit())
{
- main_loop_function();
+ mainloop_func();
}
#endif
}
@@ -249,13 +280,13 @@ rodeo_set_quit(bool quit)
void
rodeo_debug_text_draw(u_int16_t x, u_int16_t y, const char *format, ...)
{
- va_list argList;
- va_start(argList, format);
- bgfx_dbg_text_vprintf(x, y, 0x65, format, argList);
- va_end(argList);
+ mrodeo_vargs_do(format)
+ {
+ bgfx_dbg_text_vprintf(x, y, 0x65, format, vargs);
+ }
}
-rodeo_string_p
+rodeo_string_t
rodeo_renderer_name_get(void)
{
return rodeo_string_create(bgfx_get_renderer_name(bgfx_get_renderer_type()));
@@ -359,16 +390,20 @@ rodeo_rectangle_draw(
}
bgfx_shader_handle_t
-irodeo_shader_load(const rodeo_string_p path)
+irodeo_shader_load(const rodeo_string_t path)
{
- const char* path_cstr = rodeo_string_to_constcstr(path);
+ const char* path_cstr = rodeo_string_to_constcstr(&path);
bgfx_shader_handle_t invalid = BGFX_INVALID_HANDLE;
FILE *file = fopen(path_cstr, "rb");
if(!file)
{
- printf("Error: shader file \"%s\" not found\n", path_cstr);
+ rodeo_log(
+ rodeo_loglevel_error,
+ "Shader file \"%s\" not found",
+ path_cstr
+ );
return invalid;
}
@@ -382,7 +417,11 @@ irodeo_shader_load(const rodeo_string_p path)
fclose(file);
bgfx_shader_handle_t shader = bgfx_create_shader(mem);
- printf("Shader loaded as idx: %d\n", shader.idx);
+ rodeo_log(
+ rodeo_loglevel_info,
+ "Shader loaded with idx: %hu",
+ (uint8_t)shader.idx
+ );
return shader;
}
diff --git a/src/rodeo_internal.h b/src/rodeo_internal.h
index 56b188c..15e68d1 100644
--- a/src/rodeo_internal.h
+++ b/src/rodeo_internal.h
@@ -4,4 +4,4 @@
bgfx_shader_handle_t
irodeo_\
-shader_load(const rodeo_string_p path);
+shader_load(const rodeo_string_t path);
diff --git a/src/rodeo_log.c b/src/rodeo_log.c
new file mode 100644
index 0000000..0bce445
--- /dev/null
+++ b/src/rodeo_log.c
@@ -0,0 +1,62 @@
+
+// public external
+#include "rodeo_types.h"
+#include "rodeo.h"
+
+static rodeo_log_function logging_function = NULL;
+
+void
+rodeo_log(
+ rodeo_loglevel_t loglevel,
+ const char *format,
+ ...
+)
+{
+ rodeo_string_t formatted;
+ mrodeo_vargs_do(format)
+ {
+ formatted = rodeo_string_format(format, vargs);
+ }
+
+ switch(loglevel)
+ {
+ case rodeo_loglevel_info:
+ rodeo_string_prepend(
+ &formatted,
+ rodeo_string_create("[INFO]: ")
+ );
+ break;
+ case rodeo_loglevel_warning:
+ rodeo_string_prepend(
+ &formatted,
+ rodeo_string_create("\033[33m[WARN]:\033[0m ")
+ );
+ break;
+ case rodeo_loglevel_error:
+ rodeo_string_prepend(
+ &formatted,
+ rodeo_string_create("\033[31;1m[ERROR]:\033[0m ")
+ );
+ break;
+ }
+ rodeo_string_append(
+ &formatted,
+ rodeo_string_create("\n")
+ );
+
+ if(logging_function == NULL)
+ {
+ printf("%s", rodeo_string_to_constcstr(&formatted));
+ }
+ else
+ {
+ logging_function(formatted);
+ }
+}
+
+void
+rodeo_log_function_set(rodeo_log_function rodeo_log_func)
+{
+ logging_function = rodeo_log_func;
+}
+
diff --git a/src/rodeo_string.c b/src/rodeo_string.c
index 72baae7..a0a08a1 100644
--- a/src/rodeo_string.c
+++ b/src/rodeo_string.c
@@ -5,67 +5,68 @@
// external
#define i_implement
-#include <stc/cstr.h>
+#include "stc/cstr.h"
-typedef union cstr rodeo_string_t;
+// system
+#include <stdarg.h>
-rodeo_string_p
+rodeo_string_t
rodeo_string_create(const char *c_string)
{
- cstr* result = calloc(1, sizeof(cstr));
+ cstr result = cstr_NULL;
if(c_string != NULL)
{
- *result = cstr_from(c_string);
+ result = cstr_from(c_string);
}
- return (rodeo_string_p)result;
+ return *(rodeo_string_t*)(&result);
}
void
-rodeo_string_destroy(rodeo_string_p self)
+rodeo_string_destroy(rodeo_string_t *self)
{
cstr_drop((cstr*)self);
//free(string); // the above already calls free on the entire pointer
}
char*
-rodeo_string_to_cstr(rodeo_string_p self)
+rodeo_string_to_cstr(rodeo_string_t *self)
{
return cstr_data((cstr*)self);
}
const char*
-rodeo_string_to_constcstr(rodeo_string_p self)
+rodeo_string_to_constcstr(const rodeo_string_t *self)
{
return cstr_str((cstr*)self);
}
void
rodeo_string_insert(
- rodeo_string_p self,
- const rodeo_string_p insert,
+ rodeo_string_t *self,
+ const rodeo_string_t insert,
intptr_t position
)
{
- cstr_insert_s((cstr*)self, position, *(cstr*)insert);
+ cstr_insert_s((cstr*)self, position, *(cstr*)&insert);
}
void
rodeo_string_append(
- rodeo_string_p self,
- const rodeo_string_p append
+ rodeo_string_t *self,
+ const rodeo_string_t append
)
{
rodeo_string_insert(
self,
append,
- cstr_size((cstr*)self) - 1
+ cstr_size((cstr*)self)
);
}
void
rodeo_string_prepend(
- rodeo_string_p self,
- const rodeo_string_p prepend
+ rodeo_string_t *self,
+ const rodeo_string_t prepend
)
{
rodeo_string_insert(
@@ -76,16 +77,36 @@ rodeo_string_prepend(
}
void
-rodeo_string_clear(rodeo_string_p self)
+rodeo_string_clear(rodeo_string_t *self)
{
cstr_clear((cstr*)self);
}
void
-rodeo_string_set(rodeo_string_p self, char* value)
+rodeo_string_set(rodeo_string_t *self, char *value)
{
cstr_clear((cstr*)self);
cstr *temp = (cstr*)self;
*temp = cstr_from(value);
}
+rodeo_string_t
+rodeo_string_clone(const rodeo_string_t self)
+{
+ cstr temp = cstr_clone(*(cstr*)&self);
+ rodeo_string_t result = *(rodeo_string_t*)&temp;
+ return result;
+}
+
+rodeo_string_t
+rodeo_string_format(const char *format, ...)
+{
+ rodeo_string_t result;
+ mrodeo_vargs_do(format)
+ {
+ cstr temp = cstr_from_fmt(format, vargs);
+ result = *(rodeo_string_t*)&temp;
+ }
+ return result;
+}
+