Skip to content

Commit 773fefc

Browse files
Matt JohnsonMatt Johnson
Matt Johnson
authored and
Matt Johnson
committed
Added sample code and experimental writeString function to TextBuffer
1 parent dfe1c61 commit 773fefc

File tree

9 files changed

+216
-3
lines changed

9 files changed

+216
-3
lines changed

luxe_ascii/REXLoader.hx

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class REXLoader {
88

99
var compressed_bytes = Luxe.resources.bytes(path).asset;
1010

11-
trace("compressed_bytes: " + compressed_bytes);
12-
trace("compressed_bytes.bytes: " + compressed_bytes.bytes); // Uint8Array
13-
trace(compressed_bytes.bytes.length); // 242
11+
// trace("compressed_bytes: " + compressed_bytes);
12+
// trace("compressed_bytes.bytes: " + compressed_bytes.bytes); // Uint8Array
13+
// trace(compressed_bytes.bytes.length); // 242
1414

1515
var b2 = haxe.io.Bytes.alloc(Std.int(compressed_bytes.bytes.length));
1616
for(i in 0 ... compressed_bytes.bytes.length) {

luxe_ascii/TextBuffer.hx

+27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import luxe.Color;
44
import snow.api.buffers.*;
55
import luxe.Rectangle;
66

7+
typedef BlitOptions = {
8+
fg:Int,
9+
bg:Int,
10+
char:Int
11+
};
12+
713
class TextBuffer {
814

915
public var width:Int;
@@ -141,4 +147,25 @@ class TextBuffer {
141147
d.bg[2] = Std.int(c.b * 255);
142148
}
143149
}
150+
151+
public function writeString(xPos:Int, yPos:Int, str:String, fg:Color, bg:Color, ?options:BlitOptions) {
152+
var x:Int;
153+
154+
for(i in 0 ... str.length) {
155+
x = xPos + i;
156+
157+
if(x >= width)
158+
return;
159+
160+
var d:CharAttr = data[yPos * width + x];
161+
162+
d.char = str.charCodeAt(i);
163+
164+
if(fg != null)
165+
d.fg = fg;
166+
167+
if(bg != null)
168+
d.set_bg_color(bg);
169+
}
170+
}
144171
}

sample/assets/.keep

Whitespace-only changes.
6.34 KB
Loading

sample/assets/xp/luxe_ascii_logo.xp

218 Bytes
Binary file not shown.

sample/project.flow

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
3+
luxe:{
4+
window: {
5+
width:640,
6+
height:350,
7+
title:'luxe_ascii demo',
8+
fullscreen:false,
9+
resizable:true,
10+
borderless:false
11+
}
12+
},
13+
14+
project : {
15+
name : 'luxe_ascii_demo',
16+
version : '1.0.0',
17+
author : 'mattj1',
18+
19+
app : {
20+
name : 'luxe_ascii_demo',
21+
package : 'com.luxe_ascii.demo'
22+
},
23+
24+
build : {
25+
dependencies : {
26+
luxe : '*',
27+
luxe_ascii : '*',
28+
}
29+
},
30+
31+
files : {
32+
assets : 'assets/'
33+
}
34+
35+
}
36+
37+
}

sample/src/Demo.hx

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import luxe.States;
2+
import luxe_ascii.*;
3+
4+
class Demo extends State {
5+
var consoleBuffer:ConsoleBuffer;
6+
7+
var textBuffer:TextBuffer;
8+
9+
var sprite_logo:TextBuffer;
10+
11+
var stars:Array<Star>;
12+
13+
public function new() {
14+
super({name:'demo'});
15+
16+
// The ConsoleBuffer manages the geometry that renders the ASCII console
17+
consoleBuffer = new ConsoleBuffer( {
18+
width: 80,
19+
height: 25,
20+
fontFile:'assets/charmaps/cp437_8x14_terminal.png',
21+
glyph_width: 8,
22+
glyph_height:14,
23+
glyph_file_columns:16
24+
});
25+
26+
//
27+
textBuffer = new TextBuffer(80, 25);
28+
29+
sprite_logo = REXLoader.load('assets/xp/luxe_ascii_logo.xp');
30+
31+
stars = new Array<Star>();
32+
for(i in 0 ... 40) {
33+
var s:Star = new Star();
34+
s.x = Std.random(80);
35+
stars.push(s);
36+
}
37+
}
38+
39+
override function update(dt:Float) {
40+
41+
// Update the starfield
42+
for(s in stars) {
43+
s.x += s.speed;
44+
if(s.x < 0) s.reset();
45+
}
46+
47+
// Clear the buffer
48+
textBuffer.clear(ANSIColors.color(0));
49+
50+
// Draw the starfield
51+
for(s in stars) {
52+
textBuffer.set_char(Std.int(s.x), Std.int(s.y), 7);
53+
textBuffer.set_fg_color(Std.int(s.x), Std.int(s.y), s.c);
54+
}
55+
56+
// Draw the title image
57+
textBuffer.blit(sprite_logo, Std.int(textBuffer.width / 2 - sprite_logo.width / 2), 5);
58+
59+
// Update the displayed geometry
60+
consoleBuffer.blit(textBuffer);
61+
}
62+
}

sample/src/Main.hx

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import luxe.Color;
2+
import luxe.Input;
3+
import luxe.Parcel;
4+
import luxe.ParcelProgress;
5+
import luxe.States;
6+
import snow.types.Types;
7+
8+
class Main extends luxe.Game {
9+
10+
override function config(config:luxe.AppConfig) {
11+
12+
return config;
13+
14+
} //config
15+
16+
override function ready() {
17+
var parcel = new Parcel({
18+
textures : [
19+
{ id : "assets/charmaps/cp437_8x14_terminal.png" }
20+
],
21+
bytes : [
22+
{ id : "assets/xp/luxe_ascii_logo.xp" }
23+
]
24+
});
25+
26+
new ParcelProgress({
27+
parcel: parcel,
28+
background: new Color(0.15, 0.15, 0.15, 1),
29+
oncomplete: assetsLoaded
30+
});
31+
32+
parcel.load();
33+
34+
} //ready
35+
36+
function assetsLoaded(_) {
37+
Luxe.update_rate = 1 / 30;
38+
39+
var state:States;
40+
state = new States({ name:'machine' });
41+
state.add( new Demo() );
42+
state.set('demo');
43+
}
44+
45+
override function onkeyup( e:KeyEvent ) {
46+
47+
if(e.keycode == Key.escape) {
48+
Luxe.shutdown();
49+
}
50+
51+
} //onkeyup
52+
53+
override function update(dt:Float) {
54+
55+
} //update
56+
} //Main

sample/src/Star.hx

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import luxe_ascii.*;
2+
3+
import luxe.Color;
4+
5+
class Star {
6+
public var x:Float;
7+
public var y:Float;
8+
public var c:Color;
9+
public var speed:Float;
10+
11+
public function new() {
12+
reset();
13+
}
14+
15+
public function reset() {
16+
x = 80 + Std.random(5);
17+
y = Std.random(25);
18+
19+
switch(Std.random(3)) {
20+
case 0:
21+
c = ANSIColors.color(15);
22+
speed = -2;
23+
case 1:
24+
c = ANSIColors.color(7);
25+
speed = -1;
26+
case 2:
27+
c = ANSIColors.color(1);
28+
speed = -.5;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)