Skip to content

Instantly share code, notes, and snippets.

@RafaelOliveira
Created January 9, 2017 15:17
Show Gist options
  • Save RafaelOliveira/16dfe61484c1c52d322fd51db0a9a998 to your computer and use it in GitHub Desktop.
Save RafaelOliveira/16dfe61484c1c52d322fd51db0a9a998 to your computer and use it in GitHub Desktop.
Backdrop
package;
import kha.Color;
import kha.Image;
import kha.System;
import kha.graphics2.Graphics;
class Backdrop
{
public var image:Image;
var width:Int;
var height:Int;
public var scrollX:Int;
public var scrollY:Int;
var posX:Int;
var posY:Int;
var offsetX:Int;
var offsetY:Int;
var originalWidth:Int;
var originalHeight:Int;
public function new(image:Image, scrollX:Int = 0, scrollY:Int = 0):Void
{
this.scrollX = scrollX;
this.scrollY = scrollY;
originalWidth = image.width;
originalHeight = image.height;
width = System.windowWidth();
height = System.windowHeight();
var composedImage = Image.createRenderTarget(
originalWidth + width,
originalHeight + height
);
this.image = composedImage;
posX = -originalWidth;
posY = -originalHeight;
var horizontal:Int = Std.int(width / originalWidth + 1);
var vertical:Int = Std.int(height / originalHeight + 1);
if (width % image.width > 0) {
horizontal += 1;
}
if (height % image.height > 0) {
vertical += 1;
}
composedImage.g2.begin();
var start:Int = 0;
for (x in start...horizontal) {
for (y in start...vertical) {
composedImage.g2.drawImage(
image,
x * originalWidth,
y * originalHeight
);
}
}
composedImage.g2.end();
}
public function update():Void
{
offsetX = (offsetX + scrollX) % originalWidth;
offsetY = (offsetY + scrollY) % originalHeight;
}
public function render(g:Graphics):Void
{
g.color = Color.White;
g.drawScaledSubImage(
image,
0,
0,
image.width,
image.height,
posX + offsetX,
posY + offsetY,
image.width,
image.height
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment