// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//  
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#ifndef WIDGETFBO_H
#define WIDGETFBO_H

#include "widgetsingleparent.h"

/// WidgetFBO is a WidgetSingleParent that will render itself and child
/// hierarchy to a frame buffer object, then render that as one quad.
/// This is useful for alpha blending an entire hierarchy of Widgets,
/// but not applying more than one alpha blend to any resulting pixel,
/// so the result is consistent regardless of how Widgets are stacked
/// on top of each other.
LUA_WRAP class WidgetFBO : public WidgetSingleParent {
	
	public:
	
		// Unique stuff
		
		/// Set the alpha value.
		LUA_WRAP void setAlpha(float alpha);
		
		/// Get the alpha value.
		LUA_WRAP float getAlpha(void);
		
		// Inherited stuff
		void draw(void);
		bool handleEvent(const WidgetEvent *event);
		void markSizeDirty(void);
		float getMinWidth(void);
		float getMinHeight(void);
		void setWidth(float newWidth);
		void setHeight(float newHeight);
	
		// Constructor/destructor
		WidgetFBO(void);
		virtual ~WidgetFBO(void);
	
	private:
	
		// FIXME: The only place where alpha really makes sense is for
		//   the hierarchy is on one of these Widgets, but perhaps it
		//   could be moved back to the main Widget class so we don't
		//   need one of these when a single Widget uses alpha on its
		//   own?
		
		// Alpha blending value.
		float alpha;
	
		// True if we need to build a new FBO because the size changed
		// or something.
		bool needNewFBO;
		
		// GL FBO
		unsigned int fbo;
		
		// FBO texture.
		unsigned int image;
		
		// FBO depth/stencil buffer.
		unsigned int depthBuffer;
		
		// FBO actual width.
		int pow2FBOWidth;
		
		// FBO actual height.
		int pow2FBOHeight;
		
		// Get rid of the existing FBO either to create a new one or
		// just cleaning up.
		void destroyFBO(void);
		
		// Make a new FBO based on this widget's size.
		void createFBO(void);
		
};


#endif
