text drawing (Arbitrary code for Stencyl 4.0.4)


Stencyl text drawing 
Problem: cache has let to browser crash when having too much drawing. 

Does not work in Flash. 

Reversed engineered from Stencyl source files. 
Plaf > haxe > lib > stencyl > 1,00 > com > stencyl >  graphics > G.hx 




changes compared to the original 
Text position is purely anchored to screen
Font size can be adjusted by setting font scale, this.fs

Full code for copy and paste:  
package gra;

import com.stencyl.Config;
import com.stencyl.Engine;
import com.stencyl.models.Font;
import openfl.display.Graphics;
import openfl.display.BitmapData; 
import openfl.geom.Matrix; 
class G_to
{
public var alpha:Float;
public var color:Int; 
public var f_cu:Font; //current font
        public var f_de:Font; //default font
public var fs: Float;   
public var grap: Graphics;
public var lesp: Int; 
public var matr: Matrix; 

public function new()
{this.alpha = 1; 
this.f_cu = this.f_de = new Font(-1, 0, "", true); 
this.fs = 1; 
this.grap = Engine.engine.transitionLayer.graphics; 
this.lesp = 0; 
this.matr = new Matrix(); }

public function setFont(f_new:Font):Void     
        {if(f_new != null)
             {this.f_cu = f_new;}
         }

public function drawString(s,x,y):Void
{
if(this.f_cu == null)  {this.f_cu = this.f_de;}
this.matr.identity();
this.matr.translate(x,y);

var toDraw:BitmapData = null;
var w = this.f_cu.font.getTextWidth(s, this.lesp , this.fs);
var h = Std.int(this.f_cu.font.getFontHeight() * this.fs);
if(w > 0 && h > 0)
{
toDraw = new BitmapData(w, h, true, 0);
this.f_cu.font.renderToImg(toDraw,s, this.color, this.alpha, 0, 0, this.lesp, this.fs, 100,true);
// original:  this.f_cu.font.renderToImg(toDraw,s, 0, this.alpha, 0, 0, this.lesp, this.fs, 100,false);
// Stencyl used to allow change in color back in 2.x However, you can change the last one to true 
// to change color. However, your font has to be white for it work properly 
// it will work the same way as you apply tint to actors thus does not work on black fonts.   
}
if(toDraw != null)
{
this.grap.beginBitmapFill(toDraw, this.matr, false, Config.antialias);
this.grap.drawRect(x,y, toDraw.width, toDraw.height);
this.grap.endFill();
}
}
}

To use this code in design mode, 
custom code and custom import is required 

In this case
custom import: import gra.G_to; 
custom code: public var gto:G_to = new G_to(); 

Drawing 

Additional advantages: 
- Allow changes to the font size with When created. Although results in choppy graphics if this.fs is not equal to 1. 
- Allow changes to the font when created.
- Allow changes to letter spacing when created. 
All the above will reduce computing power required to run the game especially when the game is an Idle game as well as text-based games. Instead of checking every frame, it can be done in the beginning once and for all. 

WARNING: 
in order for the text to work properly, your Font must be white. it wont work on black Font. Similar to tinting, it wont work on black color. No racism intended. 
 


No comments:

Post a Comment