My favorites | Sign in
Project Logo
                
Repository:
Checkout | Browse | Changes | Clones |
 
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
#include <wm/wm.hpp>

#include <cmath>

#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>

#include <wm/opengl/opengl.hpp>

class Application
{
public:
Application(const wm::PixelFormat::Descriptor &desc)
: display(0)
, config(display)
, format(choose(config, desc))
, win(*this, display, format)
, model(Model())
, quit(false)
{
}

void run()
{
win.window.show();

namespace pt = boost::posix_time;
const pt::time_duration interval = pt::seconds(1) / 60,
update_interval = pt::seconds(1) / 120;
const float update_dt = 1.0f / 120.0f;
pt::ptime next_redraw = pt::microsec_clock::local_time(),
next_update = next_redraw;
while(!quit)
{
pt::ptime timer = pt::microsec_clock::local_time();
if(timer >= next_update)
{
for(pt::time_iterator iter(next_update, update_interval);
iter < timer;
++iter)
{
next_update = *iter + update_interval;
model.update(update_dt);
}
} else if(timer >= next_redraw)
{
win.draw();
next_redraw = timer + interval;
} else
{
display.poll();
win.window.dispatch();

#undef min // Damn windows.h macros
#undef max
boost::this_thread::sleep(
std::max(
std::min(next_redraw, next_update) - pt::microsec_clock::local_time(),
pt::time_duration(pt::milliseconds(1))
));
}
}
}

private:
wm::Display display;
wm::Configuration config;
const wm::PixelFormat &format;

struct EventHandler : public wm::EventHandler
{
explicit EventHandler(Application &app) : app(app) { }
Application &app;

virtual void handle(const wm::CloseEvent&)
{
app.quit = true;
}

virtual void handle(const wm::KeyEvent& event)
{
if(event.state() && event.symbol() == wm::keyboard::ESCAPE)
app.quit = true;
}
};

struct AppWindow
{
AppWindow(Application &app, wm::Display &display, const wm::PixelFormat& format)
: app(app)
, window(display, 400, 300, format)
, context(format)
, surface(window)
, handler(app)
, connection(window, handler, false)
{
connection.connect();
}

void draw()
{
unsigned int width, height;
window.getSize(width, height);

wm::CurrentContext current(context, surface, surface);

glViewport(0, 0, width, height);

glClearColor(0.2f, 0.4f, 0.9f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(std::fmod(app.model.gametime * 360.0f, 360.0f), 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex2f(0.0f, 0.5f);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();

surface.swap();
}

Application &app;
wm::Window window;
wm::Context context;
wm::Surface surface;
EventHandler handler;
wm::Connection connection;
} win;

struct Model
{
Model() : gametime(0) { }
void update(float dt) { gametime += dt; }
float gametime;
} model;

bool quit;
};

#include <iostream>

int wm_main(int, char*[])
{
try
{
Application app(wm::PixelFormat::Descriptor(0, 0, 0, 0, 0, 0));
app.run();
} catch(wm::Exception &e)
{
std::cerr << "wm::Exception: " << e.what() << std::endl;
return -1;
}

return 0;
}

Show details Hide details

Change log

43627a17d2 by Riku Salminen (rsalmin2 at cc.hut.fi) on Nov 26, 2009   Diff
Fixed GCC compiler warnings, Win32
implementation should be checked for
warnings later
Go to: 
Project members, sign in to write a code review

Older revisions

e1177ff8a1 by Riku Salminen (rsalmin2 at cc.hut.fi) on Nov 25, 2009   Diff
Removed makeCurrent in favor of RAII-
based CurrentContext class, Win32
broken
33b4b717ac by Riku Salminen <rsalmin2 at cc.hut.fi> on Aug 04, 2009   Diff
Changed Window:: and Display::dispatch
to Display::wait, ::poll, Window::wait
and ::dispatch, Xlib broken
9cfd757c0d by Riku Salminen <rsalmin2 at cc.hut.fi> on Aug 04, 2009   Diff
Window::repaint for Win32, got rid of
some double->float truncation MSVC++
warnings in test applications
All revisions of this file

File info

Size: 3340 bytes, 159 lines
Hosted by Google Code