瀏覽代碼

Cache textures/SetWindowPosition

John Dodis 1 年之前
父節點
當前提交
99685237b4
共有 2 個文件被更改,包括 40 次插入4 次删除
  1. 13 2
      main.lua
  2. 27 2
      ui2d/ui2d.lua

+ 13 - 2
main.lua

@@ -5,6 +5,7 @@ local win1_pos_x = 300
 local win1_pos_y = 300
 local sl1 = 20
 local sl2 = 10
+local txt = "sample text"
 
 function lovr.load()
 	UI2D.Init()
@@ -14,11 +15,16 @@ function lovr.update( dt )
 	UI2D.InputInfo()
 end
 
+function lovr.keypressed( key, scancode, repeating )
+	-- txt = txt .. "0"
+	-- UI2D.SetWindowPosition( "third##blah", 100, 150 )
+end
+
 function lovr.draw( pass )
 	pass:setProjection( 1, mat4():orthographic( pass:getDimensions() ) )
 	UI2D.NewFrame( pass )
 
-	UI2D.Begin( "first", 300,300 )
+	UI2D.Begin( "first", 300, 300 )
 	if UI2D.Button( "first button" ) then
 		print( "from 1st button" )
 	end
@@ -34,17 +40,22 @@ function lovr.draw( pass )
 	end
 	UI2D.End( pass )
 
-	UI2D.Begin( "third", 350, 240 )
+	UI2D.Begin( "third##blah", 350, 240 )
 	UI2D.Button( "blah1" )
 	UI2D.Button( "blah2" )
 	UI2D.SameLine()
 	released, sl2 = UI2D.SliderInt( "hello", sl2, 0, 100 )
 	UI2D.End( pass )
 
+	UI2D.Begin( "time", 150, 100 )
+	UI2D.Button( txt )
+	UI2D.End( pass )
+
 	local ui_passes = UI2D.RenderFrame( pass )
 
 	pass:setColor( 1, 0, 0 )
 	pass:plane( 100, 100, 0, 100, 100 )
 	table.insert( ui_passes, pass )
+	print( #ui_passes )
 	return lovr.graphics.submit( ui_passes )
 end

+ 27 - 2
ui2d/ui2d.lua

@@ -272,7 +272,7 @@ function UI2D.InputInfo()
 end
 
 function UI2D.Begin( name, x, y, is_modal )
-	local exists, idx = WindowExists( name )
+	local exists, idx = WindowExists( name ) -- TODO: Can't currently change window title on runtime
 
 	if not exists then
 		local window = {
@@ -284,6 +284,8 @@ function UI2D.Begin( name, x, y, is_modal )
 			h = 0,
 			command_list = {},
 			texture = nil,
+			texture_w = 0,
+			texture_h = 0,
 			pass = nil,
 			is_hovered = false,
 			is_modal = is_modal or false
@@ -304,8 +306,19 @@ function UI2D.End( main_pass )
 	cur_window.w = layout.total_w
 	cur_window.h = layout.total_h
 
-	if not cur_window.texture then
+	-- Cache texture
+	if cur_window.texture then
+		if cur_window.texture_w ~= cur_window.w or cur_window.texture_h ~= cur_window.h then
+			cur_window.texture:release()
+			cur_window.texture_w = cur_window.w
+			cur_window.texture_h = cur_window.h
+			cur_window.texture = lovr.graphics.newTexture( cur_window.w, cur_window.h, texture_flags )
+			cur_window.pass:setCanvas( cur_window.texture )
+		end
+	else
 		cur_window.texture = lovr.graphics.newTexture( cur_window.w, cur_window.h, texture_flags )
+		cur_window.texture_w = cur_window.w
+		cur_window.texture_h = cur_window.h
 		cur_window.pass = lovr.graphics.newPass( cur_window.texture )
 	end
 
@@ -330,6 +343,7 @@ function UI2D.End( main_pass )
 	table.insert( windows[ begin_idx ].command_list,
 		{ type = "rect_wire", bbox = { x = 0, y = 0, w = cur_window.w, h = cur_window.h }, color = colors.window_border } )
 
+	-- Do draw commands
 	for i, v in ipairs( cur_window.command_list ) do
 		if v.type == "rect_fill" then
 			if v.is_separator then
@@ -377,6 +391,17 @@ function UI2D.End( main_pass )
 	ResetLayout()
 end
 
+function UI2D.SetWindowPosition( id, x, y )
+	local exists, idx = WindowExists( id )
+	if exists then
+		windows[ idx ].x = x
+		windows[ idx ].y = y
+		return true
+	end
+
+	return false
+end
+
 function UI2D.SameLine()
 	layout.same_line = true
 end