randygaul / cute_headers Goto Github PK
View Code? Open in Web Editor NEWCollection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
Collection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
In TinyFiles I had to replace the line 54 with
#if defined( _WIN32 ) || defined ( _WIN64 )
for 64 bits compiling (on MVSC2015 and 2017 for sure).
You should add this on other header too.
It's not really important but it could be nice.
And thank's a lot for the work
Could this be a thing? I see the default background color is set to white. Seems like it would be fairly trivial to add this as an argument to the tdMakeAtlas method. Thoughts? I could issue a PR if this sounds reasonable.
tsMix has a mix (pun intended) of portable and Window's specific code. I'm open to ports to other systems and should encourage this by a little more code separation.
In line 115:
#elif TF_PLATFORM == TF_MAC || TN_PLATFORM == TN_UNIX
should be:
#elif TF_PLATFORM == TF_MAC || TF_PLATFORM == TF_UNIX
Same in line 278 and 345
Hi
i tried to compile tinysound example (sdl one) , but tons of errors appears !
does mingw considered as supported compiler for this library ?
I can't figure out how I am supposed to correctly resolve a collision between a c2Poly
and a c2Circle
.
For c2Circle
vs c2AABB
the following code worked:
c2CircletoAABBManifold(circle, aabb, &manifold);
circle.p.x += manifold.normal.x * manifold.depths[0];
circle.p.y += manifold.normal.y * manifold.depths[0];
Everything works as I expect and there is no 'jitter' when moving along the edges of the c2AABB
. The same code also works for c2AABB
vs c2Poly
(except the problem below).
However if I do the same with the c2Poly
(Note that I also have to substract this time):
c2CircletoPolyManifold(circle, poly, &manifold);
circle.p.x -= manifold.normal.x * manifold.depths[0];
circle.p.y -= manifold.normal.y * manifold.depths[0];
Then the c2Circle
'jumps' off the edges of the c2Poly
(Using a c2AABB
instead it works though):
Using just:
circle.p.x -= manifold.normal.x;
circle.p.y -= manifold.normal.y;
Works better but the movement of the c2Circle
is 'jittery' along angled edges of the c2Poly
(difficult to see in the recording).
Furthermore the c2AABB
will jump around if one of its edges is moved towards a corner of the c2Poly
:
Used c2Poly
:
poly.count = 4;
poly.verts[0] = c2V(30, 30);
poly.verts[1] = c2V(130, 40);
poly.verts[2] = c2V(100, 80);
poly.verts[3] = c2V(40, 60);
c2MakePoly(&poly);
c2CircletoRay collision seems to be always broken.
My c2Ray & c2Circle:
c2Ray ray;
ray.p = c2V(100, 100);
ray.d = c2V(100, 100);
ray.t = 1;
c2Circle circle;
circle.r = 30;
circle.p = c2V(200, 200);
I get the impact with:
c2Raycast cast;
if (c2RaytoCircle(ray, circle, &cast)) {
c2v impact = c2Impact(ray, cast.t);
}
The resulting impact is { 147.852, 147.852 }, which is outside the circle. In each case(circle position) I tested the resulting impact is always outside the circle.
I am having issues with playing the following (1.2 pitch setting):
http://www.particle-space.xyz/downloads/gong.ogg
Substituting the following resolved the issue:
__m128 phase; // = _mm_atan2_ps( imag, real );
float *phasef = (float*)&phase;
float *realf = (float*)ℜ
float *imagf = (float*)&imag;
for (int i=0; i<4; i++) phasef[i] = smbAtan2(imagf[i], realf[i]);
I suspect there is an issue with _mm_atan2_ps() related to the edge cases.
I've been treating tuShorten/tuWiden as a string-ish conversion between UTF16 and UTF 8, so I recommend this.
Here's the code; add it to the end of every tuShorten/tuWiden function.
if (out < (original_out + out_len)) out = 0;
OGG support in tinysound is done via stb_vorbis, which already has its own streaming API (more info here). Hooking up some code into tinysound to handle streaming would be a great feature to add.
This feature would be perfect for someone looking to contribute to tinysound! If tinysound is appealing, please consider adding a pull request.
My implementation suggestion would be to add another int field to tsLoadedSound
to specify a streamed file (for values 0 and 1 to specify no stream, and streamed). Additional information like sample offsets can also be stored in this struct. Then within tsMix
, on this line
tsLoadedSound* loaded = playing->loaded_sound;
The loaded sound can be examined and the stb_vorbis API can be used to perform streaming as necessary. This strategy would incur disk overhead into the mixing function, however, since most people will pop off a tsMix thread this is probably not a huge issue.
For OSX/iOS it looks like CoreAudio is the analog to DirectSound on Windows. tinysound is ready to be ported. A port would be a prime project for someone looking to contribute to open source. Here's how:
tsContext
holds OS-specific pointers or handles.tsMakeContext
initializes tsContext
and calls into OS-specific APIs.tsShutdownContext
frees any memory allocated in tsMakeContext
, and calls into OS-specific APIs.tsMix
is platform independent, portable C code. However, it calls tsPosition
and tsMemcpyToDS
, both are platform specific to Windows DirectSound. These two functions will need to be deleted/rewritten using OS-specific APIs.Summary: Loading the tsContext and shutting it down requires OS-specific code. Mixing is platform independent, and is just adding floats together then converting them into 16-bit integers. However, writing these 16-bit integers to the sound API is OS-specific (this writing out is what tsPosition
and tsMemcpyToDS
are for).
tinyheaders-master\tinyc2.h(327): error C2143: syntax error: missing ';' before 'do'
etc.
Considering the addition of tinymath2d
i think it would be nice to have a C++ wrapper for tinyc2
. I'm thinking of a base class c2Shape
that holds the position information so the user could manipulate the position of a shape without knowing what type it is (which is something i am missing from tinyc2
). Kind of like that:
namespace c2 {
struct Shape
{
c2v position;
};
struct Aabb : public Shape
{
float w;
float h;
};
struct Circle : public Shape
{
float r;
};
.
.
.
void collide(const Shape& A, const Shape& B, c2Manifold* m)
{
// Calculate the manifold
}
}
One would need to cast the Shape
to calculate the manifold then (either using the c API from tinyc2
or tinymath2d
if possible). It would be nicer though to avoid the cast, but i can't think of way to do this right now.
Anyway just wanted to hear your opinion on that
Awesome speed improvement on the pitch code!
I noticed a few issues:
You will need a tsLock() for tsSetPitch() OR read playing->pitch just once in tsMix()? In tsPlaySound() you will need an unlock:
if ( !playing )
{
tsUnlock( ctx );
return 0;
}
tsStopAllSounds() is not thread safe with tsMix()
A possible solution:
void tsStopAllSounds( tsContext* ctx )
{
tsLock( ctx );
tsPlayingSound* sound = ctx->playing;
while ( sound )
{
// let tsMix() remove the sound
sound->active = 0;
sound = sound->next;
}
tsUnlock( ctx );
}
In few places it uses C++ syntax:
true
bool literal in line 802I have checked your tinytime.h library and found one error. This error is in linux realization. Sometimes, returned delta time is less than 0. It happens every second. And i think error is in nanoseconds only. There should be also seconds. Sorry for my english. Just look to code, it can look like this:
float delta = (float) (double)(end.tv_sec - start.tv_sec) + ((double)(end.tv_nsec - start.tv_nsec) * 1.0e-9);
I just did some more testing and found out that if the c2AABB
is over ray.p
then c2Raycast::t
is negative.
Here are my c2Ray and c2AABB:
c2AABB aabb;
aabb.min = c2V(80, 80);
aabb.max = c2V(120, 120);
c2Ray ray;
ray.p = c2V(100, 100);
ray.d = c2V(100, 100);
ray.t = 1;
I included a picture to illustrate the problem.
PS: I hope it's ok if I keep opening issues for the bugs I find :)
Both Linux and Windows. I just compiled the demo with Visual Studio 2017 on Windows.
You can clearly hear it with headphones: left channel is always silent.
If change wanted.channels
to 1, then both left and right have a sound, but 2 times slower than normal.
P.S. The demo can't be compiled in VS. You need to change 636 line from:
void* buf = data;
to:
char* buf = (char*)data;
If you use c2Collide
to calculate the c2Manifold
of a collision, the normal doesn't always point from shape A to B, if the type of A is different from the type of B. If both are of the same type the normal always points from A to B. This happens because the arguments of c2XtoX
functions for different shape types have a fixed order.
E.g. Shape A is a c2Circle
and B is a c2AABB
.
// If passed in this order the normal will point from A -> B.
c2Collide(&A, NULL, C2_CIRCLE, &B, NULL, C2_AABB, &manifold);
Now switch the shapes, A now is a c2AABB
and B a c2Circle
// Now c2Collide will call c2CircletoAABBManifold(*B, *A, manifold)
// If passed in this order the normal will point from B -> A.
c2Collide(&A, NULL, C2_AABB, &B, NULL, C2_CIRCLE, &manifold);
A simple solution would be: For each call in c2Collide
where A and B are switched (e.g. c2XtoX(*B, *A, &m)
) the normal needs to be inverted.
Most people would probably use stb_vorbis with tinysound, so it would be good for me to see if a demo, or maybe a support function, would be a good addition.
This is generally a much better idea since not everyone who will use your api will be wanting to load stuff from disk. If you just let them provide you a buffert then they can pick if they want file-loading or memory-streaming or whatever.
Really nice API btw :)
In tinymemfile all offsets are signed integers and there is an ambiguity in tmseek. Assert's body suggests that smseek will add offset to the current bytes_read and checks if greater than zero but smseek overwrites bytes_read with offset. Consider two scenarios:
fp->bytes_read = 0;
tmseek(fp, MAX_INT); // assert ok, bytes_read + offset >= 0;
// assign ok, will set bytes_read to MAX_INT - a valid memory offset
tmseek(fp, 1); // assert error, bytes_read + offset < 0;
// assign ok, will set bytes-read to 1 - still a valid offset
fp->bytes_read = 0;
tmseek(fp, MAX_INT); // assert ok, bytes_read + offset >= 0;
// assign ok, will set bytes_read to MAX_INT - a valid memory offset
tmseek(fp, -1); // assert ok, bytes_read + offset >= 0;
// assign error, will set bytes-read to -1 - not a valid offset
@RandyGaul I love this collection of small, useful, tight header files! I'm using tinyc2 in a game project right now.
I'm wondering if you'd be open to a PR that integrates dotc: https://www.npmjs.com/package/dotc
The tl;dr is it's a preprocessor that copies the semantics of node's module lookup algorithm without modifying anything else about the c language.
What's really nice about this is you can expose only the things that you want to be available from your module and everything else stays private, (i.e., not in the global namespace.)
If this is something you'd be interested in I'd be happy to send a PR (would basically look like this:)
#ifdef DOTC
#export c2AABB
#export c2Ray
#export c2Circle
#export c2CircletoCircle
// etc....
#endif
I think there's an extra #endif in tinyfiles.h. Around line 88 there's this:
#if defined( TINYPATH_IMPLEMENTATION )
#endif TINYPATH_IMPLEMENTATION
That #endif doesn't look like it should be there. I get compilation errors about the final #endif having no matching #if
Some headers use LIBNAME_IMPL
and some use LIBNAME_IMPLEMENTATION
. Sometimes they just use the lib's acronym like TS_IMPL
instead of TINYSOUND_IMPLEMENTATION
.
Should standardize to: LIBENAME_IMPLEMENTATION
for consistency. Should document each one of these in a consistent spot.
Should use SSE + SSE2 in the tsMix function. tinysound was written with SIMD in mind so this should be a nearly one-to-one conversion.
Provide a method to load files through SDL_RWops objects.
Hey.
I used tinyc2 for a gamejam project and had same issues compiling it on my friend's Apple laptop.
I don't know anything about versions of his stuff.
I'll post a diff here if you are at all interested in this. The changes were minimal.
I'm sorry that line numbers aren't included, also I don't have the energy or time to make a proper diff or Github pull request.
diff linux_gamejam_2017_kopi/tinyc2.h linux_gamejam_2017_original/tinyc2.h
1357c1357
< #define C2_PLANE_AT( p, i ) (c2h){ (p)->norms[ i ], c2Dot( (p)->norms[ i ], (p)->verts[ i ] ) }
---
> #define C2_PLANE_AT( p, i ) { (p)->norms[ i ], c2Dot( (p)->norms[ i ], (p)->verts[ i ] ) }
1422c1422
< if ( h ) *h = (c2h){ c2CCW90( in ), c2Dot( c2CCW90( in ), ra ) };
---
> if ( h ) *h = { c2CCW90( in ), c2Dot( c2CCW90( in ), ra ) };
As per title, I'm using tuWiden/tuShorten, but I'm finding that they carry on iterating even after a null terminator. This shouldn't be happening, no?
Ok, I've trying to integrate tinysound for some time, and just realized that it still lacks certain features I'd consider essential to a game.
The ogg/wav files are essentially loaded wholesale into memory as far as I can see. Load times thus suffer hard; and you can't always have all your sounds loaded at initialization time (I have ~200megs of sound, some of them are 5mb+). You can keep the file pointers (or oggstreams) in your tinysound structs, and stream the data using the mixer thread.
I'm using the lower level apis, and a separate thread for mixing, pools set to 0. When I call FreeSound() in the main loop upon a keypress (with the intention of playing another piece of ambient music), FreeSound() does not remove the playingsound pointer from the context's playing list. The examples don't test for such a case either - they all call FreeSound() at termination.
Minor detail, but give an option to add DSBCAPS_GLOBALFOCUS to bufdesc.dwFlags for the secondary buffer on line 883, which allows music to be playing while the window is not focused.
All in all, tinysound isn't quite production ready yet. I can't quite consider it at version 1.0.
Randy, to correctly convert from nanoseconds to seconds on Linux change this line
float elapsed = (float)((double)(now.tv_nsec - prev.tv_nsec) * 1e9);
to
float elapsed = (float)((double)(now.tv_nsec - prev.tv_nsec) * 1e-9);
tinyfiles.h(267): error C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead.
feldspar.txt is no shit, a whole god damn book...
what the shit
tpRoot
would be a useful function to have. It would copy just the root directory of a path into an out
buffer. Would need to be compatible with paths starting with Windows style drives, like "C:/root"
, as well as relative/UNIX paths like "../root"
and "/root"
.
int tpRoot( const char* path, char* out );
See: https://github.com/RandyGaul/tinyheaders/blob/master/tinypath.h#L67
Line 566 assumes the first chunk will be a data
chunk, but this is not always the case.
In my case, I have a wav file with a fact
chunk, and the load routine errors out.
Most of the tinysound demos don't run on Apple machines. The only one that does right now is the pitch demo. Demos need to be updated so they can run on OSX/iOS as well as Windows.
In tsStopAllSounds() need to add
sound->active = 0;
or call tsStopSound();
tinysound.h:2102:1: warning: 'selectany' attribute directive ignored [-Wattributes]
_PS_CONST_TYPE( sign_mask, int, (int)0x80000000 );
^~~~~~~~~~~~~~
tinysound.h:2103:1: warning: 'selectany' attribute directive ignored [-Wattributes]
_PS_CONST_TYPE( inv_sign_mask, int, (int)~0x80000000 );
^~~~~~~~~~~~~~
It's related to following line:
#define TS_SELECTANY const __attribute__( (selectany) )
How to fix it better?
GCC info:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc-multilib/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp
Thread model: posix
gcc version 7.1.1 20170630 (GCC)
When compiling under linux g++ complains about memcpy not being declared:
In file included from main.cpp:4:0:
./tinyc2.h: In function ‘int c2Hull(c2v*, int)’:
./tinyc2.h:913:55: error: ‘memcpy’ was not declared in this scope
memcpy( verts, hull_verts, sizeof( c2v ) * out_count );
Including cstring
before tiny2c.h
fixes the error.
Os: Ubuntu 16.04 LTS
Compiler: g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005
Each header should have consistency for custom malloc/free implementations. Each one should be able to take a context pointer. Should be documented at top of header for ease to find.
When creating a c2Poly
structure, should I set the values of norms
myself? And if so, what's the proper way?
tpCompact
would be a useful function to have. It would shorten a path to a specified character limit by placing an ellipses. Would work similar to PathCompactPathEx.
int tpCompact( const char* path, char* out );
See: https://github.com/RandyGaul/tinyheaders/blob/master/tinypath.h#L68
The win32 API has been used to implement high resolution integer timers, but implementations for Apple/Linux are still missing.
See:
https://github.com/RandyGaul/tinyheaders/blob/master/tinytime.h#L10-L29
Is this by design?, in function int64_t ttElapsed(), i changed the return to:
return (int64_t)(now.QuadPart - timer->prev.QuadPart);
If you enjoyed these headers please consider contributing a Linux audio port of tinysound :)
tinysound cannot run on Linux. Right now only Windows/Apple are supported through DirectSound and CoreAudio. tinysound is ready to be ported. A port would be a prime project for someone looking to contribute to open source. Here's how:
tsContext
holds OS-specific pointers or handles.tsMakeContext
initializes tsContext
and calls into OS-specific APIs.tsShutdownContext
frees any memory allocated in tsMakeContext
, and calls into OS-specific APIs.tsMix
is platform independent, portable C code. However, it calls tsPosition
and tsMemcpyToDS
, both are platform specific to Windows DirectSound. These two functions will need to be deleted/rewritten using OS-specific APIs. There are also similar calls to CoreAudio specific functions.Summary: Loading the tsContext and shutting it down requires OS-specific code. Mixing is platform independent, and is just adding floats together then converting them into 16-bit integers. However, writing these 16-bit integers to the sound API is OS-specific.
The macro C2_PLANE_AT, defined at https://github.com/RandyGaul/tinyheaders/blob/ff76ac374a6bec431d8748a82f91ec44f2952d96/tinyc2.h#L1357 raises the following error on GCC 6.3.1, Linux:
tinyc2.h: In function ‘c2AntinormalFace’:
tinyc2.h:1357:29: error: expected expression before ‘{’ token
#define C2_PLANE_AT( p, i ) { (p)->norms[ i ], c2Dot( (p)->norms[ i ], (p)->verts[ i ] ) }
^
I think that multi-statement macros aren't ANSI/ISO C/C++, that's why it works on Visual Studio, but not on GCC. Since the macro is used in many places, the only solution I can think of is to replace it with a function or replace where the macro is used with the explicit code.
From #12
Even if I'm a windows user, the geek in me knows that an ALSA backend would be a great addition.
FYI, http://github.com/yui0/aplay-/blob/master/alsa.h (PD)
Any thoughts on having some SDL1.2 support?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.