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

128 lines
3 KiB
Haxe
Raw Normal View History

2023-09-16 18:39:20 -04:00
package funkin.graphics.framebuffer;
2023-09-23 14:59:24 -04:00
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxSprite;
2023-09-17 07:45:10 -04:00
import flixel.util.FlxColor;
2023-09-16 18:39:20 -04:00
import openfl.display.BitmapData;
2023-09-17 08:11:11 -04:00
/**
* Manages frame buffers and gives access to each frame buffer.
*/
2023-09-16 18:39:20 -04:00
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-10-02 08:05:13 -04:00
public function copySpriteTo(name:String, sprite:FlxSprite, color:Null<FlxColor> = null):Void
2023-09-16 18:39:20 -04:00
{
2024-02-12 16:49:25 -05:00
if (!frameBufferMap.exists(name))
{
FlxG.log.warn('frame buffer "$name" does not exist');
return;
}
2023-09-16 18:39:20 -04:00
frameBufferMap[name].addSpriteCopy(new SpriteCopy(sprite, color));
}
2023-10-02 08:05:13 -04:00
/**
* Adds the sprite to the frame buffer. The sprite will only be seen from the frame buffer.
* @param name the name of the frame buffer
* @param sprite the sprite
*/
public function moveSpriteTo(name:String, sprite:FlxSprite):Void
{
2024-02-12 16:49:25 -05:00
if (!frameBufferMap.exists(name))
{
FlxG.log.warn('frame buffer "$name" does not exist');
return;
}
2023-10-02 08:05:13 -04:00
frameBufferMap[name].moveSprite(sprite);
}
2023-09-16 18:39:20 -04:00
/**
2023-09-23 14:59:24 -04:00
* Call this before drawing anything.
2023-09-16 18:39:20 -04:00
*/
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();
}
}