New object types for all types.

This commit is contained in:
jussi
2023-10-29 13:21:42 +02:00
parent fd49d806cf
commit 76911d45a8
16 changed files with 653 additions and 1852 deletions

View File

@@ -194,7 +194,7 @@ static void defineMusic() {
lua_setfield( L, -2, "__gc" );
}
/* Music. */
/* Light. */
static void defineLight() {
lua_State *L = state->luaState;
@@ -203,6 +203,93 @@ static void defineLight() {
lua_setfield( L, -2, "__index" );
}
/* Material. */
static int gcMaterial( lua_State *L ) {
Material *material = luaL_checkudata ( L, 1, "Material" );
printf( "gcMaterial\n" );
// int MAX_MATERIAL_MAPS = 12;
// Unload loaded texture maps (avoid unloading default texture, managed by raylib)
// if ( material->maps != NULL ) {
// for ( int i = 0; i < MAX_MATERIAL_MAPS; i++ ) {
// if ( material->maps[i].texture.id != rlGetTextureIdDefault() ) {
// printf( "gcMaterial material->maps[i].texture.id = %d\n", material->maps[i].texture.id );
// rlUnloadTexture( material->maps[i].texture.id );
// }
// }
// }
/* Custom UnloadMaterial since we don't want to free Shaders or Textures. */
RL_FREE( material->maps );
}
static void defineMaterial() {
lua_State *L = state->luaState;
luaL_newmetatable( L, "Material" );
lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, gcMaterial );
lua_setfield( L, -2, "__gc" );
}
/* Mesh. */
static int gcMesh( lua_State *L ) {
Mesh *mesh = luaL_checkudata ( L, 1, "Mesh" );
printf( "gcMesh\n" );
UnloadMesh( *mesh );
}
static void defineMesh() {
lua_State *L = state->luaState;
luaL_newmetatable( L, "Mesh" );
lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, gcMesh );
lua_setfield( L, -2, "__gc" );
}
/* Model. */
static int gcModel( lua_State *L ) {
Model *model = luaL_checkudata ( L, 1, "Model" );
printf( "gcModel\n" );
UnloadModel( *model );
// UnloadModelKeepMeshes( *model );
}
static void defineModel() {
lua_State *L = state->luaState;
luaL_newmetatable( L, "Model" );
lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, gcModel );
lua_setfield( L, -2, "__gc" );
}
/* ModelAnimation. */
static int gcModelAnimation( lua_State *L ) {
ModelAnimation *modelAnimation = luaL_checkudata ( L, 1, "ModelAnimation" );
printf( "gcModelAnimation\n" );
UnloadModelAnimation( *modelAnimation );
}
static void defineModelAnimation() {
lua_State *L = state->luaState;
luaL_newmetatable( L, "ModelAnimation" );
lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, gcModelAnimation );
lua_setfield( L, -2, "__gc" );
}
/* Assing globals. */
static void assignGlobalInt( int value, const char *name ) {
@@ -243,7 +330,10 @@ static void defineGlobals() {
lua_getglobal( L, "RL" );
uluaPushFont( L, GetFontDefault() );
lua_setfield( L, -2, "fontDefault" );
lua_setfield( L, -2, "defaultFont" );
uluaPushMaterial( L, LoadMaterialDefault() );
lua_setfield( L, -2, "defaultMaterial" );
/*DOC_START*/
/* ConfigFlags */
@@ -1229,6 +1319,10 @@ bool luaInit( int argn, const char **argc ) {
defineSound();
defineMusic();
defineLight();
defineMaterial();
defineMesh();
defineModel();
defineModelAnimation();
/* Define globals. */
defineGlobals();
@@ -1769,7 +1863,6 @@ void luaRegister() {
assingGlobalFunction( "GenMeshHeightmap", lmodelsGenMeshHeightmap );
assingGlobalFunction( "GenMeshCustom", lmodelsGenMeshCustom );
assingGlobalFunction( "UpdateMesh", lmodelsUpdateMesh );
assingGlobalFunction( "UnloadMesh", lmodelsUnloadMesh );
assingGlobalFunction( "DrawMesh", lmodelsDrawMesh );
assingGlobalFunction( "DrawMeshInstanced", lmodelsDrawMeshInstanced );
assingGlobalFunction( "SetMeshColor", lmodelsSetMeshColor );
@@ -1779,7 +1872,6 @@ void luaRegister() {
/* Material. */
assingGlobalFunction( "LoadMaterialDefault", lmodelsLoadMaterialDefault );
assingGlobalFunction( "CreateMaterial", lmodelsCreateMaterial );
assingGlobalFunction( "UnloadMaterial", lmodelsUnloadMaterial );
assingGlobalFunction( "SetMaterialTexture", lmodelsSetMaterialTexture );
assingGlobalFunction( "SetMaterialColor", lmodelsSetMaterialColor );
assingGlobalFunction( "SetMaterialValue", lmodelsSetMaterialValue );
@@ -1793,7 +1885,6 @@ void luaRegister() {
/* Model. */
assingGlobalFunction( "LoadModel", lmodelsLoadModel );
assingGlobalFunction( "LoadModelFromMesh", lmodelsLoadModelFromMesh );
assingGlobalFunction( "UnloadModel", lmodelsUnloadModel );
assingGlobalFunction( "DrawModel", lmodelsDrawModel );
assingGlobalFunction( "DrawModelEx", lmodelsDrawModelEx );
assingGlobalFunction( "SetModelMaterial", lmodelsSetModelMaterial );
@@ -1806,7 +1897,6 @@ void luaRegister() {
/* Animations. */
assingGlobalFunction( "LoadModelAnimations", lmodelsLoadModelAnimations );
assingGlobalFunction( "UpdateModelAnimation", lmodelsUpdateModelAnimation );
assingGlobalFunction( "UnloadModelAnimations", lmodelsUnloadModelAnimations );
assingGlobalFunction( "IsModelAnimationValid", lmodelsIsModelAnimationValid );
assingGlobalFunction( "GetModelAnimationBoneCount", lmodelsGetModelAnimationBoneCount );
assingGlobalFunction( "GetModelAnimationFrameCount", lmodelsGetModelAnimationFrameCount );
@@ -2988,6 +3078,30 @@ void uluaPushLight( lua_State *L, Light light ) {
luaL_setmetatable( L, "Light" );
}
void uluaPushMaterial( lua_State *L, Material material ) {
Material *materialP = lua_newuserdata( L, sizeof( Material ) );
*materialP = material;
luaL_setmetatable( L, "Material" );
}
void uluaPushMesh( lua_State *L, Mesh mesh ) {
Mesh *meshP = lua_newuserdata( L, sizeof( Mesh ) );
*meshP = mesh;
luaL_setmetatable( L, "Mesh" );
}
void uluaPushModel( lua_State *L, Model model ) {
Model *modelP = lua_newuserdata( L, sizeof( Model ) );
*modelP = model;
luaL_setmetatable( L, "Model" );
}
void uluaPushModelAnimation( lua_State *L, ModelAnimation modelAnimation ) {
ModelAnimation *modelAnimationP = lua_newuserdata( L, sizeof( ModelAnimation ) );
*modelAnimationP = modelAnimation;
luaL_setmetatable( L, "ModelAnimation" );
}
int uluaGetTableLen( lua_State *L ) {
return uluaGetTableLenIndex( L, lua_gettop( L ) );
}

File diff suppressed because it is too large Load Diff

View File

@@ -17,37 +17,8 @@ bool stateInit( int argn, const char **argc, const char *exePath ) {
state->resolution = (Vector2){ 800, 600 };
state->luaState = NULL;
state->logLevelInvalid = LOG_ERROR;
/* Meshes. */
state->meshAlloc = ALLOC_PAGE_SIZE;
state->meshCount = 0;
state->meshes = malloc( state->meshAlloc * sizeof( Mesh* ) );
/* Materials. */
state->materialAlloc = ALLOC_PAGE_SIZE;
state->materialCount = 1;
state->materials = malloc( state->materialAlloc * sizeof( Material* ) );
/* Models. */
state->modelAlloc = ALLOC_PAGE_SIZE;
state->modelCount = 0;
state->models = malloc( state->modelAlloc * sizeof( Model* ) );
/* ModelsAnimations. */
state->animationAlloc = ALLOC_PAGE_SIZE;
state->animationCount = 0;
state->animations = malloc( state->animationAlloc * sizeof( ModelAnimations* ) );
for ( int i = 0; i < ALLOC_PAGE_SIZE; i++ ) {
state->meshes[i] = NULL;
state->models[i] = NULL;
state->animations[i] = NULL;
/* The ones we want to save the first. */
if ( 0 < i ) {
state->materials[i] = NULL;
}
}
InitWindow( state->resolution.x, state->resolution.y, "ReiLua" );
/* Has to be after InitWindod where opengl context is created. */
state->materials[0] = malloc( sizeof( Material ) );
*state->materials[0] = LoadMaterialDefault();
if ( !IsWindowReady() ) {
state->hasWindow = false;
@@ -67,36 +38,6 @@ void stateInitInterpret( int argn, const char **argc ) {
}
void stateFree() {
for ( int i = 0; i < state->modelCount; ++i ) {
if ( state->models[i] != NULL ) {
//TODO Test if UnloadModel causes segfaults on exit.
UnloadModelKeepMeshes( *state->models[i] );
// UnloadModel( *state->models[i] );
free( state->models[i] );
}
}
for ( int i = 0; i < state->meshCount; ++i ) {
if ( state->meshes[i] != NULL ) {
UnloadMesh( *state->meshes[i] );
free( state->meshes[i] );
}
}
for ( int i = 0; i < state->materialCount; ++i ) {
if ( state->materials[i] != NULL ) {
/* Prevent unloading shader that would result in double free when freeing shaders. */
state->materials[i]->shader.id = rlGetShaderIdDefault();
UnloadMaterial( *state->materials[i] );
free( state->materials[i] );
}
}
for ( int i = 0; i < state->animationCount; ++i ) {
if ( state->animations[i] != NULL ) {
UnloadModelAnimations( state->animations[i]->animations, state->animations[i]->animCount );
free( state->animations[i] );
}
}
if ( IsAudioDeviceReady() ) {
CloseAudioDevice();
}
@@ -107,10 +48,6 @@ void stateFree() {
if ( state->hasWindow ) {
CloseWindow();
}
free( state->meshes );
free( state->materials );
free( state->models );
free( state->animations );
free( state->exePath );
free( state );
}

View File

@@ -40,7 +40,7 @@ int ltextLoadFont( lua_State *L ) {
}
/*
> font = RL.LoadFontEx( string fileName, int fontSize, int fontChars{} )
> font = RL.LoadFontEx( string fileName, int fontSize, int{} fontChars )
Load font from file with extended parameters. Loading the default character set

View File

@@ -831,35 +831,21 @@ int ltexturesImageDraw( lua_State *L ) {
}
/*
> success = RL.ImageDrawTextEx( Image dst, Font font, string text, Vector2 position, float fontSize, float spacing, Color tint )
> RL.ImageDrawTextEx( Image dst, Font font, string text, Vector2 position, float fontSize, float spacing, Color tint )
Draw text ( Custom sprite font ) within an image ( Destination )
- Failure return false
- Success return true
Draw text (Custom sprite font) within an image (Destination)
*/
int ltexturesImageDrawTextEx( lua_State *L ) {
// if ( !lua_isnumber( L, 1 ) || !lua_isnumber( L, 2 ) || !lua_isstring( L, 3 ) || !lua_istable( L, 4 )
// || !lua_isnumber( L, 5 ) || !lua_isnumber( L, 6 ) || !lua_istable( L, 7 ) ) {
// TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.ImageDrawTextEx( Image dst, Font font, string text, Vector2 position, float fontSize, float spacing, Color tint )" );
// lua_pushboolean( L, false );
// return 1;
// }
// size_t imageId = lua_tointeger( L, 1 );
// size_t fontId = lua_tointeger( L, 2 );
// Vector2 position = uluaGetVector2Index( L, 4 );
// float fontSize = lua_tonumber( L, 5 );
// float spacing = lua_tonumber( L, 6 );
// Color tint = uluaGetColorIndex( L, 7 );
Image *image = luaL_checkudata( L, 1, "Image" );
Font *font = luaL_checkudata( L, 2, "Font" );
Vector2 position = uluaGetVector2Index( L, 4 );
float fontSize = luaL_checknumber( L, 5 );
float spacing = luaL_checknumber( L, 6 );
Color tint = uluaGetColorIndex( L, 7 );
// if ( !validImage( imageId ) || !validFont( fontId ) ) {
// lua_pushboolean( L, false );
// return 1;
// }
// ImageDrawTextEx( state->images[ imageId ], *state->fonts[ fontId ], lua_tostring( L, 3 ), position, fontSize, spacing, tint );
// lua_pushboolean( L, true );
ImageDrawTextEx( image, *font, luaL_checkstring( L, 3 ), position, fontSize, spacing, tint );
return 1;
return 0;
}
/*