From 70b40f67824b3d612235c382aa83821c054fa51e Mon Sep 17 00:00:00 2001 From: jussi Date: Wed, 17 Apr 2024 00:35:58 +0300 Subject: Object libraries like Vector2 optimizations. --- changelog | 3 + examples/2D_lights/main.lua | 24 +++-- examples/ReiLuaGui_examples/calculator.lua | 22 ++--- examples/ReiLuaGui_examples/file_explorer.lua | 34 +++---- examples/ReiLuaGui_examples/main.lua | 6 +- examples/automation_events/main.lua | 2 +- examples/basic_lighting/main.lua | 4 +- examples/draw_textured_polygon/main.lua | 8 +- examples/free_camera3d/main.lua | 4 +- examples/n-patches/main.lua | 2 +- examples/platformer/main.lua | 6 +- examples/pong/main.lua | 6 +- examples/raygui_examples/main.lua | 4 +- examples/raygui_extensions/main.lua | 26 ++--- examples/raygui_extensions/property_list.lua | 10 +- examples/raygui_extensions/tree_item.lua | 2 +- examples/raygui_extensions/tree_view.lua | 12 +-- examples/raygui_lib/main.lua | 4 +- examples/resources/lib/camera3d.lua | 16 +-- examples/resources/lib/color.lua | 105 +++++++++++++------- examples/resources/lib/gui.lua | 24 ++--- examples/resources/lib/matrix.lua | 20 +--- examples/resources/lib/quaternion.lua | 135 ++++++++++++++------------ examples/resources/lib/raygui.lua | 85 ++++++++-------- examples/resources/lib/rectangle.lua | 81 ++++++++++++---- examples/resources/lib/rune.lua | 20 +--- examples/resources/lib/vector2.lua | 92 +++++++++++++----- examples/resources/lib/vector3.lua | 108 ++++++++++++++------- examples/rlgl_hello_triangle/main.lua | 8 +- examples/snake/main.lua | 6 +- examples/stencil_reflection/main.lua | 6 +- 31 files changed, 521 insertions(+), 364 deletions(-) diff --git a/changelog b/changelog index 0224d49..c298294 100644 --- a/changelog +++ b/changelog @@ -7,6 +7,8 @@ KEY CHANGES: - ADDED Automation events. - CHANGE: Raygui lib callbacks to single table. - ADDED OpenGL Stencil management functions. + - CHANGE: Object libraries like Vector2 optimizations. Separated table argument style for new and set methods. + Option for pre generated temp objects. There was a lot of overhead on old new method. DETAILED CHANGES: - ADDED: GetBufferElementSize and GetBufferLength. @@ -40,6 +42,7 @@ DETAILED CHANGES: - ADDED: Raygui lib examples file browser. - CHANGE: Position argument added for GetCodepoint, GetCodepointNext and GetCodepointPrevious. - ADDED glEnable, glDisable, glGetString and glClear. + - ADDED Stencil reflection example. ------------------------------------------------------------------------ Release: ReiLua version 0.7.0 Using Raylib 5.0 and Forked Raygui 4.0 diff --git a/examples/2D_lights/main.lua b/examples/2D_lights/main.lua index 34bf8bc..6a5fd81 100644 --- a/examples/2D_lights/main.lua +++ b/examples/2D_lights/main.lua @@ -15,8 +15,8 @@ local WALL_MESH_HEIGHT = math.tan( RL.DEG2RAD * ( 90 - SHADOW_FOV / 2 ) ) * LIGH print( "WALL_MESH_HEIGHT", WALL_MESH_HEIGHT ) local monitor = 0 -local monitorPos = Vector2:new( RL.GetMonitorPosition( monitor ) ) -local monitorSize = Vector2:new( RL.GetMonitorSize( monitor ) ) +local monitorPos = Vector2:newT( RL.GetMonitorPosition( monitor ) ) +local monitorSize = Vector2:newT( RL.GetMonitorSize( monitor ) ) local winScale = 1 local winSize = Vector2:new( RESOLUTION.x * winScale, RESOLUTION.y * winScale ) @@ -32,6 +32,10 @@ local shadowMesh = nil local lights = {} local camera = nil -- 3D camera for shadow rendering. +local kissa = Color:new( 255, 255, 255 ) + +print( "Kissa", kissa ) + -- Init. local function addLight( pos, color, radius ) @@ -98,19 +102,19 @@ function RL.init() lightRender = RL.LoadRenderTexture( { LIGHTRENDER_SIZE, LIGHTRENDER_SIZE } ) tileTex = RL.LoadTexture( RL.GetBasePath().."../resources/images/tile.png" ) lightTex = RL.LoadTexture( RL.GetBasePath().."../resources/images/light.png" ) - lightTexSize = Vector2:new( RL.GetTextureSize( lightTex ) ) + lightTexSize = Vector2:newT( RL.GetTextureSize( lightTex ) ) RL.SetTextureFilter( tileTex, RL.TEXTURE_FILTER_TRILINEAR ) RL.SetTextureFilter( lightTex, RL.TEXTURE_FILTER_TRILINEAR ) createShadowMesh() - addLight( Vector2:new( 230, 480 ), Color:new( RL.ORANGE ), 512 ) - addLight( Vector2:new( 600, 200 ), Color:new( RL.RED ), 512 ) - addLight( Vector2:new( 384, 520 ), Color:new( RL.GREEN ), 400 ) - addLight( Vector2:new( 880, 750 ), Color:new( RL.BLUE ), 300 ) - addLight( Vector2:new( 800, 500 ), Color:new( RL.PURPLE ), 512 ) - addLight( Vector2:new( 200, 760 ), Color:new( RL.WHITE ), 400 ) + addLight( Vector2:new( 230, 480 ), Color:newT( RL.ORANGE ), 512 ) + addLight( Vector2:new( 600, 200 ), Color:newT( RL.RED ), 512 ) + addLight( Vector2:new( 384, 520 ), Color:newT( RL.GREEN ), 400 ) + addLight( Vector2:new( 880, 750 ), Color:newT( RL.BLUE ), 300 ) + addLight( Vector2:new( 800, 500 ), Color:newT( RL.PURPLE ), 512 ) + addLight( Vector2:new( 200, 760 ), Color:newT( RL.WHITE ), 400 ) -- Stress test @@ -136,7 +140,7 @@ end -- Update. function RL.update( delta ) - lights[1].pos:set( RL.GetMousePosition() ) + lights[1].pos:setT( RL.GetMousePosition() ) end -- Drawing. diff --git a/examples/ReiLuaGui_examples/calculator.lua b/examples/ReiLuaGui_examples/calculator.lua index a2ddfdf..f6fcb7c 100644 --- a/examples/ReiLuaGui_examples/calculator.lua +++ b/examples/ReiLuaGui_examples/calculator.lua @@ -20,7 +20,7 @@ function Calculator:new( pos ) padding = 10, onClicked = function() object:set2Top() - object.dragPos = Vec2:new( RL.GetMousePosition() ) - Vec2:new( object.handle.bounds.x, object.handle.bounds.y ) + object.dragPos = Vec2:newT( RL.GetMousePosition() ) - Vec2:new( object.handle.bounds.x, object.handle.bounds.y ) Gui.heldCallback = function() object:drag() end end, } ) @@ -30,7 +30,7 @@ function Calculator:new( pos ) texture = BgrTexture, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER, - color = Color:new( RL.LIGHTGRAY ), + color = Color:newT( RL.LIGHTGRAY ), } ) ) object.handle:add( Gui.texture:new( { @@ -38,7 +38,7 @@ function Calculator:new( pos ) texture = BorderTexture, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER, - color = Color:new( RL.LIGHTGRAY ), + color = Color:newT( RL.LIGHTGRAY ), nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = RL.NPATCH_NINE_PATCH }, } ) ) @@ -51,8 +51,8 @@ function Calculator:new( pos ) onClicked = function() object:setVisible( false ) end, - onMouseOver = function( self ) self.items[1].color = Color:new( RL.WHITE ) end, - notMouseOver = function( self ) self.items[1].color = Color:new( RL.BLACK ) end, + onMouseOver = function( this ) this.items[1].color = Color:newT( RL.WHITE ) end, + notMouseOver = function( this ) this.items[1].color = Color:newT( RL.BLACK ) end, } ) object.closeButton:add( Gui.texture:new( { @@ -73,7 +73,7 @@ function Calculator:new( pos ) texture = BgrTexture, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER, - color = Color:new( RL.GRAY ), + color = Color:newT( RL.GRAY ), } ) ) object.panel:add( Gui.texture:new( { @@ -81,7 +81,7 @@ function Calculator:new( pos ) texture = BorderTexture, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER, - color = Color:new( RL.LIGHTGRAY ), + color = Color:newT( RL.LIGHTGRAY ), nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = RL.NPATCH_NINE_PATCH }, } ) ) @@ -91,7 +91,7 @@ function Calculator:new( pos ) bounds = Rect:new( 0, 0, object.windowRect.width - 16, object.DISPLAY_HIGHT ), padding = 10, drawBounds = true, - color = Color:new( RL.WHITE ) + color = Color:newT( RL.WHITE ) } ) object.display:add( Gui.text:new( { text = "", fontSize = 30, VAling = Gui.ALING.CENTER, maxTextLen = 8 } ) ) @@ -108,8 +108,8 @@ function Calculator:new( pos ) table.insert( object.buttons, Gui.element:new( { bounds = Rect:new( 0, 0, 40, 32 ), drawBounds = true, - onMouseOver = function( self ) self.color = Color:new( RL.WHITE ) end, - notMouseOver = function( self ) self.color = Color:new( RL.LIGHTGRAY ) end, + onMouseOver = function( self ) self.color = Color:newT( RL.WHITE ) end, + notMouseOver = function( self ) self.color = Color:newT( RL.LIGHTGRAY ) end, } ) ) object.buttons[ #object.buttons ].pos = Vec2:new( 8 + x * 46, object.HANDLE_HIGHT + object.DISPLAY_HIGHT + 16 + y * 38 ) @@ -161,7 +161,7 @@ function Calculator:setPosition( pos ) end function Calculator:drag() - local mousePos = Vec2:new( RL.GetMousePosition() ) + local mousePos = Vec2:newT( RL.GetMousePosition() ) local winPos = Vec2:new( self.handle.bounds.x, self.handle.bounds.y ) self:setPosition( mousePos - self.dragPos ) diff --git a/examples/ReiLuaGui_examples/file_explorer.lua b/examples/ReiLuaGui_examples/file_explorer.lua index 7ec6104..398adef 100644 --- a/examples/ReiLuaGui_examples/file_explorer.lua +++ b/examples/ReiLuaGui_examples/file_explorer.lua @@ -20,7 +20,7 @@ function FileExplorer:new( pos ) padding = 10, onClicked = function() object:set2Top() - object.dragPos = Vec2:new( RL.GetMousePosition() ) - Vec2:new( object.handle.bounds.x, object.handle.bounds.y ) + object.dragPos = Vec2:newT( RL.GetMousePosition() ) - Vec2:new( object.handle.bounds.x, object.handle.bounds.y ) Gui.heldCallback = function() object:drag() end end, } ) @@ -30,7 +30,7 @@ function FileExplorer:new( pos ) texture = BgrTexture, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER, - color = Color:new( RL.LIGHTGRAY ), + color = Color:newT( RL.LIGHTGRAY ), } ) ) object.handle:add( Gui.texture:new( { @@ -38,7 +38,7 @@ function FileExplorer:new( pos ) texture = BorderTexture, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER, - color = Color:new( RL.LIGHTGRAY ), + color = Color:newT( RL.LIGHTGRAY ), nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = RL.NPATCH_NINE_PATCH }, } ) ) @@ -51,8 +51,8 @@ function FileExplorer:new( pos ) onClicked = function() object:setVisible( false ) end, - onMouseOver = function( self ) self.items[1].color = Color:new( RL.WHITE ) end, - notMouseOver = function( self ) self.items[1].color = Color:new( RL.BLACK ) end, + onMouseOver = function( self ) self.items[1].color = Color:newT( RL.WHITE ) end, + notMouseOver = function( self ) self.items[1].color = Color:newT( RL.BLACK ) end, } ) object.closeButton:add( Gui.texture:new( { @@ -73,7 +73,7 @@ function FileExplorer:new( pos ) texture = BgrTexture, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER, - color = Color:new( RL.GRAY ), + color = Color:newT( RL.GRAY ), } ) ) object.panel:add( Gui.texture:new( { @@ -81,7 +81,7 @@ function FileExplorer:new( pos ) texture = BorderTexture, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER, - color = Color:new( RL.LIGHTGRAY ), + color = Color:newT( RL.LIGHTGRAY ), nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = RL.NPATCH_NINE_PATCH }, } ) ) @@ -90,7 +90,7 @@ function FileExplorer:new( pos ) object.pathBox = Gui.element:new( { bounds = Rect:new( 0, 0, object.windowRect.width - 16 - 64, object.HANDLE_HIGHT ), drawBounds = true, - color = Color:new( RL.WHITE ), + color = Color:newT( RL.WHITE ), -- onClicked = function() Gui.setInputFocus( object.pathBox ) end, -- inputFocus = function() object.pathBox.color = Color:new( BLUE ) end, -- inputUnfocus = function() object.pathBox.color = Color:new( WHITE ) end, @@ -103,8 +103,8 @@ function FileExplorer:new( pos ) object.backButton = Gui.element:new( { bounds = Rect:new( 0, 0, 56, object.HANDLE_HIGHT ), drawBounds = true, - onMouseOver = function( self ) self.color = Color:new( RL.WHITE ) end, - notMouseOver = function( self ) self.color = Color:new( RL.LIGHTGRAY ) end, + onMouseOver = function( self ) self.color = Color:newT( RL.WHITE ) end, + notMouseOver = function( self ) self.color = Color:newT( RL.LIGHTGRAY ) end, onClicked = function() object:backDir() end, } ) @@ -112,7 +112,7 @@ function FileExplorer:new( pos ) bounds = Rect:new( 0, 0, object.HANDLE_HIGHT, object.HANDLE_HIGHT ), texture = BackTexture, HAling = Gui.ALING.CENTER, - color = Color:new( RL.BLACK ) + color = Color:newT( RL.BLACK ) } ) ) -- Files. @@ -130,7 +130,7 @@ function FileExplorer:new( pos ) object.fileName = Gui.element:new( { bounds = Rect:new( 0, 0, object.windowRect.width - 16 - 70, object.HANDLE_HIGHT ), drawBounds = true, - color = Color:new( RL.WHITE ), + color = Color:newT( RL.WHITE ), } ) object.fileName:add( Gui.text:new( { text = "", maxTextLen = 32, allowLineBreak = false, VAling = Gui.ALING.CENTER } ) ) @@ -140,10 +140,10 @@ function FileExplorer:new( pos ) object.openButton = Gui.element:new( { bounds = Rect:new( 0, 0, 64, object.HANDLE_HIGHT ), drawBounds = true, - color = Color:new( RL.WHITE ), + color = Color:newT( RL.WHITE ), onClicked = function() object:openFile() end, - onMouseOver = function( self ) self.color = Color:new( RL.WHITE ) end, - notMouseOver = function( self ) self.color = Color:new( RL.LIGHTGRAY ) end, + onMouseOver = function( self ) self.color = Color:newT( RL.WHITE ) end, + notMouseOver = function( self ) self.color = Color:newT( RL.LIGHTGRAY ) end, } ) object.openButton:add( Gui.text:new( { text = "Open", VAling = Gui.ALING.CENTER, HAling = Gui.ALING.CENTER } ) ) @@ -257,14 +257,14 @@ function FileExplorer:addFileToList( file, texture, color, func ) bounds = Rect:new( 0, 0, 20, 20 ), texture = texture, VAling = Gui.ALING.CENTER, - color = Color:new( color ), + color = Color:newT( color ), } ) ) element.bounds.width = element.items[1].bounds.width + element.padding + element.items[2].bounds.width end function FileExplorer:drag() - local mousePos = Vec2:new( RL.GetMousePosition() ) + local mousePos = Vec2:newT( RL.GetMousePosition() ) local winPos = Vec2:new( self.handle.bounds.x, self.handle.bounds.y ) self:setPosition( mousePos - self.dragPos ) diff --git a/examples/ReiLuaGui_examples/main.lua b/examples/ReiLuaGui_examples/main.lua index bc9b94e..c994dfa 100644 --- a/examples/ReiLuaGui_examples/main.lua +++ b/examples/ReiLuaGui_examples/main.lua @@ -51,8 +51,8 @@ local function initGui() calculator:setVisible( true ) fileExplorer:setVisible( true ) end, - onMouseOver = function( self ) self.color = Color:new( RL.LIGHTGRAY ) end, - notMouseOver = function( self ) self.color = Color:new( RL.GRAY ) end, + onMouseOver = function( self ) self.color = Color:newT( RL.LIGHTGRAY ) end, + notMouseOver = function( self ) self.color = Color:newT( RL.GRAY ) end, } ) showButton:add( Gui.text:new( { text = "Show", VAling = Gui.ALING.CENTER, HAling = Gui.ALING.CENTER } ) ) @@ -77,7 +77,7 @@ function RL.init() end function RL.update( delta ) - Gui.update( Vec2:new( RL.GetMousePosition() ) ) + Gui.update( Vec2:newT( RL.GetMousePosition() ) ) end function RL.draw() diff --git a/examples/automation_events/main.lua b/examples/automation_events/main.lua index 33589a4..3425ac6 100644 --- a/examples/automation_events/main.lua +++ b/examples/automation_events/main.lua @@ -23,7 +23,7 @@ function RL.init() end function RL.update( delta ) - local moveDir = Vec2:new() + local moveDir = Vec2:new( 0, 0 ) if RL.IsKeyDown( RL.KEY_RIGHT ) then moveDir.x = 1 diff --git a/examples/basic_lighting/main.lua b/examples/basic_lighting/main.lua index 1a5417e..52552c3 100644 --- a/examples/basic_lighting/main.lua +++ b/examples/basic_lighting/main.lua @@ -18,8 +18,8 @@ local shader = -1 local lights = {} function RL.init() - local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) - local mSize = Vec2:new( RL.GetMonitorSize( monitor ) ) + local mPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) + local mSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) local winSize = Vec2:new( 1028, 720 ) RL.SetWindowTitle( "Simple Lighting" ) diff --git a/examples/draw_textured_polygon/main.lua b/examples/draw_textured_polygon/main.lua index 391d627..ddf5efc 100644 --- a/examples/draw_textured_polygon/main.lua +++ b/examples/draw_textured_polygon/main.lua @@ -5,9 +5,9 @@ package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" Vec2 = require( "vector2" ) local monitor = 0 -local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) -local mSize = Vec2:new( RL.GetMonitorSize( monitor ) ) -local winSize = Vec2:new( RL.GetScreenSize() ) +local mPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) +local mSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) +local winSize = Vec2:newT( RL.GetScreenSize() ) local polygon = { texture = -1, @@ -56,7 +56,7 @@ function RL.update( delta ) -- Update points rotation with an angle transform -- NOTE: Base points position are not modified for i = 1, #polygon.points do - polygon.positions[i] = Vec2:new( RL.Vector2Rotate( polygon.points[i], polygon.angle ) ) + polygon.positions[i] = Vec2:newT( RL.Vector2Rotate( polygon.points[i], polygon.angle ) ) end end diff --git a/examples/free_camera3d/main.lua b/examples/free_camera3d/main.lua index 842adc8..342751e 100644 --- a/examples/free_camera3d/main.lua +++ b/examples/free_camera3d/main.lua @@ -10,8 +10,8 @@ local camera = {} local targetVisible = false function RL.init() - local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) - local mSize = Vec2:new( RL.GetMonitorSize( monitor ) ) + local mPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) + local mSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) local winSize = Vec2:new( 1028, 720 ) RL.SetWindowTitle( "Free Camera3D" ) diff --git a/examples/n-patches/main.lua b/examples/n-patches/main.lua index aba3d2c..c45b828 100644 --- a/examples/n-patches/main.lua +++ b/examples/n-patches/main.lua @@ -21,7 +21,7 @@ function RL.init() end function RL.update( delta ) - local mousePosition = Vec2:new( RL.GetMousePosition() ) + local mousePosition = Vec2:newT( RL.GetMousePosition() ) -- Resize the n-patch based on mouse position dstRec.width = mousePosition.x - dstRec.x; diff --git a/examples/platformer/main.lua b/examples/platformer/main.lua index 878311f..6c8e0c6 100644 --- a/examples/platformer/main.lua +++ b/examples/platformer/main.lua @@ -81,8 +81,8 @@ local function createMap() end function RL.init() - local monitorPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) - local monitorSize = Vec2:new( RL.GetMonitorSize( monitor ) ) + local monitorPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) + local monitorSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) RL.SetWindowTitle( "Platformer" ) RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE ) @@ -223,7 +223,7 @@ end function RL.update( delta ) if RL.IsWindowResized() then - winSize:set( RL.GetScreenSize() ) + winSize:setT( RL.GetScreenSize() ) end playerMovement( delta ) diff --git a/examples/pong/main.lua b/examples/pong/main.lua index f0867a6..4211d3f 100644 --- a/examples/pong/main.lua +++ b/examples/pong/main.lua @@ -54,8 +54,8 @@ end function RL.init() -- Set window to center of monitor. - local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) - local mSize = Vec2:new( RL.GetMonitorSize( monitor ) ) + local mPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) + local mSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) RL.SetConfigFlags( RL.FLAG_VSYNC_HINT ) RL.SetWindowSize( winSize ) @@ -125,6 +125,6 @@ function RL.draw() -- Draw score. RL.DrawText( tostring( playerLeft.score ), { 50, 10 }, 40, RL.WHITE ) - local rightTextSize = Vec2:new( RL.MeasureTextEx( RL.GetFontDefault(), tostring( playerRight.score ), 40, 2 ) ) + local rightTextSize = Vec2:newT( RL.MeasureTextEx( RL.GetFontDefault(), tostring( playerRight.score ), 40, 2 ) ) RL.DrawText( tostring( playerRight.score ), { winSize.x - 50 - rightTextSize.x, 10 }, 40, RL.WHITE ) end diff --git a/examples/raygui_examples/main.lua b/examples/raygui_examples/main.lua index 2fcf4c5..e0e7ac3 100644 --- a/examples/raygui_examples/main.lua +++ b/examples/raygui_examples/main.lua @@ -26,8 +26,8 @@ end function RL.init() local monitor = 0 - local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) - local mSize = Vec2:new( RL.GetMonitorSize( monitor ) ) + local mPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) + local mSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) local winSize = Vec2:new( 1024, 800 ) RL.SetWindowTitle( "Raygui examples" ) diff --git a/examples/raygui_extensions/main.lua b/examples/raygui_extensions/main.lua index cdfd7b8..a5d9760 100644 --- a/examples/raygui_extensions/main.lua +++ b/examples/raygui_extensions/main.lua @@ -19,11 +19,11 @@ local buttonTexture = nil local winSize = Vec2:new( 1024, 720 ) local cat = { texture = nil, - source = Rect:new(), - dest = Rect:new(), - origin = Vec2:new(), + source = Rect:new( 0, 0, 0, 0 ), + dest = Rect:new( 0, 0, 0, 0 ), + origin = Vec2:new( 0, 0 ), rotation = 0.0, - tint = Color:new( RL.WHITE ), + tint = Color:newT( RL.WHITE ), visible = true, flipped = false } @@ -88,7 +88,7 @@ local function addPropertyList() RL.GuiSetStyle( RL.SPINNER, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT ) PropertyList:addControl( PropertyList.gui:Line( - Rect:new(), + Rect:new( 0, 0, 0, 0 ), "Cat Texture" ) ) @@ -98,7 +98,7 @@ local function addPropertyList() -- Position. PropertyList:addControl( PropertyList.gui:Label( - Rect:new(), + Rect:new( 0, 0, 0, 0 ), "Position:" ), transformGroup ) PropertyList:addControl( PropertyList.gui:TextBox( @@ -133,7 +133,7 @@ local function addPropertyList() ), transformGroup ) -- Origin. PropertyList:addControl( PropertyList.gui:Label( - Rect:new(), + Rect:new( 0, 0, 0, 0 ), "Origin:" ), transformGroup ) PropertyList:addControl( PropertyList.gui:TextBox( @@ -219,19 +219,19 @@ local function addPropertyList() PropertyList:addControl( PropertyList.gui:ColorPicker( Rect:new( 0, 0, 128, 128 ), "Color Picker", - Color:new(), + Color:new( 255, 255, 255, 255 ), { -- Callbacks. edit = function( self ) cat.tint = self.color end } ), tintGroup ) PropertyList:addControl( PropertyList.gui:Line( - Rect:new(), + Rect:new( 0, 0, 0, 0 ), "Testing" ) ) PropertyList:addControl( PropertyList.gui:DropdownBox( - Rect:new(), + Rect:new( 0, 0, 0, 0 ), "Dog\nGiraffe\nLion\nHorse", 0, false, @@ -298,8 +298,8 @@ end function RL.init() local monitor = 0 - local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) - local mSize = Vec2:new( RL.GetMonitorSize( monitor ) ) + local mPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) + local mSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE ) RL.SetWindowState( RL.FLAG_VSYNC_HINT ) @@ -312,7 +312,7 @@ function RL.init() -- RL.GuiSetStyle( RL.SPINNER, RL.TEXT_PADDING, 2 ) cat.texture = RL.LoadTexture( RL.GetBasePath().."../resources/images/cat.png" ) - local texSize = Vec2:new( RL.GetTextureSize( cat.texture ) ) + local texSize = Vec2:newT( RL.GetTextureSize( cat.texture ) ) cat.source:set( 0, 0, texSize.x, texSize.y ) cat.dest = cat.source:clone() diff --git a/examples/raygui_extensions/property_list.lua b/examples/raygui_extensions/property_list.lua index d379d72..1a8c30f 100644 --- a/examples/raygui_extensions/property_list.lua +++ b/examples/raygui_extensions/property_list.lua @@ -10,8 +10,8 @@ function PropertyList:new( bounds, text, callbacks, styles, tooltip ) object.bounds = bounds:clone() object.text = text - object.scroll = Vec2:new() - object.view = Rect:new() + object.scroll = Vec2:new( 0, 0 ) + object.view = Rect:new( 0, 0, 0, 0 ) object.callbacks = callbacks -- scroll, grab, drag. object.styles = styles object.tooltip = tooltip @@ -183,8 +183,8 @@ end function PropertyList:draw() local oldScroll = self.scroll:clone() local _, scroll, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) - self.view:set( view ) - self.scroll:set( scroll ) + self.view:setT( view ) + self.scroll:setT( scroll ) if self.scroll ~= oldScroll or self._forceCheckScroll then if not self._forceCheckScroll then @@ -237,7 +237,7 @@ function PropertyList:setSize( size ) self.defaultControlSize = Vec2:new( self.content.width, self.defaultControlHeight ) local _, _, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) - self.view = Rect:new( view ) + self.view = Rect:newT( view ) self.gui.view = Rect:new( 0, 0, self.view.width, self.view.height ) self.framebufferSize = Vec2:new( self.bounds.width, self.bounds.height - self.gui.RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT ) diff --git a/examples/raygui_extensions/tree_item.lua b/examples/raygui_extensions/tree_item.lua index 1986faf..919eae0 100644 --- a/examples/raygui_extensions/tree_item.lua +++ b/examples/raygui_extensions/tree_item.lua @@ -42,7 +42,7 @@ function TreeItem:setOpenIcon() end function TreeItem:draw() - local buttonRect = Rect:new() + local buttonRect = Rect:new( 0, 0, 0, 0 ) local hasContainer = 0 < #self.controls local lineCol = RL.GetColor( RL.GuiGetStyle( RL.DEFAULT, RL.LINE_COLOR ) ) diff --git a/examples/raygui_extensions/tree_view.lua b/examples/raygui_extensions/tree_view.lua index ac73d4a..055694d 100644 --- a/examples/raygui_extensions/tree_view.lua +++ b/examples/raygui_extensions/tree_view.lua @@ -20,8 +20,8 @@ function TreeView:new( bounds, text, callbacks, styles, tooltip ) object.bounds = bounds:clone() object.text = text - object.scroll = Vec2:new() - object.view = Rect:new() + object.scroll = Vec2:new( 0, 0 ) + object.view = Rect:new( 0, 0, 0, 0 ) object.callbacks = callbacks -- select, grab, drag. object.styles = styles object.tooltip = tooltip @@ -269,7 +269,7 @@ function TreeView:itemSelect( item ) end function TreeView:update() - local mousePos = Vec2:new( RL.GetMousePosition() ) + local mousePos = Vec2:newT( RL.GetMousePosition() ) local guiMousePos = mousePos + self.gui.mouseOffset local mouseInView = self.view:checkCollisionPoint( mousePos ) @@ -336,8 +336,8 @@ end function TreeView:draw() local oldScroll = self.scroll:clone() local _, scroll, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) - self.view:set( view ) - self.scroll:set( scroll ) + self.view:setT( view ) + self.scroll:setT( scroll ) if self.scroll ~= oldScroll or self._forceCheckScroll then if not self._forceCheckScroll then @@ -386,7 +386,7 @@ function TreeView:setSize( size ) self.defaultControlSize = Vec2:new( self.content.width, self.defaultControlHeight ) local _, _, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) - self.view = Rect:new( view ) + self.view = Rect:newT( view ) self.gui.view = Rect:new( 0, 0, self.view.width, self.view.height ) self.framebufferSize = Vec2:new( self.bounds.width, self.bounds.height - self.gui.RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT ) diff --git a/examples/raygui_lib/main.lua b/examples/raygui_lib/main.lua index 1539ea3..95a733f 100644 --- a/examples/raygui_lib/main.lua +++ b/examples/raygui_lib/main.lua @@ -323,13 +323,13 @@ function RL.init() local colorpicker = Gui:ColorPicker( Rect:new( 1500, 32, 128, 128 ), "Color Picker", - Color:new(), + Color:new( 255, 255, 255, 255 ), {} -- Callbacks. ) local colorpanel = Gui:ColorPanel( Rect:new( 1700, 32, 128, 128 ), "Color Panel", - Color:new(), + Color:new( 255, 255, 255, 255 ), {} -- Callbacks. ) local colorbaralpha = Gui:ColorBarAlpha( diff --git a/examples/resources/lib/camera3d.lua b/examples/resources/lib/camera3d.lua index 9b763c2..2c84b31 100644 --- a/examples/resources/lib/camera3d.lua +++ b/examples/resources/lib/camera3d.lua @@ -61,15 +61,15 @@ function Camera3D:setProjection( projection ) end function Camera3D:getPosition() - return Vec3:new( RL.GetCamera3DPosition( self.camera ) ) + return Vec3:newT( RL.GetCamera3DPosition( self.camera ) ) end function Camera3D:getTarget() - return Vec3:new( RL.GetCamera3DTarget( self.camera ) ) + return Vec3:newT( RL.GetCamera3DTarget( self.camera ) ) end function Camera3D:getUp() - return Vec3:new( RL.GetCamera3DUp( self.camera ) ) + return Vec3:newT( RL.GetCamera3DUp( self.camera ) ) end function Camera3D:getFoyv() @@ -82,18 +82,18 @@ end --- Returns the cameras forward vector ( normalized ) function Camera3D:getForward() - return Vec3:new( RL.GetCamera3DForward( self.camera ) ) + return Vec3:newT( RL.GetCamera3DForward( self.camera ) ) end --- Returns the cameras up vector ( normalized ) Note: The up vector might not be perpendicular to the forward vector function Camera3D:getUpward() - return Vec3:new( RL.GetCamera3DUpNormalized( self.camera ) ) + return Vec3:newT( RL.GetCamera3DUpNormalized( self.camera ) ) end function Camera3D:update( delta ) if self.mode == self.MODES.FREE then if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_MIDDLE ) then - local mouseDelta = Vec2:new( RL.GetMouseDelta() ) + local mouseDelta = Vec2:newT( RL.GetMouseDelta() ) if RL.IsKeyDown( self.KEYS.PAN ) then mouseDelta = mouseDelta:scale( self.MOUSE_MOVE_SPEED * delta ) @@ -116,13 +116,13 @@ function Camera3D:update( delta ) RL.Camera3DMoveToTarget( self.camera, self.ZOOM_AMOUNT * self:getTargetDistance() * -mouseScroll ) end elseif self.mode == self.MODES.FIRST_PERSON then - local mouseDelta = Vec2:new( RL.GetMouseDelta() ) + local mouseDelta = Vec2:newT( RL.GetMouseDelta() ) mouseDelta = mouseDelta:scale( self.TURN_SPEED * delta ) RL.Camera3DYaw( self.camera, -mouseDelta.x, false ) RL.Camera3DPitch( self.camera, -mouseDelta.y, false, false, false ) - RL.SetMousePosition( Vec2:new( RL.GetScreenSize() ):scale( 0.5 ) ) + RL.SetMousePosition( Vec2:newT( RL.GetScreenSize() ):scale( 0.5 ) ) local distance = self.KEYBOARD_MOVE_SPEED * delta diff --git a/examples/resources/lib/color.lua b/examples/resources/lib/color.lua index a508fa3..a3f22f7 100644 --- a/examples/resources/lib/color.lua +++ b/examples/resources/lib/color.lua @@ -47,41 +47,33 @@ local metatable = { } function Color:new( r, g, b, a ) - if type( r ) == "table" then - r, g, b, a = table.unpack( r ) - elseif type( r ) == "nil" then - r, g, b, a = 0, 0, 0, 255 - end + local object = setmetatable( {}, metatable ) - if a == nil then - a = 255 - end + object.r = r or 255 + object.g = g or 255 + object.b = b or 255 + object.a = a or 255 + return object +end + +function Color:newT( t ) local object = setmetatable( {}, metatable ) - object.r = r - object.g = g - object.b = b - object.a = a + object.r, object.g, object.b, object.a = table.unpack( t ) - return object + return object end function Color:set( r, g, b, a ) - if type( r ) == "table" then - r, g, b, a = table.unpack( r ) - elseif type( r ) == "nil" then - r, g, b, a = 0, 0, 0, 255 - end - - if a == nil then - a = 255 - end + self.r = r or 255 + self.g = g or 255 + self.b = b or 255 + self.a = a or 255 +end - self.r = r - self.g = g - self.b = b - self.a = a +function Color:setT( t ) + self.r, self.g, self.b, self.a = table.unpack( t ) end function Color:arr() @@ -101,7 +93,7 @@ function Color:scale( scalar ) end function Color:fade( alpha ) - return Color:new( RL.Fade( self, alpha ) ) + return Color:newT( RL.Fade( self, alpha ) ) end function Color:toHex() @@ -109,7 +101,7 @@ function Color:toHex() end function Color:fromHex( hexValue ) - return Color:new( RL.GetColor( hexValue ) ) + return Color:newT( RL.GetColor( hexValue ) ) end function Color:getNormalized() @@ -117,31 +109,31 @@ function Color:getNormalized() end function Color:fromNormalized( normalized ) - return Color:new( RL.ColorFromNormalized( normalized ) ) + return Color:newT( RL.ColorFromNormalized( normalized ) ) end function Color:toHSV() - return Vector3:new( RL.ColorToHSV( self ) ) + return Vector3:newT( RL.ColorToHSV( self ) ) end function Color:fromHSV( hue, saturation, value ) - return Color:new( RL.ColorFromHSV( hue, saturation, value ) ) + return Color:newT( RL.ColorFromHSV( hue, saturation, value ) ) end function Color:tint( tint ) - return Color:new( RL.ColorTint( self, tint ) ) + return Color:newT( RL.ColorTint( self, tint ) ) end function Color:brightness( factor ) - return Color:new( RL.ColorBrightness( self, factor ) ) + return Color:newT( RL.ColorBrightness( self, factor ) ) end function Color:contrast( contrast ) - return Color:new( RL.ColorContrast( self, contrast ) ) + return Color:newT( RL.ColorContrast( self, contrast ) ) end function Color:alphaBlend( dst, src, tint ) - return Color:new( RL.ColorAlphaBlend( dst, src, tint ) ) + return Color:newT( RL.ColorAlphaBlend( dst, src, tint ) ) end function Color:lerp( color, amount ) @@ -153,4 +145,47 @@ function Color:lerp( color, amount ) ) end +local TEMP_COUNT = 100 +local tempPool = {} +local curTemp = 1 + +for _ = 1, TEMP_COUNT do + table.insert( tempPool, Color:new( 255, 255, 255, 255 ) ) +end + +-- Uses pre generated objects to avoid "slow" table generation. +function Color:temp( r, g, b, a ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.r = r or 255 + object.g = g or 255 + object.b = b or 255 + object.a = a or 255 + + return object +end + +-- Uses pre generated objects to avoid "slow" table generation. +function Color:tempT( t ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.r, object.g, object.b, object.a = table.unpack( t ) + + return object +end + +function Color:getTempId() + return curTemp +end + return Color diff --git a/examples/resources/lib/gui.lua b/examples/resources/lib/gui.lua index 413fcf6..2bae0eb 100644 --- a/examples/resources/lib/gui.lua +++ b/examples/resources/lib/gui.lua @@ -224,7 +224,7 @@ function Text:new( set ) object.text = setProperty( set, "text", "" ) object.fontSize = setProperty( set, "fontSize", Gui.fontSize ) object.spacing = setProperty( set, "spacing", Gui.spacing ) - object.color = setProperty( set, "color", Color:new( RL.BLACK ) ) + object.color = setProperty( set, "color", Color:newT( RL.BLACK ) ) object.maxTextLen = setProperty( set, "maxTextLen", nil ) object.allowLineBreak = setProperty( set, "allowLineBreak", false ) @@ -243,7 +243,7 @@ function Text:set( text ) self.text = text end - local textSize = Vec2:new( RL.MeasureTextEx( self.font, self.text, self.fontSize, self.spacing ) ) + local textSize = Vec2:newT( RL.MeasureTextEx( self.font, self.text, self.fontSize, self.spacing ) ) self.bounds.width = textSize.x self.bounds.height = textSize.y @@ -273,7 +273,7 @@ function Texture:new( set ) object.source = setProperty( set, "source", Rect:new( 0, 0, 0, 0 ) ) object.origin = setProperty( set, "origin", Vec2:new( 0, 0 ) ) object.rotation = setProperty( set, "rotation", 0 ) - object.color = setProperty( set, "color", Color:new( RL.WHITE ) ) + object.color = setProperty( set, "color", Color:newT( RL.WHITE ) ) object.nPatchInfo = setProperty( set, "nPatchInfo", nil ) object.visible = setProperty( set, "visible", true ) @@ -289,7 +289,7 @@ function Texture:set( texture ) return end - local texSize = Vec2:new( RL.GetTextureSize( texture ) ) + local texSize = Vec2:newT( RL.GetTextureSize( texture ) ) if self.bounds.width == 0 or self.bounds.height == 0 then self.bounds.width = texSize.x @@ -349,7 +349,7 @@ function Shape:new( set ) object.roundness = setProperty( set, "roundness", 1 ) object.segments = setProperty( set, "segments", 4 ) - object.color = setProperty( set, "color", Color:new( RL.WHITE ) ) + object.color = setProperty( set, "color", Color:newT( RL.WHITE ) ) object.visible = setProperty( set, "visible", true ) object._parent = nil @@ -418,7 +418,7 @@ function Element:new( set ) object.visible = setProperty( set, "visible", true ) object.disabled = setProperty( set, "disabled", false ) object.drawBounds = setProperty( set, "drawBounds", false ) - object.color = setProperty( set, "color", Color:new( RL.GRAY ) ) + object.color = setProperty( set, "color", Color:newT( RL.GRAY ) ) object.items = {} @@ -488,7 +488,7 @@ function Element:draw() local usedScissor = false if self._visibilityBounds ~= nil then - local rect = Rect:new( RL.GetCollisionRec( self.bounds, self._visibilityBounds ) ) + local rect = Rect:newT( RL.GetCollisionRec( self.bounds, self._visibilityBounds ) ) -- Use scissor mode only on partyally visible. if rect.width == 0 and rect.height == 0 then @@ -547,7 +547,7 @@ function Container:new( set ) object.showScrollbar = setProperty( set, "showScrollbar", false ) object.scrollbarWidth = setProperty( set, "scrollbarWidth", Gui.scrollbarWidth ) object.scrollAmount = setProperty( set, "scrollAmount", Gui.scrollAmount ) -- When using mouse scroll. - object.color = setProperty( set, "color", Color:new( RL.WHITE ) ) + object.color = setProperty( set, "color", Color:newT( RL.WHITE ) ) object.drawBounds = setProperty( set, "drawBounds", false ) object.drawScrollRect = setProperty( set, "drawScrollRect", false ) -- For grid container. Do not set both. @@ -691,7 +691,7 @@ function Container:update() self._VScrollbar = Element:new( { padding = 0, drawBounds = true, - color = Color:new( RL.GRAY ), + color = Color:newT( RL.GRAY ), onClicked = function() Gui.heldCallback = function() self:mouseScroll( Vec2:new( 0, 1 ) ) end end, } ) @@ -700,7 +700,7 @@ function Container:update() VAling = Gui.ALING.NONE, bounds = Rect:new( 0, 0, 0, 0 ), shape = Gui.SHAPE.RECTANGLE_ROUNDED, - color = Color:new( RL.LIGHTGRAY ), + color = Color:newT( RL.LIGHTGRAY ), } ) ) end @@ -708,7 +708,7 @@ function Container:update() self._HScrollbar = Element:new( { padding = 0, drawBounds = true, - color = Color:new( RL.GRAY ), + color = Color:newT( RL.GRAY ), onClicked = function() Gui.heldCallback = function() self:mouseScroll( Vec2:new( 1, 0 ) ) end end, } ) @@ -717,7 +717,7 @@ function Container:update() VAling = Gui.ALING.CENTER, bounds = Rect:new( 0, 0, 0, 0 ), shape = Gui.SHAPE.RECTANGLE_ROUNDED, - color = Color:new( RL.LIGHTGRAY ), + color = Color:newT( RL.LIGHTGRAY ), } ) ) end end diff --git a/examples/resources/lib/matrix.lua b/examples/resources/lib/matrix.lua index 3128610..28d4305 100644 --- a/examples/resources/lib/matrix.lua +++ b/examples/resources/lib/matrix.lua @@ -50,31 +50,15 @@ Matrix.meta = { function Matrix:new( m ) local object = setmetatable( {}, Matrix.meta ) - if type( m ) == "table" then - object.m = deepCopy( m ) - else - object.m = RL.MatrixIdentity() - end + object.m = deepCopy( m ) return object end function Matrix:set( m ) - if type( m ) == "table" then - self.m = deepCopy( m ) - else - self.m = RL.MatrixIdentity() - end + self.m = deepCopy( m ) end --- function Matrix:arr() --- return self.m --- end - --- function Matrix:unpack() --- return self.m --- end - function Matrix:clone() return Matrix:new( self ) end diff --git a/examples/resources/lib/quaternion.lua b/examples/resources/lib/quaternion.lua index dfb07f4..e95102e 100644 --- a/examples/resources/lib/quaternion.lua +++ b/examples/resources/lib/quaternion.lua @@ -13,19 +13,19 @@ local metatable = { return "{"..tostring( q.x )..", "..tostring( q.y )..", "..tostring( q.z )..", "..tostring( q.w ).."}" end, __add = function( q1, q2 ) - return Quaternion:new( RL.QuaternionAdd( q1, q2 ) ) + return Quaternion:newT( RL.QuaternionAdd( q1, q2 ) ) end, __sub = function( q1, q2 ) - return Quaternion:new( RL.QuaternionSubtract( q1, q2 ) ) + return Quaternion:newT( RL.QuaternionSubtract( q1, q2 ) ) end, __mul = function( q1, q2 ) - return Quaternion:new( RL.QuaternionMultiply( q1, q2 ) ) + return Quaternion:newT( RL.QuaternionMultiply( q1, q2 ) ) end, __div = function( q1, q2 ) - return Quaternion:new( RL.QuaternionDivide( q1, q2 ) ) + return Quaternion:newT( RL.QuaternionDivide( q1, q2 ) ) end, __unm = function( q ) - return Quaternion:new( RL.QuaternionInvert( q ) ) + return Quaternion:newT( RL.QuaternionInvert( q ) ) end, __len = function() return 4 @@ -39,33 +39,33 @@ local metatable = { } function Quaternion:new( x, y, z, w ) - if type( x ) == "table" then - x, y, z, w = table.unpack( x ) - elseif type( x ) == "nil" then - x, y, z, w = 0, 0, 0, 1 -- QuaternionIdentity. - end + local object = setmetatable( {}, metatable ) + + object.x = x or 0 + object.y = y or 0 + object.z = z or 0 + object.w = w or 1 + + return object +end +function Quaternion:newT( t ) local object = setmetatable( {}, metatable ) - object.x = x - object.y = y - object.z = z - object.w = w + object.x, object.y, object.z, object.w = table.unpack( t ) return object end function Quaternion:set( x, y, z, w ) - if type( x ) == "table" then - x, y, z, w = table.unpack( x ) - elseif type( x ) == "nil" then - x, y, z, w = 0, 0, 0, 1 -- QuaternionIdentity. - end + self.x = x or 0 + self.y = y or 0 + self.z = z or 0 + self.w = w or 1 +end - self.x = x - self.y = y - self.z = z - self.w = w +function Quaternion:setT( t ) + self.x, self.y, self.z, self.w = table.unpack( t ) end function Quaternion:arr() @@ -81,15 +81,15 @@ function Quaternion:clone() end function Quaternion:addValue( value ) - return Quaternion:new( RL.QuaternionAddValue( self, value ) ) + return Quaternion:newT( RL.QuaternionAddValue( self, value ) ) end function Quaternion:subValue( value ) - return Quaternion:new( RL.QuaternionSubtractValue( self, value ) ) + return Quaternion:newT( RL.QuaternionSubtractValue( self, value ) ) end function Quaternion:identity() - return Quaternion:new( RL.QuaternionIdentity() ) + return Quaternion:newT( RL.QuaternionIdentity() ) end function Quaternion:length() @@ -97,88 +97,103 @@ function Quaternion:length() end function Quaternion:normalize() - return Quaternion:new( RL.QuaternionNormalize( self ) ) + return Quaternion:newT( RL.QuaternionNormalize( self ) ) end function Quaternion:invert() - return Quaternion:new( RL.QuaternionInvert( self ) ) + return Quaternion:newT( RL.QuaternionInvert( self ) ) end function Quaternion:scale( scalar ) - return Quaternion:new( RL.QuaternionScale( self, scalar ) ) + return Quaternion:newT( RL.QuaternionScale( self, scalar ) ) end function Quaternion:lerp( q2, value ) - return Quaternion:new( RL.QuaternionLerp( self, q2, value ) ) + return Quaternion:newT( RL.QuaternionLerp( self, q2, value ) ) end function Quaternion:nLerp( q2, value ) - return Quaternion:new( RL.QuaternionNLerp( self, q2, value ) ) + return Quaternion:newT( RL.QuaternionNLerp( self, q2, value ) ) end function Quaternion:sLerp( q2, value ) - return Quaternion:new( RL.QuaternionSLerp( self, q2, value ) ) + return Quaternion:newT( RL.QuaternionSLerp( self, q2, value ) ) end function Quaternion:fromVector3ToVector3( from, to ) - return Vector3:new( RL.QuaternionFromVector3ToVector3( from, to ) ) + return Vector3:newT( RL.QuaternionFromVector3ToVector3( from, to ) ) end function Quaternion:fromMatrix( mat ) - return Quaternion:new( RL.QuaternionFromMatrix( mat ) ) + return Quaternion:newT( RL.QuaternionFromMatrix( mat ) ) end function Quaternion:toMatrix() - return Matrix:new( RL.QuaternionToMatrix( self ) ) + return Matrix:newT( RL.QuaternionToMatrix( self ) ) end function Quaternion:fromAxisAngle( axis, angle ) - return Quaternion:new( RL.QuaternionFromAxisAngle( axis, angle ) ) + return Quaternion:newT( RL.QuaternionFromAxisAngle( axis, angle ) ) end function Quaternion:toAxisAngle() - local axis, angle = Quaternion:new( RL.QuaternionToAxisAngle( self ) ) + local axis, angle = Quaternion:newT( RL.QuaternionToAxisAngle( self ) ) return Vector3:new( axis ), angle end function Quaternion:fromEuler( pitch, yaw, roll ) - return Quaternion:new( RL.QuaternionFromEuler( pitch, yaw, roll ) ) + return Quaternion:newT( RL.QuaternionFromEuler( pitch, yaw, roll ) ) end function Quaternion:toEuler() - return Vector3:new( RL.QuaternionToEuler( self ) ) + return Vector3:newT( RL.QuaternionToEuler( self ) ) end function Quaternion:transform( mat ) - return Quaternion:new( RL.QuaternionTransform( self, mat ) ) + return Quaternion:newT( RL.QuaternionTransform( self, mat ) ) end -function Quaternion:addEq( q2 ) - self.x = self.x + q2.x - self.y = self.y + q2.y - self.z = self.z + q2.z - self.w = self.w + q2.w +local TEMP_COUNT = 100 +local tempPool = {} +local curTemp = 1 + +for _ = 1, TEMP_COUNT do + table.insert( tempPool, Quaternion:new( 0, 0, 0, 1 ) ) end -function Quaternion:subEq( q2 ) - self.x = self.x - q2.x - self.y = self.y - q2.y - self.z = self.z - q2.z - self.w = self.w - q2.w +-- Uses pre generated objects to avoid "slow" table generation. +function Quaternion:temp( x, y, z, w ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x = x or 0 + object.y = y or 0 + object.z = z or 0 + object.w = w or 1 + + return object end -function Quaternion:mulEq( q2 ) - self.x = self.x * q2.x - self.y = self.y * q2.y - self.z = self.z * q2.z - self.w = self.w * q2.w +-- Uses pre generated objects to avoid "slow" table generation. +function Quaternion:tempT( t ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x, object.y, object.z, object.w = table.unpack( t ) + + return object end -function Quaternion:divEq( q2 ) - self.x = self.x / q2.x - self.y = self.y / q2.y - self.z = self.z / q2.z - self.w = self.w / q2.w +function Quaternion:getTempId() + return curTemp end return Quaternion diff --git a/examples/resources/lib/raygui.lua b/examples/resources/lib/raygui.lua index d4e515d..1cd0269 100644 --- a/examples/resources/lib/raygui.lua +++ b/examples/resources/lib/raygui.lua @@ -259,7 +259,7 @@ function ScrollPanel:new( bounds, text, content, scroll, callbacks, styles, tool object.text = text object.content = content:clone() object.scroll = scroll:clone() - object.view = Rect:new() + object.view = Rect:new( 0, 0, 0, 0 ) object.callbacks = callbacks -- scroll, grab, drag. object.visible = true @@ -278,8 +278,8 @@ end function ScrollPanel:draw() local oldScroll = self.scroll:clone() local _, scroll, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) - self.view:set( view ) - self.scroll:set( scroll ) + self.view:setT( view ) + self.scroll:setT( scroll ) if self.scroll ~= oldScroll then self._gui:checkScrolling() @@ -581,10 +581,10 @@ function CheckBox:new( bounds, text, checked, callbacks, styles, tooltip ) object.visible = true object.disabled = false - object.textBounds = Rect:new() + object.textBounds = Rect:new( 0, 0, 0, 0 ) object.focusBounds = bounds:clone() - object._focusBoundsOffset = Vec2:new() -- Used in set position. + object._focusBoundsOffset = Vec2:new( 0, 0 ) -- Used in set position. object.styles = styles object.tooltip = tooltip @@ -600,7 +600,7 @@ function CheckBox:draw() local textBounds = nil _, self.checked, textBounds = RL.GuiCheckBox( self.bounds, self.text, self.checked ) - self.textBounds:set( textBounds ) + self.textBounds:setT( textBounds ) self.focusBounds = self.bounds:fit( self.textBounds ) self._focusBoundsOffset:set( self.focusBounds.x - self.bounds.x, self.focusBounds.y - self.bounds.y ) @@ -758,10 +758,10 @@ function Spinner:new( bounds, text, value, minValue, maxValue, editMode, callbac object.visible = true object.disabled = false - object.textBounds = Rect:new() + object.textBounds = Rect:new( 0, 0, 0, 0 ) object.viewBounds = bounds:clone() - object._viewBoundsOffset = Vec2:new() + object._viewBoundsOffset = Vec2:new( 0, 0 ) object.styles = styles object.tooltip = tooltip @@ -782,7 +782,7 @@ function Spinner:draw() local textBounds result, self.value, textBounds = RL.GuiSpinner( self.bounds, self.text, self.value, self.minValue, self.maxValue, self.editMode ) - self.textBounds:set( textBounds ) + self.textBounds:setT( textBounds ) self.viewBounds = self.bounds:fit( self.textBounds ) self._viewBoundsOffset:set( self.viewBounds.x - self.bounds.x, self.viewBounds.y - self.bounds.y ) @@ -829,10 +829,10 @@ function ValueBox:new( bounds, text, value, minValue, maxValue, editMode, callba object.visible = true object.disabled = false - object.textBounds = Rect:new() + object.textBounds = Rect:new( 0, 0, 0, 0 ) object.viewBounds = bounds:clone() - object._viewBoundsOffset = Vec2:new() + object._viewBoundsOffset = Vec2:new( 0, 0 ) object.styles = styles object.tooltip = tooltip @@ -853,7 +853,7 @@ function ValueBox:draw() local textBounds result, self.value, textBounds = RL.GuiValueBox( self.bounds, self.text, self.value, self.minValue, self.maxValue, self.editMode ) - self.textBounds:set( textBounds ) + self.textBounds:setT( textBounds ) self.viewBounds = self.bounds:fit( self.textBounds ) self._viewBoundsOffset:set( self.viewBounds.x - self.bounds.x, self.viewBounds.y - self.bounds.y ) @@ -949,11 +949,11 @@ function Slider:new( bounds, textLeft, textRight, value, minValue, maxValue, cal object.visible = true object.disabled = false - object.textLeftBounds = Rect:new() - object.textRightBounds = Rect:new() + object.textLeftBounds = Rect:new( 0, 0, 0, 0 ) + object.textRightBounds = Rect:new( 0, 0, 0, 0 ) object.viewBounds = bounds:clone() - object._viewBoundsOffset = Vec2:new() + object._viewBoundsOffset = Vec2:new( 0, 0 ) object.styles = styles object.tooltip = tooltip @@ -968,8 +968,8 @@ function Slider:draw() local oldValue, textLeftBounds, textRightBounds = self.value, nil, nil _, self.value, textLeftBounds, textRightBounds = RL.GuiSlider( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) - self.textLeftBounds:set( textLeftBounds ) - self.textRightBounds:set( textRightBounds ) + self.textLeftBounds:setT( textLeftBounds ) + self.textRightBounds:setT( textRightBounds ) self.viewBounds = self.bounds:fit( self.textLeftBounds ):fit( self.textRightBounds ) self._viewBoundsOffset:set( self.viewBounds.x - self.bounds.x, self.viewBounds.y - self.bounds.y ) @@ -1013,11 +1013,11 @@ function SliderBar:new( bounds, textLeft, textRight, value, minValue, maxValue, object.visible = true object.disabled = false - object.textLeftBounds = Rect:new() - object.textRightBounds = Rect:new() + object.textLeftBounds = Rect:new( 0, 0, 0, 0 ) + object.textRightBounds = Rect:new( 0, 0, 0, 0 ) object.viewBounds = bounds:clone() - object._viewBoundsOffset = Vec2:new() + object._viewBoundsOffset = Vec2:new( 0, 0 ) object.styles = styles object.tooltip = tooltip @@ -1032,8 +1032,8 @@ function SliderBar:draw() local oldValue, textLeftBounds, textRightBounds = self.value, nil, nil _, self.value, textLeftBounds, textRightBounds = RL.GuiSliderBar( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) - self.textLeftBounds:set( textLeftBounds ) - self.textRightBounds:set( textRightBounds ) + self.textLeftBounds:setT( textLeftBounds ) + self.textRightBounds:setT( textRightBounds ) self.viewBounds = self.bounds:fit( self.textLeftBounds ):fit( self.textRightBounds ) self._viewBoundsOffset:set( self.viewBounds.x - self.bounds.x, self.viewBounds.y - self.bounds.y ) @@ -1077,11 +1077,11 @@ function ProgressBar:new( bounds, textLeft, textRight, value, minValue, maxValue object.visible = true object.disabled = false - object.textLeftBounds = Rect:new() - object.textRightBounds = Rect:new() + object.textLeftBounds = Rect:new( 0, 0, 0, 0 ) + object.textRightBounds = Rect:new( 0, 0, 0, 0 ) object.viewBounds = bounds:clone() - object._viewBoundsOffset = Vec2:new() + object._viewBoundsOffset = Vec2:new( 0, 0 ) object.styles = styles object.tooltip = tooltip @@ -1096,8 +1096,8 @@ function ProgressBar:draw() local oldValue, textLeftBounds, textRightBounds = self.value, nil, nil _, self.value, textLeftBounds, textRightBounds = RL.GuiProgressBar( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) - self.textLeftBounds:set( textLeftBounds ) - self.textRightBounds:set( textRightBounds ) + self.textLeftBounds:setT( textLeftBounds ) + self.textRightBounds:setT( textRightBounds ) self.viewBounds = self.bounds:fit( self.textLeftBounds ):fit( self.textRightBounds ) self._viewBoundsOffset:set( self.viewBounds.x - self.bounds.x, self.viewBounds.y - self.bounds.y ) @@ -1204,7 +1204,7 @@ function Grid:new( bounds, text, spacing, subdivs, callbacks, styles, tooltip ) object.subdivs = subdivs object.callbacks = callbacks -- cellChange. - object.mouseCell = Vec2:new() + object.mouseCell = Vec2:new( 0, 0 ) object.visible = true object.disabled = false object.styles = styles @@ -1222,7 +1222,7 @@ function Grid:draw() local mouseCell = {} _, mouseCell = RL.GuiGrid( self.bounds, self.text, self.spacing, self.subdivs, self.mouseCell ) - self.mouseCell:set( mouseCell ) + self.mouseCell:setT( mouseCell ) if oldCell ~= self.mouseCell and self.callbacks.cellChange ~= nil then self.callbacks.cellChange( self ) @@ -1455,7 +1455,7 @@ function ColorPicker:new( bounds, text, color, callbacks, styles, tooltip ) object.visible = true object.disabled = false - object.focusBounds = Rect:new() + object.focusBounds = Rect:new( 0, 0, 0, 0 ) object.styles = styles object.tooltip = tooltip @@ -1483,7 +1483,7 @@ function ColorPicker:draw() local oldColor = self.color:clone() local _, color = RL.GuiColorPicker( self.bounds, self.text, self.color ) - self.color = Color:new( color ) + self.color = Color:newT( color ) if self.color ~= oldColor then if not self._gui:clickedInBounds( self.focusBounds ) then @@ -1535,7 +1535,7 @@ function ColorPanel:draw() local oldColor = self.color:clone() local _, color = RL.GuiColorPanel( self.bounds, self.text, self.color ) - self.color = Color:new( color ) + self.color = Color:newT( color ) if oldColor ~= self.color then if not self._gui:clickedInBounds( self.bounds ) then @@ -1718,15 +1718,15 @@ function Raygui:new() object.controls = {} object.focused = 0 object.dragging = nil - object.grabPos = Vec2:new() + object.grabPos = Vec2:new( 0, 0 ) object.scrolling = false object.textEdit = false object.textEditControl = nil object.defaultTexture = RL.GetTextureDefault() object.defaultRect = Rect:new( 0, 0, 1, 1 ) -- For texture. object.defaultFont = RL.GuiGetFont() - object.mouseOffset = Vec2:new() - object.view = Rect:new() -- Active if larger than 0. Then only controls in view will be updated and drawn. + object.mouseOffset = Vec2:new( 0, 0 ) + object.view = Rect:new( 0, 0, 0, 0 ) -- Active if larger than 0. Then only controls in view will be updated and drawn. object.tooltip = { text = nil, offset = Vec2:new( 12, 24 ), @@ -1759,7 +1759,7 @@ function Raygui:update() RL.SetMouseOffset( self.mouseOffset ) if RL.IsMouseButtonPressed( RL.MOUSE_BUTTON_LEFT ) then - self._mousePressPos:set( RL.GetMousePosition() ) + self._mousePressPos:setT( RL.GetMousePosition() ) end -- Focused is 0 if not over any control. self.focused = 0 @@ -1784,7 +1784,7 @@ function Raygui:update() self.tooltip.timer = self.tooltip.timer + RL.GetFrameTime() else self.tooltip.text = control.tooltip - self.tooltip.position = Vec2:new( RL.GetMousePosition() ) + self.tooltip.offset + self.tooltip.position = Vec2:newT( RL.GetMousePosition() ) + self.tooltip.offset end end @@ -1802,12 +1802,12 @@ function Raygui:update() end function Raygui:drag( control ) - local mousePos = Vec2:new( RL.GetMousePosition() ) + local mousePos = Vec2:tempT( RL.GetMousePosition() ) local mouseOver = RL.CheckCollisionPointRec( mousePos, control.bounds ) if not control.disabled and control.draggable and control ~= self.dragging and RL.IsMouseButtonPressed( RL.MOUSE_BUTTON_LEFT ) and mouseOver and mousePos.y - control.bounds.y <= self.RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT then - self.grabPos = mousePos - Vec2:new( control.bounds.x, control.bounds.y ) + self.grabPos = mousePos - Vec2:temp( control.bounds.x, control.bounds.y ) if control.callbacks.grab ~= nil then control.callbacks.grab( control ) @@ -1843,7 +1843,7 @@ function Raygui:_addLastProperty( property ) end function Raygui:drawTooltip() - local textSize = Vec2:new( RL.MeasureTextEx( + local textSize = Vec2:tempT( RL.MeasureTextEx( self.defaultFont, self.tooltip.text, RL.GuiGetStyle( RL.DEFAULT, RL.TEXT_SIZE ), @@ -1853,11 +1853,11 @@ function Raygui:drawTooltip() local view = self.view:clone() -- If no view size, clamp to window size. if view.width == 0 or view.height == 0 then - local screenSize = Vec2:new( RL.GetScreenSize() ) + local screenSize = Vec2:tempT( RL.GetScreenSize() ) view.width = screenSize.x view.height = screenSize.y end - + RL.GuiDummyRec( tooltipRect:clampInside( view ), self.tooltip.text ) end @@ -1922,6 +1922,7 @@ function Raygui:checkScrolling() end function Raygui:clickedInBounds( bounds ) + print( self._mousePressPos, bounds ) return RL.CheckCollisionPointRec( self._mousePressPos, bounds ) end diff --git a/examples/resources/lib/rectangle.lua b/examples/resources/lib/rectangle.lua index 4b8d53b..edb06e2 100644 --- a/examples/resources/lib/rectangle.lua +++ b/examples/resources/lib/rectangle.lua @@ -44,33 +44,33 @@ Rectangle.meta = { } function Rectangle:new( x, y, width, height ) - if type( x ) == "table" then - x, y, width, height = table.unpack( x ) - elseif type( x ) == "nil" then - x, y, width, height = 0, 0, 0, 0 - end + local object = setmetatable( {}, Rectangle.meta ) + + object.x = x or 0 + object.y = y or 0 + object.width = width or 0 + object.height = height or 0 + return object +end + +function Rectangle:newT( t ) local object = setmetatable( {}, Rectangle.meta ) - object.x = x - object.y = y - object.width = width - object.height = height + object.x, object.y, object.width, object.height = table.unpack( t ) - return object + return object end function Rectangle:set( x, y, width, height ) - if type( x ) == "table" then - x, y, width, height = table.unpack( x ) - elseif type( x ) == "nil" then - x, y, width, height = 0, 0, 0, 0 - end + self.x = x or 0 + self.y = y or 0 + self.width = width or 0 + self.height = height or 0 +end - self.x = x - self.y = y - self.width = width - self.height = height +function Rectangle:setT( t ) + self.x, self.y, self.width, self.height = table.unpack( t ) end function Rectangle:arr() @@ -153,4 +153,47 @@ function Rectangle:getCollisionRec( rec ) return Rectangle:new( RL.GetCollisionRec( self, rec ) ) end +local TEMP_COUNT = 100 +local tempPool = {} +local curTemp = 1 + +for _ = 1, TEMP_COUNT do + table.insert( tempPool, Rectangle:new( 0, 0, 0, 0 ) ) +end + +-- Uses pre generated objects to avoid "slow" table generation. +function Rectangle:temp( x, y, width, height ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x = x or 0 + object.y = y or 0 + object.width = width or 0 + object.height = height or 0 + + return object +end + +-- Uses pre generated objects to avoid "slow" table generation. +function Rectangle:tempT( t ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x, object.y, object.width, object.height = table.unpack( t ) + + return object +end + +function Rectangle:getTempId() + return curTemp +end + return Rectangle diff --git a/examples/resources/lib/rune.lua b/examples/resources/lib/rune.lua index fa9f59d..12bf72d 100644 --- a/examples/resources/lib/rune.lua +++ b/examples/resources/lib/rune.lua @@ -21,27 +21,15 @@ local metatable = { } function Rune:new( string ) - if type( string ) == "table" then - string = RL.LoadUTF8( string ) - elseif type( string ) == "nil" then - string = "" - end - local object = setmetatable( {}, metatable ) - object.string = string + object.string = string or "" return object end function Rune:set( string ) - if type( string ) == "table" then - string = RL.LoadUTF8( string ) - elseif type( string ) == "nil" then - string = "" - end - - self.string = string + self.string = string or "" end function Rune:clone() @@ -57,12 +45,12 @@ function Rune:getCodepoints() end function Rune:getCodepoint( index ) - local codepoint = RL.GetCodepoint( self:sub( index, index ) ) + local codepoint = RL.GetCodepoint( self.string, index ) return codepoint end function Rune:getCodepointSize( index ) - local _, codepointSize = RL.GetCodepoint( self:sub( index, index ) ) + local _, codepointSize = RL.GetCodepoint( self.string, index ) return codepointSize end diff --git a/examples/resources/lib/vector2.lua b/examples/resources/lib/vector2.lua index 7672589..fa178a7 100644 --- a/examples/resources/lib/vector2.lua +++ b/examples/resources/lib/vector2.lua @@ -11,6 +11,7 @@ local metatable = { end, __add = function( v1, v2 ) return Vector2:new( v1.x + v2.x, v1.y + v2.y ) + -- return Vector2:newOld( v1.x + v2.x, v1.y + v2.y ) end, __sub = function( v1, v2 ) return Vector2:new( v1.x - v2.x, v1.y - v2.y ) @@ -42,27 +43,29 @@ local metatable = { } function Vector2:new( x, y ) - if type( x ) == "table" then - x, y = table.unpack( x ) - elseif type( x ) == "nil" then - x, y = 0, 0 - end + local object = setmetatable( {}, metatable ) + object.x = x or 0 + object.y = y or 0 + + return object +end + +function Vector2:newT( t ) local object = setmetatable( {}, metatable ) - object.x = x - object.y = y + object.x, object.y = table.unpack( t ) return object end function Vector2:set( x, y ) - if type( x ) == "table" then - x, y = table.unpack( x ) - end + self.x = x or 0 + self.y = y or 0 +end - self.x = x - self.y = y +function Vector2:setT( t ) + self.x, self.y = table.unpack( t ) end function Vector2:arr() @@ -98,11 +101,11 @@ function Vector2:ceil() end function Vector2:addValue( value ) - return Vector2:new( RL.Vector2AddValue( self, value ) ) + return Vector2:newT( RL.Vector2AddValue( self, value ) ) end function Vector2:subValue( value ) - return Vector2:new( RL.Vector2SubtractValue( self, value ) ) + return Vector2:newT( RL.Vector2SubtractValue( self, value ) ) end function Vector2:length() @@ -138,43 +141,43 @@ function Vector2:atan2() end function Vector2:scale( scale ) - return Vector2:new( RL.Vector2Scale( self, scale ) ) + return Vector2:newT( RL.Vector2Scale( self, scale ) ) end function Vector2:normalize() - return Vector2:new( RL.Vector2Normalize( self ) ) + return Vector2:newT( RL.Vector2Normalize( self ) ) end function Vector2:transform( mat ) - return Vector2:new( RL.Vector2Transform( self, mat ) ) + return Vector2:newT( RL.Vector2Transform( self, mat ) ) end function Vector2:lerp( v2, value ) - return Vector2:new( RL.Vector2Lerp( self, v2, value ) ) + return Vector2:newT( RL.Vector2Lerp( self, v2, value ) ) end function Vector2:reflect( normal ) - return Vector2:new( RL.Vector2Reflect( self, normal ) ) + return Vector2:newT( RL.Vector2Reflect( self, normal ) ) end function Vector2:rotate( angle ) - return Vector2:new( RL.Vector2Rotate( self, angle ) ) + return Vector2:newT( RL.Vector2Rotate( self, angle ) ) end function Vector2:moveTowards( target, maxDistance ) - return Vector2:new( RL.Vector2MoveTowards( self, target, maxDistance ) ) + return Vector2:newT( RL.Vector2MoveTowards( self, target, maxDistance ) ) end function Vector2:invert() - return Vector2:new( RL.Vector2Invert( self ) ) + return Vector2:newT( RL.Vector2Invert( self ) ) end function Vector2:clamp( min, max ) - return Vector2:new( RL.Vector2Clamp( self, min, max ) ) + return Vector2:newT( RL.Vector2Clamp( self, min, max ) ) end function Vector2:clampValue( min, max ) - return Vector2:new( RL.Vector2ClampValue( self, min, max ) ) + return Vector2:newT( RL.Vector2ClampValue( self, min, max ) ) end function Vector2:equals( v2 ) @@ -201,4 +204,45 @@ function Vector2:divEq( v2 ) self.y = self.y / v2.y end +local TEMP_COUNT = 100 +local tempPool = {} +local curTemp = 1 + +for _ = 1, TEMP_COUNT do + table.insert( tempPool, Vector2:new( 0, 0 ) ) +end + +-- Uses pre generated objects to avoid "slow" table generation. +function Vector2:temp( x, y ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x = x or 0 + object.y = y or 0 + + return object +end + +-- Uses pre generated objects to avoid "slow" table generation. +function Vector2:tempT( t ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x, object.y = table.unpack( t ) + + return object +end + +function Vector2:getTempId() + return curTemp +end + return Vector2 diff --git a/examples/resources/lib/vector3.lua b/examples/resources/lib/vector3.lua index eda39e2..cdc4576 100644 --- a/examples/resources/lib/vector3.lua +++ b/examples/resources/lib/vector3.lua @@ -44,31 +44,31 @@ local metatable = { } function Vector3:new( x, y, z ) - if type( x ) == "table" then - x, y, z = table.unpack( x ) - elseif type( x ) == "nil" then - x, y, z = 0, 0, 0 - end + local object = setmetatable( {}, metatable ) + object.x = x or 0 + object.y = y or 0 + object.z = z or 0 + + return object +end + +function Vector3:newT( t ) local object = setmetatable( {}, metatable ) - object.x = x - object.y = y - object.z = z + object.x, object.y, object.z = table.unpack( t ) return object end function Vector3:set( x, y, z ) - if type( x ) == "table" then - x, y, z = table.unpack( x ) - elseif type( x ) == "nil" then - x, y, z = 0, 0, 0 - end + self.x = x or 0 + self.y = y or 0 + self.z = z or 0 +end - self.x = x - self.y = y - self.z = z +function Vector3:setT( t ) + self.x, self.y, self.z = table.unpack( t ) end function Vector3:arr() @@ -100,11 +100,11 @@ function Vector3:abs() end function Vector3:min( v2 ) - return Vector3:new( RL.Vector3Min( self, v2 ) ) + return Vector3:newT( RL.Vector3Min( self, v2 ) ) end function Vector3:max( v2 ) - return Vector3:new( RL.Vector3Max( self, v2 ) ) + return Vector3:newT( RL.Vector3Max( self, v2 ) ) end function Vector3:floor() @@ -116,23 +116,23 @@ function Vector3:ceil() end function Vector3:addValue( value ) - return Vector3:new( RL.Vector3AddValue( self, value ) ) + return Vector3:newT( RL.Vector3AddValue( self, value ) ) end function Vector3:subValue( value ) - return Vector3:new( RL.Vector3SubtractValue( self, value ) ) + return Vector3:newT( RL.Vector3SubtractValue( self, value ) ) end function Vector3:scale( scalar ) - return Vector3:new( RL.Vector3Scale( self, scalar ) ) + return Vector3:newT( RL.Vector3Scale( self, scalar ) ) end function Vector3:cross( v2 ) - return Vector3:new( RL.Vector3CrossProduct( self, v2 ) ) + return Vector3:newT( RL.Vector3CrossProduct( self, v2 ) ) end function Vector3:perpendicular() - return Vector3:new( RL.Vector3Perpendicular( self ) ) + return Vector3:newT( RL.Vector3Perpendicular( self ) ) end function Vector3:length() @@ -160,11 +160,11 @@ function Vector3:angle( v2 ) end function Vector3:negate() - return Vector3:new( RL.Vector3Negate( self ) ) + return Vector3:newT( RL.Vector3Negate( self ) ) end function Vector3:normalize() - return Vector3:new( RL.Vector3Normalize( self ) ) + return Vector3:newT( RL.Vector3Normalize( self ) ) end function Vector3:orthoNormalize( v2 ) @@ -173,35 +173,35 @@ function Vector3:orthoNormalize( v2 ) end function Vector3:transform( mat ) - return Vector3:new( RL.Vector3Transform( self, mat ) ) + return Vector3:newT( RL.Vector3Transform( self, mat ) ) end function Vector3:rotateByQuaternion( q ) - return Vector3:new( RL.Vector3RotateByQuaternion( self, q ) ) + return Vector3:newT( RL.Vector3RotateByQuaternion( self, q ) ) end function Vector3:rotateByAxisAngle( axis, angle ) - return Vector3:new( RL.Vector3RotateByAxisAngle( self, axis, angle ) ) + return Vector3:newT( RL.Vector3RotateByAxisAngle( self, axis, angle ) ) end function Vector3:lerp( v2, value ) - return Vector3:new( RL.Vector3Lerp( self, v2, value ) ) + return Vector3:newT( RL.Vector3Lerp( self, v2, value ) ) end function Vector3:reflect( normal ) - return Vector3:new( RL.Vector3Reflect( self, normal ) ) + return Vector3:newT( RL.Vector3Reflect( self, normal ) ) end function Vector3:invert() - return Vector3:new( RL.Vector3Invert( self ) ) + return Vector3:newT( RL.Vector3Invert( self ) ) end function Vector3:clamp( min, max ) - return Vector3:new( RL.Vector3Clamp( self, min, max ) ) + return Vector3:newT( RL.Vector3Clamp( self, min, max ) ) end function Vector3:clampValue( min, max ) - return Vector3:new( RL.Vector3ClampValue( self, min, max ) ) + return Vector3:newT( RL.Vector3ClampValue( self, min, max ) ) end function Vector3:equals( v2 ) @@ -232,4 +232,46 @@ function Vector3:divEq( v2 ) self.z = self.z / v2.z end +local TEMP_COUNT = 100 +local tempPool = {} +local curTemp = 1 + +for _ = 1, TEMP_COUNT do + table.insert( tempPool, Vector3:new( 0, 0, 0 ) ) +end + +-- Uses pre generated objects to avoid "slow" table generation. +function Vector3:temp( x, y, z ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x = x or 0 + object.y = y or 0 + object.z = z or 0 + + return object +end + +-- Uses pre generated objects to avoid "slow" table generation. +function Vector3:tempT( t ) + local object = tempPool[ curTemp ] + curTemp = curTemp + 1 + + if TEMP_COUNT < curTemp then + curTemp = 1 + end + + object.x, object.y, object.z = table.unpack( t ) + + return object +end + +function Vector3:getTempId() + return curTemp +end + return Vector3 diff --git a/examples/rlgl_hello_triangle/main.lua b/examples/rlgl_hello_triangle/main.lua index 571acb0..b9ebf9f 100644 --- a/examples/rlgl_hello_triangle/main.lua +++ b/examples/rlgl_hello_triangle/main.lua @@ -12,7 +12,7 @@ local triangle = { texture = { id = -1, data = nil, - size = Vec2:new(), + size = Vec2:new( 0, 0 ), mipmaps = 0, format = 0, }, @@ -60,7 +60,7 @@ local function loadTexture( path ) local image = RL.LoadImage( path ) triangle.texture.data = RL.GetImageData( image ) - triangle.texture.size = Vec2:new( RL.GetImageSize( image ) ) + triangle.texture.size = Vec2:newT( RL.GetImageSize( image ) ) triangle.texture.format = RL.GetImageFormat( image ) triangle.texture.mipmaps = RL.GetImageMipmaps( image ) @@ -131,8 +131,8 @@ local function createTriangle() end function RL.init() - local monitorPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) - local monitorSize = Vec2:new( RL.GetMonitorSize( monitor ) ) + local monitorPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) + local monitorSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) RL.SetWindowTitle( "RLGL Hello Triangle" ) RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE ) diff --git a/examples/snake/main.lua b/examples/snake/main.lua index d28901d..3e047ee 100644 --- a/examples/snake/main.lua +++ b/examples/snake/main.lua @@ -13,8 +13,8 @@ local STATE = { TITLE = 0, GAME = 1, OVER = 2 } -- Enum. -- Resources local framebuffer = nil local monitor = 0 -local monitorPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) -local monitorSize = Vec2:new( RL.GetMonitorSize( monitor ) ) +local monitorPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) +local monitorSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) local winScale = 6 local winSize = Vec2:new( RESOLUTION.x * winScale, RESOLUTION.y * winScale ) local gameState = STATE.GAME @@ -248,6 +248,4 @@ function RL.draw() 0.0, RL.WHITE ) - - RL.DrawFPS( { 20, 20 } ) end diff --git a/examples/stencil_reflection/main.lua b/examples/stencil_reflection/main.lua index 9b57a47..f9ca2cc 100644 --- a/examples/stencil_reflection/main.lua +++ b/examples/stencil_reflection/main.lua @@ -13,9 +13,9 @@ function RL.init() RL.SetWindowState( RL.FLAG_VSYNC_HINT ) RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE ) - local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) - local mSize = Vec2:new( RL.GetMonitorSize( monitor ) ) - local winSize = Vec2:new( RL.GetScreenSize() ) + local mPos = Vec2:newT( RL.GetMonitorPosition( monitor ) ) + local mSize = Vec2:newT( RL.GetMonitorSize( monitor ) ) + local winSize = Vec2:newT( RL.GetScreenSize() ) RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } ) -- cgit v1.2.3