summaryrefslogtreecommitdiff
path: root/examples/compress_data/main.lua
blob: bf6c162d55c67c11daad4bef2a35223b765023a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
local textColor = RL.BLACK
local text = "Put data here"
local deCompressedText = ""
local editMode = false

function RL.init()
	RL.SetWindowTitle( "Buffer" )
	RL.SetWindowState( RL.FLAG_VSYNC_HINT )
end

local function compressDecompressData()
	local strT = {}

	for i = 1, #text do
		table.insert( strT, string.byte( text:sub( i, i ) ) )
	end

	local strBuffer = RL.LoadBuffer( strT, RL.BUFFER_UNSIGNED_CHAR )
	local compBuffer = RL.CompressData( strBuffer )
	local deCompBuffer = RL.DecompressData( compBuffer )

	deCompressedText = ""

	for _, c in ipairs( RL.GetBufferData( deCompBuffer ) ) do
		deCompressedText = deCompressedText..string.char( c )
	end
end

function RL.draw()
	RL.ClearBackground( RL.RAYWHITE )
    RL.DrawText( "Decompressed text: "..deCompressedText, { 20, 200 }, 20, textColor )

	if RL.GuiButton( { 20, 20, 168, 32 }, "Compress/Decompress Data" ) then
		compressDecompressData()
	end
	
	local pressed = false
	pressed, text = RL.GuiTextBox( { 220, 20, 400, 32 }, text, 64, editMode )

	if pressed then
		editMode = not editMode
	end
end