Add Seek for asset

This commit is contained in:
Milan Nikolic 2017-02-02 14:29:53 +01:00
parent e365f75e34
commit ea0628725d
5 changed files with 26 additions and 8 deletions

View file

@ -5,8 +5,12 @@ package raylib
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "reflect"
import (
"io"
"reflect"
"unsafe"
)
// Some basic Defines
const (
@ -450,6 +454,12 @@ func NewBoundingBoxFromPointer(ptr unsafe.Pointer) BoundingBox {
return *(*BoundingBox)(ptr)
}
// Asset file
type Asset interface {
io.ReadSeeker
io.Closer
}
// Close Window and Terminate Context
func CloseWindow() {
C.CloseWindow()

View file

@ -16,6 +16,7 @@ import "C"
import (
"errors"
"fmt"
"io"
"unsafe"
)
@ -47,7 +48,7 @@ func androidMain(app *C.struct_android_app) {
}
// Open asset
func OpenAsset(name string) (io.ReadCloser, error) {
func OpenAsset(name string) (Asset, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@ -72,6 +73,14 @@ func (a *asset) Read(p []byte) (n int, err error) {
return n, nil
}
func (a *asset) Seek(offset int64, whence int) (int64, error) {
off := C.AAsset_seek(a.ptr, C.off_t(offset), C.int(whence))
if off == -1 {
return 0, errors.New(fmt.Sprintf("bad result for offset=%d, whence=%d", offset, whence))
}
return int64(off), nil
}
func (a *asset) Close() error {
C.AAsset_close(a.ptr)
return nil

View file

@ -55,7 +55,7 @@ func DisableCursor() {
}
// Open asset
func OpenAsset(name string) (io.ReadCloser, error) {
func OpenAsset(name string) (Asset, error) {
f, err := os.Open(name)
if err != nil {
return nil, err

View file

@ -9,7 +9,6 @@ package raylib
import "C"
import (
"io"
"os"
"unsafe"
)
@ -86,7 +85,7 @@ func ClearDroppedFiles() {
}
// Open asset
func OpenAsset(name string) (io.ReadCloser, error) {
func OpenAsset(name string) (Asset, error) {
f, err := os.Open(name)
if err != nil {
return nil, err

View file

@ -142,7 +142,7 @@ func LoadResource(fileName string) []byte {
// Load resource from file by id
// NOTE: Returns uncompressed data with parameters, search resource by id
func LoadResourceByID(fileName string, rresID int) (data []byte) {
file, err := os.Open(fileName)
file, err := OpenAsset(fileName)
if err != nil {
TraceLog(LogWarning, "[%s] rRES raylib resource file could not be opened", fileName)
return
@ -164,7 +164,7 @@ func LoadResourceByID(fileName string, rresID int) (data []byte) {
// Verify "rRES" identifier
id := fmt.Sprintf("%c", fileHeader.ID)
if id != "[r R E S]" {
TraceLog(LogWarning, "[%s] This is not a valid raylib resource file", fileName)
TraceLog(LogWarning, "[%s] is not a valid raylib resource file", fileName)
return
}