-
-
Save YellowAfterlife/971f874d6447200269da to your computer and use it in GitHub Desktop.
MinimalFramework, for AS3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.display.MovieClip; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.events.KeyboardEvent; | |
import flash.events.MouseEvent; | |
import flash.geom.Point; | |
import flash.geom.Rectangle; | |
import flash.utils.Dictionary; | |
import flash.utils.getTimer; | |
/** | |
* MinimalFramework | |
* A really tiny (2.9KB base size) framework that I have made for cases when I would want | |
* to participate in contests revolving around extremely limited SWF size. Only several | |
* games are 'powered by it' so far, mostly being 3..5-hour jam games, doing their purposes. | |
* @author YellowAfterlife | |
*/ | |
public class Main extends MovieClip | |
{ | |
//{ Inner functions | |
public var swidth:uint = 160; // screen width | |
public var sheight:uint = 120; // screen height | |
public var scolor:uint = 0x553344; // color to clear screen with | |
// time variables | |
public var elapsed:Number; // time elapsed since last frame | |
public var last:int; | |
public var time:int; | |
// screen buffers: | |
public var target:BitmapData; // buffer to draw to right now | |
public var screen:Vector. = new Vector.(2); // pair of buffers | |
public var screenc:int = 0; // index of current buffer | |
public var screens:Sprite = new Sprite; // sprite to represent actual bitmap on screen | |
// input: | |
public var keydown: = new Vector.(256); // states of keys (held) | |
public var keypressed:Vector. = new Vector.(256); // states of keys (pressed) | |
public var keyreleased:Vector. = new Vector.(256); // states of keys (released) | |
public var mousedown:Boolean, mousepressed:Boolean, mousereleased:Boolean; // mouse states | |
//{ Event handlers | |
private function onkeyd(e:KeyboardEvent):void | |
{ | |
var c:uint = e.keyCode; | |
if (keydown[c]) return; | |
keypressed[c] = true; | |
keydown[c] = true; | |
} | |
private function onkeyu(e:KeyboardEvent):void | |
{ | |
var c:uint = e.keyCode; | |
if (!keydown[c]) return; | |
keyreleased[c] = true; | |
keydown[c] = false; | |
} | |
private function onmd(e:MouseEvent):void | |
{ | |
if (mousedown) return; | |
mousepressed = true; | |
mousedown = true; | |
} | |
private function onmu(e:MouseEvent):void | |
{ | |
if (!mousedown) return; | |
mousereleased = true; | |
mousedown = false; | |
} | |
//} | |
//{ onFrame | |
private function onframe(e:Event):void | |
{ | |
// update elapsed: | |
time = getTimer(); | |
elapsed = (time - last) / 1000; | |
last = time; | |
// update game: | |
update(); | |
// input update: | |
var i:uint; | |
for (i = 0; i < 256; i++) | |
{ | |
if (keypressed[i]) keypressed[i] = false; | |
if (keyreleased[i]) keyreleased[i] = false; | |
} | |
mousepressed = mousereleased = false; | |
// render game: | |
_render(); | |
} | |
//} | |
//{ render function: | |
public function _render():void | |
{ | |
screenc = 1 - screenc; | |
target = screen[screenc].bitmapData; | |
target.fillRect(target.rect, scolor); | |
render(); | |
screen[screenc].visible = true; | |
screen[1 - screenc].visible = false; | |
} | |
//} | |
//{ addedToStage event | |
private function added(e:Event = null):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, added); | |
// | |
var i:int; | |
for (i = 0; i < 2; i++) | |
{ | |
screen[i] = new Bitmap(new BitmapData(swidth, sheight, false, 0), "never"); | |
screens.addChild(screen[i]); | |
} | |
stage.addChild(screens); | |
target = screen[0].bitmapData; | |
// bind events: | |
stage.addEventListener(KeyboardEvent.KEY_DOWN, onkeyd); | |
stage.addEventListener(KeyboardEvent.KEY_UP, onkeyu); | |
stage.addEventListener(MouseEvent.MOUSE_DOWN, onmd); | |
stage.addEventListener(MouseEvent.MOUSE_UP, onmu); | |
// | |
stage.align = "TL"; | |
stage.quality = "high"; | |
stage.scaleMode = "noScale"; | |
stage.displayState = "normal"; | |
// | |
start(); | |
// start events: | |
last = getTimer(); | |
stage.addEventListener(Event.ENTER_FRAME, onframe); | |
} | |
//} | |
//} | |
//{ Bitmap functions | |
protected const bmp_alpha:String = "0123456789ABCDEFGHJIKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz()"; | |
protected var bmp_str:String; | |
protected var bmp_array:Array; | |
protected var bmp_point:Point = new Point; | |
protected var bmp_int:int; | |
protected var bmp_trace:String = ""; | |
/** | |
* Converts string to a BitmapData, following defined rules | |
* bmp_str -> 'dictionary' with letters | |
* bmp_array -> colors for each element in bmp_str | |
* bmp_int -> integer multiplier to scale everything up by | |
* @param src string with data | |
* @param w number of letters in row | |
* @return 'decoded' BitmapData | |
*/ | |
protected function str2bmp(src:String, w:uint):BitmapData | |
{ | |
var scale:uint = bmp_int; | |
var r:Rectangle = new Rectangle(0, 0, scale, scale); | |
var l:uint = src.length; | |
var b:BitmapData = new BitmapData(w * scale, uint(l / w) * scale, true, 0); | |
var c:int; | |
for (var k:uint = 0; k < l; k++) | |
{ | |
c = bmp_str.indexOf(src.charAt(k)); | |
if (c == -1) continue; | |
r.x = (k % w) * scale; | |
r.y = uint(k / w) * scale; | |
b.fillRect(r, bmp_array[c]); | |
} | |
return b; | |
} | |
// Simple function to draw a bitmap to coordinates: | |
protected function bmap_draw(s:BitmapData, x:Number, y:Number):void | |
{ | |
bmp_point.x = x; | |
bmp_point.y = y; | |
target.copyPixels(s, s.rect, bmp_point, null, null, true); | |
} | |
//} | |
//{ Pseudo-font drawing: | |
protected var font ictionary = new Dictionary; // stores letter BitmapData's | |
protected var fontx:int = 1; // horizontal spacing between letters | |
protected var fonty:int = 8; // vertical spacing (line height) | |
protected var fontp:Point = new Point; | |
protected function text_draw(x:Number, y:Number, s:String):void | |
{ | |
var i:int, c:String, b:BitmapData; | |
fontp.x = x; fontp.y = y; // fontp is working like a cursor here, being position to draw to | |
for (i = 0; i < s.length; i++) | |
{ | |
c = s.charAt(i); | |
// 'special cases' go here: | |
if (c == "\n" || c == "\r") | |
{ | |
fontp.x = x; | |
fontp.y += fonty; | |
} | |
// no matching character in font sheet: | |
if (font[c] == null) continue; | |
// draw the thing: | |
b = font[c] as BitmapData; | |
target.copyPixels(b, b.rect, fontp, null, null, true); | |
// shift drawing 'cursor' to next position: | |
fontp.x += b.width + fontx; | |
} | |
} | |
protected function text_init():void | |
{ | |
bmp_str = "x"; // used for decoding. | |
bmp_array = [0xFFFF00CC]; // font color | |
bmp_int = 1; // font size | |
var fd:String = "h00000f11101h55000jAVAVAq4E3C74j9842Jj61M9Mf11000g21112g12221h52500h02720p000021h00700f00001jG8421i69996g32222i7861Fi78687i465F4iF1787i61796iF8422i69696i69E86f01010f01011h42124h07070h12421i78602jEHTLEi69F99i79797h61116i79997h71717h71711iE1D9Ei99F99h72227iC8896i95359h11117jHRLHHi9BD99i69996i79971q699968i79979iE1687h72222i99996i99552jHLLLAi99699i99E87h74217g31113j1248Gg32223h25000i0000Fg12000i0E99Ei17997h06116i8E99Ei06D36h42722y0699E86i17999f10111w2022221i19579f11111j0FLLLi07999i06996y0799711y0E99E88h05311i0E3C7h27224i0999Ei09952j0LLAAh05225y0999E87i0F42Fh62126f11111h32423"; // 04b03 font. Very compressed. | |
var i:int = 0, j:int, k:int, c:int = " ".charCodeAt(); | |
var z:int, q:int, s:String; | |
while (i < fd.length) { | |
z = bmp_alpha.indexOf(fd.charAt(i++)); | |
s = ""; | |
for (j = 0; j < (z >>> 3); j++) { | |
q = bmp_alpha.indexOf(fd.charAt(i++)); | |
for (k = 0; k < (z & 7); k++) { | |
s += (q & (1 << k)) != 0 ? "x" : " "; | |
} | |
} | |
font[String.fromCharCode(c++)] = str2bmp(s, z & 7); | |
} | |
} | |
//} | |
// Executed on startup: | |
public function start():void | |
{ | |
screens.scaleX = screens.scaleY = 4; | |
text_init(); | |
} | |
// Executed every step: | |
public function update():void | |
{ | |
// step actions here. use 'elapsed' to get time from previous step | |
} | |
// Executed when drawing: | |
public function render():void | |
{ | |
// draw actions here. | |
text_draw(4, 4, 'Oh hi there.\nI am an extremely small framework,\nsomewhat inspired by FlashPunk.\nMaybe you can find a use for me.'); | |
} | |
public function Main():void | |
{ | |
if (stage) added(); | |
else addEventListener(Event.ADDED_TO_STAGE, added); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment