Skip to content

Instantly share code, notes, and snippets.

@larsiusprime
Created July 22, 2015 16:47
Show Gist options
  • Save larsiusprime/913f4851c4bd720c28f9 to your computer and use it in GitHub Desktop.
Save larsiusprime/913f4851c4bd720c28f9 to your computer and use it in GitHub Desktop.
package com.leveluplabs.tdrpg;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
import flixel.FlxG;
import flixel.graphics.FlxGraphic;
import openfl.Lib;
/**
* ...
* @author Ohmnivore
*/
class TestFeatures
{
static public function artefactFix(orig:BitmapData, FrameWidth:Int, FrameHeight:Int, key:String, spacingX:Int=2, spacingY:Int=2):String
{
if (FlxG.bitmap.checkCache(key)) {
return key;
}
var cont:BitmapData = TestFeatures.returnContainer(orig.width, orig.height, FrameWidth, FrameHeight, spacingX, spacingY);
TestFeatures.placeTiles(orig, cont, FrameWidth, FrameHeight, spacingX, spacingY);
TestFeatures.placeSafePixelColumn(orig, cont, FrameWidth, FrameHeight, spacingX, spacingY);
var newcached:FlxGraphic = FlxG.bitmap.add(cont, false, key);
//top left corner of the first tile
var startX:Int = 1;
var startY:Int = 1;
//tile size
var tileWidth:Int = FrameWidth;
var tileHeight:Int = FrameHeight;
//end of tiles
var width:Int = Std.int(cont.width - startX);
var height:Int = Std.int(cont.height - startY);
return key;
}
static public function placeSafePixelColumn(Source:BitmapData, Dest:BitmapData, TileWidth:Int, TileHeight:Int, spacingX:Int, spacingY:Int):Void
{
var borderX:Int = Std.int(spacingX / 2);
var borderY:Int = Std.int(spacingY / 2);
var x_tot:Int = Std.int(Source.width / TileWidth);
var y_tot:Int = Std.int(Source.height / TileHeight);
var x_count:Int = 0;
var y_count:Int = 0;
var rect:Rectangle = new Rectangle();
var point:Point = new Point();
while (x_count <= x_tot)
{
while (y_count <= y_tot)
{
if(x_count > 0){
//To the left
rect.setTo(x_count * TileWidth - (TileWidth-borderX), y_count * TileHeight,
1, TileHeight);
point.setTo(borderX + x_count * TileWidth + x_count * spacingX - (TileWidth+spacingX) - 1,
borderY + y_count * TileHeight + y_count * spacingY);
for(i in 0...borderX){
Dest.copyPixels(Source, rect, point, true);
point.x--;
}
//To the right
rect.setTo(x_count * TileWidth - (borderX), y_count * TileHeight,
1, TileHeight);
point.setTo(borderX + x_count * TileWidth + x_count * spacingX - spacingX,
borderY + y_count * TileHeight + y_count * spacingY);
for(i in 0...borderX){
Dest.copyPixels(Source, rect, point, true);
point.x++;
}
}
if(y_count > 0){
//To the top
rect.setTo(x_count * TileWidth, y_count * TileHeight - (TileHeight-borderY),
TileWidth, 1);
point.setTo(borderX + x_count * TileWidth + x_count * spacingX,
borderY + y_count * TileHeight + y_count * spacingY - (TileHeight+spacingY) - 1);
for(i in 0...borderY){
Dest.copyPixels(Source, rect, point, true);
point.y--;
}
//To the bottom
rect.setTo(x_count * TileWidth, y_count * TileHeight - (borderY),
TileWidth, 1);
point.setTo(borderX + x_count * TileWidth + x_count * spacingX,
borderY + y_count * TileHeight + y_count * spacingY - spacingY);
for(i in 0...borderY){
Dest.copyPixels(Source, rect, point, true);
point.y++;
}
}
y_count++;
}
y_count = 0;
x_count++;
}
}
static public function placeTiles(Source:BitmapData, Dest:BitmapData, TileWidth:Int, TileHeight:Int, spacingX:Int, spacingY:Int):Void
{
var borderX:Int = Std.int(spacingX/2);
var borderY:Int = Std.int(spacingY/2);
var x_tot:Int = Std.int(Source.width / TileWidth);
var y_tot:Int = Std.int(Source.height / TileHeight);
var x_count:Int = 0;
var y_count:Int = 0;
while (x_count < x_tot)
{
while (y_count < y_tot)
{
var rect:Rectangle = new Rectangle(x_count * TileWidth, y_count * TileWidth,
TileWidth, TileHeight);
var point:Point = new Point(borderX + x_count * TileWidth + x_count * spacingX,
borderY + y_count * TileHeight + y_count * spacingY);
Dest.copyPixels(Source, rect, point, true);
y_count++;
}
y_count = 0;
x_count++;
}
}
static public function returnContainer(Width:Int, Height:Int, TileWidth:Int, TileHeight:Int, spacingX:Int, spacingY:Int):BitmapData
{
var width:Int = Width + (Std.int(Width / TileWidth) - 1) * spacingX + spacingX;
var height:Int = Height + (Std.int(Height / TileHeight) - 1) * spacingY + spacingY;
return new BitmapData(width, height, true, 0x00ffffff);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment