LoadBufferFormatted, SetBufferData and CopyBufferData. Compressed resource file example.

This commit is contained in:
jussi
2024-08-27 21:41:31 +03:00
parent af2bf6f7a2
commit 35e73465ac
9 changed files with 304 additions and 43 deletions

View File

@@ -0,0 +1,94 @@
-- Simple example to store compressed image to file.
-- To store multiple assets, some sort of addressing would be required.
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
Vector2 = require( "vector2" )
local texture = nil
local dataFileName = "data"
local compress = true
-- local compress = false
local function writeDataFile( path )
local image = RL.LoadImage( RL.GetBasePath().."../resources/images/arcade_platformerV2.png" )
if not image then
return
end
local imgData = {
size = Vector2:newT( RL.GetImageSize( image ) ),
mipmaps = RL.GetImageMipmaps( image ),
format = RL.GetImageFormat( image ),
data = RL.GetImageData( image ),
}
-- Header and image data. We devide imgData.data by 4 since we use unsigned ints as buffer type.
local totalLength = 4 + RL.GetBufferLength( imgData.data ) / 4
local buffer = RL.LoadBufferFormatted( totalLength, RL.BUFFER_UNSIGNED_INT, 0 )
RL.SetBufferData( buffer, 0, imgData.size.x )
RL.SetBufferData( buffer, 1, imgData.size.y )
RL.SetBufferData( buffer, 2, imgData.mipmaps )
RL.SetBufferData( buffer, 3, imgData.format )
RL.CopyBufferData( buffer, imgData.data, 4, 0, RL.GetBufferLength( imgData.data ) )
if compress then
RL.ExportBuffer( RL.CompressData( buffer ), path )
else
RL.ExportBuffer( buffer, path )
end
end
local function loadDataFile( path )
local buffer = RL.LoadBufferFromFile( path, RL.BUFFER_UNSIGNED_INT )
if not buffer then
return
end
if compress then
buffer = RL.DecompressData( buffer )
end
local imgData = {
size = Vector2:newT( RL.GetBufferData( buffer, 0, 2 ) ),
mipmaps = RL.GetBufferData( buffer, 2, 1 )[1],
format = RL.GetBufferData( buffer, 3, 1 )[1],
data = nil,
}
local imageDataSize = RL.GetPixelDataSize( imgData.size, imgData.format )
imgData.data = RL.LoadBufferFormatted( imageDataSize, RL.BUFFER_UNSIGNED_CHAR, 0 )
RL.CopyBufferData( imgData.data, buffer, 0, 4, imageDataSize / 4 )
local image = RL.LoadImageFromData(
imgData.data,
imgData.size,
imgData.mipmaps,
imgData.format
)
texture = RL.LoadTextureFromImage( image )
end
function RL.init()
RL.SetWindowTitle( "Compressed resource file" )
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
local path = RL.GetBasePath()..dataFileName
writeDataFile( path )
loadDataFile( path )
end
function RL.update( delta )
end
function RL.draw()
RL.ClearBackground( RL.RAYWHITE )
if texture then
RL.DrawTextureEx( texture, { 0, 0 }, 0, 2, RL.WHITE )
end
end

View File

@@ -1,11 +1,11 @@
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
Util = require( "utillib" )
Vec2 = require( "vector2" )
Rect = require( "rectangle" )
Vector2 = require( "vector2" )
Rectangle = require( "rectangle" )
-- Defines
local RESOLUTION = Vec2:new( 128, 128 )
local RESOLUTION = Vector2:new( 128, 128 )
local TILE_SIZE = 8
local LEVEL_SIZE = RESOLUTION.x / TILE_SIZE
local STATE = { TITLE = 0, GAME = 1, OVER = 2 } -- Enum.
@@ -13,10 +13,10 @@ local STATE = { TITLE = 0, GAME = 1, OVER = 2 } -- Enum.
-- Resources
local framebuffer = nil
local monitor = 0
local monitorPos = Vec2:newT( RL.GetMonitorPosition( monitor ) )
local monitorSize = Vec2:newT( RL.GetMonitorSize( monitor ) )
local monitorPos = Vector2:newT( RL.GetMonitorPosition( monitor ) )
local monitorSize = Vector2:newT( RL.GetMonitorSize( monitor ) )
local winScale = 6
local winSize = Vec2:new( RESOLUTION.x * winScale, RESOLUTION.y * winScale )
local winSize = Vector2:new( RESOLUTION.x * winScale, RESOLUTION.y * winScale )
local gameState = STATE.GAME
local grassTexture = nil
local snakeTexture = nil
@@ -28,10 +28,10 @@ local applePos = {}
local function setSnake()
snake = {
heading = Vec2:new( 1, 0 ),
control = Vec2:new( 1, 0 ),
headPos = Vec2:new( LEVEL_SIZE / 2, LEVEL_SIZE / 2 ),
segments = {}, -- { pos Vec2, heading Vec2 }
heading = Vector2:new( 1, 0 ),
control = Vector2:new( 1, 0 ),
headPos = Vector2:new( LEVEL_SIZE / 2, LEVEL_SIZE / 2 ),
segments = {}, -- { pos Vector2, heading Vector2 }
grow = 2,
}
end
@@ -52,7 +52,7 @@ local function addSegment()
end
local function setApplePos()
applePos = Vec2:new( math.random( 0, LEVEL_SIZE - 1 ), math.random( 0, LEVEL_SIZE - 1 ) )
applePos = Vector2:new( math.random( 0, LEVEL_SIZE - 1 ), math.random( 0, LEVEL_SIZE - 1 ) )
local search = true
while search do
@@ -167,16 +167,16 @@ end
--[[ Check if next segment is on left side. There are more mathematically elegant solution to this, but there is
only four possibilities so we can just check them all. ]]--
local function onLeft( this, nextSeg )
return ( this == Vec2:temp( 0, -1 ) and nextSeg == Vec2:temp( -1, 0 ) )
or ( this == Vec2:temp( -1, 0 ) and nextSeg == Vec2:temp( 0, 1 ) )
or ( this == Vec2:temp( 0, 1 ) and nextSeg == Vec2:temp( 1, 0 ) )
or ( this == Vec2:temp( 1, 0 ) and nextSeg == Vec2:temp( 0, -1 ) )
return ( this == Vector2:temp( 0, -1 ) and nextSeg == Vector2:temp( -1, 0 ) )
or ( this == Vector2:temp( -1, 0 ) and nextSeg == Vector2:temp( 0, 1 ) )
or ( this == Vector2:temp( 0, 1 ) and nextSeg == Vector2:temp( 1, 0 ) )
or ( this == Vector2:temp( 1, 0 ) and nextSeg == Vector2:temp( 0, -1 ) )
end
local function drawSnake()
for i, seg in ipairs( snake.segments ) do
local angle = seg.heading:atan2()
local source = Rect:temp( 16, 0, 8, 8 )
local source = Rectangle:temp( 16, 0, 8, 8 )
if i == 1 then -- Tail segment. Yes tail is actually the 'first' segment.
source.x = 8