Minor code layout changes

This commit is contained in:
gingerBill 2016-05-21 00:53:05 +01:00
parent 679a82c2d4
commit 8d68c46340
4 changed files with 77 additions and 44 deletions

View File

@ -4,9 +4,9 @@ gb single-file public domain libraries for C & C++
library | latest version | category | description
----------------|----------------|----------|-------------
**gb.h** | 0.16 | misc | Helper library (Standard library _improvement_)
**gb_math.h** | 0.06c | math | Vector math library geared towards game development
**gb_gl.h** | 0.04c | graphics | OpenGL Helper Library
**gb.h** | 0.16a | misc | Helper library (Standard library _improvement_)
**gb_math.h** | 0.06d | math | Vector math library geared towards game development
**gb_gl.h** | 0.04d | graphics | OpenGL Helper Library
**gb_string.h** | 0.95 | strings | A better string library (this is built into gb.h too with custom allocator support!)
**gb_ini.h** | 0.93 | misc | Simple ini file loader library
**gb_regex.h** | 0.01c | regex | Highly experimental regular expressions library

85
gb.h
View File

@ -1,4 +1,4 @@
/* gb.h - v0.16 - Ginger Bill's C Helper Library - public domain
/* gb.h - v0.16a - Ginger Bill's C Helper Library - public domain
- no warranty implied; use at your own risk
This is a single header file with a bunch of useful stuff
@ -229,7 +229,6 @@ extern "C" {
/* TODO(bill): How many of these headers do I really need? */
#include <stdarg.h>
#include <stddef.h>
#include <math.h> /* fmod */
#if defined(GB_SYSTEM_WINDOWS)
#define NOMINMAX 1
@ -706,9 +705,8 @@ GB_DEF isize gb_pointer_diff (void const *begin, void const *end);
GB_DEF void gb_zero_size(void *ptr, isize size);
/* TODO(bill): Should gb_zero_struct be renamed to gb_zero_elem(ent)? */
#ifndef gb_zero_struct
#define gb_zero_struct(t) gb_zero_size((t), gb_size_of(*(t))) /* NOTE(bill): Pass pointer of struct */
#ifndef gb_zero_item
#define gb_zero_item(t) gb_zero_size((t), gb_size_of(*(t))) /* NOTE(bill): Pass pointer of struct */
#define gb_zero_array(a, count) gb_zero_size((a), gb_size_of(*(a))*count)
#endif
@ -1986,8 +1984,8 @@ typedef struct gbWindow {
#endif
void *memory;
isize memory_size;
i32 pitch;
i32 bits_per_pixel;
i32 pitch;
i32 bits_per_pixel;
} software;
};
} gbWindow;
@ -3167,7 +3165,7 @@ gb_mutex_unlock(gbMutex *m)
void
gb_thread_init(gbThread *t)
{
gb_zero_struct(t);
gb_zero_item(t);
#if defined(GB_SYSTEM_WINDOWS)
t->win32_handle = INVALID_HANDLE_VALUE;
#else
@ -3637,7 +3635,7 @@ gb_pool_init_align(gbPool *pool, gbAllocator backing, isize num_blocks, isize bl
void *data, *curr;
uintptr *end;
gb_zero_struct(pool);
gb_zero_item(pool);
pool->backing = backing;
pool->block_size = block_size;
@ -7113,13 +7111,44 @@ gb_random_range_i64(gbRandom *r, i64 lower_inc, i64 higher_inc)
return i;
}
/* NOTE(bill): Semi-cc'ed from gb_math to remove need for fmod */
f64
gb__copy_sign64(f64 x, f64 y)
{
i64 ix, iy;
ix = *(i64 *)&x;
iy = *(i64 *)&y;
ix &= 0x7fffffffffffffff;
ix |= iy & 0x8000000000000000;
return *cast(f64 *)&ix;
}
f64 gb__floor64 (f64 x) { return cast(f64)((x >= 0.0) ? cast(i64)x : cast(i64)(x-0.9999999999999999)); }
f64 gb__ceil64 (f64 x) { return cast(f64)((x < 0) ? cast(i64)x : (cast(i64)x)+1); }
f64 gb__round64 (f64 x) { return cast(f64)((x >= 0.0) ? gb__floor64(x + 0.5) : gb__ceil64(x - 0.5)); }
f64 gb__remainder64(f64 x, f64 y) { return x - (gb__round64(x/y)*y); }
f64 gb__abs64 (f64 x) { return x < 0 ? -x : x; }
f64 gb__sign64 (f64 x) { return x < 0 ? -1.0 : +1.0; }
f64
gb__mod64(f64 x, f64 y)
{
f64 result;
y = gb__abs64(y);
result = gb__remainder64(gb__abs64(x), y);
if (gb__sign64(result)) result += y;
return gb__copy_sign64(result, x);
}
f64
gb_random_range_f64(gbRandom *r, f64 lower_inc, f64 higher_inc)
{
u64 u = gb_random_next(r);
f64 f = *cast(f64 *)&u;
f64 diff = higher_inc-lower_inc+1.0f;
f = fmod(f, diff); /* TODO(bill): Replace fmod, maybe... */
f64 diff = higher_inc-lower_inc+1.0;
f = gb__mod64(f, diff); /* TODO(bill): Replace fmod, maybe... */
f += lower_inc;
return f;
}
@ -7297,7 +7326,7 @@ gb__window_resize_dib_section(gbWindow *window, i32 width, i32 height)
void
gb_platform_init(gbPlatform *p)
{
gb_zero_struct(p);
gb_zero_item(p);
{ /* Load XInput */
gbDllHandle xinput_library = gb_dll_load("xinput1_4.dll");
@ -7675,6 +7704,8 @@ gb__win32_main_window_callback(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
LRESULT result = 0;
gbWindow *window = cast(gbWindow *)GetWindowLongPtr(wnd, GWLP_USERDATA);
/* TODO(bill): Do more in here? */
switch (msg) {
case WM_CLOSE:
case WM_DESTROY:
@ -7694,14 +7725,13 @@ gb__win32_main_window_callback(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
gbWindow *
gb_window_init(gbPlatform *p, char const *title, gbVideoMode mode, u32 flags)
{
gbWindow *window = NULL;
gbWindow *window = &p->window;
WNDCLASSEXW wc = {gb_size_of(WNDCLASSEXW)};
DWORD ex_style, style;
RECT wr;
char16 title_buffer[256] = {0}; /* TODO(bill): gb_local_persist this? */
window = &p->window;
gb_zero_struct(window);
gb_zero_item(window);
wc.style = CS_HREDRAW | CS_VREDRAW; /* | CS_OWNDC */
wc.lpfnWndProc = gb__win32_main_window_callback;
@ -7823,7 +7853,7 @@ gb_window_destroy(gbWindow *w)
DestroyWindow(cast(HWND)w->handle);
gb_zero_struct(w);
gb_zero_item(w);
}
@ -7924,13 +7954,6 @@ gb_video_mode_bits(i32 width, i32 height, i32 bits_per_pixel)
}
b32
gb_video_mode_is_valid(gbVideoMode mode)
{
gbVideoMode modes[256];
gb_video_mode_get_fullscreen_modes(modes, gb_count_of(modes));
return gb_binary_search_array(modes, gb_count_of(modes), &mode, gb_video_mode_cmp) == -1;
}
gbVideoMode
gb_video_mode_get_desktop(void)
@ -7940,7 +7963,7 @@ gb_video_mode_get_desktop(void)
return gb_video_mode_bits(win32_mode.dmPelsWidth, win32_mode.dmPelsHeight, win32_mode.dmBitsPerPel);
}
void
void
gb_video_mode_get_fullscreen_modes(gbVideoMode *modes, isize max_mode_count)
{
DEVMODE win32_mode = {gb_size_of(win32_mode)};
@ -7954,6 +7977,17 @@ gb_video_mode_get_fullscreen_modes(gbVideoMode *modes, isize max_mode_count)
gb_sort_array(modes, i, gb_video_mode_inv_cmp);
}
#endif
b32
gb_video_mode_is_valid(gbVideoMode mode)
{
gbVideoMode modes[256];
gb_video_mode_get_fullscreen_modes(modes, gb_count_of(modes));
return gb_binary_search_array(modes, gb_count_of(modes), &mode, gb_video_mode_cmp) == -1;
}
GB_COMPARE_PROC(gb_video_mode_cmp)
{
gbVideoMode const *x = cast(gbVideoMode const *)a;
@ -7972,9 +8006,6 @@ GB_COMPARE_PROC(gb_video_mode_inv_cmp)
return -gb_video_mode_cmp(a, b);
}
#endif
#endif /* GB_PLATFORM */

19
gb_gl.h
View File

@ -1,4 +1,4 @@
/* gb.h - v0.04c - OpenGL Helper Library - public domain
/* gb.h - v0.04d - OpenGL Helper Library - public domain
- no warranty implied; use at your own risk
This is a single header file with a bunch of useful stuff
@ -55,6 +55,7 @@ Conventions used:
Version History:
0.04d - Use new gb.h file handling system
0.04c - Use new gb.h file handling system
0.04b - Work with the new gb.h
0.04a - Better Documentation
@ -1009,7 +1010,7 @@ gbgl_load_shader_from_file(gbglShader *shader, u32 type_bits, char const *filena
b32 loaded_shader[GBGL_SHADER_TYPE_COUNT] = {0};
i32 i;
gb_zero_struct(shader);
gb_zero_item(shader);
shader->type_flags = type_bits;
gb_strncpy(shader->base_name, filename, gb_size_of(shader->base_name));
@ -1034,7 +1035,7 @@ gbgl_load_shader_from_memory_vf(gbglShader *s, char const *vert_source, char con
{
gbglShaderError err = GBGL_SHADER_ERROR_NONE;
gb_zero_struct(s);
gb_zero_item(s);
s->type_flags = GB_BIT(GBGL_SHADER_TYPE_VERTEX) | GB_BIT(GBGL_SHADER_TYPE_FRAGMENT);
err = gbgl__load_single_shader_from_memory(s, GBGL_SHADER_TYPE_VERTEX, vert_source);
@ -1052,7 +1053,7 @@ gbgl_load_shader_from_memory_vfg(gbglShader *s, char const *vert_source, char co
{
gbglShaderError err = GBGL_SHADER_ERROR_NONE;
gb_zero_struct(s);
gb_zero_item(s);
s->type_flags = GB_BIT(GBGL_SHADER_TYPE_VERTEX) | GB_BIT(GBGL_SHADER_TYPE_FRAGMENT) | GB_BIT(GBGL_SHADER_TYPE_GEOMETRY);
err = gbgl__load_single_shader_from_memory(s, GBGL_SHADER_TYPE_VERTEX, vert_source);
@ -1227,7 +1228,7 @@ gbgl_init_render_buffer(gbglRenderBuffer *rb, i32 width, i32 height, i32 channel
{
if ((rb->width == width) && (rb->height == height) && (rb->channel_count == channel_count)) return true;
gbgl_destroy_render_buffer(rb);
gb_zero_struct(rb);
gb_zero_item(rb);
rb->width = width;
rb->height = height;
@ -1297,7 +1298,7 @@ gbgl_load_texture2d_from_memory(gbglTexture *tex, void const *data, i32 width, i
{
b32 result = true;
gb_zero_struct(tex);
gb_zero_item(tex);
tex->width = width;
tex->height = height;
@ -1570,7 +1571,7 @@ gbgl_cache_font(gbglFontCache *fc, char const *ttf_filename, f32 font_size)
return NULL;
}
gb_zero_struct(f);
gb_zero_item(f);
// NOTE(bill): Make sure the character list file has been loaded
if (!fc->font_char_list) {
@ -1626,7 +1627,7 @@ gbgl_cache_font(gbglFontCache *fc, char const *ttf_filename, f32 font_size)
GB_ASSERT_NOT_NULL(*ttf_cache);
(*ttf_cache)->name = NULL;
(*ttf_cache)->ttf = NULL;
gb_zero_struct(&(*ttf_cache)->finfo);
gb_zero_item(&(*ttf_cache)->finfo);
(*ttf_cache)->next = NULL;
@ -1749,7 +1750,7 @@ gbgl_cache_font(gbglFontCache *fc, char const *ttf_filename, f32 font_size)
}
} else {
GB_PANIC("Failure loading font");
gb_zero_struct(&f);
gb_zero_item(&f);
}
}
return f;

View File

@ -1,8 +1,9 @@
/* gb_math.h - v0.06c - public domain C math library - no warranty implied; use at your own risk
/* gb_math.h - v0.06d - public domain C math library - no warranty implied; use at your own risk
A C math library geared towards game development
use '#define GB_MATH_IMPLEMENTATION' before including to create the implementation in _ONE_ file
Version History:
0.06d - Bug fix
0.06c - Remove extra needed define for C++ and inline all operators
0.06b - Just formatting
0.06a - Implement rough versions of mod, remainder, copy_sign
@ -797,7 +798,7 @@ gb_copy_sign(float x, float y)
ix &= 0x7fffffff;
ix |= iy & 0x80000000;
return *(float *)ix;
return *(float *)&ix;
}
float
@ -1014,9 +1015,9 @@ float gb_fast_exp2(float x) { return gb_fast_exp(GB_MATH_LOG_TWO * x); }
float gb_round(float x) { return (x >= 0.0f) ? gb_floor(x + 0.5f) : gb_ceil(x - 0.5f); }
float gb_floor(float x) { return (x >= 0.0f) ? (int)x : (int)(x-0.9999999999999999f); }
float gb_ceil(float x) { return (x < 0) ? (int)x : ((int)x)+1; }
float gb_round(float x) { return (float)((x >= 0.0f) ? gb_floor(x + 0.5f) : gb_ceil(x - 0.5f)); }
float gb_floor(float x) { return (float)((x >= 0.0f) ? (int)x : (int)(x-0.9999999999999999f)); }
float gb_ceil(float x) { return (float)((x < 0) ? (int)x : ((int)x)+1); }