X-Git-Url: https://harrygodden.com/git/?p=vg.git;a=blobdiff_plain;f=vg_platform.h;h=319f7a9abd1892e2fb4b8d03860484aa4c5725a0;hp=295f03f71703b24f081b638a5090340b32d3706d;hb=HEAD;hpb=545783c1d937b2bed42e0555c9343fa00b8b7a22 diff --git a/vg_platform.h b/vg_platform.h index 295f03f..b846b06 100644 --- a/vg_platform.h +++ b/vg_platform.h @@ -1,13 +1,21 @@ -#ifndef VG_PLATFORM_H -#define VG_PLATFORM_H +#pragma once -//#include "vg.h" -#include "vg_stdint.h" - -/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ +#include +#include + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef float f32; +typedef double f64; +typedef uint8_t bool; typedef unsigned int uint; - typedef int v2i[2]; typedef int v3i[3]; typedef int v4i[4]; @@ -20,258 +28,15 @@ typedef v3f m4x3f[4]; typedef v4f m4x4f[4]; typedef v3f boxf[2]; -// Resource types -typedef struct vg_tex2d vg_tex2d; - -struct vg_achievement -{ - int is_set; - const char *name; -}; - -#define vg_static_assert _Static_assert -#define vg_list_size( A ) (sizeof(A)/sizeof(A[0])) -#define VG_MUST_USE_RESULT __attribute__((warn_unused_result)) - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -enum strncpy_behaviour{ - k_strncpy_always_add_null = 0, - k_strncpy_allow_cutoff = 1, - k_strncpy_overflow_fatal = 2 -}; - -static void vg_fatal_error( const char *fmt, ... ); -static u32 vg_strncpy( const char *src, char *dst, u32 len, - enum strncpy_behaviour behaviour ) -{ - for( u32 i=0; ilen == -1 ){ - if( str->buffer ){ - vg_str_dynamic *arr = (vg_str_dynamic *)str->buffer; - return (arr-1)->len; - } - else return 0; - } - else return str->len; -} - -/* - * Reset string. If len is -1 (dynamically allocated), buffer must be either - * NULL or be acquired from malloc or realloc - */ -static void vg_strnull( vg_str *str, char *buffer, i32 len ){ - str->buffer = buffer; - if( buffer ) - str->buffer[0] = '\0'; - - str->i = 0; - str->len = len; - - assert(len); -} - -static void vg_strfree( vg_str *str ){ - if( str->len == -1 ){ - if( str->buffer ){ - vg_str_dynamic *arr = (vg_str_dynamic *)str->buffer; - free( arr-1 ); - - str->buffer = NULL; - str->i = 0; - } - } -} - -/* - * Double the size of the dynamically allocated string. If unallocated, alloc of - * 16 bytes minimum. - */ -static i32 vg_str_dynamic_grow( vg_str *str ){ - if( str->buffer ){ - vg_str_dynamic *hdr = ((vg_str_dynamic *)str->buffer) - 1; - i32 total = (hdr->len + sizeof(vg_str_dynamic)) * 2; - hdr = realloc( hdr, total ); - hdr->len = total - sizeof(vg_str_dynamic); - str->buffer = (char *)(hdr+1); - return hdr->len; - } - else { - vg_str_dynamic *hdr = malloc(16); - hdr->len = 16-sizeof(vg_str_dynamic); - str->buffer = (char *)(hdr+1); - str->buffer[0] = '\0'; - return hdr->len; - } -} - -/* - * Append null terminated string to vg_str - */ -static void vg_strcat( vg_str *str, const char *append ){ - if( !append || (str->i == -1) ) return; - - i32 max = vg_str_storage( str ), - i = 0; - -append: - if( str->i == max ){ - if( str->len == -1 ) - max = vg_str_dynamic_grow( str ); - else{ - str->i = -1; - str->buffer[ max-1 ] = '\0'; - return; - } - } - - char c = append[ i ++ ]; - str->buffer[ str->i ] = c; - - if( c == '\0' ) - return; - - str->i ++; - goto append; -} - -/* - * Append character to vg_str - */ -static void vg_strcatch( vg_str *str, char c ){ - vg_strcat( str, (char[]){ c, '\0' } ); -} - -/* - * FIXME: Negative numbers - */ -static void vg_strcati32( vg_str *str, i32 value ){ - if( value ){ - char temp[32]; - int i=0; - while( value && (i<31) ){ - temp[ i ++ ] = '0' + (value % 10); - value /= 10; - } - - char reverse[32]; - for( int j=0; j=n ) - break; +/* anything compiled against VG shall implement this function somewhere. */ +void vg_fatal_error( const char *fmt, ... ); - temp[ n-1 - (i ++) ] = '0' + (value % 10); - value /= 10; +#define VG_ASSERT( ITEM, ... ) \ + if( !( ITEM ) ) { \ + vg_fatal_error( "Assertion failed: " VG_LOG_MCSTR(ITEM) "\n" \ + VG_LOG_WHERE ); \ } - for( ;ii == -1 ) return 0; - else return 1; -} - -/* - * Returns pointer to first instance of character - */ -static char *vg_strch( vg_str *str, char c ){ - char *ptr = NULL; - for( i32 i=0; ii; i++ ){ - if( str->buffer[i] == c ) - ptr = str->buffer+i; - } - - return ptr; -} - -static u32 vg_strdjb2( const char *str ){ - u32 hash = 5381, c; - - while( (c = *str++) ) - hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ - - return hash; -} - -static int vg_strdjb2_eq( const char *s1, u32 h1, - const char *s2, u32 h2 ) -{ - if( h1 == h2 ){ - if(!strcmp(s1, s2)) return 1; - else return 0; - } else return 0; -} - -#define VG_STRDJB2_EQ( CS1, S2, H2 ) \ - vg_strdjb2_eq( CS1, vg_strdjb2(CS1), S2, H2 ) - - #define VG_MIN( A, B ) ((A)<(B)?(A):(B)) #define VG_MAX( A, B ) ((A)>(B)?(A):(B)) -#endif +#define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))