/*

    Castle Draco

    Copyright Søren Schimkat <www.castle.schimkat.dk>

    This program and it´s source code is released with the following licence:
    
    Permission is granted to anyone to use the program and recompile the 
    sourcecode on any architecture. Noone may redistribute the binarys or
    the sourcecode. For personal interests, you may alter the sourcecode
    if you like - but the altered sourcecode may not be distributed. The 
    sourcecode, parts of the sourcecode or the altered sourcecode may not
    appear in any other programs.
    
*/


#include <irrlicht.h>
#include <stdio.h>


using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


#pragma comment(lib, "Irrlicht.lib")


IrrlichtDevice *device = 0;
bool StartGame   = false;


class MenuEventReceiver : public IEventReceiver {
   public: virtual bool OnEvent(SEvent event) {
      if (event.EventType == EET_KEY_INPUT_EVENT) {
         if (event.KeyInput.Key == KEY_ESCAPE) { 
            device->closeDevice();
            StartGame = false;
            return true;
         }
      }
      else if (event.EventType == EET_GUI_EVENT) {
         s32 id = event.GUIEvent.Caller->getID();
         IGUIEnvironment* env = device->getGUIEnvironment();
         switch(event.GUIEvent.EventType) {
            case EGET_BUTTON_CLICKED:
			   if (id == 1) {
		          device->closeDevice();
                  StartGame = true;
			      return true;
               }
			   else if (id == 2) {
                  device->closeDevice();
                  StartGame = false;
                  return true;
               }
               break;
			}
		}
		return false;
	}
};


int menu(int& Renderer, int& Resolution, int& Depth) {

   MenuEventReceiver receiver;
   device = createDevice(video::EDT_SOFTWARE, core::dimension2d<s32>(370, 190), 32, false, false, false, &receiver);
   device->setWindowCaption(L"Castle loader");

   video::IVideoDriver* driver = device->getVideoDriver();
   IGUIEnvironment* env = device->getGUIEnvironment();


   /* Mounting datafile */

   device->getFileSystem()->addZipFileArchive("Menu.dat", true, true); 

	
   /* Setting colors and font */
		
   env->getSkin()->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(0,255,255,255));
   IGUISkin* skin = env->getSkin();
   skin->setFont(env->getFont("font.bmp"));
   

   /* Placing background image, texts, listboxes and buttons */

   IGUIImage* img = env->addImage(rect<int>(0,0,370,190));
   img->setImage(driver->getTexture("castle.jpg"));
   env->addStaticText(L"Castle Draco 0.0.5", rect<int>(1, 1, 100, 30), false);
   env->addStaticText(L"Copyright Soren Schimkat", rect<int>(240, 1, 369, 30), false);


   env->addStaticText(L"Select renderer", rect<int>(2, 130, 92, 144), false);
   IGUIListBox* RendererBox = env->addListBox(rect<s32>(2, 145, 92, 188));
   RendererBox->addItem(L"OpenGL");
   // RendererBox->addItem(L"DirectX");
   // RendererBox->addItem(L"Software");
   RendererBox->setSelected(Renderer - 1);

	
   env->addStaticText(L"Select resolution", rect<int>(94, 130, 184, 144), false);
   IGUIListBox* ResolutionBox = env->addListBox(rect<s32>(94, 145, 184, 188));
   ResolutionBox->addItem(L"640 x 480");
   ResolutionBox->addItem(L"800 x 600");
   ResolutionBox->addItem(L"1024 x 768");
   ResolutionBox->addItem(L"1280 x 1024");
   ResolutionBox->addItem(L"1600 x 1200");
   ResolutionBox->setSelected(Resolution - 1);


   env->addStaticText(L"Select depth", rect<int>(186, 130, 276, 144), false);
   IGUIListBox* DepthBox = env->addListBox(rect<s32>(186, 145, 276, 188));
   DepthBox->addItem(L"8 bpp");
   DepthBox->addItem(L"16 bpp");
   DepthBox->addItem(L"32 bpp");
   DepthBox->setSelected(Depth - 1);
   
	
   env->addButton(rect<s32>(278,145,369,166), 0, 1, L"Start Castle Draco");
   env->addButton(rect<s32>(278,167,369,189), 0, 2, L"Exit");


   for (s32 i=0; i < EGDC_COUNT ; ++i) {
      SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
      col.setAlpha(100);
      env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
   }
	

   /* Entering menu loop */

	while(device->run() && driver)
	if (device->isWindowActive()) {
		driver->beginScene(true, true, video::SColor(0,192,192,192));
		env->drawAll();
		driver->endScene();
	}
	
	Renderer = (RendererBox->getSelected()) + 1;
	Resolution = (ResolutionBox->getSelected()) + 1;
	Depth = (DepthBox->getSelected()) + 1;
	
  	device->drop();
	return (StartGame);
	
}
