1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-12-14 17:35:13 +00:00
Funkin/source/funkin/graphics/framebuffer/FrameBufferManager.hx

128 lines
3 KiB
Haxe
Raw Normal View History

2023-09-16 22:39:20 +00:00
package funkin.graphics.framebuffer;
2023-09-23 18:59:24 +00:00
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxSprite;
2023-09-17 11:45:10 +00:00
import flixel.util.FlxColor;
2023-09-16 22:39:20 +00:00
import openfl.display.BitmapData;
2023-09-17 12:11:11 +00:00
/**
* Manages frame buffers and gives access to each frame buffer.
*/
2023-09-16 22:39:20 +00: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 11:45:10 +00: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 22:39:20 +00:00
*/
2023-09-17 11:45:10 +00:00
public function createFrameBuffer(name:String, bgColor:FlxColor):BitmapData
2023-09-16 22:39:20 +00:00
{
if (frameBufferMap.exists(name))
{
FlxG.log.warn('frame buffer "$name" already exists');
2023-09-17 11:45:10 +00:00
frameBufferMap[name].dispose();
frameBufferMap.remove(name);
2023-09-16 22:39:20 +00:00
}
2023-09-17 11:45:10 +00:00
final fb = new FrameBuffer();
fb.create(camera.width, camera.height, bgColor);
frameBufferMap[name] = fb;
return fb.bitmap;
2023-09-16 22:39:20 +00: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 11:45:10 +00: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 22:39:20 +00:00
*/
2023-10-02 12:05:13 +00:00
public function copySpriteTo(name:String, sprite:FlxSprite, color:Null<FlxColor> = null):Void
2023-09-16 22:39:20 +00:00
{
2024-02-12 21:49:25 +00:00
if (!frameBufferMap.exists(name))
{
FlxG.log.warn('frame buffer "$name" does not exist');
return;
}
2023-09-16 22:39:20 +00:00
frameBufferMap[name].addSpriteCopy(new SpriteCopy(sprite, color));
}
2023-10-02 12:05:13 +00: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 21:49:25 +00:00
if (!frameBufferMap.exists(name))
{
FlxG.log.warn('frame buffer "$name" does not exist');
return;
}
2023-10-02 12:05:13 +00:00
frameBufferMap[name].moveSprite(sprite);
}
2023-09-16 22:39:20 +00:00
/**
2023-09-23 18:59:24 +00:00
* Call this before drawing anything.
2023-09-16 22:39:20 +00:00
*/
public function lock():Void
{
for (_ => fb in frameBufferMap)
{
2023-09-17 11:45:10 +00:00
fb.follow(camera);
2023-09-16 22:39:20 +00:00
fb.lock();
}
}
/**
2023-09-17 11:45:10 +00:00
* Unlocks the frame buffers. This updates the bitmap data of each frame buffer.
2023-09-16 22:39:20 +00:00
*/
2023-09-17 11:45:10 +00:00
public function unlock():Void
2023-09-16 22:39:20 +00: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 11:45:10 +00:00
* @return the bitmap data
2023-09-16 22:39:20 +00:00
*/
public function getFrameBuffer(name:String):BitmapData
{
return frameBufferMap[name].bitmap;
}
/**
2023-09-17 11:45:10 +00:00
* Disposes all frame buffers. The instance can be reused.
2023-09-16 22:39:20 +00:00
*/
public function dispose():Void
{
for (_ => fb in frameBufferMap)
{
fb.dispose();
}
frameBufferMap.clear();
}
}