www.pudn.com > ICE_1.4.zip > gs_configure.cpp


// Class automatically generated by Dev-C++ New Class wizard 
 
#include "gs_configure.h" // class's header file 
 
    #define BTN_ROW_SPACING  40 
    #define BTN_W            90 
    #define BTN_H            30 
 
// class constructor 
// 1) initializes GUI  
// 2) reads config options from file 
gs_Configure::gs_Configure(game_StateHandler*handler) : game_State(handler) 
{ 
    using namespace irr; 
    newDeviceSettings = false; 
 
    //1) initialize GUI  
    //load logo 
    m_driver->setTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_QUALITY, true);  
    m_pIceLogo = m_driver->getTexture("data/ice.bmp"); 
    if(m_pIceLogo) m_driver->makeColorKeyTexture(m_pIceLogo, irr::core::position2d< irr::s32 >(0,0) ); 
         
    //TODO: get resolution from device, and set pos according to that 
    int btn_x = 100 ; 
    int btn_y = 60 ; 
 
    //set title text 
    m_txt_title = m_env->addStaticText ( L"ICE v2.0 Device Configuration", true,  
            core::rect(btn_x, btn_y, btn_x + BTN_W*2, btn_y + BTN_H)  ) ; 
             
    //create device type list box 
        btn_y += BTN_ROW_SPACING; // move down 
        m_lst_dev = m_env->addListBox(core::rect(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H*2), 
                0, GSCNF_GUI_LST_DEV); 
        btn_y += BTN_ROW_SPACING; //allow for extra height of list box 
     
    //create color depths list box 
        btn_y += BTN_ROW_SPACING; // move down 
        m_lst_color = m_env->addListBox(core::rect(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H), 
                0, GSCNF_GUI_LST_COLOR); 
        //btn_y += BTN_ROW_SPACING; //allow for extra height of list box         
 
    //create resolutions list box 
        btn_y += BTN_ROW_SPACING; // move down 
        m_lst_res = m_env->addListBox(core::rect(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H*2), 
                0, GSCNF_GUI_LST_RES); 
        btn_y += BTN_ROW_SPACING; //allow for extra height of list box 
      
     //create fullscreen check box 
     btn_y += BTN_ROW_SPACING; // move down 
     m_chk_fullscreen = m_env->addCheckBox( false, //(ICE_Options::getConfig("fullscreen") == "T"),  
          core::rect(btn_x, btn_y, btn_x + BTN_W*2, btn_y + BTN_H),  0,  GSCNF_GUI_CHK_FULLSCRN,   
           L"Fullscreen" ); 
      
    //set OK button 
    btn_y += BTN_ROW_SPACING; // move down 
    m_btn_ok = m_env->addButton(core::rect(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H), 0,  
            GSCNF_GUI_BTN_OK, L"OK");     
         
 
 
	//2)read config options from file 
 
	//new tinyXML way 
 
	TiXmlDocument doc; 
	doc.LoadFile(GS_TINY_CONFIG_FILE); 
	TiXmlNode* node = NULL; 
	TiXmlElement *elem = NULL; 
	TiXmlElement *option = NULL; 
	TiXmlAttribute *attr = NULL; 
	wchar_t _buffer[1024];  
	irr::s32 idx; 
 
	node = doc.FirstChild( "configOptions" ); 
	if(!node) { 
		printf("gs_Configure: ERROR! malformed config file, could not find configOptions node"); 
		return; 
	} 
 
	elem = node->FirstChild("Resolutions")->ToElement(); 
	if(!elem) { 
		printf("gs_Configure: WARNING! malformed config file, could not find Resolutions node"); 
		return; 
	} 
	option = elem->FirstChildElement(); 
	while( option != NULL ) { 
		attr = option->FirstAttribute(); 
		 
		mbstowcs(_buffer, attr->Value(), 1024 ); 
		idx = m_lst_res->addItem( _buffer ); 
		//if this is the current resolution choice, select it by default 
		if(std::string(attr->Value()) == ICE_Options::getConfig("resolution") ) { 
   				m_lst_res->setSelected(idx); 
		} 
		option = option->NextSiblingElement(); 
	} 
 
	elem = node->FirstChild("ColorDepths")->ToElement(); 
	if(!elem) { 
		printf("gs_Configure: WARNING! malformed config file, could not find ColorDepths node"); 
		return; 
	} 
	option = elem->FirstChildElement(); 
	while( option != NULL ) { 
		attr = option->FirstAttribute(); 
		 
		mbstowcs(_buffer, attr->Value(), 1024 ); 
		idx = m_lst_color->addItem( _buffer ); 
		//if this is the current resolution choice, select it by default 
		if(std::string(attr->Value()) == ICE_Options::getConfig("colordepth") ) { 
   				m_lst_color->setSelected(idx); 
		} 
		option = option->NextSiblingElement(); 
	} 
 
 
	elem = node->FirstChild("Devices")->ToElement(); 
	if(!elem) { 
		printf("gs_Configure: WARNING! malformed config file, could not find Devices node"); 
		return; 
	} 
	option = elem->FirstChildElement(); 
	while( option != NULL ) { 
		attr = option->FirstAttribute(); 
       mbstowcs(_buffer, attr->Value(), 1024 ); 
	   idx = m_lst_dev->addItem( _buffer ); 
	   //if this is the current color choice, select it by default 
	   if(std::string(attr->Value()) == ICE_Options::getConfig("device") ) { 
   		     m_lst_dev->setSelected(idx); 
       } 
		option = option->NextSiblingElement(); 
	} 
} 
 
// class destructor 
// 1) delete GUI objects 
// 2) if settings change, reinit Irrlicht to let changes take place 
gs_Configure::~gs_Configure() 
{ 
    // 1) delete GUI objects 
   m_btn_ok->remove(); 
   m_txt_title->remove(); 
   m_lst_res->remove(); 
   m_lst_color->remove(); 
   m_lst_dev->remove(); 
   m_chk_fullscreen->remove(); 
    
   // 2) if settings change, reinit Irrlicht 
   //implement driver change here 
   if(newDeviceSettings) {  
      GS_DEBUG( printf("changing device settings\n"); ); 
      m_device->closeDevice(); 
      m_device->drop(); 
      GS_DEBUG( printf("old device closed\n"); ); 
    
      irr::video::EDriverType device_type; 
      int res_x, res_y;  
      bool fullscreen = ICE_Options::getConfig("fullscreen") == "T" ? true : false; 
      int colordepth = ICE_Options::getConfig("colordepth") == "16" ? 16 : 32 ; 
      std::string res, device; 
       
      	res = ICE_Options::getConfig("resolution"); 
      	GS_DEBUG( printf("using res: %s  \n", device.c_str()); ); 
    	if(res == "640x480") { 
    		res_x = 640; res_y = 480; 
    	}else if( res == "800x600" ) { 
    	    res_x = 800; res_y = 600; 
        }else if( res == "1024x768" ) { 
            res_x = 1024; res_y = 768; 
        } 
        device = ICE_Options::getConfig("device"); 
        GS_DEBUG( printf("using device: %s  \n", device.c_str()); ); 
        if(device == "OpenGL") { 
            device_type = irr::video::EDT_OPENGL; 
        }else if(device == "DirectX8") { 
            device_type = irr::video::EDT_DIRECTX8; 
        }else { 
            device_type = irr::video::EDT_SOFTWARE; 
        } 
        GS_DEBUG( printf("starting up new device...\n"); ); 
         
 
      //reinitialize device 
      if(  
         ((game_Engine*)m_handler)->Init(device_type,res_x,res_y,fullscreen,colordepth) 
        ){ 
          GS_DEBUG( printf("new device SUCCESSFUL!\n"); ); 
                  //TODO: uncomment this when gs_Configure is done 
          ICE_Options::setConfig("configured", "T"); 
          ICE_Options::Save();  
        }else { 
          GS_DEBUG( printf("new device FAILED\n"); ); 
        }//end if-else: device success/fail  
   }//end if: newDeviceSettings 
    
}//end deconstructor 
 
void gs_Configure::Render() { 
    using namespace irr; 
    m_driver->beginScene(true, true, video::SColor(0,150,150,165)); 
 
    //TODO: replace this with a title splash screen 
    //DrawRect(video::SColor(100,200,255,255), core::rect(50, 50, 100, 100)); 
     
    if(m_pIceLogo) m_driver->draw2DImage(m_pIceLogo,  
        irr::core::position2d< irr::s32 >(50,50),  
        irr::core::rect< irr::s32 >(0,0,32,32), 
        0, 
        irr::video::SColor(255, 255, 255, 255), 
        true 
        ); 
     
    m_env->drawAll(); 
     
    m_driver->endScene(); 
} 
 
bool gs_Configure::onEvent(irr::SEvent event) { 
    using namespace irr; 
    using namespace gui; 
 
    if (event.EventType == EET_GUI_EVENT) { 
        s32 id = event.GUIEvent.Caller->getID(); 
        switch(event.GUIEvent.EventType) { 
        case EGET_CHECKBOX_CHANGED: 
                if(id == GSCNF_GUI_CHK_FULLSCRN) { 
                  if(ICE_Options::getConfig("fullscreen") == "T") 
                       ICE_Options::setConfig("fullscreen", "F"); 
                  else  
                       ICE_Options::setConfig("fullscreen", "T");    
                        
                  newDeviceSettings = true; 
                  return true; 
                } 
         
                break; 
        case EGET_BUTTON_CLICKED: 
                if(id == GSCNF_GUI_BTN_OK) {  
                  m_handler->RequestStateChange(GS_INTRO); 
                  return true;           
                } 
                break; 
     
        case EGET_LISTBOX_CHANGED: 
                if(id == GSCNF_GUI_LST_RES) { 
                  char c_buffer[64]; 
                  wcstombs(c_buffer, m_lst_res->getListItem(m_lst_res->getSelected()), 64); 
                  ICE_Options::setConfig("resolution", c_buffer); 
                  newDeviceSettings = true; 
                  return true; //if supported list id 
                }else if( id == GSCNF_GUI_LST_COLOR){ 
                  char c_buffer[64]; 
                  wcstombs(c_buffer, m_lst_color->getListItem(m_lst_color->getSelected()), 64); 
                  ICE_Options::setConfig("colordepth", c_buffer); 
                  newDeviceSettings = true; 
                  return true; //if supported list id 
                }else if( id == GSCNF_GUI_LST_DEV){ 
                  char c_buffer[64]; 
                  wcstombs(c_buffer, m_lst_dev->getListItem(m_lst_dev->getSelected()), 64); 
                  ICE_Options::setConfig("device", c_buffer); 
                  newDeviceSettings = true; 
                  return true; //if supported list id 
                }         
                break;         
        }//end switch: type of GUI event 
    }//end if: GUI event 
     
    return false; 
} 
 
void gs_Configure::Update() { 
 
} 
 
 
	/* NEW tinyXML way of creating file (incomplete, made by hand) 
	//create version tag 
	elem = new TiXmlElement("Version"); 
	elem->SetAttribute("ver", GS_CONFIG_FILE_VER ); 
	doc.InsertEndChild( *elem ); 
	doc.SaveFile(GS_TINY_CONFIG_FILE); 
	*/ 
 
/*** OLD qXML file way (replaced by using tinyXML) *** 
    //* create device ini file (GS_CONFIG_FILE_VER "ICE ini v1.0") 
   	q_XML* xml = new q_XML_WRITENEW(GS_CONFIG_FILE); 
	//( xml->isError() ) return; 
	xml->addAttrStr("version", GS_CONFIG_FILE_VER); 
	xml->createNewNode("resolutions", "list of resolutions"); 
	xml->addAttrStr("1", "640x480"); 
	xml->addAttrStr("2", "800x600"); 
	xml->addAttrStr("3", "1024x768"); 
	xml->endCurrNode(); 
	xml->createNewNode("colordepths", "list of color depths"); 
	xml->addAttrStr("16bit", "16"); 
	xml->addAttrStr("32bit", "32"); 
	xml->endCurrNode(); 
	xml->createNewNode("devices", "list of device drivers"); 
	xml->addAttrStr("OpenGL", "OpenGL"); 
	xml->addAttrStr("DirectX8", "DirectX8"); 
	xml->addAttrStr("Software", "Software"); 
	delete xml; 
    //*/ 
 
/*** READ IN from device ini file (GS_CONFIG_FILE_VER 1.0) *** 
	attr_info attr; 
	wchar_t _buffer[1024];  
    q_XML* xml = new q_XML_READER(GS_CONFIG_FILE); 
    //if(xml->isError()) error! 
    xml->nextAttr(); 
    attr = xml->getCurrAttr(); 
    if(attr.attrName != "version" || attr.attrValue != GS_CONFIG_FILE_VER) { 
        GS_DEBUG( printf("GS_CONFIG: error! mismatched config file version\n"); ); 
    } 
    //GET ALL RESOLUTIONS 
    if( xml->nextNode() != "resolutions" ) { 
        GS_DEBUG( printf("GS_CONFIG: error! did not find expected node 'resolutions'\n"); ); 
    } 
	while(xml->nextAttr() != "" && !xml->isError()) { 
       irr::s32 idx; 
       attr = xml->getCurrAttr(); 
       mbstowcs(_buffer, attr.attrValue.c_str(), 1024 ); 
	   idx = m_lst_res->addItem( _buffer ); 
	   //if this is the current resolution choice, select it by default 
	   if(attr.attrValue == ICE_Options::getConfig("resolution") ) { 
   		     m_lst_res->setSelected(idx); 
       } 
	} 
	//GET ALL COLOR DEPTHS 
    if( xml->nextNode() != "colordepths" ) { 
        GS_DEBUG( printf("GS_CONFIG: error! did not find expected node 'colordepths'\n"); ); 
    } 
	while(xml->nextAttr() != "" && !xml->isError()) { 
       irr::s32 idx; 
       attr = xml->getCurrAttr(); 
       mbstowcs(_buffer, attr.attrValue.c_str(), 1024 ); 
	   idx = m_lst_color->addItem( _buffer ); 
	   //if this is the current color choice, select it by default 
	   if(attr.attrValue == ICE_Options::getConfig("colordepth") ) { 
   		     m_lst_color->setSelected(idx); 
       } 
	} 
	//GET ALL DEVICE TYPES 
    if( xml->nextNode() != "devices" ) { 
        GS_DEBUG( printf("GS_CONFIG: error! did not find expected node 'devices'\n"); ); 
    } 
	while(xml->nextAttr() != "" && !xml->isError()) { 
       irr::s32 idx; 
       attr = xml->getCurrAttr(); 
       mbstowcs(_buffer, attr.attrValue.c_str(), 1024 ); 
	   idx = m_lst_dev->addItem( _buffer ); 
	   //if this is the current color choice, select it by default 
	   if(attr.attrValue == ICE_Options::getConfig("device") ) { 
   		     m_lst_dev->setSelected(idx); 
       } 
	} 
	//CLOSE FILE 
    delete xml;    
	//*/