| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include <osgViewer/SimpleViewer> |
|---|
| 7 | #include <osgGA/TrackballManipulator> |
|---|
| 8 | #include <osgDB/ReadFile> |
|---|
| 9 | |
|---|
| 10 | #include "SDL.h" |
|---|
| 11 | |
|---|
| 12 | #include <iostream> |
|---|
| 13 | |
|---|
| 14 | bool convertEvent(SDL_Event& event, osgGA::EventQueue& eventQueue) |
|---|
| 15 | { |
|---|
| 16 | switch (event.type) { |
|---|
| 17 | |
|---|
| 18 | case SDL_MOUSEMOTION: |
|---|
| 19 | eventQueue.mouseMotion(event.motion.x, event.motion.y); |
|---|
| 20 | return true; |
|---|
| 21 | |
|---|
| 22 | case SDL_MOUSEBUTTONDOWN: |
|---|
| 23 | eventQueue.mouseButtonPress(event.button.x, event.button.y, event.button.button); |
|---|
| 24 | return true; |
|---|
| 25 | |
|---|
| 26 | case SDL_MOUSEBUTTONUP: |
|---|
| 27 | eventQueue.mouseButtonRelease(event.button.x, event.button.y, event.button.button); |
|---|
| 28 | return true; |
|---|
| 29 | |
|---|
| 30 | case SDL_KEYUP: |
|---|
| 31 | eventQueue.keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode); |
|---|
| 32 | return true; |
|---|
| 33 | |
|---|
| 34 | case SDL_KEYDOWN: |
|---|
| 35 | eventQueue.keyPress( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode); |
|---|
| 36 | return true; |
|---|
| 37 | |
|---|
| 38 | case SDL_VIDEORESIZE: |
|---|
| 39 | eventQueue.windowResize(0, 0, event.resize.w, event.resize.h ); |
|---|
| 40 | return true; |
|---|
| 41 | |
|---|
| 42 | default: |
|---|
| 43 | break; |
|---|
| 44 | } |
|---|
| 45 | return false; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | int main( int argc, char **argv ) |
|---|
| 49 | { |
|---|
| 50 | if (argc<2) |
|---|
| 51 | { |
|---|
| 52 | std::cout << argv[0] <<": requires filename argument." << std::endl; |
|---|
| 53 | return 1; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) |
|---|
| 58 | { |
|---|
| 59 | fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); |
|---|
| 60 | exit(1); |
|---|
| 61 | } |
|---|
| 62 | atexit(SDL_Quit); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]); |
|---|
| 67 | if (!loadedModel) |
|---|
| 68 | { |
|---|
| 69 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 70 | return 1; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | unsigned int windowWidth = 0; |
|---|
| 75 | unsigned int windowHeight = 0; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | unsigned int bitDepth = 0; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | const SDL_version* linked_version = SDL_Linked_Version(); |
|---|
| 82 | if(linked_version->major == 1 && linked_version->minor == 2) |
|---|
| 83 | { |
|---|
| 84 | if(linked_version->patch < 10) |
|---|
| 85 | { |
|---|
| 86 | windowWidth = 1280; |
|---|
| 87 | windowHeight = 1024; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); |
|---|
| 92 | SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); |
|---|
| 93 | SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); |
|---|
| 94 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); |
|---|
| 95 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | SDL_Surface* screen = SDL_SetVideoMode(windowWidth, windowHeight, bitDepth, SDL_OPENGL | SDL_FULLSCREEN | SDL_RESIZABLE); |
|---|
| 99 | if ( screen == NULL ) |
|---|
| 100 | { |
|---|
| 101 | std::cerr<<"Unable to set "<<windowWidth<<"x"<<windowHeight<<" video: %s\n"<< SDL_GetError()<<std::endl; |
|---|
| 102 | exit(1); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | SDL_EnableUNICODE(1); |
|---|
| 106 | |
|---|
| 107 | osgViewer::SimpleViewer viewer; |
|---|
| 108 | viewer.setSceneData(loadedModel.get()); |
|---|
| 109 | viewer.setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | windowWidth = screen->w; |
|---|
| 113 | windowHeight = screen->h; |
|---|
| 114 | viewer.getEventQueue()->windowResize(0, 0, windowWidth, windowHeight ); |
|---|
| 115 | |
|---|
| 116 | bool done = false; |
|---|
| 117 | while( !done ) |
|---|
| 118 | { |
|---|
| 119 | SDL_Event event; |
|---|
| 120 | |
|---|
| 121 | while ( SDL_PollEvent(&event) ) |
|---|
| 122 | { |
|---|
| 123 | |
|---|
| 124 | convertEvent(event, *viewer.getEventQueue()); |
|---|
| 125 | |
|---|
| 126 | switch (event.type) { |
|---|
| 127 | |
|---|
| 128 | case SDL_KEYUP: |
|---|
| 129 | |
|---|
| 130 | if (event.key.keysym.sym==SDLK_ESCAPE) done = true; |
|---|
| 131 | if (event.key.keysym.sym=='f') SDL_WM_ToggleFullScreen(screen); |
|---|
| 132 | |
|---|
| 133 | break; |
|---|
| 134 | |
|---|
| 135 | case SDL_QUIT: |
|---|
| 136 | done = true; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | if (done) continue; |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | viewer.frame(); |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | SDL_GL_SwapBuffers(); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | return 0; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|