2 * SDL_RWops - ruby interface
4 * Author:: Ed Sinjiashvili (slimb@vlinkmail.com)
11 #include "rb_physfs.h"
12 #include "rb_sdl_rwops.h"
19 VALUE sdl_rwops_new (SDL_RWops *ops)
26 result = Data_Wrap_Struct (classRWops, 0, SDL_FreeRW, ops);
31 * PhysicsFS::RWops::from_file name, mode
33 * create RWops object from file
35 VALUE sdl_rwops_from_file (VALUE self, VALUE name, VALUE mode)
37 SDL_RWops *ops = SDL_RWFromFile(STR2CSTR(name), STR2CSTR(mode));
38 return sdl_rwops_new (ops);
42 * PhysicsFS::RWops::from_memory string
44 * create RWops object from memory
46 VALUE sdl_rwops_from_mem (VALUE self, VALUE str)
48 int len = RSTRING(str)->len;
49 void *mem = STR2CSTR(str);
50 SDL_RWops *ops = SDL_RWFromMem(mem, len);
52 return sdl_rwops_new (ops);
56 * PhysicsFS::RWops#seek offset, whence
58 * position RWops object
60 VALUE sdl_rwops_seek (VALUE self, VALUE offset, VALUE whence)
65 Data_Get_Struct (self, SDL_RWops, ops);
69 result = SDL_RWseek(ops, FIX2INT(offset), FIX2INT(whence));
70 return INT2FIX(result);
74 * PhysicsFS::RWops#close
76 * close RWops. No use of the object is possible after that.
78 VALUE sdl_rwops_close (VALUE self)
83 Data_Get_Struct (self, SDL_RWops, ops);
87 result = SDL_RWclose (ops);
90 return INT2FIX(result);
94 * PhysicsFS::RWops#read
96 * read from RWops object objCount objSize'd entities.
97 * return string containing raw data or nil
99 VALUE sdl_rwops_read (VALUE self, VALUE objSize, VALUE objCount)
106 Data_Get_Struct (self, SDL_RWops, ops);
110 buffer = malloc (FIX2UINT(objSize) * FIX2UINT(objCount));
114 objRead = SDL_RWread (ops, buffer, FIX2UINT(objSize), FIX2UINT(objCount));
121 result = rb_str_new (buffer, objRead * FIX2UINT(objSize));
127 * PhysicsFS::RWops#write buffer, size, n
129 * write raw string containing n objects size length each.
130 * return number of objects written or nil
132 VALUE sdl_rwops_write (VALUE self, VALUE buffer, VALUE size, VALUE n)
137 Data_Get_Struct (self, SDL_RWops, ops);
141 result = SDL_RWwrite (ops, STR2CSTR(buffer), FIX2INT(size), FIX2INT(n));
146 return INT2FIX(result);
149 void init_sdl_rwops (void)
151 classRWops = rb_define_class_under (modulePhysfs, "RWops", rb_cObject);
153 rb_define_method (classRWops, "seek", sdl_rwops_seek, 2);
154 rb_define_method (classRWops, "read", sdl_rwops_read, 2);
155 rb_define_method (classRWops, "write", sdl_rwops_write, 3);
156 rb_define_method (classRWops, "close", sdl_rwops_close, 0);
158 rb_define_singleton_method (classRWops, "from_file",
159 sdl_rwops_from_file, 2);
160 rb_define_singleton_method (classRWops, "from_memory",
161 sdl_rwops_from_mem, 1);