|
楼主 |
发表于 2009-8-11 14:09:36
|
显示全部楼层
没有用过SDL来做界面框架~~~
FlySky 发表于 2009-8-10 22:33
就是osg的源码中的例子 osgviewerSDL 我只是去掉那句SDL_FULL改了下窗口的设置等.- #include <osgViewer/Viewer>
- #include <osgViewer/ViewerEventHandlers>
- #include <osgGA/TrackballManipulator>
- #include <osgDB/ReadFile>
- #include "SDL.h"
- #include <iostream>
- bool convertEvent(SDL_Event& event, osgGA::EventQueue& eventQueue)
- {
- switch (event.type) {
- case SDL_MOUSEMOTION:
- eventQueue.mouseMotion(event.motion.x, event.motion.y);
- return true;
- case SDL_MOUSEBUTTONDOWN:
- eventQueue.mouseButtonPress(event.button.x, event.button.y, event.button.button);
- return true;
- case SDL_MOUSEBUTTONUP:
- eventQueue.mouseButtonRelease(event.button.x, event.button.y, event.button.button);
- return true;
- case SDL_KEYUP:
- eventQueue.keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode);
- return true;
- case SDL_KEYDOWN:
- eventQueue.keyPress( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode);
- return true;
- case SDL_VIDEORESIZE:
- eventQueue.windowResize(0, 0, event.resize.w, event.resize.h );
- return true;
- default:
- break;
- }
- return false;
- }
- int main( int argc, char **argv )
- {
- // init SDL
- if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
- {
- fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
- exit(1);
- }
- atexit(SDL_Quit);
-
- // load the scene.
- osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osg");
- if (!loadedModel)
- {
- std::cout << argv[0] <<": No data loaded." << std::endl;
- return 1;
- }
- unsigned int windowWidth = 0;
- unsigned int windowHeight = 0;
- unsigned int bitDepth = 16;
- windowWidth = 600;
- windowHeight = 480;
- SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
- SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
- SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
- SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
- SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
-
- // set up the surface to render to
- SDL_Surface* screen = SDL_SetVideoMode(windowWidth, windowHeight, bitDepth, SDL_OPENGL | SDL_RESIZABLE);
- if ( screen == NULL )
- {
- std::cerr<<"Unable to set "<<windowWidth<<"x"<<windowHeight<<" video: %s\n"<< SDL_GetError()<<std::endl;
- exit(1);
- }
- SDL_EnableUNICODE(1);
-
- // If we used 0 to set the fields, query the values so we can pass it to osgViewer
- windowWidth = screen->w;
- windowHeight = screen->h;
-
- osgViewer::Viewer viewer;
- osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = viewer.setUpViewerAsEmbeddedInWindow(0,0,windowWidth,windowHeight);
- viewer.setSceneData(loadedModel.get());
- viewer.setCameraManipulator(new osgGA::TrackballManipulator);
- viewer.addEventHandler(new osgViewer::StatsHandler);
- viewer.realize();
- bool done = false;
- while( !done )
- {
- SDL_Event event;
- while ( SDL_PollEvent(&event) )
- {
- // pass the SDL event into the viewers event queue
- convertEvent(event, *(gw->getEventQueue()));
- switch (event.type) {
- case SDL_VIDEORESIZE:
- SDL_SetVideoMode(event.resize.w, event.resize.h, bitDepth, SDL_OPENGL | SDL_RESIZABLE);
- gw->resized(0, 0, event.resize.w, event.resize.h );
- std::cout<<event.resize.w<<" "<<event.resize.h<<std::endl;
- break;
- case SDL_KEYUP:
- if (event.key.keysym.sym==SDLK_ESCAPE) done = true;
- if (event.key.keysym.sym=='f')
- {
- SDL_WM_ToggleFullScreen(screen);
- gw->resized(0, 0, screen->w, screen->h );
- }
- break;
- case SDL_QUIT:
- done = true;
- }
- }
- if (done) continue;
- // draw the new frame
- viewer.frame();
- // Swap Buffers
- SDL_GL_SwapBuffers();
- }
-
- return 0;
- }
复制代码 |
|