File: osgwidgetperformance.cpp

package info (click to toggle)
openscenegraph 3.2.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 32,820 kB
  • ctags: 34,610
  • sloc: cpp: 370,040; ansic: 9,071; java: 1,020; yacc: 548; objc: 288; makefile: 285; xml: 155; lex: 151
file content (154 lines) | stat: -rw-r--r-- 4,134 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <osg/ArgumentParser>
#include <osgDB/ReadFile>
#include <osgWidget/Canvas>
#include <osgWidget/WindowManager>
#include <osgWidget/Util>

void setupArguments(osg::ArgumentParser& args) {
    args.getApplicationUsage()->setDescription(
        args.getApplicationName() + " is a performance testing application for osgWidget."
    );

    args.getApplicationUsage()->setCommandLineUsage(
        args.getApplicationName() + " [options] widgets"
    );

    args.getApplicationUsage()->addCommandLineOption(
        "--width <int>",
        "The WindowManager width."
    );

    args.getApplicationUsage()->addCommandLineOption(
        "--height <int>",
        "The WindowManager height."
    );

    args.getApplicationUsage()->addCommandLineOption(
        "--size <int>",
        "The size of the square Widgets."
    );

    args.getApplicationUsage()->addCommandLineOption(
        "--single-window",
        "All widgets are put inside a single Window."
    );

    args.getApplicationUsage()->addCommandLineOption(
        "--multi-window",
        "All widgets are in their own Windows."
    );
}

void readSize(osg::ArgumentParser& args, const char* opt, unsigned int& val) {
    std::string size;

    while(args.read(opt, size)) {
        int s = std::atoi(size.c_str());

        if(s > 0) val = s;
    }
}

int doError(const char* errorMsg) {
    osgWidget::warn() << errorMsg << std::endl;

    return 1;
}

int doApp(osgViewer::Viewer& viewer, osg::Node* node, unsigned int width, unsigned int height) {
    osgWidget::WindowManager* wm = new osgWidget::WindowManager(&viewer, width, height, 0x12);

    wm->addChild(node);

    return osgWidget::createExample(viewer, wm);
}

int main(int argc, char** argv) {
    osg::ArgumentParser args(&argc, argv);

    setupArguments(args);

    osgViewer::Viewer viewer(args);

    while(args.read("--help")) {
        args.getApplicationUsage()->write(
            std::cout,
            osg::ApplicationUsage::COMMAND_LINE_OPTION
        );

        return 0;
    }

    std::string size;

    unsigned int width        = 1280;
    unsigned int height       = 1024;
    unsigned int wSize        = 10;
    bool         singleWindow = false;
    bool         multiWindow  = false;

    readSize(args, "--width", width);
    readSize(args, "--height", height);
    readSize(args, "--size", wSize);

    while(args.read("--single-window")) singleWindow = true;
    
    while(args.read("--multi-window")) multiWindow = true;

    unsigned int numWidgets = 0;

    if(args.argc() >= 2) numWidgets = std::atoi(args[1]);

    else return doError("Please specify the number of Widgets to use.");

    if(numWidgets <= 0) return doError("Please specify one or more Widgets to use.");

    if(!singleWindow && !multiWindow) return doError(
        "Please specify one of --single-window or --multi-window."
    );

    if(singleWindow) {
        osgWidget::Canvas* canvas = new osgWidget::Canvas("canvas");

        canvas->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f);

        unsigned int rows = height / (wSize + 2);
        unsigned int cols = (numWidgets / rows) + 1;
        unsigned int w    = 0;

        /*
        osg::Image*     image   = osgDB::readImageFile("osgWidget/natascha.png");
        osg::Texture2D* texture = new osg::Texture2D();

        texture->setImage(0, image);
        */

        for(unsigned int c = 0; c < cols; c++) {
            for(unsigned int r = 0; r < rows; r++) {
                if(w >= numWidgets) break;

                osgWidget::Widget* widget = new osgWidget::Widget(
                    "",
                    wSize,
                    wSize
                );

                float col = static_cast<float>(w) / static_cast<float>(numWidgets);

                widget->setColor(col, col, col, 0.9f);
                // widget->setTexture(texture, true);

                canvas->addWidget(widget, c * (wSize + 2), r * (wSize + 2));

                w++;
            }
        }

        return doApp(viewer, canvas, width, height);
    }

    else doError("Not supported yet.");

    return 1;
}