Input_injection not working
Posted: Thu Sep 30, 2010 02:33
Leadwerks + CEGUI + SDL in C++
Can someone please tell me why the following code only shows a MessageBox once?
As you can see, I've extracted the contents of the inject_input() function down to within the main render loop. I made it show the MessageBox for each event that occurs, but apparently only ONE event occurs at startup. So only ONE message box shows. I click & click, drag & drag, type & type, AND no events occur?
Can someone please tell me why the following code only shows a MessageBox once?
Code: Select all
/*
//Load the engine
#include "engine.h"
//Load the scene processer
#include "ProcessScene.h"
//Load the UI engine
#include "include/CEGUI.h"
#include "include/RendererModules/OpenGL/CEGUIOpenGLRenderer.h"
//Load the input engine
#include "sdl/include/SDL.h"
void handle_mouse_down(Uint8 button)
{
switch ( button )
{
// handle real mouse buttons
case SDL_BUTTON_LEFT:
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
break;
case SDL_BUTTON_MIDDLE:
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
break;
case SDL_BUTTON_RIGHT:
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
break;
// handle the mouse wheel
case SDL_BUTTON_WHEELDOWN:
CEGUI::System::getSingleton().injectMouseWheelChange( -1 );
break;
case SDL_BUTTON_WHEELUP:
CEGUI::System::getSingleton().injectMouseWheelChange( +1 );
break;
}
}
void handle_mouse_up(Uint8 button)
{
switch ( button )
{
case SDL_BUTTON_LEFT:
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
break;
case SDL_BUTTON_MIDDLE:
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);
break;
case SDL_BUTTON_RIGHT:
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
break;
}
}
void inject_input()
{
}
void inject_time_pulse(double& last_time_pulse)
{
// get current "run-time" in seconds
double t = 0.001*SDL_GetTicks();
// inject the time that passed since the last call
CEGUI::System::getSingleton().injectTimePulse( float(t-last_time_pulse) );
// store the new time as the last time
last_time_pulse = t;
}
void render_gui()
{
// clear the colour buffer
glClear( GL_COLOR_BUFFER_BIT );
// render the GUI :)
CEGUI::System::getSingleton().renderGUI();
// Update the screen
SDL_GL_SwapBuffers();
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
Initialize() ;
RegisterAbstractPath("C:/vita");
SetAppTitle( "Vita" ) ;
Graphics( 800, 600 ) ;
/*
*======================================================
* UI
*======================================================
*/
CEGUI::OpenGLRenderer &ceguiRenderer = CEGUI::OpenGLRenderer::create();
CEGUI::System::create(ceguiRenderer);
ceguiRenderer.enableExtraStateSettings(true);
CEGUI::DefaultResourceProvider *rp = static_cast<CEGUI::DefaultResourceProvider*>(CEGUI::System::getSingleton().getResourceProvider());
rp->setResourceGroupDirectory("schemes", "GUI/schemes/");
rp->setResourceGroupDirectory("layouts", "GUI/layouts/");
rp->setResourceGroupDirectory("looknfeel", "GUI/looknfeel/");
rp->setResourceGroupDirectory("imagesets", "GUI/imagesets/");
rp->setResourceGroupDirectory("fonts", "GUI/fonts/");
CEGUI::Scheme::setDefaultResourceGroup("schemes");
CEGUI::WindowManager::setDefaultResourceGroup("layouts");
CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeel");
CEGUI::Imageset::setDefaultResourceGroup("imagesets");
CEGUI::Font::setDefaultResourceGroup("fonts");
CEGUI::SchemeManager::getSingleton().create("TaharezLook.scheme");
CEGUI::FontManager::getSingleton().create( "DejaVuSans-10.font" );
CEGUI::System::getSingleton().setDefaultFont("DejaVuSans-10");
CEGUI::WindowManager &wm = CEGUI::WindowManager::getSingleton();
CEGUI::Window *root = wm.loadWindowLayout("TextDemo.layout");
CEGUI::System::getSingleton().setGUISheet(root);
//Set cursor
CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
CEGUI::System::getSingleton().setDefaultTooltip( "TaharezLook/Tooltip" );
//Button->setTooltipText("This button will cause the self-destruct!");
//===========================
// USER INPUT
//
//Before we can do anything, we need to initialise our libraries. First SDL:
if (SDL_Init(SDL_INIT_VIDEO)<0)
{
fprintf(stderr, "Unable to initialise SDL: %s", SDL_GetError());
exit(0);
}
/*
As keypress characters needs to be injected into CEGUI,
we activate unicode translation for SDL key events
*/
SDL_EnableUNICODE(1);
/*
This makes it alot easier as we don't have to worry about modifier keys
and keyboard layouts ourselves. More about this later on...
Key repeat is a nice feature for the text input widgets in CEGUI, so we use SDL to generate them
*/
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
//===========================
AFilter() ;
TFilter() ;
TWorld world;
TBuffer gbuffer;
TCamera camera;
TMesh mesh;
TLight light;
TMesh ground;
TMaterial material;
world = CreateWorld() ;
if (!world) {
MessageBoxA(0,"Error","Failed to create world.",0);
return Terminate();
}
//Lighting Render buffer
gbuffer=CreateBuffer(GraphicsWidth(),GraphicsHeight(),BUFFER_COLOR|BUFFER_DEPTH|BUFFER_NORMAL);
camera=CreateCamera();
TVec3 camrotation=Vec3(0);
float mx=0;
float my=0;
float move=0;
float strafe=0;
//Center Mouse
MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);
/*
==================================
*/
//Load scene
//TEntity scene=LoadScene("Maps/default.sbx");
//ProcessScene(scene);
PositionEntity(camera,Vec3(0,0,-2));
// Game loop
while( !KeyHit() && !AppTerminate() )
{
if( !AppSuspended() ) // We are not in focus!
{
//===========================
//NOCLIP - Updates every frame
//Capture Mouse movements
mx=Curve(MouseX()-GraphicsWidth()/2,mx,6);
my=Curve(MouseY()-GraphicsHeight()/2,my,6);
camrotation.X=camrotation.X+my/10.0;
camrotation.Y=camrotation.Y-mx/10.0;
RotateEntity(camera, camrotation);
//Hide the cursor
//ShowCursor(false);
//If the shift key is pressed, move faster
if(KeyDown(KEY_LSHIFT))
{
move=Curve(KeyDown(KEY_W)-KeyDown(KEY_S),move,50);
strafe=Curve(KeyDown(KEY_D)-KeyDown(KEY_A),strafe,50);
MoveEntity(camera, Vec3(strafe/5.0,0,move/5.0));
}
move=Curve(KeyDown(KEY_W)-KeyDown(KEY_S),move,50);
strafe=Curve(KeyDown(KEY_D)-KeyDown(KEY_A),strafe,50);
MoveEntity(camera, Vec3(strafe/20.0,0,move/20.0));
//Center the mouse on screen (Cannot while menu is open)
//MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);
//===========================
// USER INPUT
//
// we inject the mouse position directly.
/*CEGUI::System::getSingleton().injectMousePosition(
static_cast<float>(MouseX()),
static_cast<float>(MouseY())
);*/
bool must_quit = false;
// get "run-time" in seconds
double last_time_pulse = 0.001*static_cast<double>(SDL_GetTicks());
SDL_Event e;
// go through all available events
while (SDL_PollEvent(&e))
{
MessageBox(NULL, L"An entry", NULL, NULL);
// we use a switch to determine the event type
switch (e.type)
{
// mouse motion handler
case SDL_MOUSEMOTION:
// we inject the mouse position directly.
CEGUI::System::getSingleton().injectMousePosition(
static_cast<float>(e.motion.x),
static_cast<float>(e.motion.y)
);
break;
// mouse down handler
case SDL_MOUSEBUTTONDOWN:
// let a special function handle the mouse button down event
handle_mouse_down(e.button.button);
break;
// mouse up handler
case SDL_MOUSEBUTTONUP:
// let a special function handle the mouse button up event
handle_mouse_up(e.button.button);
break;
// key down
case SDL_KEYDOWN:
// to tell CEGUI that a key was pressed, we inject the scancode.
CEGUI::System::getSingleton().injectKeyDown(e.key.keysym.scancode);
// as for the character it's a litte more complicated. we'll use for translated unicode value.
// this is described in more detail below.
if ((e.key.keysym.unicode != 0))
{
CEGUI::System::getSingleton().injectChar(e.key.keysym.unicode);
}
break;
// key up
case SDL_KEYUP:
// like before we inject the scancode directly.
CEGUI::System::getSingleton().injectKeyUp(e.key.keysym.scancode);
break;
// WM quit event occured
case SDL_QUIT:
//must_quit = true;
break;
}
}
inject_time_pulse(last_time_pulse);
render_gui();
RECT r;
GetWindowRect( FindWindow(NULL, TEXT("Vita") ), &r);
ClipCursor( &r );
//===========================
// Update timing and world
UpdateAppTime();
UpdateWorld(AppSpeed()) ;
// Render
SetBuffer(gbuffer);
RenderWorld();
SetBuffer(BackBuffer());
RenderLights(gbuffer);
//Render UI
glPixelStoref(0x806E, 0);
glPixelStoref(GL_PACK_ROW_LENGTH, 0);
glPixelStoref(GL_UNPACK_ROW_LENGTH, 0);
CEGUI::System::getSingleton().renderGUI();
// Send to screen
Flip(0) ;
//FINISH UI
}
ClipCursor (NULL);
}
// Done
return Terminate() ;
}
As you can see, I've extracted the contents of the inject_input() function down to within the main render loop. I made it show the MessageBox for each event that occurs, but apparently only ONE event occurs at startup. So only ONE message box shows. I click & click, drag & drag, type & type, AND no events occur?