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

Revision 6941, 5.6 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • 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                    gw->resized(0, 0, event.resize.w, event.resize.h );
150                    break;
151
152                case SDL_KEYUP:
153
154                    if (event.key.keysym.sym==SDLK_ESCAPE) done = true;
155                    if (event.key.keysym.sym=='f')
156                    {
157                        SDL_WM_ToggleFullScreen(screen);
158                        gw->resized(0, 0, screen->w, screen->h );
159                    }
160
161                    break;
162
163                case SDL_QUIT:
164                    done = true;
165            }
166        }
167
168        if (done) continue;
169
170
171        // draw the new frame
172        viewer.frame();
173
174        // Swap Buffers
175        SDL_GL_SwapBuffers();
176    }
177   
178    return 0;
179}
180
181/*EOF*/
Note: See TracBrowser for help on using the browser.