root/OpenSceneGraph/trunk/examples/osgviewerSDL/osgsimpleviewerSDL.cpp @ 6812

Revision 5718, 4.2 kB (checked in by robert, 8 years ago)

From Eric Wing, add version check for use of automatic resizeing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
2// (C) 2005 Mike Weiblen http://mew.cx/ released under the OSGPL.
3// Simple example using GLUT to create an OpenGL window and OSG for rendering.
4// Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp
5
6#include <osgViewer/SimpleViewer>
7#include <osgGA/TrackballManipulator>
8#include <osgDB/ReadFile>
9
10#include "SDL.h"
11
12#include <iostream>
13
14bool 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
48int 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    // init SDL
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    // load the scene.
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    // Starting with SDL 1.2.10, passing in 0 will use the system's current resolution.
74    unsigned int windowWidth = 0;
75    unsigned int windowHeight = 0;
76
77    // Passing in 0 for bitdepth also uses the system's current bitdepth. This works before 1.2.10 too.
78    unsigned int bitDepth = 0;
79
80    // If not linked to SDL 1.2.10+, then we must use hardcoded values
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    // set up the surface to render to
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    // If we used 0 to set the fields, query the values so we can pass it to osgViewer
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            // pass the SDL event into the viewers event queue
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        // draw the new frame
144        viewer.frame();
145
146        // Swap Buffers
147        SDL_GL_SwapBuffers();
148    }
149   
150    return 0;
151}
152
153/*EOF*/
Note: See TracBrowser for help on using the browser.