#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/GLUT.h>
#include <gl/gl.h>
#include <tchar.h>
#include <cegui.h>
#include <RendererModules\OpenGL\CEGUIOpenGL.h>
#include <RendererModules\OpenGL\CEGUIOpenGLRenderer.h>
#pragma comment( lib, "glu32.lib" )
#pragma comment( lib, "CEGUIBase_d.lib" )
#pragma comment( lib, "CEGUIOpenGLRenderer_d.lib" )
#pragma comment ( lib , "CEGUIExpatParser_d.lib" )
#define EDIT_CONTORL_NAME "Root/EditBox"
using namespace CEGUI;
int G_lastFrameTime = 0;
bool G_quitFlag = false;
void mouseMotion( int x, int y );
void mouseButton( int button, int state, int x, int y );
void KeyCharDown( unsigned char key, int x, int y );
void KeyCharUp( unsigned char key, int x, int y );
void RenderScene();
void InitGUI();
class GUI
{
public:
GUI(){
}
~GUI(){}
public:
static bool handleKeyDown( const CEGUI::EventArgs& args )
{
using namespace CEGUI;
const CEGUI::KeyEventArgs& keyArgs = static_cast<const CEGUI::KeyEventArgs&>(args);
Window* d_root = CEGUI::System::getSingleton().getGUISheet();
// get the text entry editbox
Editbox* editbox = static_cast<Editbox*>(d_root->getChild(EDIT_CONTORL_NAME));
if ( keyArgs.scancode == CEGUI::Key::Return ){
editbox->setText("");
}else{
// get text out of the editbox
String edit_text(editbox->getText());
editbox->setText(edit_text+keyArgs.scancode );
}
// re-activate the text entry box
editbox->activate();
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
glutInit( &argc,argv );
glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA ); //GLUT_DOUBLE glutSwapBuffers()
glutInitWindowPosition( 100,100 );
glutInitWindowSize( 800,600 );
glutCreateWindow( "GUITest" );
glutSetCursor( GLUT_CURSOR_NONE ); //否则双鼠标
// Initilise CEGUI,must initilise OpenGL context before CEGUI::OpenGLRenderer::create();
// or else,OpenGLRenderer failed to initialize the GLEW library. Missing GL version
InitGUI();
glutDisplayFunc( RenderScene );
glutMotionFunc( mouseMotion );
glutPassiveMotionFunc( mouseMotion );
glutMouseFunc( mouseButton );
glutKeyboardFunc( KeyCharDown );
//glutKeyboardUpFunc( KeyCharUp );
glutMainLoop();
return 0;
}
void InitGUI()
{
using namespace CEGUI;
CEGUI::OpenGLRenderer& myRenderer = CEGUI::OpenGLRenderer::create();
CEGUI::System& sys = CEGUI::System::create( myRenderer );
//Init resource directory
CEGUI::DefaultResourceProvider* rp = static_cast<CEGUI::DefaultResourceProvider*>(CEGUI::System::getSingleton().getResourceProvider());
rp->setResourceGroupDirectory("schemes", "../datafiles/schemes/");
rp->setResourceGroupDirectory("imagesets","../datafiles/imagesets/");
rp->setResourceGroupDirectory("fonts", "../datafiles/fonts/");
rp->setResourceGroupDirectory("layouts", "../datafiles/layouts/");
rp->setResourceGroupDirectory("looknfeels","../datafiles/looknfeel/");
rp->setResourceGroupDirectory("lua_scripts","../datafiles/lua_scripts/");
CEGUI::Scheme::setDefaultResourceGroup("schemes");
CEGUI::Imageset::setDefaultResourceGroup("imagesets" );
CEGUI::Font::setDefaultResourceGroup("fonts");
CEGUI::WindowManager::setDefaultResourceGroup("layouts");
CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeels");
CEGUI::ScriptModule::setDefaultResourceGroup("lua_scripts");
WindowManager& winMgr = WindowManager::getSingleton();
SchemeManager::getSingleton().create( "TaharezLook.scheme" );
System::getSingleton().setDefaultMouseCursor( "TaharezLook", "MouseArrow" );
FontManager::getSingleton().create( "Girl.font" );
Window* root = winMgr.loadWindowLayout( "Myself.layout" );
System::getSingleton().setGUISheet( root );
winMgr.getWindow( EDIT_CONTORL_NAME )->subscribeEvent( CEGUI::Editbox::EventTextAccepted, CEGUI::Event::Subscriber(&GUI::handleKeyDown));
}
void mouseMotion( int x,int y )
{
CEGUI::System::getSingleton().injectMousePosition( x,y );
}
void mouseButton( int button, int state, int x, int y )
{
switch( button )
{
case GLUT_LEFT_BUTTON:
{
if ( state == GLUT_UP )
CEGUI::System::getSingleton().injectMouseButtonUp( CEGUI::LeftButton );
else
CEGUI::System::getSingleton().injectMouseButtonDown( CEGUI::LeftButton );
}
break;
case GLUT_RIGHT_BUTTON:
{
if ( state == GLUT_UP )
CEGUI::System::getSingleton().injectMouseButtonUp( CEGUI::RightButton );
else
CEGUI::System::getSingleton().injectMouseButtonDown( CEGUI::RightButton );
}
break;
case GLUT_MIDDLE_BUTTON:
{
if ( state == GLUT_UP )
CEGUI::System::getSingleton().injectMouseButtonUp( CEGUI::MiddleButton );
else
CEGUI::System::getSingleton().injectMouseButtonDown( CEGUI::MiddleButton );
}
break;
}
}
void KeyCharDown( unsigned char key, int x, int y )
{
if ( key == VK_ESCAPE )
G_quitFlag = TRUE;
CEGUI::System::getSingleton().injectChar( static_cast<CEGUI::utf32>(key) );
}
void KeyCharUp( unsigned char key, int x, int y )
{
//CEGUI::System::getSingleton().injectKeyUp( static_cast<CEGUI::utf32>(key) );
}
void RenderScene()
{
int thisTime = glutGet( GLUT_ELAPSED_TIME );
float elapsed = static_cast<float>(thisTime - G_lastFrameTime );
G_lastFrameTime = thisTime;
//inject the time pulse
CEGUI::System::getSingleton().injectTimePulse( elapsed/1000.f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
CEGUI::System::getSingleton().renderGUI();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
if ( G_quitFlag )
exit(0);
}
参考:
http://www.ispinel.com/2010/05/26/961http://www.ispinel.com/2010/05/26/971http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c42246031c2eb8b92127171980852d3d5aeb1e41eaf235702a0121aa9bc2954ddbbb94232c883034074fc70358c75cf28b102a947ed71cfeaf68b6eda575c8b9d3a3c85520dd52756d81839c5b7055812eb6436ef4a7e95f142c07bb9d27648c4e7659ca7406f71ca5b56c3941dcaaca5c3dd42aa77610e7ef62ec6845f546e344&p=8b2a950e8c934eab03f1c93f5f&user=baidu
posted on 2010-06-19 19:57
风轻云淡 阅读(1492)
评论(1) 编辑 收藏 引用