New object types for Wave, Sound, Music and Light.

This commit is contained in:
jussi
2023-10-28 16:42:49 +03:00
parent af03c7364e
commit fd49d806cf
14 changed files with 405 additions and 1270 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -15,7 +15,7 @@ Copy a block of pixels from one framebuffer object to another.
Use -1 RenderTexture for window framebuffer.
*/
int lglBlitFramebuffer( lua_State *L ) {
// TOCO Currently doesn't support setting window render target because of luaL_checkudata.
// TODO Currently doesn't support setting window render target because of luaL_checkudata.
RenderTexture *srcTex = luaL_checkudata( L, 1, "RenderTexture" );
RenderTexture *dstTex = luaL_checkudata( L, 2, "RenderTexture" );
Rectangle srcRect = uluaGetRectangleIndex( L, 3 );

View File

@@ -7,45 +7,6 @@
#define RLIGHTS_IMPLEMENTATION
#include "rlights.h"
static void checkLightRealloc( int i ) {
if ( i == state->lightCount ) {
state->lightCount++;
}
if ( state->lightCount == state->lightAlloc ) {
state->lightAlloc += ALLOC_PAGE_SIZE;
state->lights = realloc( state->lights, state->lightAlloc * sizeof( Light* ) );
for ( i = state->lightCount; i < state->lightAlloc; i++ ) {
state->lights[i] = NULL;
}
}
}
bool validLight( size_t id ) {
if ( id < 0 || state->lightCount < id || state->lights[ id ] == NULL ) {
TraceLog( state->logLevelInvalid, "%s %d", "Invalid light", id );
return false;
}
else {
return true;
}
}
static int newLight() {
int i = 0;
for ( i = 0; i < state->lightCount; i++ ) {
if ( state->lights[i] == NULL ) {
break;
}
}
state->lights[i] = malloc( sizeof( Light ) );
checkLightRealloc( i );
return i;
}
/*
## Lights - Basics
*/
@@ -55,8 +16,7 @@ static int newLight() {
Create a light and get shader locations
- Failure return -1
- Success return int
- Success return Light
*/
int llightsCreateLight( lua_State *L ) {
int type = luaL_checkinteger( L, 1 );
@@ -65,9 +25,7 @@ int llightsCreateLight( lua_State *L ) {
Color color = uluaGetColorIndex( L, 4 );
Shader *shader = luaL_checkudata( L, 5, "Shader" );
int i = newLight();
*state->lights[i] = CreateLight( type, position, target, color, *shader );
lua_pushinteger( L, i );
uluaPushLight( L, CreateLight( type, position, target, color, *shader ) );
return 1;
}
@@ -79,150 +37,81 @@ Send light properties to shader
*/
int llightsUpdateLightValues( lua_State *L ) {
Shader *shader = luaL_checkudata( L, 1, "Shader" );
size_t lightId = lua_tointeger( L, 2 );
Light *light = luaL_checkudata( L, 2, "Light" );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
UpdateLightValues( *shader, *state->lights[ lightId ] );
UpdateLightValues( *shader, *light );
return 0;
}
/*
> success = RL.SetLightType( Light light, int type )
> RL.SetLightType( Light light, int type )
Set light type
- Failure return false
- Success return true
*/
int llightsSetLightType( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) || !lua_isnumber( L, 2 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.SetLightType( Light light, int type )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
int type = lua_tointeger( L, 2 );
Light *light = luaL_checkudata( L, 1, "Light" );
int type = luaL_checkinteger( L, 2 );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
state->lights[ lightId ]->type = type;
lua_pushboolean( L, true );
light->type = type;
return 1;
return 0;
}
/*
> success = RL.SetLightPosition( Light light, Vector3 position )
> RL.SetLightPosition( Light light, Vector3 position )
Set light position
- Failure return false
- Success return true
*/
int llightsSetLightPosition( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) || !lua_istable( L, 2 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.SetLightPosition( Light light, Vecto3 position )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
Light *light = luaL_checkudata( L, 1, "Light" );
Vector3 position = uluaGetVector3Index( L, 2 );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
state->lights[ lightId ]->position = position;
lua_pushboolean( L, true );
light->position = position;
return 1;
return 0;
}
/*
> success = RL.SetLightTarget( Light light, Vector3 target )
> RL.SetLightTarget( Light light, Vector3 target )
Set light target
- Failure return false
- Success return true
*/
int llightsSetLightTarget( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) || !lua_istable( L, 2 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.SetLightTarget( Light light, Vecto3 target )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
Light *light = luaL_checkudata( L, 1, "Light" );
Vector3 target = uluaGetVector3Index( L, 2 );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
state->lights[ lightId ]->target = target;
lua_pushboolean( L, true );
light->target = target;
return 1;
return 0;
}
/*
> success = RL.SetLightColor( Light light, Color color )
> RL.SetLightColor( Light light, Color color )
Set light color
- Failure return false
- Success return true
*/
int llightsSetLightColor( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) || !lua_istable( L, 2 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.SetLightColor( Light light, Color color )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
Light *light = luaL_checkudata( L, 1, "Light" );
Color color = uluaGetColorIndex( L, 2 );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
state->lights[ lightId ]->color = color;
lua_pushboolean( L, true );
light->color = color;
return 1;
return 0;
}
/*
> success = RL.SetLightEnabled( Light light, bool enabled )
> RL.SetLightEnabled( Light light, bool enabled )
Set light enabled
- Failure return false
- Success return true
*/
int llightsSetLightEnabled( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) || !lua_isboolean( L, 2 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.SetLightEnabled( Light light, bool enabled )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
bool enabled = lua_toboolean( L, 2 );
Light *light = luaL_checkudata( L, 1, "Light" );
bool enabled = uluaGetBoolean( L, 2 );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
state->lights[ lightId ]->enabled = enabled;
lua_pushboolean( L, true );
light->enabled = enabled;
return 1;
return 0;
}
/*
@@ -230,22 +119,12 @@ int llightsSetLightEnabled( lua_State *L ) {
Get light type
- Failure return false
- Success return int
*/
int llightsGetLightType( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GetLightType( Light light )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
Light *light = luaL_checkudata( L, 1, "Light" );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
lua_pushinteger( L, state->lights[ lightId ]->type );
lua_pushinteger( L, light->type );
return 1;
}
@@ -255,22 +134,12 @@ int llightsGetLightType( lua_State *L ) {
Get light position
- Failure return false
- Success return Vector3
*/
int llightsGetLightPosition( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GetLightPosition( Light light )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
Light *light = luaL_checkudata( L, 1, "Light" );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
uluaPushVector3( L, state->lights[ lightId ]->position );
uluaPushVector3( L, light->position );
return 1;
}
@@ -280,22 +149,12 @@ int llightsGetLightPosition( lua_State *L ) {
Get light target
- Failure return false
- Success return Vector3
*/
int llightsGetLightTarget( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GetLightTarget( Light light )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
Light *light = luaL_checkudata( L, 1, "Light" );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
uluaPushVector3( L, state->lights[ lightId ]->target );
uluaPushVector3( L, light->target );
return 1;
}
@@ -305,22 +164,12 @@ int llightsGetLightTarget( lua_State *L ) {
Get light color
- Failure return false
- Success return Color
*/
int llightsGetLightColor( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GetLightColor( Light light )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
Light *light = luaL_checkudata( L, 1, "Light" );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
uluaPushColor( L, state->lights[ lightId ]->color );
uluaPushColor( L, light->color );
return 1;
}
@@ -330,22 +179,12 @@ int llightsGetLightColor( lua_State *L ) {
Get light enabled
- Failure return nil
- Success return boolean
*/
int llightsIsLightEnabled( lua_State *L ) {
if ( !lua_isnumber( L, 1 ) ) {
TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.IsLightEnabled( Light light )" );
lua_pushnil( L );
return 1;
}
size_t lightId = lua_tointeger( L, 1 );
Light *light = luaL_checkudata( L, 1, "Light" );
if ( !validLight( lightId ) ) {
lua_pushnil( L );
return 1;
}
lua_pushboolean( L, state->lights[ lightId ]->enabled );
lua_pushboolean( L, light->enabled );
return 1;
}

View File

@@ -140,6 +140,69 @@ static void defineFont() {
lua_setfield( L, -2, "__gc" );
}
/* Wave. */
static int gcWave( lua_State *L ) {
Wave *wave = luaL_checkudata ( L, 1, "Wave" );
printf( "gcWave\n" );
UnloadWave( *wave );
}
static void defineWave() {
lua_State *L = state->luaState;
luaL_newmetatable( L, "Wave" );
lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, gcWave );
lua_setfield( L, -2, "__gc" );
}
/* Sound. */
static int gcSound( lua_State *L ) {
Sound *sound = luaL_checkudata ( L, 1, "Sound" );
printf( "gcSound\n" );
UnloadSound( *sound );
}
static void defineSound() {
lua_State *L = state->luaState;
luaL_newmetatable( L, "Sound" );
lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, gcSound );
lua_setfield( L, -2, "__gc" );
}
/* Music. */
static int gcMusic( lua_State *L ) {
Music *music = luaL_checkudata ( L, 1, "Music" );
printf( "gcMusic\n" );
UnloadMusicStream( *music );
}
static void defineMusic() {
lua_State *L = state->luaState;
luaL_newmetatable( L, "Music" );
lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, gcMusic );
lua_setfield( L, -2, "__gc" );
}
/* Music. */
static void defineLight() {
lua_State *L = state->luaState;
luaL_newmetatable( L, "Light" );
lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" );
}
/* Assing globals. */
static void assignGlobalInt( int value, const char *name ) {
@@ -1162,6 +1225,10 @@ bool luaInit( int argn, const char **argc ) {
defineCamera3D();
defineShader();
defineFont();
defineWave();
defineSound();
defineMusic();
defineLight();
/* Define globals. */
defineGlobals();
@@ -1777,8 +1844,6 @@ void luaRegister() {
assingGlobalFunction( "LoadSound", laudioLoadSound );
assingGlobalFunction( "LoadWave", laudioLoadWave );
assingGlobalFunction( "LoadSoundFromWave", laudioLoadSoundFromWave );
assingGlobalFunction( "UnloadSound", laudioUnloadSound );
assingGlobalFunction( "UnloadWave", laudioUnloadWave );
assingGlobalFunction( "ExportWave", laudioExportWave );
assingGlobalFunction( "ExportWaveAsCode", laudioExportWaveAsCode );
/* Wave/Sound management */
@@ -2899,6 +2964,30 @@ void uluaPushFont( lua_State *L, Font font ) {
luaL_setmetatable( L, "Font" );
}
void uluaPushWave( lua_State *L, Wave wave ) {
Wave *waveP = lua_newuserdata( L, sizeof( Wave ) );
*waveP = wave;
luaL_setmetatable( L, "Wave" );
}
void uluaPushSound( lua_State *L, Sound sound ) {
Sound *soundP = lua_newuserdata( L, sizeof( Sound ) );
*soundP = sound;
luaL_setmetatable( L, "Sound" );
}
void uluaPushMusic( lua_State *L, Music music ) {
Music *musicP = lua_newuserdata( L, sizeof( Music ) );
*musicP = music;
luaL_setmetatable( L, "Music" );
}
void uluaPushLight( lua_State *L, Light light ) {
Light *lightP = lua_newuserdata( L, sizeof( Light ) );
*lightP = light;
luaL_setmetatable( L, "Light" );
}
int uluaGetTableLen( lua_State *L ) {
return uluaGetTableLenIndex( L, lua_gettop( L ) );
}

View File

@@ -17,18 +17,6 @@ bool stateInit( int argn, const char **argc, const char *exePath ) {
state->resolution = (Vector2){ 800, 600 };
state->luaState = NULL;
state->logLevelInvalid = LOG_ERROR;
/* Waves. */
state->waveAlloc = ALLOC_PAGE_SIZE;
state->waveCount = 0;
state->waves = malloc( state->waveAlloc * sizeof( Wave* ) );
/* Sounds. */
state->soundAlloc = ALLOC_PAGE_SIZE;
state->soundCount = 0;
state->sounds = malloc( state->soundAlloc * sizeof( Sound* ) );
/* Musics. */
state->musicAlloc = ALLOC_PAGE_SIZE;
state->musicCount = 0;
state->musics = malloc( state->musicAlloc * sizeof( Music* ) );
/* Meshes. */
state->meshAlloc = ALLOC_PAGE_SIZE;
state->meshCount = 0;
@@ -45,18 +33,11 @@ bool stateInit( int argn, const char **argc, const char *exePath ) {
state->animationAlloc = ALLOC_PAGE_SIZE;
state->animationCount = 0;
state->animations = malloc( state->animationAlloc * sizeof( ModelAnimations* ) );
/* Lights. */
state->lightAlloc = ALLOC_PAGE_SIZE;
state->lightCount = 0;
state->lights = malloc( state->lightAlloc * sizeof( Light* ) );
for ( int i = 0; i < ALLOC_PAGE_SIZE; i++ ) {
state->waves[i] = NULL;
state->sounds[i] = NULL;
state->meshes[i] = NULL;
state->models[i] = NULL;
state->animations[i] = NULL;
state->lights[i] = NULL;
/* The ones we want to save the first. */
if ( 0 < i ) {
@@ -86,24 +67,6 @@ void stateInitInterpret( int argn, const char **argc ) {
}
void stateFree() {
for ( int i = 0; i < state->waveCount; ++i ) {
if ( state->waves[i] != NULL ) {
UnloadWave( *state->waves[i] );
free( state->waves[i] );
}
}
for ( int i = 0; i < state->soundCount; ++i ) {
if ( state->sounds[i] != NULL ) {
UnloadSound( *state->sounds[i] );
free( state->sounds[i] );
}
}
for ( int i = 0; i < state->musicCount; ++i ) {
if ( state->musics[i] != NULL ) {
UnloadMusicStream( *state->musics[i] );
free( state->musics[i] );
}
}
for ( int i = 0; i < state->modelCount; ++i ) {
if ( state->models[i] != NULL ) {
//TODO Test if UnloadModel causes segfaults on exit.
@@ -134,13 +97,6 @@ void stateFree() {
}
}
#if !defined( PLATFORM_RPI ) || !defined( PLATFORM_DRM )
for ( int i = 0; i < state->lightCount; ++i ) {
if ( state->lights[i] != NULL ) {
free( state->lights[i] );
}
}
#endif
if ( IsAudioDeviceReady() ) {
CloseAudioDevice();
}
@@ -151,14 +107,10 @@ void stateFree() {
if ( state->hasWindow ) {
CloseWindow();
}
free( state->waves );
free( state->sounds );
free( state->musics );
free( state->meshes );
free( state->materials );
free( state->models );
free( state->animations );
free( state->lights );
free( state->exePath );
free( state );
}

View File

@@ -33,7 +33,7 @@ int ltextLoadFont( lua_State *L ) {
return 1;
}
TraceLog( state->logLevelInvalid, "Invalid file path '%s'", lua_tostring( L, 1 ) );
TraceLog( state->logLevelInvalid, "Invalid file '%s'", lua_tostring( L, 1 ) );
lua_pushnil( L );
return 1;
@@ -73,7 +73,7 @@ int ltextLoadFontEx( lua_State *L ) {
return 1;
}
TraceLog( state->logLevelInvalid, "Invalid file path '%s'", lua_tostring( L, 1 ) );
TraceLog( state->logLevelInvalid, "Invalid file '%s'", lua_tostring( L, 1 ) );
lua_pushnil( L );
return 1;
@@ -82,7 +82,7 @@ int ltextLoadFontEx( lua_State *L ) {
/*
> font = RL.LoadFontFromImage( Image image, Color key, int firstChar )
Load font from Image ( XNA style )
Load font from Image ( XNA style)
- Success return Font
*/

View File

@@ -21,7 +21,7 @@ int ltexturesLoadImage( lua_State *L ) {
return 1;
}
TraceLog( state->logLevelInvalid, "Invalid file path '%s'", lua_tostring( L, 1 ) );
TraceLog( state->logLevelInvalid, "Invalid file '%s'", lua_tostring( L, 1 ) );
lua_pushnil( L );
return 1;
@@ -929,7 +929,7 @@ int ltexturesLoadTexture( lua_State *L ) {
return 1;
}
TraceLog( state->logLevelInvalid, "Invalid file path '%s'", lua_tostring( L, 1 ) );
TraceLog( state->logLevelInvalid, "Invalid file '%s'", lua_tostring( L, 1 ) );
lua_pushnil( L );
return 1;