////////////////////////////////////////////////////////////////////////////////
// GLWindow.cpp
// Copyright Bjoern Ganster
// Developed since 2005
////////////////////////////////////////////////////////////////////////////////

#include "GLWindow.h"

#define FILE_NUMBER FILE_NO_GLWINDOW_CPP

////////////////////////////////////////////////////////////////////////////////

SDL_Surface * s_screen;

void storeScreenSettings (SDL_Surface * screen)
{ 
   s_screen = screen;
}

////////////////////////////////////////////////////////////////////////////////
// Destructor

GLWindow::~GLWindow()
{ 
   if (parent != NULL)
      parent->remove (this); 
}

////////////////////////////////////////////////////////////////////////////////

GLWindow* GLWindow::getMainWindow()
{
   GLWindow* curr = this;
   while (curr->getParent() != NULL)
      curr = curr->getParent ();
   return curr;
}

////////////////////////////////////////////////////////////////////////////////

void GLWindow::setViewport (int x, int y, int w, int h)
{
	SET_SCOPE;
	GLWindow* mainWin = getMainWindow();
	if (mainWin == nullptr) {
		RETURN;
	}

	int rx = x+left;
	int totalHeight = mainWin->getHeight ();
	int ry = totalHeight-y-top-height;
	glViewport (rx, ry, width, height);

	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
	RETURN;
}

////////////////////////////////////////////////////////////////////////////////

void GLWindow::prepareToRender (int x, int y, int w, int h)
{
	SET_SCOPE;

	// Get main window
	GLWindow* mainWin = getMainWindow();
	if (mainWin == nullptr) {
		RETURN;
	}

	// Render frame
	int rx = x+left;
	int totalHeight = mainWin->getHeight ();
	int ry = totalHeight-y-top-height;
	glViewport (rx-1, ry-1, width+1, height+1);

	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();

	glTranslated (-1.0, 1.0, 0.0);
	glScaled (2/((double) getWidth()), -2/((double) getHeight()), 1.0);

	glDisable (GL_TEXTURE_2D);
	glDisable (GL_DEPTH_TEST);
	glDepthMask (0); // Prevent writes to depth buffer
	//glClearColor(1.0,1.0,1.0,0.0); // Defines color for blending
	//glBlendColor4f (1, 1, 1, 1);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	renderShadedRectangle (1, 0, width-1, height-1);

	glBegin (GL_POLYGON);
	backgroundColor.glColor();
		glVertex2i (2, 1);
		glVertex2i (width-2, 1);
		glVertex2i (width-2, height-2);
		glVertex2i (2, height-2);
	glEnd();

	glViewport (x+left, ry, width, height);
	glDepthMask (~0); // Allow writes to depth buffer
	glLoadIdentity ();
	glDepthMask (~0);
	glDisable(GL_BLEND);
	RETURN;
}

////////////////////////////////////////////////////////////////////////////////
// Remove all child windows

void GLWindow::removeAll()
{
}

////////////////////////////////////////////////////////////////////////////////
// Render a shaded rectangle

void renderShadedRectangle (double x1, double y1, double x2, double y2)
{
   glBegin (GL_LINES);
      glColor3d (0.8, 0.8, 0.8);
      glVertex2d (x1, y1);
      glVertex2d (x1, y2);
      glVertex2d (x1, y1);
      glVertex2d (x2, y1);

      glColor3d (0.6, 0.6, 0.6);
      glVertex2d (x1, y2);
      glVertex2d (x2, y2);
      glVertex2d (x2, y1);
      glVertex2d (x2, y2);
   glEnd ();
}

////////////////////////////////////////////////////////////////////////////////
// Fill rectangle in current color

void fillRect(double x1, double y1, double x2, double y2)
{
   glBegin (GL_POLYGON);
      glVertex2d (x1, y1);
      glVertex2d (x2, y1);
      glVertex2d (x2, y2);
      glVertex2d (x1, y2);
   glEnd ();
}
