summaryrefslogtreecommitdiff
path: root/examples/images.cpp
blob: 8e8201d0c76271ba3fd41aabfc2f9af398a1675a (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * DISTRHO Plugin Framework (DPF)
 * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
 *
 * Permission to use, copy, modify, and/or distribute this software for any purpose with
 * or without fee is hereby granted, provided that the above copyright notice and this
 * permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

// ------------------------------------------------------
// Pics

#include "images_src/CatPics.cpp"

// ------------------------------------------------------
// DGL Stuff

#include "Image.hpp"
#include "Widget.hpp"
#include "StandaloneWindow.hpp"

// ------------------------------------------------------
// use namespace

using namespace DGL;

// ------------------------------------------------------
// our widget

class ExampleImagesWidget : public App::IdleCallback,
                                   Widget
{
public:
    static const int kImg1y = 0;
    static const int kImg2y = 500/2-CatPics::cat2Height/2;
    static const int kImg3x = 400/3-CatPics::cat3Width/3;

    static const int kImg1max = 500-CatPics::cat1Width;
    static const int kImg2max = 500-CatPics::cat2Width;
    static const int kImg3max = 400-CatPics::cat3Height;

    ExampleImagesWidget(Window& win)
        : Widget(win),
          fImgTop1st(1),
          fImgTop2nd(2),
          fImgTop3rd(3),
          fImg1x(0),
          fImg2x(kImg2max),
          fImg3y(kImg3max),
          fImg1rev(false),
          fImg2rev(true),
          fImg3rev(true),
          fImg1(CatPics::cat1Data, CatPics::cat1Width, CatPics::cat1Height, GL_BGR),
          fImg2(CatPics::cat2Data, CatPics::cat2Width, CatPics::cat2Height, GL_BGR),
          fImg3(CatPics::cat3Data, CatPics::cat3Width, CatPics::cat3Height, GL_BGR)
    {
    }

private:
    void idleCallback() override
    {
        if (fImg1rev)
        {
            fImg1x -= 2;
            if (fImg1x <= -50)
            {
                fImg1rev = false;
                setNewTopImg(1);
            }
        }
        else
        {
            fImg1x += 2;
            if (fImg1x >= kImg1max+50)
            {
                fImg1rev = true;
                setNewTopImg(1);
            }
        }

        if (fImg2rev)
        {
            fImg2x -= 1;
            if (fImg2x <= -50)
            {
                fImg2rev = false;
                setNewTopImg(2);
            }
        }
        else
        {
            fImg2x += 4;
            if (fImg2x >= kImg2max+50)
            {
                fImg2rev = true;
                setNewTopImg(2);
            }
        }

        if (fImg3rev)
        {
            fImg3y -= 3;
            if (fImg3y <= -50)
            {
                fImg3rev = false;
                setNewTopImg(3);
            }
        }
        else
        {
            fImg3y += 3;
            if (fImg3y >= kImg3max+50)
            {
                fImg3rev = true;
                setNewTopImg(3);
            }
        }

        repaint();
    }

    void setNewTopImg(const int imgId)
    {
        if (fImgTop1st == imgId)
            return;

        if (fImgTop2nd == imgId)
        {
            fImgTop2nd = fImgTop1st;
            fImgTop1st =  imgId;
            return;
        }

        fImgTop3rd = fImgTop2nd;
        fImgTop2nd = fImgTop1st;
        fImgTop1st =  imgId;
    }

    void onDisplay() override
    {
        switch (fImgTop3rd)
        {
        case 1:
            fImg1.draw(fImg1x, kImg1y);
            break;
        case 2:
            fImg2.draw(fImg2x, kImg2y);
            break;
        case 3:
            fImg3.draw(kImg3x, fImg3y);
            break;
        };

        switch (fImgTop2nd)
        {
        case 1:
            fImg1.draw(fImg1x, kImg1y);
            break;
        case 2:
            fImg2.draw(fImg2x, kImg2y);
            break;
        case 3:
            fImg3.draw(kImg3x, fImg3y);
            break;
        };

        switch (fImgTop1st)
        {
        case 1:
            fImg1.draw(fImg1x, kImg1y);
            break;
        case 2:
            fImg2.draw(fImg2x, kImg2y);
            break;
        case 3:
            fImg3.draw(kImg3x, fImg3y);
            break;
        };
    }

    int fImgTop1st, fImgTop2nd, fImgTop3rd;
    int fImg1x, fImg2x, fImg3y;
    bool fImg1rev, fImg2rev, fImg3rev;
    Image fImg1, fImg2, fImg3;
};

// ------------------------------------------------------
// main entry point

int main()
{
    App app;
    Window win(app);
    ExampleImagesWidget images(win);

    app.addIdleCallback(&images);

    win.setResizable(false);
    win.setSize(500, 400);
    win.setTitle("Images");
    win.show();
    app.exec();

    return 0;
}

// ------------------------------------------------------