// --------------------------------------------------------------------------- // // @file TwSimpleSDL.c // @brief A simple example that uses AntTweakBar with OpenGL and SDL 1.2 // // AntTweakBar: http://anttweakbar.sourceforge.net/doc // OpenGL: http://www.opengl.org // SDL: http://www.libsdl.org // // @author Philippe Decaudin // // --------------------------------------------------------------------------- #include #if defined(_WIN32) || defined(_WIN64) // MiniSDL12.h is provided to avoid the need of having SDL installed to // recompile this example. Do not use it in your own programs, better // install and use the actual SDL library SDK. # define USE_MINI_SDL #endif #ifdef USE_MINI_SDL # include "../src/MiniSDL12.h" #else # include #endif #include #include #include #if defined(_WIN32) || defined(_WIN64) # include // required by gl.h #endif #include #include // SDL redefines main #ifdef main # undef main #endif int main() { const SDL_VideoInfo* video = NULL; int width = 640, height = 480; int bpp, flags; int quit = 0; TwBar *bar; int n, numCubes = 30; float color0[] = { 1.0f, 0.5f, 0.0f }; float color1[] = { 0.5f, 1.0f, 0.0f }; double ka = 5.3, kb = 1.7, kc = 4.1; // Initialize SDL, then get the current video mode and use it to create a SDL window. if( SDL_Init(SDL_INIT_VIDEO)<0 ) { fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError()); SDL_Quit(); exit(1); } video = SDL_GetVideoInfo(); if( !video ) { fprintf(stderr, "Video query failed: %s\n", SDL_GetError()); SDL_Quit(); exit(1); } SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); //SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); bpp = video->vfmt->BitsPerPixel; flags = SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE; //flags |= SDL_FULLSCREEN; if( !SDL_SetVideoMode(width, height, bpp, flags) ) { fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError()); SDL_Quit(); exit(1); } SDL_WM_SetCaption("AntTweakBar simple example using SDL", "AntTweakBar+SDL"); // Enable SDL unicode and key-repeat SDL_EnableUNICODE(1); SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); // Set OpenGL viewport and states glViewport(0, 0, width, height); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // use default light diffuse and position glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glDisable(GL_CULL_FACE); glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); // Initialize AntTweakBar TwInit(TW_OPENGL, NULL); // Tell the window size to AntTweakBar TwWindowSize(width, height); // Create a tweak bar bar = TwNewBar("TweakBar"); TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with SDL and OpenGL.\nPress [Space] to toggle fullscreen.' "); // Message added to the help bar. // Add 'width' and 'height' to 'bar': they are read-only (RO) variables of type TW_TYPE_INT32. TwAddVarRO(bar, "Width", TW_TYPE_INT32, &width, " label='Wnd width' help='Width of the graphics window (in pixels)' "); TwAddVarRO(bar, "Height", TW_TYPE_INT32, &height, " label='Wnd height' help='Height of the graphics window (in pixels)' "); // Add 'numCurves' to 'bar': this is a modifiable variable of type TW_TYPE_INT32. Its shortcuts are [c] and [C]. TwAddVarRW(bar, "NumCubes", TW_TYPE_INT32, &numCubes, " label='Number of cubes' min=1 max=100 keyIncr=c keyDecr=C help='Defines the number of cubes in the scene.' "); // Add 'ka', 'kb and 'kc' to 'bar': they are modifiable variables of type TW_TYPE_DOUBLE TwAddVarRW(bar, "ka", TW_TYPE_DOUBLE, &ka, " label='X path coeff' keyIncr=1 keyDecr=CTRL+1 min=-10 max=10 step=0.01 "); TwAddVarRW(bar, "kb", TW_TYPE_DOUBLE, &kb, " label='Y path coeff' keyIncr=2 keyDecr=CTRL+2 min=-10 max=10 step=0.01 "); TwAddVarRW(bar, "kc", TW_TYPE_DOUBLE, &kc, " label='Z path coeff' keyIncr=3 keyDecr=CTRL+3 min=-10 max=10 step=0.01 "); // Add 'color0' and 'color1' to 'bar': they are modifable variables of type TW_TYPE_COLOR3F (3 floats color) TwAddVarRW(bar, "color0", TW_TYPE_COLOR3F, &color0, " label='Start color' help='Color of the first cube.' "); TwAddVarRW(bar, "color1", TW_TYPE_COLOR3F, &color1, " label='End color' help='Color of the last cube. Cube colors are interpolated between the Start and End colors.' "); // Add 'quit' to 'bar': this is a modifiable (RW) variable of type TW_TYPE_BOOL32 // (a boolean stored in a 32 bits integer). Its shortcut is [ESC]. TwAddVarRW(bar, "Quit", TW_TYPE_BOOL32, &quit, " label='Quit?' true='+' false='-' key='ESC' help='Quit program.' "); // Main loop: // - Draw some cubes // - Process events while( !quit ) { SDL_Event event; int handled; // Clear screen glClearColor(0.5f, 0.75f, 0.8f, 1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // Set OpenGL camera glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40, (double)width/height, 1, 10); gluLookAt(0,0,3, 0,0,0, 0,1,0); // Draw cubes for( n=0; n