Created
August 1, 2018 03:10
-
-
Save rafaelvasco/ef848dce1d6a46498c37c17fa792255b to your computer and use it in GitHub Desktop.
Heaps Bitmap Painting
This file contains hidden or 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
// TODO: Handle Out of Bounds Painting | |
package; | |
import hxd.Math; | |
import sys.io.File; | |
import hxd.Key in K; | |
class Test extends hxd.App { | |
var tex: h3d.mat.Texture; | |
var bmp: h2d.Bitmap; | |
var pixels: hxd.Pixels; | |
var painting: Bool; | |
var curX: Int; | |
var curY: Int; | |
var lastX: Int; | |
var lastY: Int; | |
var pixelSize: Int = 8; | |
var imageSize: Int = 512; | |
override function init() { | |
tex = new h3d.mat.Texture(imageSize,imageSize); | |
pixels = hxd.Pixels.alloc(imageSize, imageSize, h3d.mat.Texture.nativeFormat); | |
for(x in 0...imageSize) { | |
for(y in 0...imageSize) { | |
pixels.setPixel(x,y, 0xFFEFEFEF); | |
} | |
} | |
tex.uploadPixels(pixels); | |
bmp = new h2d.Bitmap(h2d.Tile.fromTexture(tex), s2d); | |
bmp.x = s2d.width / 2 - imageSize / 2; | |
bmp.y = s2d.height / 2 - imageSize / 2; | |
var interactive = new h2d.Interactive(bmp.getSize().width, bmp.getSize().height, bmp); | |
interactive.cursor = Default; | |
interactive.onPush = function(e) { | |
var x: Int; | |
var y: Int; | |
if (pixelSize > 1) { | |
x = curX = snap(Std.int(e.relX), pixelSize); | |
y = curY = snap(Std.int(e.relY), pixelSize); | |
} | |
else { | |
x = curX = Std.int(e.relX); | |
y = curY = Std.int(e.relY); | |
} | |
for(px in x...x+pixelSize) { | |
for(py in y...y+pixelSize) { | |
pixels.setPixel(px,py, 0xFF1010FF); | |
} | |
} | |
tex.uploadPixels(pixels); | |
painting = true; | |
} | |
interactive.onRelease = function(e) { | |
painting = false; | |
} | |
interactive.onMove = function(e) { | |
if(!painting) { | |
return; | |
} | |
var x: Int; | |
var y: Int; | |
lastX = curX; | |
lastY = curY; | |
if(pixelSize > 1) { | |
x = curX = snap(Std.int(e.relX), pixelSize); | |
y = curY = snap(Std.int(e.relY), pixelSize); | |
} | |
else { | |
x = curX = Std.int(e.relX); | |
y = curY = Std.int(e.relY); | |
} | |
var dx = Std.int(Math.abs(x - lastX)); | |
var dy = Std.int(Math.abs(y - lastY)); | |
if((dx == pixelSize && dy == 0) || (dx == 0 && dy == pixelSize) || (dx == pixelSize && dy == pixelSize)) { | |
for(px in x...x+pixelSize) { | |
for(py in y...y+pixelSize) { | |
pixels.setPixel(px,py, 0xFF1010FF); | |
} | |
} | |
} | |
else if(dx >= pixelSize || dy >= pixelSize) { | |
blitLine(pixels, lastX, lastY, x, y, pixelSize, 0xFF1010FF); | |
} | |
tex.uploadPixels(pixels); | |
} | |
} | |
override function update(dt:Float) { | |
//sys.io.File.saveBytes(dstPath, pix.toPNG()); | |
if(K.isDown(K.CTRL) && K.isPressed(K.S)){ | |
File.saveBytes("result.png", pixels.toPNG()); | |
trace("Saved File"); | |
} | |
} | |
static function blitLine(pixels: hxd.Pixels, x0: Int, y0: Int, x1: Int, y1: Int, size: Int, col: Int) { | |
var lastX = x0; | |
var lastY = y0; | |
var dx = Std.int(Math.abs(x1-x0)); | |
var dy = -Std.int(Math.abs(y1-y0)); | |
var sx = if (x0 < x1) size else -size; | |
var sy = if (y0 < y1) size else -size; | |
var error = dx + dy; | |
var e2: Int; | |
while(true) { | |
for(y in y0 ... y0+size){ | |
for(x in x0 ... x0+size){ | |
pixels.setPixel(x, y, col); | |
} | |
} | |
lastX = x0; | |
lastY = y0; | |
if((x0 == x1) && (y0 == y1)) { | |
break; | |
} | |
e2 = error * 2; | |
if(e2 >= dy) { | |
error += dy; | |
x0 += sx; | |
} | |
if(e2 <= dx) { | |
error += dx; | |
y0 += sy; | |
} | |
} | |
} | |
static inline function fastAbs(v: Int): Int { | |
return (v ^ (v >> 31)) - (v >> 31); | |
} | |
static inline function fastFloor(v: Float): Int { | |
return Std.int(v); | |
} | |
static inline function snap(v: Int, cellSize: Int) { | |
return Std.int(v / cellSize) * cellSize; | |
} | |
static function main() { | |
new Test(); | |
} | |
} |
This file contains hidden or 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
-main Test | |
-lib heaps | |
-lib hlsdl | |
-dce full | |
-hl bin/Test.hl | |
-D windowSize=800x600 | |
-D windowTitle=Test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment