//--- < TUT01.CPP > -------------------| cZUI - A zoomable UI Framework | // // Tutorial #1 - simplest scene // // $Author: botik32 $ // $Revision: 1.4 $ // $Date: 2004/05/25 13:14:15 $ //-----------------------------------------------------------------------------/ #include #include #include #include #ifdef WIN32 #include #else #include #endif // WIN32 #include #include // general client header #include // video driver #include // event driver #include // easy-to-use leaf, we inherit from it #include // Handles all cameras that paint in one window #include // Leaf nodes cannot be placed directly // into the scene. We need a scenenode // above every leaf. #include "alloc_list.h" // keeps track of allocated nodes class TextLeaf : public ActiveLeaf { protected: char *txt; public: TextLeaf( double px, double py, const char *t ) : ActiveLeaf(px,py) { txt = new char[ strlen(t) + 1 ]; strcpy( txt, t ); }; void paint( Camera *c ) { ScrollableView *v = c->get_view(); v->pen( 0,0,0, 1 ); // color:black, thickness:1 v->setfontsize( 8 ); v->textout( xpos, ypos+8, txt ); }; void paint_hover( Camera *c ) { ScrollableView *v = c->get_view(); v->setfontsize( 8 ); v->pen( 120,0,0, 1 ); // color:darkred, thickness:1 v->textout( xpos, ypos+8, txt ); }; void get_boundrect( double &bx1, double &by1, double &bx2, double &by2 ) { bx1 = xpos; by1 = ypos; bx2 = bx1 + strlen(txt)*5.5; by2 = ypos + 8; }; bool check_event_fitspixels( Event * ) { // always catch mouse events return true; }; }; int main( int argc, char* argv[] ) { alloc_list heap; if( SDL_Init( SDL_INIT_VIDEO /*| SDL_INIT_NOPARACHUTE*/ ) ) { std::cout << "Problems initializing video, exiting" << std::endl; abort(); } unsigned long framecnt = 0; // Open a window of 640x480, 16bit. SDL_Surface *s1 = SDL_SetVideoMode(400, 240, 16, 0 /*SDL_RESIZABLE*/); // And create a driver that uses it. SDLSGESurfaceDriver driver1(s1); // The second camera uses the same window SDLSGESurfaceDriver driver2(s1); // create the scene Scene sc; // A Frame-rate window, 100 FPS GraphWindow window1( &sc ); // A Camera that fills most of the window. SurfaceCamera camera1( "Camera 1", &driver1, 0xff, 0xff, 0xff, 0, 30, 400, 210); // A Camera taking a small portion of the screen (10,10,60,60) SurfaceCamera camera2( "Camera 2", &driver2, 0xff,0xff,0xff, /*bkgcolor*/ 0, 0, 400, 30 ); window1.cameras.add_surface_camera( &camera1 ); camera1.assign_parent( &window1 ); window1.cameras.add_surface_camera( &camera2 ); camera2.assign_parent( &window1 ); // All scene nodes are created dynamically to be automatically // freed at layer destruction LayerNode txtLayer; txtLayer.add_child( heap.add( new SceneNode( heap.add( new TextLeaf( 20, 20, "Test1" ) ) ) ) ); camera1.add_layer( &txtLayer ); camera2.add_layer( &txtLayer ); CameraSetEvent cse1( &camera1, 0,0, 60,40 ); camera1.handle_event( &cse1 ); CameraOnEvent cmr1_on( &camera1 ); camera1.handle_event( &cmr1_on ); CameraSetEvent cse2( &camera1, -100,-60, 140,100 ); camera2.handle_event( &cse2 ); CameraOnEvent cmr2_on( &camera2 ); camera2.handle_event( &cmr2_on ); SDL_Event event; bool quit = false; #ifdef WIN32 time_t t1, t2; time(&t1); #else timeval t1, t2; gettimeofday( &t1, NULL ); #endif while( !quit ){ //SDL_WaitEvent(&event); while( SDL_PollEvent(&event) ) { switch(event.type){ case SDL_QUIT: quit = 1; break; default: Event *e = SDLEventDriver::handle_event(&event); window1.handle_event( e ); delete e; break; } } window1.advance_time(); window1.process_internal_events(); window1.repaint(); framecnt++; usleep( 2000 ); } #ifdef WIN32 time(&t2); float difsecs = t2 - t1; #else gettimeofday( &t2, NULL ); float difsecs = t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec)/1000000.; #endif fprintf( stderr, "Average FPS: %f\n", framecnt/difsecs ); // cleanup SDL_Quit(); return 0; }