Created
February 11, 2012 05:22
-
-
Save darkf/1796694 to your computer and use it in GitHub Desktop.
Start of a HaXe canvas API
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
class Color | |
{ | |
public var r : Int; | |
public var g : Int; | |
public var b : Int; | |
public function new(r=255, g=255, b=255) | |
{ | |
this.r = r; | |
this.g = g; | |
this.b = b; | |
} | |
} | |
class Vec2 | |
{ | |
public var x : Int; | |
public var y : Int; | |
public function new(x=0, y=0) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
} | |
class Rectangle { | |
public var x : Int; | |
public var y : Int; | |
public var w : Int; | |
public var h : Int; | |
public function new(x=0, y=0, w=0, h=0) | |
{ | |
this.x = x; | |
this.y = y; | |
this.w = w; | |
this.h = h; | |
} | |
} | |
interface IRenderer { | |
function init(screenSize : Vec2) : Void; | |
function beginFrame() : Void; | |
function endFrame() : Void; | |
function clear(color : Color) : Void; | |
function drawFilledRectangle(rect : Rectangle, fillColor : Color) : Void; | |
function drawCircle(center : Vec2, radius : Int) : Void; | |
function getScreenSize() : Vec2; | |
} | |
class CanvasRenderer implements IRenderer { | |
private var screenRect : Rectangle; | |
private var canvas : js.Dom.HtmlDom; | |
private var ctx : CanvasContext2D; | |
public function new() {} | |
public function init(screenSize : Vec2) | |
{ | |
this.screenRect = new Rectangle(0, 0, screenSize.x, screenSize.y); | |
// create canvas | |
this.canvas = js.Lib.document.createElement("CANVAS"); | |
this.canvas.setAttribute("width", ""+screenRect.w); | |
this.canvas.setAttribute("height", ""+screenRect.h); | |
js.Lib.document.body.appendChild(this.canvas); | |
this.ctx = untyped this.canvas.getContext("2d"); | |
} | |
public function beginFrame() {} | |
public function endFrame() {} | |
public function clear(color) | |
{ | |
this.drawFilledRectangle(this.screenRect, color); | |
} | |
public function drawFilledRectangle(rect : Rectangle, fillColor : Color) : Void | |
{ | |
ctx.fillStyle = "rgb("+fillColor.r+","+fillColor.g+","+fillColor.b+")"; // Std.format doesn't work? | |
ctx.fillRect(rect.x, rect.y, rect.w, rect.h); | |
} | |
public function drawCircle(center, radius) | |
{ | |
} | |
public function getScreenSize() | |
{ | |
return new Vec2(this.screenRect.x, this.screenRect.y); | |
} | |
} | |
class Main { | |
static function main() { | |
//js.Lib.alert("test"); | |
//js.Lib.document.getElementById("cnv"); | |
var renderer = new CanvasRenderer(); | |
renderer.init(new Vec2(640, 480)); | |
renderer.clear(new Color(127, 127, 127)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment