RL.config and InitWindow.
This commit is contained in:
19
src/core.c
19
src/core.c
@@ -28,6 +28,21 @@ void unloadBuffer( Buffer* buffer ) {
|
||||
## Core - Window-related functions
|
||||
*/
|
||||
|
||||
/*
|
||||
> RL.InitWindow( Vector2 size, string title )
|
||||
|
||||
Initialize window and OpenGL context. Note! Should be called only in RL.config.
|
||||
InitWindow will still be called automatically before RL.init
|
||||
*/
|
||||
int lcoreInitWindow( lua_State* L ) {
|
||||
Vector2 size = uluaGetVector2( L, 1 );
|
||||
const char* title = lua_tostring( L, 2 );
|
||||
|
||||
InitWindow( (int)size.x, (int)size.y, title );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
> RL.CloseWindow()
|
||||
|
||||
@@ -176,7 +191,7 @@ int lcoreClearWindowState( lua_State* L ) {
|
||||
/*
|
||||
> RL.ToggleFullscreen()
|
||||
|
||||
Toggle window state: fullscreen/windowed (only PLATFORM_DESKTOP)
|
||||
Toggle window state: fullscreen/windowed, resizes monitor to match window resolution
|
||||
*/
|
||||
int lcoreToggleFullscreen( lua_State* L ) {
|
||||
ToggleFullscreen();
|
||||
@@ -187,7 +202,7 @@ int lcoreToggleFullscreen( lua_State* L ) {
|
||||
/*
|
||||
> RL.ToggleBorderlessWindowed()
|
||||
|
||||
Toggle window state: borderless windowed (only PLATFORM_DESKTOP)
|
||||
Toggle window state: borderless windowed, resizes window to match monitor resolution
|
||||
*/
|
||||
int lcoreToggleBorderlessWindowed( lua_State* L ) {
|
||||
ToggleBorderlessWindowed();
|
||||
|
||||
@@ -1149,7 +1149,7 @@ int luaTraceback( lua_State* L ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool luaCallMain() {
|
||||
void luaCallMain() {
|
||||
lua_State* L = state->luaState;
|
||||
|
||||
char path[ STRING_LEN ] = { '\0' };
|
||||
@@ -1168,6 +1168,11 @@ bool luaCallMain() {
|
||||
snprintf( path, STRING_LEN, "%smain", state->basePath );
|
||||
}
|
||||
#endif
|
||||
if ( !FileExists( path ) ) {
|
||||
TraceLog( LOG_ERROR, "Cannot find file: %s\n", path );
|
||||
state->run = false;
|
||||
return;
|
||||
}
|
||||
luaL_dofile( L, path );
|
||||
|
||||
/* Check errors in main.lua */
|
||||
@@ -1180,27 +1185,49 @@ bool luaCallMain() {
|
||||
/* Apply custom callback here. */
|
||||
SetTraceLogCallback( logCustom );
|
||||
|
||||
lua_getglobal( L, "RL" );
|
||||
lua_getfield( L, -1, "config" );
|
||||
|
||||
if ( lua_isfunction( L, -1 ) ) {
|
||||
if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
|
||||
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
|
||||
state->run = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_pop( L, -1 );
|
||||
/* If InitWindow is not called in RL.config, call it here. */
|
||||
if ( !IsWindowReady() ) {
|
||||
InitWindow( 800, 600, "ReiLua" );
|
||||
}
|
||||
/* Set shader locs after we have window. */
|
||||
if ( IsWindowReady() ) {
|
||||
stateContextInit();
|
||||
}
|
||||
else {
|
||||
state->run = false;
|
||||
}
|
||||
}
|
||||
|
||||
void luaCallInit() {
|
||||
lua_State* L = state->luaState;
|
||||
lua_pushcfunction( L, luaTraceback );
|
||||
int tracebackidx = lua_gettop(L);
|
||||
|
||||
lua_getglobal( L, "RL" );
|
||||
lua_getfield( L, -1, "init" );
|
||||
|
||||
if ( lua_isfunction( L, -1 ) ) {
|
||||
if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
|
||||
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
|
||||
return false;
|
||||
state->run = false;
|
||||
}
|
||||
}
|
||||
//TODO Should this be removed?
|
||||
else {
|
||||
TraceLog( LOG_ERROR, "%s", "No Lua init found!" );
|
||||
return false;
|
||||
}
|
||||
lua_pop( L, -1 );
|
||||
|
||||
return state->run;
|
||||
}
|
||||
|
||||
void luaCallUpdate() {
|
||||
|
||||
void luaCallUpdate() {
|
||||
#if defined PLATFORM_DESKTOP_SDL && defined LUA_EVENTS
|
||||
platformSendEvents();
|
||||
#endif
|
||||
@@ -1218,8 +1245,6 @@ void luaCallUpdate() {
|
||||
if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
|
||||
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
|
||||
state->run = false;
|
||||
lua_pop( L, -1 );
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_pop( L, -1 );
|
||||
@@ -1239,7 +1264,6 @@ void luaCallDraw() {
|
||||
if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
|
||||
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
|
||||
state->run = false;
|
||||
return;
|
||||
}
|
||||
EndDrawing();
|
||||
}
|
||||
@@ -1258,7 +1282,6 @@ void luaCallExit() {
|
||||
if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
|
||||
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
|
||||
state->run = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_pop( L, -1 );
|
||||
@@ -1270,6 +1293,7 @@ void luaRegister() {
|
||||
|
||||
/* Core. */
|
||||
/* Window-related functions. */
|
||||
assingGlobalFunction( "InitWindow", lcoreInitWindow );
|
||||
assingGlobalFunction( "CloseWindow", lcoreCloseWindow );
|
||||
assingGlobalFunction( "IsWindowReady", lcoreIsWindowReady );
|
||||
assingGlobalFunction( "IsWindowFullscreen", lcoreIsWindowFullscreen );
|
||||
|
||||
@@ -65,14 +65,15 @@ int main( int argn, const char** argc ) {
|
||||
else {
|
||||
printVersion();
|
||||
stateInit( argn, argc, basePath );
|
||||
state->run = luaCallMain();
|
||||
luaCallMain();
|
||||
luaCallInit();
|
||||
|
||||
while ( state->run ) {
|
||||
luaCallUpdate();
|
||||
luaCallDraw();
|
||||
if ( WindowShouldClose() ) {
|
||||
state->run = false;
|
||||
}
|
||||
luaCallUpdate();
|
||||
luaCallDraw();
|
||||
}
|
||||
luaCallExit();
|
||||
}
|
||||
|
||||
100
src/models.c
100
src/models.c
@@ -10,102 +10,6 @@ void unloadMaterial( Material* material ) {
|
||||
free( material->maps );
|
||||
}
|
||||
|
||||
void DrawBillboardProNoRatio( Camera camera, Texture2D texture, Rectangle source, Vector3 position, Vector3 up, Vector2 size, Vector2 origin, float rotation, Color tint ) {
|
||||
// NOTE: Billboard size will maintain source rectangle aspect ratio, size will represent billboard width
|
||||
// Vector2 sizeRatio = { size.x*(float)source.width/source.height, size.y };
|
||||
Vector2 sizeRatio = { size.x, size.y };
|
||||
|
||||
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
|
||||
|
||||
Vector3 right = { matView.m0, matView.m4, matView.m8 };
|
||||
//Vector3 up = { matView.m1, matView.m5, matView.m9 };
|
||||
|
||||
Vector3 rightScaled = Vector3Scale(right, sizeRatio.x/2);
|
||||
Vector3 upScaled = Vector3Scale(up, sizeRatio.y/2);
|
||||
|
||||
Vector3 p1 = Vector3Add(rightScaled, upScaled);
|
||||
Vector3 p2 = Vector3Subtract(rightScaled, upScaled);
|
||||
|
||||
Vector3 topLeft = Vector3Scale(p2, -1);
|
||||
Vector3 topRight = p1;
|
||||
Vector3 bottomRight = p2;
|
||||
Vector3 bottomLeft = Vector3Scale(p1, -1);
|
||||
|
||||
if (rotation != 0.0f)
|
||||
{
|
||||
float sinRotation = sinf(rotation*DEG2RAD);
|
||||
float cosRotation = cosf(rotation*DEG2RAD);
|
||||
|
||||
// NOTE: (-1, 1) is the range where origin.x, origin.y is inside the texture
|
||||
float rotateAboutX = sizeRatio.x*origin.x/2;
|
||||
float rotateAboutY = sizeRatio.y*origin.y/2;
|
||||
|
||||
float xtvalue, ytvalue;
|
||||
float rotatedX, rotatedY;
|
||||
|
||||
xtvalue = Vector3DotProduct(right, topLeft) - rotateAboutX; // Project points to x and y coordinates on the billboard plane
|
||||
ytvalue = Vector3DotProduct(up, topLeft) - rotateAboutY;
|
||||
rotatedX = xtvalue*cosRotation - ytvalue*sinRotation + rotateAboutX; // Rotate about the point origin
|
||||
rotatedY = xtvalue*sinRotation + ytvalue*cosRotation + rotateAboutY;
|
||||
topLeft = Vector3Add(Vector3Scale(up, rotatedY), Vector3Scale(right, rotatedX)); // Translate back to cartesian coordinates
|
||||
|
||||
xtvalue = Vector3DotProduct(right, topRight) - rotateAboutX;
|
||||
ytvalue = Vector3DotProduct(up, topRight) - rotateAboutY;
|
||||
rotatedX = xtvalue*cosRotation - ytvalue*sinRotation + rotateAboutX;
|
||||
rotatedY = xtvalue*sinRotation + ytvalue*cosRotation + rotateAboutY;
|
||||
topRight = Vector3Add(Vector3Scale(up, rotatedY), Vector3Scale(right, rotatedX));
|
||||
|
||||
xtvalue = Vector3DotProduct(right, bottomRight) - rotateAboutX;
|
||||
ytvalue = Vector3DotProduct(up, bottomRight) - rotateAboutY;
|
||||
rotatedX = xtvalue*cosRotation - ytvalue*sinRotation + rotateAboutX;
|
||||
rotatedY = xtvalue*sinRotation + ytvalue*cosRotation + rotateAboutY;
|
||||
bottomRight = Vector3Add(Vector3Scale(up, rotatedY), Vector3Scale(right, rotatedX));
|
||||
|
||||
xtvalue = Vector3DotProduct(right, bottomLeft)-rotateAboutX;
|
||||
ytvalue = Vector3DotProduct(up, bottomLeft)-rotateAboutY;
|
||||
rotatedX = xtvalue*cosRotation - ytvalue*sinRotation + rotateAboutX;
|
||||
rotatedY = xtvalue*sinRotation + ytvalue*cosRotation + rotateAboutY;
|
||||
bottomLeft = Vector3Add(Vector3Scale(up, rotatedY), Vector3Scale(right, rotatedX));
|
||||
}
|
||||
|
||||
// Translate points to the draw center (position)
|
||||
topLeft = Vector3Add(topLeft, position);
|
||||
topRight = Vector3Add(topRight, position);
|
||||
bottomRight = Vector3Add(bottomRight, position);
|
||||
bottomLeft = Vector3Add(bottomLeft, position);
|
||||
|
||||
rlSetTexture(texture.id);
|
||||
|
||||
rlBegin(RL_QUADS);
|
||||
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
|
||||
|
||||
// Bottom-left corner for texture and quad
|
||||
rlTexCoord2f((float)source.x/texture.width, (float)source.y/texture.height);
|
||||
rlVertex3f(topLeft.x, topLeft.y, topLeft.z);
|
||||
|
||||
// Top-left corner for texture and quad
|
||||
rlTexCoord2f((float)source.x/texture.width, (float)(source.y + source.height)/texture.height);
|
||||
rlVertex3f(bottomLeft.x, bottomLeft.y, bottomLeft.z);
|
||||
|
||||
// Top-right corner for texture and quad
|
||||
rlTexCoord2f((float)(source.x + source.width)/texture.width, (float)(source.y + source.height)/texture.height);
|
||||
rlVertex3f(bottomRight.x, bottomRight.y, bottomRight.z);
|
||||
|
||||
// Bottom-right corner for texture and quad
|
||||
rlTexCoord2f((float)(source.x + source.width)/texture.width, (float)source.y/texture.height);
|
||||
rlVertex3f(topRight.x, topRight.y, topRight.z);
|
||||
rlEnd();
|
||||
|
||||
rlSetTexture(0);
|
||||
}
|
||||
|
||||
void DrawBillboardRecNoRatio( Camera camera, Texture2D texture, Rectangle source, Vector3 position, Vector2 size, Color tint ) {
|
||||
// NOTE: Billboard locked on axis-Y
|
||||
Vector3 up = { 0.0f, 1.0f, 0.0f };
|
||||
|
||||
DrawBillboardProNoRatio(camera, texture, source, position, up, size, Vector2Zero(), 0.0f, tint);
|
||||
}
|
||||
|
||||
/*
|
||||
## Models - Basic geometric 3D shapes drawing functions
|
||||
*/
|
||||
@@ -1060,7 +964,7 @@ int lmodelsDrawBillboardRec( lua_State* L ) {
|
||||
Vector2 size = uluaGetVector2( L, 5 );
|
||||
Color tint = uluaGetColor( L, 6 );
|
||||
|
||||
DrawBillboardRecNoRatio( *camera, *texture, source, position, size, tint );
|
||||
DrawBillboardRec( *camera, *texture, source, position, size, tint );
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1081,7 +985,7 @@ int lmodelsDrawBillboardPro( lua_State* L ) {
|
||||
float rotation = luaL_checknumber( L, 8 );
|
||||
Color tint = uluaGetColor( L, 9 );
|
||||
|
||||
DrawBillboardProNoRatio( *camera, *texture, source, position, up, size, origin, rotation, tint );
|
||||
DrawBillboardPro( *camera, *texture, source, position, up, size, origin, rotation, tint );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
30
src/state.c
30
src/state.c
@@ -8,46 +8,36 @@ State* state;
|
||||
|
||||
bool stateInit( int argn, const char** argc, const char* basePath ) {
|
||||
state = malloc( sizeof( State ) );
|
||||
|
||||
state->basePath = malloc( STRING_LEN * sizeof( char ) );
|
||||
strncpy( state->basePath, basePath, STRING_LEN - 1 );
|
||||
|
||||
state->hasWindow = true;
|
||||
state->run = true;
|
||||
state->resolution = (Vector2){ 800, 600 };
|
||||
state->luaState = NULL;
|
||||
state->run = luaInit( argn, argc );;
|
||||
state->logLevelInvalid = LOG_ERROR;
|
||||
state->gcUnload = true;
|
||||
state->lineSpacing = 15;
|
||||
state->mouseOffset = (Vector2){ 0, 0 };
|
||||
state->mouseScale = (Vector2){ 1, 1 };
|
||||
|
||||
InitWindow( state->resolution.x, state->resolution.y, "ReiLua" );
|
||||
return state->run;
|
||||
}
|
||||
|
||||
if ( !IsWindowReady() ) {
|
||||
state->hasWindow = false;
|
||||
state->run = false;
|
||||
}
|
||||
if ( state->run ) {
|
||||
state->run = luaInit( argn, argc );
|
||||
}
|
||||
/* Init after InitWindow. (When there is OpenGL context.) */
|
||||
void stateContextInit() {
|
||||
state->defaultFont = GetFontDefault();
|
||||
state->guiFont = GuiGetFont();
|
||||
state->defaultMaterial = LoadMaterialDefault();
|
||||
state->defaultTexture = (Texture){ 1, 1, 1, 1, 7 };
|
||||
state->shapesTexture = (Texture){ 1, 1, 1, 1, 7 };
|
||||
state->RLGLcurrentShaderLocs = malloc( RL_MAX_SHADER_LOCATIONS * sizeof( int ) );
|
||||
#ifdef PLATFORM_DESKTOP_SDL
|
||||
state->SDL_eventQueue = malloc( PLATFORM_SDL_EVENT_QUEUE_LEN * sizeof( SDL_Event ) );
|
||||
state->SDL_eventQueueLen = 0;
|
||||
#endif
|
||||
int* defaultShaderLocs = rlGetShaderLocsDefault();
|
||||
|
||||
for ( int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++ ) {
|
||||
state->RLGLcurrentShaderLocs[i] = defaultShaderLocs[i];
|
||||
}
|
||||
#ifdef PLATFORM_DESKTOP_SDL
|
||||
state->SDL_eventQueue = malloc( PLATFORM_SDL_EVENT_QUEUE_LEN * sizeof( SDL_Event ) );
|
||||
state->SDL_eventQueueLen = 0;
|
||||
#endif
|
||||
|
||||
return state->run;
|
||||
}
|
||||
|
||||
void stateInitInterpret( int argn, const char** argc ) {
|
||||
@@ -63,7 +53,7 @@ void stateFree() {
|
||||
lua_close( state->luaState );
|
||||
state->luaState = NULL;
|
||||
}
|
||||
if ( state->hasWindow ) {
|
||||
if ( IsWindowReady() ) {
|
||||
CloseWindow();
|
||||
}
|
||||
#ifdef PLATFORM_DESKTOP_SDL
|
||||
|
||||
Reference in New Issue
Block a user