This commit is contained in:
LordMZTE 2021-07-13 00:54:30 +02:00
commit b9ee309449
7 changed files with 145 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin

9
build.hxml Normal file
View File

@ -0,0 +1,9 @@
-cp src
-main Main
-lib heaps
-lib hlsdl
-lib tink_color
-dce full

7
build_native.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
clang -O3 -o bin/rainbowant -I bin bin/rainbowant.c -lhl -std=c11 -lm \
-l:sdl.hdll \
-l:openal.hdll \
-l:fmt.hdll \
-l:ui.hdll \
-l:libOpenGL.so

2
hl.hxml Normal file
View File

@ -0,0 +1,2 @@
build.hxml
-hl bin/rainbowant.hl

3
hl_native.hxml Normal file
View File

@ -0,0 +1,3 @@
build.hxml
-hl bin/rainbowant.c
-cmd ./build_native.sh

64
src/Ant.hx Normal file
View File

@ -0,0 +1,64 @@
class Ant {
public var x:Int;
public var y:Int;
public var mapHeight:Int;
public var mapWidth:Int;
public var rotation:Rotation;
public function new(mapWidth:Int, mapHeight:Int) {
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
x = mapWidth >> 1;
y = mapHeight >> 1;
rotation = Up;
}
public function rotRight() {
rotation = switch (rotation) {
case Up: Right;
case Right: Down;
case Down: Left;
case Left: Up;
}
}
public function rotLeft() {
rotation = switch (rotation) {
case Up: Left;
case Left: Down;
case Down: Right;
case Right: Up;
}
}
public function move() {
switch (rotation) {
case Up:
y++;
case Right:
x++;
case Down:
y--;
case Left:
x--;
}
if (y > mapHeight - 1)
y = 0;
if (y < 0)
y = mapHeight - 1;
if (x > mapWidth - 1)
x = 0;
if (x < 0)
x = mapWidth - 1;
}
}
enum Rotation {
Up;
Right;
Down;
Left;
}

59
src/Main.hx Normal file
View File

@ -0,0 +1,59 @@
import tink.color.Color;
import h2d.Tile;
import h2d.Bitmap;
import hxd.BitmapData;
using StringTools;
class Main extends hxd.App {
var map:BitmapData;
var ant:Ant;
var tile:Tile;
var bitmap:Bitmap;
var hue:Float;
override function init() {
map = new BitmapData(s2d.width, s2d.height);
ant = new Ant(s2d.width, s2d.height);
bitmap = new Bitmap(tile = Tile.fromBitmap(map), s2d);
hue = 0;
}
override function onResize() {
map.dispose();
bitmap.remove();
ant = new Ant(s2d.width, s2d.height);
map = new BitmapData(s2d.width, s2d.height);
map.fill(0, 0, s2d.width, s2d.height, 0xFF000000);
bitmap = new Bitmap(tile = Tile.fromBitmap(map), s2d);
hue = 0;
}
override function update(dt:Float) {
var color = Color.hsv(hue, 1, 1);
map.lock();
for (_ in 0...10000) {
if (map.getPixel(ant.x, ant.y) == 0) {
map.setPixel(ant.x, ant.y, cast(color, Int));
ant.rotRight();
ant.move();
} else {
map.setPixel(ant.x, ant.y, 0);
ant.rotLeft();
ant.move();
}
hue = (hue + 0.0001) % 360;
}
map.unlock();
tile.getTexture().uploadPixels(map.getPixels());
}
static function main() {
new Main();
}
}