9 #ifndef OGLPLUS_STANDALONE_GLUT_GLEW_EXAMPLE_1203161253_HPP
10 #define OGLPLUS_STANDALONE_GLUT_GLEW_EXAMPLE_1203161253_HPP
14 #include <oglplus/config/gl.hpp>
16 #if OGLPLUS_USE_FREEGLUT
17 # include <GL/freeglut.h>
32 class StandaloneExample
35 std::chrono::time_point<std::chrono::system_clock> _start;
36 std::chrono::time_point<std::chrono::system_clock> _now;
37 double _frame_time, _frame_duration;
38 unsigned long _frame_number;
40 GLuint _width, _height;
42 GLuint _prev_mouse_x, _prev_mouse_y;
43 GLuint _curr_mouse_x, _curr_mouse_y;
47 void HandleUpdate(
void)
49 static const double period =
50 double(std::chrono::system_clock::period::num)/
51 double(std::chrono::system_clock::period::den);
52 auto now = std::chrono::system_clock::now();
53 _frame_duration = (now - _now).count() * period;
55 _frame_time = (_now - _start).count() * period;
59 void HandleResize(GLuint width, GLuint height)
65 void HandleMouseMove(GLuint mouse_x, GLuint mouse_y)
67 _prev_mouse_x = _curr_mouse_x;
68 _prev_mouse_y = _curr_mouse_y;;
69 _curr_mouse_x = mouse_x;
70 _curr_mouse_y = mouse_y;
73 void HandleKeyPress(GLuint key)
79 double FrameTime(
void)
const {
return _frame_time; }
80 double FrameDuration(
void)
const {
return _frame_duration; }
81 unsigned long FrameNumber(
void)
const {
return _frame_number; }
83 double CurrentFPS(
void)
const {
return 1.0 / _frame_duration; }
84 double AverageFPS(
void)
const {
return _frame_number / _frame_time; }
86 GLuint
Width(
void)
const {
return _width; }
87 GLuint
Height(
void)
const {
return _height; }
88 double Aspect(
void)
const {
return double(_width)/double(_height); }
90 GLuint MouseX(
void)
const {
return _curr_mouse_x; }
91 GLuint MouseY(
void)
const {
return _curr_mouse_y; }
93 int MouseDiffX(
void)
const
95 return _curr_mouse_x - _prev_mouse_x;
98 int MouseDiffY(
void)
const
100 return _curr_mouse_y - _prev_mouse_y;
103 double NormMouseX(
void)
const
105 return double(2*_curr_mouse_x)/_width - 1.0;
108 double NormMouseY(
void)
const
110 return double(2*_curr_mouse_y)/_height - 1.0;
113 double NormMouseDiffX(
void)
const
115 return double(2*MouseDiffX())/_width;
118 double NormMouseDiffY(
void)
const
120 return double(2*MouseDiffY())/_height;
123 GLuint Key(
void)
const {
return _curr_key; }
126 StandaloneExample(
const StandaloneExample&);
129 StandaloneExample(
void)
134 virtual ~StandaloneExample(
void)
137 void Startup(GLuint width, GLuint height)
139 HandleResize(width, height);
142 HandleMouseMove(_width/2, _height/2);
143 HandleMouseMove(_width/2, _height/2);
146 _start = std::chrono::system_clock::now();
151 virtual void Render(
void) = 0;
153 virtual void Reshape(
void) = 0;
155 virtual void Motion(
void)
159 virtual void PassiveMotion(
void)
163 virtual void KeyPress(
void)
168 template <
typename Example>
169 class SingleExampleTpl
172 static Example*& SingleInstance(
void)
174 static Example* _ptr =
nullptr;
178 SingleExampleTpl(
int argc,
const char** argv)
180 assert(!SingleInstance());
181 SingleInstance() =
new Example(argc, argv);
184 ~SingleExampleTpl(
void)
186 delete SingleInstance();
189 void Startup(GLuint width, GLuint height)
191 assert(SingleInstance());
192 SingleInstance()->Startup(width, height);
195 static void CloseFunc(
void)
197 assert(SingleInstance());
198 delete SingleInstance();
199 SingleInstance() =
nullptr;
202 static void DisplayFunc(
void)
204 assert(SingleInstance());
205 SingleInstance()->HandleUpdate();
206 SingleInstance()->Render();
210 static void ReshapeFunc(
int width,
int height)
212 assert(SingleInstance());
213 SingleInstance()->HandleResize(width, height);
214 SingleInstance()->Reshape();
217 static void MotionFunc(
int x,
int y)
219 assert(SingleInstance());
220 SingleInstance()->HandleMouseMove(x, y);
221 SingleInstance()->Motion();
224 static void PassiveMotionFunc(
int x,
int y)
226 assert(SingleInstance());
227 SingleInstance()->HandleMouseMove(x, y);
228 SingleInstance()->PassiveMotion();
231 static void KeyboardFunc(
unsigned char k,
int,
int)
235 #if OGLPLUS_USE_FREEGLUT
243 assert(SingleInstance());
244 SingleInstance()->HandleKeyPress(k);
245 SingleInstance()->KeyPress();
258 const char* window_title,
263 glutInit(&argc, argv);
272 glutInitWindowPosition(xpos, ypos);
273 glutInitWindowSize(width, height);
275 glutCreateWindow(window_title);
284 if(glewInit() != GLEW_OK)
286 throw std::runtime_error(
287 "GLEW initialization error"
294 template <
typename Example>
295 class GlutGlewExampleApp
300 typedef SingleExampleTpl<Example> SingleExample;
301 SingleExample example;
303 GlutGlewExampleApp(
const GlutGlewExampleApp&);
310 const char* window_title,
322 , example(argc, const_cast<const char**>(argv))
324 glutReshapeFunc(&SingleExample::ReshapeFunc);
326 glutMotionFunc(&SingleExample::MotionFunc);
327 glutPassiveMotionFunc(&SingleExample::PassiveMotionFunc);
329 glutKeyboardFunc(&SingleExample::KeyboardFunc);
331 glutDisplayFunc(&SingleExample::DisplayFunc);
332 glutIdleFunc(&SingleExample::DisplayFunc);
334 example.Startup(width, height);
337 void Run(
int ,
const char** )
339 #if OGLPLUS_USE_FREEGLUT
341 GLUT_ACTION_ON_WINDOW_CLOSE,
342 GLUT_ACTION_GLUTMAINLOOP_RETURNS
344 glutCloseFunc(&SingleExample::CloseFunc);
350 template <
typename Example>
351 int GlutGlewMain(
const char* title,
int argc,
char* argv[])
355 std::cout <<
"Started" << std::endl;
357 GLuint width = 800, height = 600;
358 GlutGlewExampleApp<Example> app(
364 app.Run(argc, const_cast<const char**>(argv));
366 std::cout <<
"Finished" << std::endl;
372 <<
"Program build error (in "
405 <<
"Object error (in "
434 catch(std::exception& se)
436 std::cerr <<
"Error: " << se.what() << std::endl;
Exception indicating exceeded implementation-defined limits.
Definition: limit.hpp:30
const String & ObjectDesc(void) const
Object textual description.
GLint ObjectName(void) const
Object GL name.
Base class for program compilation or linking errors.
Definition: program.hpp:27
const char * ObjectTypeName(void) const
Returns the class name.
Exception class for general OpenGL errors.
Definition: basic.hpp:43
Exception class for GL object-related errors.
Definition: object.hpp:24
const char * EnumParamName(void) const
Returns the name of the enumeration parameter related to the error.