summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/rodeo.h28
-rw-r--r--include/rodeo_types.h39
-rw-r--r--src/rodeo.c105
-rw-r--r--src/rodeo_internal.h6
-rw-r--r--src/rodeo_internal_types.h19
-rw-r--r--src/rodeo_math.c36
6 files changed, 179 insertions, 54 deletions
diff --git a/include/rodeo.h b/include/rodeo.h
index b67970c..1adb258 100644
--- a/include/rodeo.h
+++ b/include/rodeo.h
@@ -3,7 +3,7 @@
// public
#include "rodeo_types.h"
-// system
+// -- system --
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
@@ -38,8 +38,14 @@ rodeo_color_RGBAFloat_to_RGBA8(const rodeo_color_RGBAFloat_t color);
rodeo_color_RGBAFloat_t
rodeo_color_RGBA8_to_RGBAFloat(const rodeo_color_RGBA8_t color);
-int32_t
-rodeo_random_simple_get(void);
+void
+rodeo_random_seed_set(uint64_t seed);
+
+double
+rodeo_random_double_get(void);
+
+uint64_t
+rodeo_random_uint64_get(void);
/// --- Core ---
@@ -60,14 +66,20 @@ mrodeo_window_do( \
void
rodeo_window_init(
- int screen_height,
- int screen_width,
- char* title
+ uint16_t screen_height,
+ uint16_t screen_width,
+ rodeo_string_t title
);
void
rodeo_window_deinit(void);
+uint16_t
+rodeo_screen_width_get(void);
+
+uint16_t
+rodeo_screen_height_get(void);
+
#define \
mrodeo_frame_do( \
state \
@@ -108,8 +120,8 @@ rodeo_texture_2d_default_get(void);
rodeo_texture_2d_t
rodeo_texture_2d_create_from_RGBA8(
- const uint32_t width,
- const uint32_t height,
+ const uint16_t width,
+ const uint16_t height,
const uint8_t memory[]
);
diff --git a/include/rodeo_types.h b/include/rodeo_types.h
index 18c3691..a75da40 100644
--- a/include/rodeo_types.h
+++ b/include/rodeo_types.h
@@ -8,7 +8,8 @@
typedef struct irodeo_texture_internal_t irodeo_texture_internal_t;
typedef irodeo_texture_internal_t *rodeo_texture_internal_p;
-typedef union
+typedef
+union
{
struct {
float red;
@@ -20,7 +21,8 @@ typedef union
}
rodeo_color_RGBAFloat_t;
-typedef union
+typedef
+union
{
struct
{
@@ -35,7 +37,8 @@ typedef union
rodeo_color_RGBA8_t;
-typedef struct
+typedef
+struct
{
float x;
float y;
@@ -55,7 +58,8 @@ typedef
void
(*rodeo_mainLoop_function)(void);
-typedef struct
+typedef
+struct
{
float x;
float y;
@@ -64,7 +68,8 @@ typedef struct
}
rodeo_rectangle_t;
-typedef struct
+typedef
+struct
{
rodeo_texture_internal_p internal_texture;
uint32_t width;
@@ -95,7 +100,8 @@ typedef union {
/// --- Log ---
-typedef enum
+typedef
+enum
{
rodeo_logLevel_info,
rodeo_logLevel_warning,
@@ -107,4 +113,25 @@ typedef
void
(*rodeo_log_function)(rodeo_string_t text);
+typedef
+union
+{
+ struct {
+ float x;
+ float y;
+ };
+ float array[2];
+}
+rodeo_vector2_t;
+typedef
+union
+{
+ struct {
+ float x;
+ float y;
+ float z;
+ };
+ float array[3];
+}
+rodeo_vector3_t;
diff --git a/src/rodeo.c b/src/rodeo.c
index fd344a9..7a31e40 100644
--- a/src/rodeo.c
+++ b/src/rodeo.c
@@ -14,10 +14,11 @@
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_syswm.h"
#include "bgfx/c99/bgfx.h"
-//#define CGLM_FORCE_LEFT_HANDED
+/*#define CGLM_FORCE_LEFT_HANDED*/
#define CGLM_FORCE_DEPTH_ZERO_TO_ONE
-//#define CGLM_CLIPSPACE_INCLUDE_ALL
+/*#define CGLM_CLIPSPACE_INCLUDE_ALL*/
#include "cglm/cglm.h"
+#include "stc/crandom.h"
// -- system --
#include <time.h>
@@ -26,9 +27,9 @@ static irodeo_state_t state = {0};
void
rodeo_window_init(
- int screen_height,
- int screen_width,
- char* title
+ uint16_t screen_height,
+ uint16_t screen_width,
+ rodeo_string_t title
)
{
state.window = NULL;
@@ -60,7 +61,7 @@ rodeo_window_init(
"Initializing SDL window..."
);
state.window = SDL_CreateWindow(
- title,
+ rodeo_string_to_constcstr(&title),
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
screen_width,
@@ -243,6 +244,8 @@ rodeo_window_init(
state.active_texture_p = &state.default_texture.internal_texture->texture_bgfx;
}
+ rodeo_random_seed_set(SDL_GetTicks64());
+
state.end_frame = SDL_GetPerformanceCounter();
}
@@ -260,6 +263,18 @@ rodeo_window_deinit(void)
SDL_Quit();
}
+uint16_t
+rodeo_screen_width_get(void)
+{
+ return state.screen_width;
+}
+
+uint16_t
+rodeo_screen_height_get(void)
+{
+ return state.screen_height;
+}
+
void
rodeo_frame_begin(void)
{
@@ -277,8 +292,8 @@ rodeo_frame_begin(void)
// but 'no' is incorrect
glm_ortho_rh_zo(
0,
- state.screen_width,
- state.screen_height,
+ (float)state.screen_width,
+ (float)state.screen_height,
0,
// near
-0.1f,
@@ -353,7 +368,9 @@ rodeo_debug_text_draw(u_int16_t x, u_int16_t y, const char *format, ...)
rodeo_string_t
rodeo_renderer_name_get(void)
{
- return rodeo_string_create(bgfx_get_renderer_name(bgfx_get_renderer_type()));
+ return rodeo_string_create(
+ bgfx_get_renderer_name(bgfx_get_renderer_type())
+ );
}
void
@@ -410,7 +427,7 @@ rodeo_renderer_flush(void)
);
// submit vertices & batches
- bgfx_submit(0, state.program_shader, 0, BGFX_DISCARD_ALL);
+ bgfx_submit(0, state.program_shader, 0, BGFX_DISCARD_NONE);
// reset arrays
state.vertex_size = 0;
@@ -428,8 +445,8 @@ rodeo_texture_2d_default_get(void)
rodeo_texture_2d_t
rodeo_texture_2d_create_from_RGBA8(
- const uint32_t width,
- const uint32_t height,
+ const uint16_t width,
+ const uint16_t height,
const uint8_t memory[]
)
{
@@ -437,14 +454,14 @@ rodeo_texture_2d_create_from_RGBA8(
texture.internal_texture = malloc(sizeof(irodeo_texture_internal_t));
texture.internal_texture->texture_bgfx =
bgfx_create_texture_2d(
- width,
- height,
- false,
- 0,
- BGFX_TEXTURE_FORMAT_RGBA8,
- BGFX_SAMPLER_UVW_CLAMP | BGFX_SAMPLER_MAG_POINT,
- bgfx_copy(memory, width * height * sizeof(uint8_t) * 4)
- );
+ width,
+ height,
+ false,
+ 0,
+ BGFX_TEXTURE_FORMAT_RGBA8,
+ BGFX_SAMPLER_UVW_CLAMP | BGFX_SAMPLER_MAG_POINT,
+ bgfx_copy(memory, (uint32_t)width * (uint32_t)height * sizeof(uint8_t) * 4)
+ );
texture.width = width;
texture.height = height;
@@ -492,10 +509,10 @@ rodeo_texture_2d_draw(
if(source != NULL && texture != NULL)
{
source_applied = (rodeo_rectangle_t){
- .x = source->x / texture->width,
- .y = source->y / texture->height,
- .width = source->width / texture->width,
- .height = source->height / texture->height,
+ .x = source->x / (float)texture->width,
+ .y = source->y / (float)texture->height,
+ .width = source->width / (float)texture->width,
+ .height = source->height / (float)texture->height,
};
}
else
@@ -589,7 +606,7 @@ rodeo_texture_2d_draw(
};
state.vertex_size += 1;
- int indices[] =
+ index_type_t indices[] =
{
0, 1, 3,
1, 2, 3
@@ -662,8 +679,8 @@ rodeo_texture_2d_create_from_path(rodeo_string_t path)
// load the pixel data into our own texture
rodeo_texture_2d_t texture = rodeo_texture_2d_create_from_RGBA8(
- converted_surface->w,
- converted_surface->h,
+ (uint16_t)converted_surface->w,
+ (uint16_t)converted_surface->h,
converted_surface->pixels
);
@@ -692,9 +709,27 @@ irodeo_shader_load(const rodeo_string_t path)
}
fseek(file, 0, SEEK_END);
- int64_t file_size = ftell(file);
+ int64_t file_size_temp = ftell(file);
+ if(file_size_temp < 0)
+ {
+ rodeo_log(
+ rodeo_logLevel_error,
+ "Failed to get current file position of given stream(ftell() error)"
+ );
+ exit(EXIT_FAILURE);
+ }
+ if((file_size_temp + 1) > UINT32_MAX)
+ {
+ rodeo_log(
+ rodeo_logLevel_error,
+ "File size larger then what bgfx can allocate"
+ );
+ exit(EXIT_FAILURE);
+ }
+ uint32_t file_size = (uint32_t)file_size_temp;
fseek(file, 0, SEEK_SET);
+
const bgfx_memory_t *mem = bgfx_alloc(file_size + 1);
fread(mem->data, 1, file_size, file);
mem->data[mem->size - 1] = '\0';
@@ -751,6 +786,18 @@ rodeo_frame_limit_get(void)
#endif
}
+void
+irodeo_random_seed_set(stc64_t seed)
+{
+ state.random_seed = seed;
+}
+
+stc64_t*
+irodeo_random_seed_get(void)
+{
+ return &state.random_seed;
+}
+
// measures how much time there is left in the remaining frame until
// the frame target time is reached. If we surpassed the target time
// then this will be negative
@@ -784,7 +831,7 @@ irodeo_frame_stall(void)
float stall_time = irodeo_frame_remaining_get();
if(stall_time > 0.0005)
{
- SDL_Delay(stall_time * 0.9995);
+ SDL_Delay((uint32_t)(stall_time * 0.9995));
}
// then we spinlock for the small remaining amount of time
diff --git a/src/rodeo_internal.h b/src/rodeo_internal.h
index 4939edd..5d78ebf 100644
--- a/src/rodeo_internal.h
+++ b/src/rodeo_internal.h
@@ -19,6 +19,12 @@ bgfx_shader_handle_t
irodeo_\
shader_load(const rodeo_string_t path);
+void
+irodeo_random_seed_set(stc64_t seed);
+
+stc64_t*
+irodeo_random_seed_get(void);
+
float
irodeo_frame_remaining_get(void);
diff --git a/src/rodeo_internal_types.h b/src/rodeo_internal_types.h
index 2c059e7..2719b07 100644
--- a/src/rodeo_internal_types.h
+++ b/src/rodeo_internal_types.h
@@ -5,21 +5,24 @@
#include "rodeo_config.h"
#include "rodeo_types.h"
-// -- system --
+// -- external --
#if __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include "SDL2/SDL.h"
#include "SDL2/SDL_syswm.h"
#include "bgfx/c99/bgfx.h"
+#include "stc/crandom.h"
+
+typedef uint16_t index_type_t;
typedef struct
{
SDL_Window* window;
SDL_Surface* screen_surface;
SDL_SysWMinfo wmi;
- int screen_width;
- int screen_height;
+ uint16_t screen_width;
+ uint16_t screen_height;
SDL_Event sdl_event;
bool quit;
@@ -27,10 +30,10 @@ typedef struct
bgfx_dynamic_vertex_buffer_handle_t vertex_buffer_handle;
bgfx_dynamic_index_buffer_handle_t index_buffer_handle;
rodeo_vertex_t batched_vertices[mrodeo_vertex_size_max];
- uint16_t vertex_size;
- uint16_t index_count;
- uint16_t index_size;
- uint16_t batched_indices[(mrodeo_vertex_size_max / 4) * 6];
+ index_type_t vertex_size;
+ index_type_t index_count;
+ index_type_t index_size;
+ index_type_t batched_indices[(mrodeo_vertex_size_max / 4) * 6];
rodeo_texture_2d_t default_texture;
bgfx_texture_handle_t *active_texture_p;
bgfx_shader_handle_t vertex_shader;
@@ -38,6 +41,8 @@ typedef struct
bgfx_program_handle_t program_shader;
bgfx_uniform_handle_t texture_uniforms[2];
+ stc64_t random_seed;
+
uint64_t frame_count;
uint64_t start_frame;
uint64_t end_frame;
diff --git a/src/rodeo_math.c b/src/rodeo_math.c
index 0e19a1e..0e421ed 100644
--- a/src/rodeo_math.c
+++ b/src/rodeo_math.c
@@ -2,12 +2,18 @@
// -- internal --
// public
#include "rodeo.h"
+// private
+#include "rodeo_internal.h"
// -- system --
#include <stdint.h>
+#include <math.h>
// -- external --
#include "SDL2/SDL.h"
+#include "stc/crandom.h"
+#define CGLM_FORCE_DEPTH_ZERO_TO_ONE
+#include "cglm/cglm.h"
// rounds to nearest rather then truncation
rodeo_color_RGBA8_t
@@ -32,17 +38,39 @@ rodeo_color_RGBA8_to_RGBAFloat(const rodeo_color_RGBA8_t color)
};
}
+void
+rodeo_random_seed_set(uint64_t seed)
+{
+ irodeo_random_seed_set(stc64_new(seed));
+}
+
double
-rodeo_random_simple_float_get(void)
+rodeo_random_double_get(void)
{
- return 0;
+ stc64_t *seed = irodeo_random_seed_get();
+ return stc64_randf(seed);
}
uint64_t
-rodeo_random_simple_uint64_get(void)
+rodeo_random_uint64_get(void)
{
- return 0;
+ stc64_t *seed = irodeo_random_seed_get();
+ return stc64_rand(seed);
}
+// need to test this, might be wrong
+/*
+rodeo_vector2_t
+rodeo_angle_to_vector2(float angle)
+{
+ rodeo_vector2_t result = { {1.0f, 0.0f} };
+ glm_vec2_rotate(
+ (float*)&(result.array),
+ angle * 2.0f * (float)GLM_PI,
+ (float*)&result.array
+ );
+ return result;
+}
+*/