How to integrate AntTweakBarFirst download and install the AntTweakBar library. The download page also explains how to compile, link and run your program with AntTweakBar. Integration into your applicationAntTweakBar has a C interface, and can be directly integrated in any C++ or C program that uses OpenGL or DirectX 9 to 11. We will now see how to integrate AntTweakBar into your source code.
Note that you can found some full examples and their commented sources in the 1. IncludeFirst, include the AntTweakBar header with your other includes. #include <AntTweakBar.h>
2. Initialize AntTweakBar
After OpenGL or DirectX initializations and after the creation of your graphic window, initialize AntTweakBar by calling If you are using OpenGL, the command is: TwInit(TW_OPENGL, NULL); // or TwInit(TW_OPENGL_CORE, NULL); // for core profile If you are using DirectX, the command is: TwInit(TW_DIRECT3D9, myD3DDevice); // for Direct3D 9 // or TwInit(TW_DIRECT3D10, myD3DDevice); // for Direct3D 10 // or TwInit(TW_DIRECT3D11, myD3DDevice); // for Direct3D 11
where
Then you tell the size of the graphic window to AntTweakBar by calling TwWindowSize(myWindowWidth, myWindowHeight); 3. Create a tweak bar and add variables
Declare one or more pointers of type TwBar *myBar; myBar = TwNewBar("NameOfMyTweakBar");
Then, you can add variables to your tweak bar(s) by calling TwAddVarRW(myBar, "NameOfMyVariable", TW_TYPE_xxxx, &myVar, ""); You will find examples of use of these functions in the program examples provided with the library. 4. Draw your tweak bar
Call // main loop while( ... ) { // clear the frame buffer // update view and camera // update your scene // draw your scene TwDraw(); // draw the tweak bar(s) // present/swap the frame buffer } // end of main loop 5. Handle mouse and keyboard events, and window size changesAt this point, a tweak bar has been created and is displayed into the graphic window of your application, but it does not react to user inputs and window size changes.
For that, you should send the keyboard, mouse and window size events to AntTweakBar by calling Depending on your windowing system, you will probably have to translate the incoming events before sending them to AntTweakBar. To help in debugging your key event translation routine, the translated key received by the AntTweakBar library is displayed at the bottom of the Help bar for a short while. If you are using SDL, GLUT, GLFW or Windows (DirectX, DXUT), the following functions do the translation for you. With SDL
Use SDL_Event event; int handled; // ... // inside the main loop while( SDL_PollEvent(&event) ) // process events { // send event to AntTweakBar handled = TwEventSDL(&event SDL_MAJOR_VERSION, SDL_MINOR_VERSION); if( !handled ) // if event has not been handled by AntTweakBar, process it { // ... } } See TwSimpleSDL for a full example based on SDL.
If you want to see how With GLUT
Call
It is also required that you send the
// after GLUT initialization // directly redirect GLUT events to AntTweakBar glutMouseFunc((GLUTmousebuttonfun)TwEventMouseButtonGLUT); glutMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT); glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT); // same as MouseMotion glutKeyboardFunc((GLUTkeyboardfun)TwEventKeyboardGLUT); glutSpecialFunc((GLUTspecialfun)TwEventSpecialGLUT); // send the ''glutGetModifers'' function pointer to AntTweakBar TwGLUTModifiersFunc(glutGetModifiers); // send window size events to AntTweakBar glutReshapeFunc(MyReshape); // and call TwWindowSize in the function MyReshape See TwSimpleGLUT for a full example based on GLUT.
If you do not want to redirect events directly to AntTweakBar (because your app also need to handle them), define your own GLUT callbacks and call void GLUTCALLBACK OnMouseMotion(int mouseX, int mouseY) // your callback function called by GLUT when mouse has moved { if( !TwEventMouseMotionGLUT(mouseX, mouseY) ) // send event to AntTweakBar { // event has not been handled by AntTweakBar // your code here to handle the event // ... } } // ... // somewhere in main() glutMotionFunc(OnMouseMotion);
If you want to see how the With GLFW
Call They can also be directly used as GLFW event callbacks. // after GLFW initialization // directly redirect GLFW events to AntTweakBar glfwSetMouseButtonCallback((GLFWmousebuttonfun)TwEventMouseButtonGLFW); glfwSetMousePosCallback((GLFWmouseposfun)TwEventMousePosGLFW); glfwSetMouseWheelCallback((GLFWmousewheelfun)TwEventMouseWheelGLFW); glfwSetKeyCallback((GLFWkeyfun)TwEventKeyGLFW); glfwSetCharCallback((GLFWcharfun)TwEventCharGLFW); // send window size events to AntTweakBar glfwSetWindowSizeCallback(MyResize); // and call TwWindowSize in the function MyResize See TwSimpleGLFW for a full example based on GLFW.
If you do not want to redirect events directly to AntTweakBar (because your app also need to handle them), define your own GLFW callbacks and call void GLFWCALL OnMousePos(int mouseX, int mouseY) // your callback function called by GLFW when mouse has moved { if( !TwEventMousePosGLFW(mouseX, mouseY) ) // send event to AntTweakBar { // event has not been handled by AntTweakBar // your code here to handle the event // ... } } // ... // somewhere in main() glfwSetMousePosCallback(OnMousePos); The TwAdvanced1 example follows this approach.
If you want to see how the With SFML
Use // inside the main loop sf::Event event; while (app.GetEvent(event)) { // send event to AntTweakBar int handled = TwEventSFML(&event, 1, 6); // assume SFML version 1.6 here if( !handled ) // if event has not been handled by AntTweakBar, process it { // ... } } See TwSimpleSFML for a full example based on SFML.
If you want to see how With Windows (and DirectX)
Use // In the Windows MessageProc callback LRESULT CALLBACK MessageProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) { if( TwEventWin(wnd, msg, wParam, lParam) ) // send event message to AntTweakBar return 0; // event has been handled by AntTweakBar // else process the event message // ... }
In your main loop, make sure that you call the
See TwSimpleDX9 for a full example based on DirectX9 and Windows.
If you want to see how
Note: if you need to release the graphics resources allocated by AntTweakBar, call With DXUT (DirectX SDK)AntTweakBar can also be integrated in a DXUT-based application. DXUT is a framework included in the DirectX SDK. The integration scheme is as follow: #include <DXUT.h> #include <AntTweakBar.h> //... LRESULT CALLBACK MessageProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam, bool*, void*) { if( TwEventWin(wnd, msg, wParam, lParam) // send event to AntTweakBar return 0; // event has been handled by AntTweakBar // process remaining events... } HRESULT CALLBACK OnCreateDevice(IDxxDevicex *d3dDevice,...) { TwInit(TW_DIRECT3Dxx, d3dDevice); // replace xx by 9 or 10 // create tweak bars and add variables here. // continue with the D3D code... } HRESULT CALLBACK OnDeviceReset(...) { TwWindowSize(backbuffer->Width, backbuffer->Height); // tell AntTweakBar the new backbuffer size // continue with the D3D code... } void CALLBACK OnDeviceLost(void*) { TwWindowSize(0, 0); // release resources allocated by AntTweakBar // release other D3D resources... } void CALLBACK OnFrameRender(...) { // first, clear the render target and draw your scene TwDraw(); // then draw the tweak bar(s) } void CALLBACK OnDestroyDevice(void*) { TwTerminate(); // uninitialize AntTweakBar // other D3D uninitializations... } //... int WINAPI wWinMain(...) { DXUTSetCallbackMsgProc(MessageProc); DXUTSetCallbackD3DxxDeviceCreated(OnCreateDevice); // replace xx by 9 or 10 DXUTSetCallbackD3D9DeviceReset(OnDeviceReset) or DXUTSetCallbackD3D10SwapChainResized(OnDeviceReset); DXUTSetCallbackD3D9DeviceLost(OnDeviceLost) or DXUTSetCallbackD3D10SwapChainReleasing(OnDeviceLost); DXUTSetCallbackD3DxxFrameRender(OnFrameRender); DXUTSetCallbackD3DxxDeviceDestroyed(OnDestroyDevice); // continue with the DXUT code: DXUTInit, DXUTCreateWindow, DXUTCreateDevice and so on. } With X11
Use
If you want to see how 6. Terminate AntTweakBar
Just before closing your graphic window and uninitializing the graphic API, call // at the end of your program TwTerminate(); // close your graphic window // exit your application
We now got one or more fully functional tweak bars.
The examples cited above will also give you a general survey of possibilities offered by the For advanced use of AntTweakBar, have a look to the TwAdvanced1 or TwDualGLUT examples. It’s up to you now. Enjoy!
|