code.witches.io/go/sdl2@v0.1.1/rwops.go (about) 1 package sdl 2 3 // #include <SDL2/SDL_rwops.h> 4 // 5 // Sint64 RWsize(struct SDL_RWops *context) { 6 // return context->size(context); 7 // } 8 // 9 // Sint64 RWseek(struct SDL_RWops *context, Sint64 offset, int whence) { 10 // return context->seek(context, offset, whence); 11 // } 12 // 13 // size_t RWread(struct SDL_RWops *context, void *ptr, size_t size, size_t maxnum) { 14 // return context->read(context, ptr, size, maxnum); 15 // } 16 // 17 // size_t RWwrite(struct SDL_RWops *context, const void *ptr, size_t size, size_t num) { 18 // return context->write(context, ptr, size, num); 19 // } 20 // 21 // int RWclose(struct SDL_RWops *context) { 22 // return context->close(context); 23 // } 24 import "C" 25 import ( 26 "io" 27 "unsafe" 28 ) 29 30 type RWOpsType uint32 31 32 const ( 33 RWOpsUnknown RWOpsType = iota 34 RWOpsWin32File 35 RWOpsStdFile 36 RWOpsJNIFile 37 RWOpsMemory 38 RWOpsMemoryReadOnly 39 ) 40 41 type RWOps C.struct_SDL_RWops 42 43 func RWFromFile(file, mode string) (*RWOps, error) { 44 nativeFile := C.CString(file) 45 defer C.free(unsafe.Pointer(nativeFile)) 46 47 nativeMode := C.CString(mode) 48 defer C.free(unsafe.Pointer(nativeMode)) 49 50 nativeRWOps := C.SDL_RWFromFile(nativeFile, nativeMode) 51 if nativeRWOps == nil { 52 return nil, GetError() 53 } 54 55 return (*RWOps)(nativeRWOps), nil 56 } 57 58 func RWFromMem(mem unsafe.Pointer, size int) (*RWOps, error) { 59 nativeRWOps := C.SDL_RWFromMem(mem, C.int(size)) 60 if nativeRWOps == nil { 61 return nil, GetError() 62 } 63 64 return (*RWOps)(nativeRWOps), nil 65 } 66 67 func RWFromConstMem(mem unsafe.Pointer, size int) (*RWOps, error) { 68 nativeRWOps := C.SDL_RWFromConstMem(mem, C.int(size)) 69 if nativeRWOps == nil { 70 return nil, GetError() 71 } 72 73 return (*RWOps)(nativeRWOps), nil 74 } 75 76 func AllocRW() (*RWOps, error) { 77 nativeRWOps := C.SDL_AllocRW() 78 if nativeRWOps == nil { 79 return nil, GetError() 80 } 81 82 return (*RWOps)(nativeRWOps), nil 83 } 84 85 func FreeRW(area *RWOps) { 86 C.SDL_FreeRW((*C.struct_SDL_RWops)(area)) 87 } 88 89 func (o *RWOps) Size() int64 { 90 nativeRWOps := (*C.struct_SDL_RWops)(o) 91 //return nativeRWOps.size.(func(*C.struct_SDL_RWops) C.int64)(nativeRWOps) 92 return int64(C.RWsize(nativeRWOps)) 93 } 94 95 func (o *RWOps) Seek(offset int64, whence int) (int64, error) { 96 nativeRWOps := (*C.struct_SDL_RWops)(o) 97 return int64(C.RWseek(nativeRWOps, C.Sint64(offset), C.int(whence))), nil 98 } 99 100 func (o *RWOps) Read(p []byte) (int, error) { 101 nativeRWOps := (*C.struct_SDL_RWops)(o) 102 n := int(C.RWread(nativeRWOps, unsafe.Pointer(&p[0]), 1, C.size_t(len(p)))) 103 if n == 0 { 104 err := GetError() 105 if err == nil { 106 return 0, io.EOF 107 } 108 return 0, err 109 } 110 return n, nil 111 } 112 113 func (o *RWOps) Write(p []byte) (int, error) { 114 nativeRWOps := (*C.struct_SDL_RWops)(o) 115 n := int(C.RWwrite(nativeRWOps, unsafe.Pointer(&p[0]), 1, C.size_t(len(p)))) 116 if n != len(p) { 117 return n, GetError() 118 } 119 return n, nil 120 } 121 122 func (o *RWOps) Close() error { 123 nativeRWOps := (*C.struct_SDL_RWops)(o) 124 r := C.RWclose(nativeRWOps) 125 if r != 0 { 126 return GetError() 127 } 128 return nil 129 }