Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Requires the SGS-SDL context
- /*
- Utility functions
- */
- function io_normalize_path( path ){ return string_replace( path, "\\", "/" ); }
- function io_dirname( path )
- {
- path = string_explode( io_normalize_path( path ), "/" );
- path.pop();
- return string_implode( path, "/" );
- }
- function sgson_eval( text ){ return eval( "return " $ text $ ";" ); }
- /*
- class SS_SpriteSheet
- - create( spec, imgroot = "./" )
- - createFromText( sgsontext, imgroot = "./" )
- - createFromFile( sgsonfile, imgroot = dirname(file)$"/" )
- - createGrid( image, hsz, vsz, hsp = 0, vsp = 0, namefunc = (=> %x_%y) )
- - getSprite( name )
- + texture : SS_Texture
- */
- global SS_SpriteSheet = {};
- function SS_SpriteSheet.create( spec, imgroot )
- {
- imgroot ||= "./";
- if( !@spec.image )
- WARNING( "expected SPEC.image" );
- if( !is_dict( @spec.sprites ) )
- WARNING( "expected SPEC.sprites as dict" );
- texture = SS_CreateTexture( imgroot $ spec.image, "nolerp" );
- if( !texture )
- return;
- tw = texture.width;
- th = texture.height;
- sprites = {};
- foreach( spritename, spritespec : spec.sprites )
- {
- tx1 = @spritespec.x1;
- ty1 = @spritespec.y1;
- tx2 = @spritespec.x2;
- ty2 = @spritespec.y2;
- ogx = toint( @spritespec.origin_x );
- ogy = toint( @spritespec.origin_y );
- if( tx1 === null ||
- ty1 === null ||
- tx2 === null ||
- ty2 === null )
- WARNING( "all coords (x1,y1,x2,y2) must be set for each sprite" );
- if( ty1 >= ty2 || tx1 >= tx2 )
- WARNING( "coord 1 is greater than or equal to coord 2" );
- sprites[ spritename ] =
- [
- tx2 - tx1, ty2 - ty1, ogx, ogy,
- tx1 / tw, ty1 / th, tx2 / tw, ty2 / th,
- tx1, ty1, tx2, ty2
- ];
- }
- data =
- {
- texture = texture,
- sprites = sprites,
- };
- return class( data, SS_SpriteSheet );
- }
- function SS_SpriteSheet.createFromText( sgsontext, imgroot )
- {
- spec = sgson_eval( sgsontext );
- if( !spec )
- return;
- return this.create( spec, imgroot );
- }
- function SS_SpriteSheet.createFromFile( sgsonfile, imgroot )
- {
- imgroot ||= io_dirname( sgsonfile ) $ "/";
- text = io_file_read( sgsonfile );
- if( text === null )
- return;
- return this.createFromText( text, imgroot );
- }
- function SS_SpriteSheet.createGrid( image, hsz, vsz, hsp, vsp, namefunc )
- {
- hsz = toint(hsz);
- vsz = toint(vsz);
- hsp = toint(hsp);
- vsp = toint(vsp);
- namefunc ||= function( x, y, i ){ return x $ "_" $ y; };
- texture = SS_CreateTexture( image, "nolerp" );
- if( !texture )
- return;
- tw = texture.width;
- th = texture.height;
- sprites = {};
- i = 0;
- ix = 0;
- iy = 0;
- for( y = 0; y < th - vsz; y += vsz + vsp )
- {
- for( x = 0; x < tw - hsz; x += hsz + hsp )
- {
- name = tostring( namefunc( ix, iy, i ) );
- sprites[ name ] =
- [
- hsz, vsz, 0, 0,
- x / tw, y / th, ( x + hsz ) / tw, ( y + vsz ) / th,
- x, y, x + hsz, y + vsz
- ];
- i++;
- ix++;
- }
- iy++;
- ix = 0;
- }
- data =
- {
- texture = texture,
- sprites = sprites,
- };
- return class( data, SS_SpriteSheet );
- }
- function SS_SpriteSheet.getSprite( name )
- {
- return @this.sprites[ name ];
- }
- /*
- class SS_SpriteSheetAnim
- - create()
- - tick( deltaTime )
- - getSheet()
- - getFrame()
- - setAnim( name, spec )
- - play( name, loop = true )
- - stop()
- */
- global SS_SpriteSheetAnim = {};
- function SS_SpriteSheetAnim.create( sheet )
- {
- data =
- {
- sheet = sheet,
- anims = {},
- cur_anim = null,
- cur_time = 0.0,
- cur_loop = false,
- cur_fid = 1,
- };
- return class( data, SS_SpriteSheetAnim );
- }
- function SS_SpriteSheetAnim.tick( deltaTime )
- {
- if( this.cur_anim )
- {
- // advance time
- this.cur_time += deltaTime;
- if( this.cur_loop && this.cur_time >= this.cur_anim[0] )
- {
- this.cur_time -= this.cur_anim[0];
- this.cur_fid = 1;
- }
- // advance frame ID
- while( this.cur_fid + 2 < this.cur_anim.size &&
- this.cur_anim[ this.cur_fid + 2 ] < this.cur_time )
- this.cur_fid += 2;
- }
- }
- function SS_SpriteSheetAnim.getSheet()
- {
- return this.sheet;
- }
- function SS_SpriteSheetAnim.getFrame()
- {
- return if( this.cur_anim, this.cur_anim[ this.cur_fid + 1 ], null );
- }
- function SS_SpriteSheetAnim.setAnim( name, spec )
- {
- if( !is_array( spec ) || spec.size < 3 )
- WARNING( "anim.spec must be an array with at least 3 items (length, frame 1 offset, frame 1 name)" );
- spec[ 0 ] = toreal( spec[ 0 ] );
- prev = 0.0;
- for( i = 1; i < spec.size; i += 2 )
- {
- sir = parsereal( spec[ i ] );
- if( sir === null )
- WARNING( "item at index " $ i $ " is not a number" );
- spec[ i ] = sir;
- if( spec[ i ] < prev )
- WARNING( "frames are not ordered" );
- prev = spec[ i ];
- }
- this.anims[ name ] = spec;
- }
- function SS_SpriteSheetAnim.play( name, loop )
- {
- if( !isset( this.anims, name ) )
- WARNING( "animation '" $ name $ "' was not found" );
- loop = if( loop === null, true, tobool( loop ) );
- this.cur_anim = this.anims[ name ];
- this.cur_time = 0.0;
- this.cur_loop = loop;
- this.cur_fid = 1;
- }
- function SS_SpriteSheetAnim.stop()
- {
- this.cur_anim = null;
- }
- /*
- class SS_PixelDraw
- - create()
- - beginDraw()
- - endDraw()
- - setViewOffset( x, y )
- - drawSprite( sheet, name, x, y, col = 1.0 )
- - _flush()
- - _prepareState( texture )
- - _updateView()
- */
- global SS_PixelDraw = {};
- function SS_PixelDraw.create( canvas )
- {
- if( !canvas )
- WARNING( "must specify canvas (object with .width/.height)" );
- data =
- {
- last_texture = null,
- renderbuffer = SS_CreateRenderBuffer(),
- vertexformat = SS_MakeVertexFormat( "pf2tf2cb4" ),
- viewofs_x = 0,
- viewofs_y = 0,
- drawing = false,
- numverts = 0,
- canvas = canvas,
- };
- return class( data, SS_PixelDraw );
- }
- function SS_PixelDraw.beginDraw()
- {
- if( this.drawing )
- WARNING( "already drawing" );
- this.drawing = true;
- this.renderbuffer.begin();
- this.numverts = 0;
- this._updateView();
- }
- function SS_PixelDraw.endDraw()
- {
- if( !this.drawing )
- WARNING( "not drawing" );
- this._flush();
- this.drawing = false;
- }
- function SS_PixelDraw.setViewOffset( x, y )
- {
- this.viewofs_x = toint(x);
- this.viewofs_y = toint(y);
- }
- function SS_PixelDraw.drawSprite( sheet, name, x, y, flip_x, flip_y, col )
- {
- if( !this.drawing )
- WARNING( "not drawing" );
- if( !sheet || !name )
- WARNING( "sheet and name must be specified" );
- spr = sheet.getSprite( name );
- if( !spr )
- WARNING( "sprite not found" );
- this._prepareState( sheet.texture );
- rb = this.renderbuffer;
- cr = if( col, col.r, 1.0 );
- cg = if( col, col.g, 1.0 );
- cb = if( col, col.b, 1.0 );
- ca = if( col, col.a, 1.0 );
- x -= spr[2];
- y -= spr[3];
- x2 = x + spr[0];
- y2 = y + spr[1];
- tx1 = spr[4];
- ty1 = spr[5];
- tx2 = spr[6];
- ty2 = spr[7];
- if( flip_x ){ tmp = tx1; tx1 = tx2; tx2 = tmp; }
- if( flip_y ){ tmp = ty1; ty1 = ty2; ty2 = tmp; }
- rb.f( x, y, tx1, ty1 ).cf2b( cr, cg, cb, ca );
- rb.f( x2, y, tx2, ty1 ).cf2b( cr, cg, cb, ca );
- rb.f( x2, y2, tx2, ty2 ).cf2b( cr, cg, cb, ca );
- rb.f( x2, y2, tx2, ty2 ).cf2b( cr, cg, cb, ca );
- rb.f( x, y2, tx1, ty2 ).cf2b( cr, cg, cb, ca );
- rb.f( x, y, tx1, ty1 ).cf2b( cr, cg, cb, ca );
- this.numverts += 6;
- }
- function SS_PixelDraw._flush()
- {
- this.renderbuffer.draw( this.last_texture, this.vertexformat, 0, this.numverts, SS_PT_TRIANGLES );
- this.renderbuffer.begin();
- this.numverts = 0;
- }
- function SS_PixelDraw._prepareState( texture )
- {
- if( this.last_texture != texture )
- {
- this._flush();
- this.last_texture = texture;
- }
- }
- function SS_PixelDraw._updateView()
- {
- x = this.viewofs_x;
- y = this.viewofs_y;
- SS_SetCameraUI( x, x + this.canvas.width, y, y + this.canvas.height );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement