Skip to content
Tangent 128 edited this page Mar 28, 2015 · 3 revisions

SDL.RWCreate

  • SDL.RWCreate
    • Function
      • SYNOPSIS
      • ARGUMENTS
      • RETURNS
    • Table description
      • Size
        • Returns
      • Seek
        • Arguments
        • Returns
      • Read
        • Arguments
        • Returns
      • Write
        • Arguments
        • Returns
      • Close
    • EXAMPLE

Create a custom RWops object.

Function

SYNOPSIS

rw, err = function SDL.RWCreate(funcs)

ARGUMENTS

  • funcs, the table with the functions (see Table description).

RETURNS

  • rw, the RWops object or nil
  • err, the error message

Table description

Size

function size()

Returns

The size of the stream

Seek

function seek(offset, whence)

Arguments

  • offset, the offset count
  • whence, type of seek

Returns

The seek

Read

function read(n, size)

Arguments

  • n, the number to read
  • size, each block to read

Returns

  1. The string read
  2. The number of bytes read

Write

function write(data, n, size)

Arguments

  • data, the data string
  • n, the number to write
  • size, each block to write

Returns

  1. The number of bytes written

Close

function close()

EXAMPLE

local SDL       = require "SDL" 

-- The table for RW ops
local reader    = { }

function reader.size()
return -1
end

function reader.read(n, size)
local r = reader.file:read(n * size)    

if not r then
return nil, 0
end

return r, #r
end

function reader.write(data, n, size)
local r = reader.file:write(data)

if not r then
return 0
end

return n * size
end

function reader.seek(offset, whence)
local v = nil

if whence == SDL.rwopsSeek.Set then
                v = "set" 
elseif whence == SDL.rwopsSeek.Current then
                v = "cur" 
elseif whence == SDL.rwopsSeek.End then
                v = "end" 
end

if not v then
error("unknown whence")
end

local r = reader.file:seek(v, offset)

if not r then
return 0
end

return r
end

function reader.close()
        reader.file:close()
end

reader.file = io.open("mario.png", "r")
if not reader.file then
-- Handle your error here
error("Failed to load")
end

local rw = SDL.RWCreate(reader)
Clone this wiki locally