diff options
| author | jussi | 2024-04-17 00:35:58 +0300 |
|---|---|---|
| committer | jussi | 2024-04-17 00:35:58 +0300 |
| commit | 70b40f67824b3d612235c382aa83821c054fa51e (patch) | |
| tree | 5d594cc457d6fed8af87e31977a0b9352677a4f5 /examples/resources | |
| parent | 41b67398247d9031aed4ca6e3ffd6799ec8b1bca (diff) | |
| download | reilua-enhanced-70b40f67824b3d612235c382aa83821c054fa51e.tar.gz reilua-enhanced-70b40f67824b3d612235c382aa83821c054fa51e.tar.bz2 reilua-enhanced-70b40f67824b3d612235c382aa83821c054fa51e.zip | |
Object libraries like Vector2 optimizations.
Diffstat (limited to 'examples/resources')
| -rw-r--r-- | examples/resources/lib/camera3d.lua | 16 | ||||
| -rw-r--r-- | examples/resources/lib/color.lua | 105 | ||||
| -rw-r--r-- | examples/resources/lib/gui.lua | 24 | ||||
| -rw-r--r-- | examples/resources/lib/matrix.lua | 20 | ||||
| -rw-r--r-- | examples/resources/lib/quaternion.lua | 135 | ||||
| -rw-r--r-- | examples/resources/lib/raygui.lua | 85 | ||||
| -rw-r--r-- | examples/resources/lib/rectangle.lua | 81 | ||||
| -rw-r--r-- | examples/resources/lib/rune.lua | 20 | ||||
| -rw-r--r-- | examples/resources/lib/vector2.lua | 92 | ||||
| -rw-r--r-- | examples/resources/lib/vector3.lua | 108 |
10 files changed, 419 insertions, 267 deletions
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 |
