#include "AnimationRender.h"
#include "FrameCallback.h"

void RenderAnimation(u32 width, u32 height, u32 totalFrames, const FrameCallback** callbacks, usize sizeCallbacks) {
	RGBImage frameBuffer = RGBImage_new(width, height);

	struct FrameContext fc;
	fc.startFrame = 0;
	fc.endFrame = totalFrames;
	fc.frameBuffer = frameBuffer;

	for (u32 f = 0; f < totalFrames; ++f) {
		fc.startFrame = 0;
		fc.endFrame = totalFrames;
		fc.frame = f;

		for (usize i = 0; i < sizeCallbacks; ++i) {
			// Soft context reset
			if (callbacks[i] == NULL) {
				fc.startFrame = 0;
				fc.endFrame = totalFrames;
				fc.frame = f;
			}
			else if (fc.startFrame <= fc.frame && fc.frame <= fc.endFrame)
				callbacks[i]->callback(callbacks[i]->callback_self, &fc);
		}

		ppm6_write(stdout, frameBuffer);
	}

	RGBImage_delete(&frameBuffer);
}
