[ascii] blitSurface basic

This commit is contained in:
2021-04-25 14:55:12 -06:00
parent 0e98e72245
commit 4f3f599909
13 changed files with 265 additions and 12 deletions

View File

@@ -63,7 +63,6 @@ class Stream {
content = content.replace('\r', '');
}
// Life is easier with a trailing newline
if (content.charAt(content.length - 1) != "\n")
content += "\n";

View File

@@ -0,0 +1,184 @@
COLORS
! 128 128 255
" 255 255 255
# 98 49 0
$ 159 80 0
% 41 41 41
& 128 128 128
' 106 106 106
( 0 0 0
INFO CODES
! .
" POINT_screen
SIZE
68 33
CHARACTERS
BACKGROUND COLORS
!!!!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!!!!
!!!!%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&%!!!!
!!!!%&''''''''''''''''''''''''''''''''''''''''''''''''''''''''&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((('&%!!!!
!!!!%&''''''''''''''''''''''''''''''''''''''''''''''''''''''''&%!!!!
!!!!%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&%!!!!
!!!!%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%!!!!
!!!%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&%!!!
!!!%&&''''''''''''''''''''''''''''''''''''''''''''''''''''''''&&%!!!
!!%&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&%!!
!!%&&''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&&%!!
!%&&'&&&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&%!
!%&&''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&&%!
%&&'&&&&&'&&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&'&&&&&'&&%
%&&''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&&%
CHARACTER COLORS
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
OPACITY
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111111111111111111111110000
00011111111111111111111111111111111111111111111111111111111111111000
00011111111111111111111111111111111111111111111111111111111111111000
00111111111111111111111111111111111111111111111111111111111111111100
00111111111111111111111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111111111111111111111110
01111111111111111111111111111111111111111111111111111111111111111110
11111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111
SPECIAL INFO
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

View File

@@ -3,6 +3,8 @@ package;
import asciilib.GameLogic;
import asciilib.Graphics;
import asciilib.Colors;
import asciilib.Assets;
import asciilib.Game;
import kiss.Prelude;
@:build(kiss.Kiss.build())

View File

@@ -2,9 +2,10 @@
(defmethod new [] 0)
(defmethod :Void update [:Float deltaSeconds] 0)
(defmethod :Void draw [:Void->Graphics graphics]
(defmethod :Void initialize [:Assets assets]
(assets.loadSurface "laptop" AssetPaths.laptop__srf))
(defmethod :Void update [:Game game :Float deltaSeconds] 0)
(defmethod :Void draw [:Void->Graphics graphics :Assets assets]
(when firstDraw
(set firstDraw false)
(.setBackgroundColor (graphics) 5 5 Colors.White)
(.setLetter (graphics) 10 5 (object char "@" color Colors.Red))))
(.blitSurface (graphics) (assets.getSurface "laptop") 0 0)))

View File

@@ -12,7 +12,7 @@ class PlayState extends FlxState
override public function create()
{
super.create();
game = new Game("Beware Yon Death Trap", 40, 24, 8, 12, new DeathTrapLogic(),
game = new Game("Beware Yon Death Trap", 40, 24, 8, 12, new DeathTrapLogic(), new FlxAssetsBackend(),
new FlxGraphicsBackend(this, FlxGraphic.fromAssetKey("assets/images/size12.png"),
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.;:/?!@#$%^&*()-_=+[]{}~ÁÉÍÑÓÚÜáéíñóúü¡¿0123456789\"'<>|"));
}

View File

@@ -0,0 +1,4 @@
package asciilib;
@:build(kiss.Kiss.build())
class Assets {}

View 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.

View File

@@ -0,0 +1,5 @@
package asciilib;
interface AssetsBackend {
function loadText(filePath:String):String;
}

View File

@@ -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))))

View File

@@ -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;
}

View File

@@ -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)))))

View File

@@ -0,0 +1,7 @@
package asciilib.backends.flixel;
import asciilib.AssetsBackend;
import openfl.Assets;
@:build(kiss.Kiss.build())
class FlxAssetsBackend implements AssetsBackend {}

View File

@@ -0,0 +1,3 @@
(defmethod new [] 0)
(defmethod loadText [filePath] (Assets.getText filePath))