Funkin/source/funkin/graphics/framebuffer/FrameBufferManager.hx

104 lines
2.4 KiB
Haxe
Raw Normal View History

2023-09-16 18:39:20 -04:00
package funkin.graphics.framebuffer;
2023-09-17 07:45:10 -04:00
import flixel.util.FlxColor;
2023-09-16 18:39:20 -04:00
import openfl.display.BitmapData;
import flixel.FlxSprite;
import flixel.FlxCamera;
class FrameBufferManager
{
final camera:FlxCamera;
final frameBufferMap:Map<String, FrameBuffer> = [];
/**
* Creates a frame buffer manager that targets `camera`.
* @param camera the target camera.
*/
public function new(camera:FlxCamera)
{
this.camera = camera;
}
/**
* Creates a new frame buffer with a name.
* @param name the name
2023-09-17 07:45:10 -04:00
* @param bgColor the background color
* @return the bitmap data of the frame buffer. the bitmap data instance
* will not be changed through frame buffer updates.
2023-09-16 18:39:20 -04:00
*/
2023-09-17 07:45:10 -04:00
public function createFrameBuffer(name:String, bgColor:FlxColor):BitmapData
2023-09-16 18:39:20 -04:00
{
if (frameBufferMap.exists(name))
{
FlxG.log.warn('frame buffer "$name" already exists');
2023-09-17 07:45:10 -04:00
frameBufferMap[name].dispose();
frameBufferMap.remove(name);
2023-09-16 18:39:20 -04:00
}
2023-09-17 07:45:10 -04:00
final fb = new FrameBuffer();
fb.create(camera.width, camera.height, bgColor);
frameBufferMap[name] = fb;
return fb.bitmap;
2023-09-16 18:39:20 -04:00
}
/**
* Adds a copy of the sprite to the frame buffer.
* @param name the name of the frame buffer
* @param sprite the sprite
2023-09-17 07:45:10 -04:00
* @param color if this is not `null`, the sprite will be filled with the color.
* if this is `null`, the sprite will keep its original color.
2023-09-16 18:39:20 -04:00
*/
2023-09-17 07:45:10 -04:00
public function addSpriteCopyTo(name:String, sprite:FlxSprite, color:Null<FlxColor> = null):Void
2023-09-16 18:39:20 -04:00
{
frameBufferMap[name].addSpriteCopy(new SpriteCopy(sprite, color));
}
/**
* Call this before everything is drawn.
*/
public function lock():Void
{
for (_ => fb in frameBufferMap)
{
2023-09-17 07:45:10 -04:00
fb.follow(camera);
2023-09-16 18:39:20 -04:00
fb.lock();
}
}
/**
2023-09-17 07:45:10 -04:00
* Unlocks the frame buffers. This updates the bitmap data of each frame buffer.
2023-09-16 18:39:20 -04:00
*/
2023-09-17 07:45:10 -04:00
public function unlock():Void
2023-09-16 18:39:20 -04:00
{
for (_ => fb in frameBufferMap)
{
fb.render();
}
for (_ => fb in frameBufferMap)
{
fb.unlock();
}
}
/**
* Returns the bitmap data of the frame buffer
* @param name the name of the frame buffer
2023-09-17 07:45:10 -04:00
* @return the bitmap data
2023-09-16 18:39:20 -04:00
*/
public function getFrameBuffer(name:String):BitmapData
{
return frameBufferMap[name].bitmap;
}
/**
2023-09-17 07:45:10 -04:00
* Disposes all frame buffers. The instance can be reused.
2023-09-16 18:39:20 -04:00
*/
public function dispose():Void
{
for (_ => fb in frameBufferMap)
{
fb.dispose();
}
frameBufferMap.clear();
}
}