root/OpenSceneGraph/trunk/examples/osgviewerSDL/osgviewerSDL.cpp @ 13385

Revision 7621, 5.7 kB (checked in by robert, 6 years ago)

From Jeremy Moles, "This extra call to SDL_VideoSetMode() prevents my viewport from getting
all wonky when I resize the SDL window. I'm not sure if this bug has
always existed or not, but according to the SDL documentation this
operation should be safe regardless, and doesn't not introduce the need
for extra memory management (that is: there is no need to "cleanup" the
old screen variable, that is all handled by SDL for us).
"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgviewerSDL.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19// (C) 2005 Mike Weiblen http://mew.cx/ released under the OSGPL.
20// Simple example using GLUT to create an OpenGL window and OSG for rendering.
21// Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp
22
23#include <osgViewer/Viewer>
24#include <osgViewer/ViewerEventHandlers>
25#include <osgGA/TrackballManipulator>
26#include <osgDB/ReadFile>
27
28#include "SDL.h"
29
30#include <iostream>
31
32bool convertEvent(SDL_Event& event, osgGA::EventQueue& eventQueue)
33{
34    switch (event.type) {
35
36        case SDL_MOUSEMOTION:
37            eventQueue.mouseMotion(event.motion.x, event.motion.y);
38            return true;
39
40        case SDL_MOUSEBUTTONDOWN:
41            eventQueue.mouseButtonPress(event.button.x, event.button.y, event.button.button);
42            return true;
43
44        case SDL_MOUSEBUTTONUP:
45            eventQueue.mouseButtonRelease(event.button.x, event.button.y, event.button.button);
46            return true;
47
48        case SDL_KEYUP:
49            eventQueue.keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode);
50            return true;
51
52        case SDL_KEYDOWN:
53            eventQueue.keyPress( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode);
54            return true;
55
56        case SDL_VIDEORESIZE:
57            eventQueue.windowResize(0, 0, event.resize.w, event.resize.h );
58            return true;
59
60        default:
61            break;
62    }
63    return false;
64}
65
66int main( int argc, char **argv )
67{
68    if (argc<2)
69    {
70        std::cout << argv[0] <<": requires filename argument." << std::endl;
71        return 1;
72    }
73
74    // init SDL
75    if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
76    {
77        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
78        exit(1);
79    }
80    atexit(SDL_Quit);
81   
82
83    // load the scene.
84    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
85    if (!loadedModel)
86    {
87        std::cout << argv[0] <<": No data loaded." << std::endl;
88        return 1;
89    }
90
91    // Starting with SDL 1.2.10, passing in 0 will use the system's current resolution.
92    unsigned int windowWidth = 0;
93    unsigned int windowHeight = 0;
94
95    // Passing in 0 for bitdepth also uses the system's current bitdepth. This works before 1.2.10 too.
96    unsigned int bitDepth = 0;
97
98    // If not linked to SDL 1.2.10+, then we must use hardcoded values
99    const SDL_version* linked_version = SDL_Linked_Version();
100    if(linked_version->major == 1 && linked_version->minor == 2)
101    {
102        if(linked_version->patch < 10)
103        {
104            windowWidth = 1280;
105            windowHeight = 1024;
106        }
107    }
108
109    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
110    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
111    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
112    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
113    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
114   
115    // set up the surface to render to
116    SDL_Surface* screen = SDL_SetVideoMode(windowWidth, windowHeight, bitDepth, SDL_OPENGL | SDL_FULLSCREEN | SDL_RESIZABLE);
117    if ( screen == NULL )
118    {
119        std::cerr<<"Unable to set "<<windowWidth<<"x"<<windowHeight<<" video: %s\n"<< SDL_GetError()<<std::endl;
120        exit(1);
121    }
122
123    SDL_EnableUNICODE(1);
124   
125    // If we used 0 to set the fields, query the values so we can pass it to osgViewer
126    windowWidth = screen->w;
127    windowHeight = screen->h;
128   
129    osgViewer::Viewer viewer;
130    osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = viewer.setUpViewerAsEmbeddedInWindow(0,0,windowWidth,windowHeight);
131    viewer.setSceneData(loadedModel.get());
132    viewer.setCameraManipulator(new osgGA::TrackballManipulator);
133    viewer.addEventHandler(new osgViewer::StatsHandler);
134    viewer.realize();
135
136    bool done = false;
137    while( !done )
138    {
139        SDL_Event event;
140
141        while ( SDL_PollEvent(&event) )
142        {
143            // pass the SDL event into the viewers event queue
144            convertEvent(event, *(gw->getEventQueue()));
145
146            switch (event.type) {
147
148                case SDL_VIDEORESIZE:
149                    SDL_SetVideoMode(event.resize.w, event.resize.h, bitDepth, SDL_OPENGL | SDL_RESIZABLE);
150                    gw->resized(0, 0, event.resize.w, event.resize.h );
151                    break;
152
153                case SDL_KEYUP:
154
155                    if (event.key.keysym.sym==SDLK_ESCAPE) done = true;
156                    if (event.key.keysym.sym=='f')
157                    {
158                        SDL_WM_ToggleFullScreen(screen);
159                        gw->resized(0, 0, screen->w, screen->h );
160                    }
161
162                    break;
163
164                case SDL_QUIT:
165                    done = true;
166            }
167        }
168
169        if (done) continue;
170
171
172        // draw the new frame
173        viewer.frame();
174
175        // Swap Buffers
176        SDL_GL_SwapBuffers();
177    }
178   
179    return 0;
180}
181
182/*EOF*/
Note: See TracBrowser for help on using the browser.