This commit is contained in:
jussi
2022-04-05 01:38:03 +03:00
parent 2a46afbf91
commit fbfe1e0cb6
13 changed files with 458 additions and 11 deletions

View File

@@ -44,7 +44,7 @@ bool validCamera3D( size_t id ) {
}
}
static inline bool validShader( size_t id ) {
bool validShader( size_t id ) {
if ( id < 0 || state->shaderCount < id || state->shaders[ id ] == NULL ) {
TraceLog( LOG_WARNING, "%s %d", "Invalid shader", id );
return false;
@@ -859,8 +859,8 @@ int lcoreLoadShader( lua_State *L ) {
}
state->shaders[i] = malloc( sizeof( Shader ) );
// *state->shaders[i] = LoadShader( lua_tostring( L, -2 ), lua_tostring( L, -1 ) );
// *state->shaders[i] = LoadShader( vsFileName, fsFileName );
*state->shaders[i] = LoadShader( 0, fsFileName );
*state->shaders[i] = LoadShader( vsFileName, fsFileName );
// *state->shaders[i] = LoadShader( 0, fsFileName );
lua_pushinteger( L, i );
checkShaderRealloc( i );
@@ -876,13 +876,23 @@ Load shader from code strings and bind default locations
- Success return int
*/
//TODO Should also allow only one shader.
int lcoreLoadShaderFromMemory( lua_State *L ) {
if ( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadShaderFromMemory( string vsCode, string fsCode )" );
lua_pushinteger( L, -1 );
return 1;
// if ( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) ) {
// TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadShaderFromMemory( string vsCode, string fsCode )" );
// lua_pushinteger( L, -1 );
// return 1;
// }
char fs[ STRING_LEN ] = { '\0' };
char vs[ STRING_LEN ] = { '\0' };
if ( lua_isstring( L, -1 ) ) {
strcpy( fs, lua_tostring( L, -1 ) );
}
if ( lua_isstring( L, -2 ) ) {
strcpy( vs, lua_tostring( L, -2 ) );
}
int i = 0;
for ( i = 0; i < state->shaderCount; i++ ) {
@@ -891,7 +901,8 @@ int lcoreLoadShaderFromMemory( lua_State *L ) {
}
}
state->shaders[i] = malloc( sizeof( Shader ) );
*state->shaders[i] = LoadShaderFromMemory( lua_tostring( L, -2 ), lua_tostring( L, -1 ) );
// *state->shaders[i] = LoadShaderFromMemory( lua_tostring( L, -2 ), lua_tostring( L, -1 ) );
*state->shaders[i] = LoadShaderFromMemory( vs, fs );
lua_pushinteger( L, i );
checkShaderRealloc( i );
@@ -985,6 +996,63 @@ int lcoreGetShaderLocationAttrib( lua_State *L ) {
return 1;
}
/*
> success = RL_SetShaderLocationIndex( Shader shader, int shaderLocationIndex, int location )
Set shader location index
- Failure return false
- Success return true
*/
int lcoreSetShaderLocationIndex( lua_State *L ) {
if ( !lua_isnumber( L, -3 ) || !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_SetShaderLocationIndex( Shader shader, int shaderLocationIndex, int location )" );
lua_pushboolean( L, false );
return 1;
}
int location = lua_tointeger( L, -1 );
lua_pop( L, 1 );
int shaderLocationIndex = lua_tointeger( L, -1 );
lua_pop( L, 1 );
size_t shaderId = lua_tointeger( L, -1 );
if ( !validShader( shaderId ) ) {
lua_pushboolean( L, false );
return 1;
}
state->shaders[ shaderId ]->locs[ shaderLocationIndex ] = location;
lua_pushboolean( L, true );
return 1;
}
/*
> location = RL_GetShaderLocationIndex( Shader shader, int shaderLocationIndex )
Get shader location index
- Failure return false
- Success return int
*/
int lcoreGetShaderLocationIndex( lua_State *L ) {
if ( !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_GetShaderLocationIndex( Shader shader, int shaderLocationIndex )" );
lua_pushboolean( L, false );
return 1;
}
int shaderLocationIndex = lua_tointeger( L, -1 );
lua_pop( L, 1 );
size_t shaderId = lua_tointeger( L, -1 );
if ( !validShader( shaderId ) ) {
lua_pushboolean( L, false );
return 1;
}
lua_pushinteger( L, state->shaders[ shaderId ]->locs[ shaderLocationIndex ] );
return 1;
}
/*
> success = RL_SetShaderValueMatrix( Shader shader, int locIndex, Matrix mat )

108
src/lights.c Normal file
View File

@@ -0,0 +1,108 @@
#include "main.h"
#include "state.h"
#include "lua_core.h"
#include "core.h"
#include "lights.h"
#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( LOG_WARNING, "%s %d", "Invalid light", id );
return false;
}
else {
return true;
}
}
/*
## Lights - Basics
*/
/*
> light = RL_CreateLight( int type, Vector3 position, Vector3 target, Color color, Shader shader )
Create a light and get shader locations
- Failure return -1
- Success return int
*/
int llightsCreateLight( lua_State *L ) {
if ( !lua_isnumber( L, -5 ) || !lua_istable( L, -4 ) || !lua_istable( L, -3 ) || !lua_istable( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_CreateLight( int type, Vector3 position, Vector3 target, Color color, Shader shader )" );
lua_pushinteger( L, -1 );
return 1;
}
size_t shaderId = lua_tointeger( L, -1 );
lua_pop( L, 1 );
Color color = uluaGetColor( L );
lua_pop( L, 1 );
Vector3 target = uluaGetVector3( L );
lua_pop( L, 1 );
Vector3 position = uluaGetVector3( L );
lua_pop( L, 1 );
int type = lua_tointeger( L, -1 );
int i = 0;
for ( i = 0; i < state->lightCount; i++ ) {
if ( state->lights[i] == NULL ) {
break;
}
}
state->lights[i] = malloc( sizeof( Light ) );
*state->lights[i] = CreateLight( type, position, target, color, *state->shaders[ shaderId ] );
lua_pushinteger( L, i );
checkLightRealloc( i );
return 1;
}
/*
> success = RL_UpdateLightValues( Shader shader, Light light )
Send light properties to shader
- Failure return false
- Success return true
*/
int llightsUpdateLightValues( lua_State *L ) {
if ( !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UpdateLightValues( Shader shader, Light light )" );
lua_pushboolean( L, false );
return 1;
}
size_t lightId = lua_tointeger( L, -1 );
lua_pop( L, 1 );
size_t shaderId = lua_tointeger( L, -1 );
if ( !validLight( lightId ) ) {
lua_pushboolean( L, false );
return 1;
}
if ( !validShader( shaderId ) ) {
lua_pushboolean( L, false );
return 1;
}
UpdateLightValues( *state->shaders[ shaderId ], *state->lights[ lightId ] );
lua_pushboolean( L, true );
return 1;
}

View File

@@ -9,7 +9,7 @@
#include "audio.h"
#include "rmath.h"
#include "rgui.h"
#include "raygui.h"
#include "lights.h"
static void assignGlobalInt( int value, const char *name ) {
lua_State *L = state->luaState;
@@ -289,6 +289,9 @@ void defineGlobals() {
assignGlobalInt( HUEBAR_PADDING, "HUEBAR_PADDING" );
assignGlobalInt( HUEBAR_SELECTOR_HEIGHT, "HUEBAR_SELECTOR_HEIGHT" );
assignGlobalInt( HUEBAR_SELECTOR_OVERFLOW, "HUEBAR_SELECTOR_OVERFLOW" );
/* LightType */
assignGlobalInt( LIGHT_DIRECTIONAL, "LIGHT_DIRECTIONAL" );
assignGlobalInt( LIGHT_POINT, "LIGHT_POINT" );
/*DOC_END*/
}
@@ -530,6 +533,8 @@ void luaRegister() {
lua_register( L, "RL_EndShaderMode", lcoreEndShaderMode );
lua_register( L, "RL_GetShaderLocation", lcoreGetShaderLocation );
lua_register( L, "RL_GetShaderLocationAttrib", lcoreGetShaderLocationAttrib );
lua_register( L, "RL_SetShaderLocationIndex", lcoreSetShaderLocationIndex );
lua_register( L, "RL_GetShaderLocationIndex", lcoreGetShaderLocationIndex );
lua_register( L, "RL_SetShaderValueMatrix", lcoreSetShaderValueMatrix );
lua_register( L, "RL_SetShaderValueTexture", lcoreSetShaderValueTexture );
lua_register( L, "RL_SetShaderValue", lcoreSetShaderValue );
@@ -879,6 +884,10 @@ void luaRegister() {
lua_register( L, "RL_GuiSetIconPixel", lguiGuiSetIconPixel );
lua_register( L, "RL_GuiClearIconPixel", lguiGuiClearIconPixel );
lua_register( L, "RL_GuiCheckIconPixel", lguiGuiCheckIconPixel );
/* Lights */
/* Basics. */
lua_register( L, "RL_CreateLight", llightsCreateLight );
lua_register( L, "RL_UpdateLightValues", llightsUpdateLightValues );
}
/* Lua util functions. */

View File

@@ -1321,7 +1321,7 @@ int lmodelsCreateMaterial( lua_State *L ) {
}
}
else if ( strcmp( "shader", (char*)lua_tostring( L, -2 ) ) == 0 && lua_isnumber( L, -1 ) ) {
/* TODO Shader when implemented. */
state->materials[i]->shader = *state->shaders[ lua_tointeger( L, -1 ) ];
}
lua_pop( L, 1 );
}

View File

@@ -61,6 +61,10 @@ bool stateInit( const char *exePath ) {
state->shaderAlloc = ALLOC_PAGE_SIZE;
state->shaderCount = 0;
state->shaders = malloc( state->shaderAlloc * sizeof( Shader* ) );
/* 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->images[i] = NULL;
@@ -72,6 +76,7 @@ bool stateInit( const char *exePath ) {
state->models[i] = NULL;
state->animations[i] = NULL;
state->shaders[i] = NULL;
state->lights[i] = NULL;
/* The ones we want to save the first. */
if ( 0 < i ) {
@@ -152,6 +157,9 @@ void stateFree() {
}
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] );
}
@@ -168,6 +176,11 @@ void stateFree() {
free( state->shaders[i] );
}
}
for ( int i = 0; i < state->lightCount; ++i ) {
if ( state->lights[i] != NULL ) {
free( state->lights[i] );
}
}
if ( IsAudioDeviceReady() ) {
CloseAudioDevice();
@@ -190,5 +203,6 @@ void stateFree() {
free( state->models );
free( state->animations );
free( state->shaders );
free( state->lights );
free( state );
}