[ascii] blitSurface basic
This commit is contained in:
4
projects/asciilib2/src/asciilib/Assets.hx
Normal file
4
projects/asciilib2/src/asciilib/Assets.hx
Normal file
@@ -0,0 +1,4 @@
|
||||
package asciilib;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class Assets {}
|
14
projects/asciilib2/src/asciilib/Assets.kiss
Normal file
14
projects/asciilib2/src/asciilib/Assets.kiss
Normal file
@@ -0,0 +1,14 @@
|
||||
(defnew [assetsBackend]
|
||||
// TODO use &prop for this
|
||||
[:AssetsBackend backend assetsBackend
|
||||
:Map<String,Surface> _surfaces (new Map)])
|
||||
|
||||
// TODO don't allow overriding a key -- use a macro so all load___() calls check their maps first
|
||||
(defmethod loadSurface [key path]
|
||||
(dictSet _surfaces key (Surface.fromString (backend.loadText path))))
|
||||
|
||||
// TODO runtime-assert that the key exists. Use a macro so all get___() calls check their maps first
|
||||
(defmethod getSurface [key]
|
||||
(dictGet _surfaces key))
|
||||
|
||||
// TODO freeSurface() etc.
|
5
projects/asciilib2/src/asciilib/AssetsBackend.hx
Normal file
5
projects/asciilib2/src/asciilib/AssetsBackend.hx
Normal file
@@ -0,0 +1,5 @@
|
||||
package asciilib;
|
||||
|
||||
interface AssetsBackend {
|
||||
function loadText(filePath:String):String;
|
||||
}
|
@@ -4,19 +4,22 @@
|
||||
letterWidth
|
||||
letterHeight
|
||||
_gameLogic
|
||||
assetsBackend
|
||||
_graphicsBackend]
|
||||
|
||||
// TODO the type annotation on this line feels like a bit much, but is necessary:
|
||||
[:Graphics graphics (new Graphics width height)
|
||||
:GameLogic gameLogic _gameLogic
|
||||
:Assets assets (new Assets assetsBackend)
|
||||
:GraphicsBackend graphicsBackend _graphicsBackend]
|
||||
|
||||
(graphicsBackend.initialize title width height letterWidth letterHeight))
|
||||
(graphicsBackend.initialize title width height letterWidth letterHeight)
|
||||
(gameLogic.initialize assets))
|
||||
|
||||
(defmethod update [:Float deltaSeconds]
|
||||
(gameLogic.update deltaSeconds))
|
||||
(gameLogic.update this deltaSeconds))
|
||||
|
||||
(defmethod draw []
|
||||
(let [&mut changedGraphics false]
|
||||
(gameLogic.draw (lambda [] (set changedGraphics true) graphics))
|
||||
(gameLogic.draw (lambda [] (set changedGraphics true) graphics) assets)
|
||||
(when changedGraphics (graphicsBackend.draw graphics))))
|
@@ -1,6 +1,10 @@
|
||||
package asciilib;
|
||||
|
||||
// Your game's logic is an interface contained in Game instead of a class that extends Game.
|
||||
// This allows ASCIILib to support libraries like HaxeFlixel where your main class is expected
|
||||
// to extend another class already.
|
||||
interface GameLogic {
|
||||
public function update(deltaSeconds:Float):Void;
|
||||
public function draw(graphicsHandle:() -> Graphics):Void;
|
||||
public function initialize(assets:Assets):Void;
|
||||
public function update(game:Game, deltaSeconds:Float):Void;
|
||||
public function draw(graphicsHandle:() -> Graphics, assets:Assets):Void;
|
||||
}
|
||||
|
@@ -20,6 +20,32 @@
|
||||
(letters.setChar x y letter.char)
|
||||
(letterColors.setPixel x y letter.color))
|
||||
|
||||
(defmethod isCellOpaque [x y]
|
||||
(opacity.getCell x y))
|
||||
|
||||
(defmethod setCellOpacity [x y value]
|
||||
(opacity.setCell x y value))
|
||||
|
||||
(defmethod getSpecialInfo [x y]
|
||||
(specialInfo.getCell x y))
|
||||
|
||||
(defmethod setSpecialInfo [x y value]
|
||||
(specialInfo.setCell x y value))
|
||||
|
||||
// TODO rectangle type
|
||||
// TODO optional source rectangle argument
|
||||
(defmethod blitSurface [:Surface surface x y]
|
||||
(doFor [srcX destX] (zip (collect (range 0 surface.width)) (collect (range x (+ x surface.width))))
|
||||
(when (< -1 destX width)
|
||||
(doFor [srcY destY] (zip (collect (range 0 surface.height)) (collect (range y (+ y surface.height))))
|
||||
(when (< -1 destY height)
|
||||
(when (surface.isCellOpaque srcX srcY)
|
||||
(setBackgroundColor destX destY (surface.getBackgroundColor srcX srcY))
|
||||
(setLetter destX destY (surface.getLetter srcX srcY))
|
||||
(setSpecialInfo destX destY (surface.getSpecialInfo srcX srcY))
|
||||
// Cover transparent cells in the lower surface with opaque ones
|
||||
(setCellOpacity destX destY true)))))))
|
||||
|
||||
(defun fromString [text]
|
||||
(let [stream (Stream.fromString text)
|
||||
:Map<String,Color> colors (new Map)
|
||||
@@ -67,4 +93,5 @@
|
||||
(stream.dropString "SPECIAL INFO\n")
|
||||
(doFor y (range height)
|
||||
(doFor x (range width)
|
||||
(surface.specialInfo.setCell x y (dictGet infoCodes (stream.expect "a special info code" ->{(stream.takeChars 1)}))))))))))
|
||||
(surface.specialInfo.setCell x y (dictGet infoCodes (stream.expect "a special info code" ->{(stream.takeChars 1)})))))
|
||||
surface)))))
|
@@ -0,0 +1,7 @@
|
||||
package asciilib.backends.flixel;
|
||||
|
||||
import asciilib.AssetsBackend;
|
||||
import openfl.Assets;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class FlxAssetsBackend implements AssetsBackend {}
|
@@ -0,0 +1,3 @@
|
||||
(defmethod new [] 0)
|
||||
|
||||
(defmethod loadText [filePath] (Assets.getText filePath))
|
Reference in New Issue
Block a user