function save_sprite(sprite, filename){ var file = OpenRawFile(filename,true); //header data - commented where not self explanatory file.write(CreateByteArrayFromString(".rss"));//file signiture Write_16Word(3, file);//rss version number - always 3 Write_16Word(sprite.images.length, file); Write_16Word(sprite.images[0].width, file); Write_16Word(sprite.images[0].height, file); Write_16Word(sprite.directions.length, file); Write_16Word(sprite.base.x1, file); Write_16Word(sprite.base.y1, file); Write_16Word(sprite.base.x2, file); Write_16Word(sprite.base.y2, file); file.write(CreateByteArray(106));//blank space needed by format /*this next bit is horrid (it works but it's super slow) the format requires 4 bytes per colur in rgba order not aware of any other way to do it with sphere's available functions:( */ var temp_s; var col; var temp_c = CreateByteArray(1); for(var i = 0,x=0,y=0; i < sprite.images.length; ++ i) { temp_s = sprite.images[i].createSurface(); for(y=0;y<sprite.images[0].height;++y) { for(x=0;x<sprite.images[0].width;++x) { col = temp_s.getPixel(x,y); temp_c[0] = col.red & 0xff; file.write(temp_c); temp_c[0] = col.green & 0xff; file.write(temp_c); temp_c[0] = col.blue & 0xff; file.write(temp_c); temp_c[0] = col.alpha & 0xff; file.write(temp_c); } } } //Direction data - this is easier //the CreateByteArray calls are for padding the format needs for(i = 0; i<sprite.directions.length; ++ i) { Write_16Word(sprite.directions[i].frames.length,file); file.write(CreateByteArray(6)); Write_16Word(CreateByteArrayFromString(sprite.directions[i].name).length,file); file.write(CreateByteArrayFromString(sprite.directions[i].name)); for(x=0;x<sprite.directions[i].frames.length;++x) { Write_16Word(sprite.directions[i].frames[x].index,file); Write_16Word(sprite.directions[i].frames[x].delay,file); file.write(CreateByteArray(4)); } } file.close();}function Write_16Word(word, file){ var array_of_integer = new Array(); array_of_integer[0] = (word & 0xff00) >> 8; array_of_integer[1] = (word & 0x00ff); var byte_array_of_integer = CreateByteArray(2); byte_array_of_integer[0] = array_of_integer[1]; byte_array_of_integer[1] = array_of_integer[0]; file.write(byte_array_of_integer);}
const FileWriter = require('struct').FileWriter;var stream = new FileStream(filename, FileOp.Write);var file = new FileWriter(stream);file.writeString('.rss');file.writeUint16(3);file.writeUint16(sprite.images.length);// etc.
EDIT: New thought - considering that the whole point of surfaces (as opposed to images) is that you can edit and fiddle with them shouldn't they be stored as something other than hardware textures to give faster access? Could this be an optimisation to the engine or would it be far more pain than it's worth?
Quote from: Rhuan on May 20, 2017, 07:17:31 pmEDIT: New thought - considering that the whole point of surfaces (as opposed to images) is that you can edit and fiddle with them shouldn't they be stored as something other than hardware textures to give faster access? Could this be an optimisation to the engine or would it be far more pain than it's worth?I actually tried this at one point; it ended up slowing down the majority of Sphere v1 games using surfaces. It seems that most games (including my own... ) use them as offscreen buffers more often than they do for composition, so it ends up being slower to keep them in system memory. Incidentally, your suggestion is exactly how Surfaces work in Sphere 1.5 and things usually perform okay there; best I can tell, the default DirectDraw graphics plugins are mostly software-rendered, so software surfaces are not a bottleneck.