#include "stdafx.h"
#include "Painter.h"
#include "Surface.h"
#include "Exception.h"

namespace gui {

	Painter3D::Painter3D() {}

	Painter3D::~Painter3D() {
		detach();
	}

	Bool Painter3D::render(Size size, Graphics3D *graphics) {
		return false;
	}

	void Painter3D::render(Surface *surface, Bool &more, Bool &ok) {
		graphics->beforeRender();
		more = render(surface->size / surface->scale, graphics);
		ok = graphics->afterRender();
	}

	void Painter3D::onAttach(Surface *surface) {
		if (Surface3D *s = as<Surface3D>(surface)) {
			graphics = s->createGraphics(engine());
		} else {
			throw new (this) InternalError(S("We expect a 3D surface for 3D painters!"));
		}
	}

	void Painter3D::onDestroy() {
		if (graphics) {
			graphics->destroy();
		}
		graphics = null;
	}

	void Painter3D::onResize() {
		if (graphics) {
			// todo
		}
	}

	Bool Painter3D::prepare(Resource3D *resource) {
		if (!graphics)
			return false;

		resource->forGraphics(graphics);
		return true;
	}

}
