11
2+ #include < iostream>
23#include < algorithm>
34#include < random>
45
6+ #include < glm/vec2.hpp>
7+
58#include < glbinding/gl/gl.h>
69#include < glbinding/ContextInfo.h>
710#include < glbinding/Version.h>
1720
1821
1922using namespace gl ;
20- using namespace globjects ;
21-
22-
23- namespace {
24- bool g_toggleFS = false ;
25- bool g_isFS = false ;
26-
27- Texture * g_texture = nullptr ;
28- ScreenAlignedQuad * g_quad = nullptr ;
29- }
3023
3124
32- void key_callback (GLFWwindow * window, int key, int /* scancode */ , int action, int /* modes */ )
25+ namespace
3326{
34- if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
35- glfwSetWindowShouldClose (window, true );
36-
37- if (key == GLFW_KEY_F5 && action == GLFW_RELEASE)
38- File::reloadAll ();
39-
40- if (key == GLFW_KEY_F11 && action == GLFW_RELEASE)
41- g_toggleFS = true ;
42- }
43-
44- GLFWwindow * createWindow (bool fs = false )
45- {
46- // Set GLFW window hints
47- glfwSetErrorCallback ( [] (int /* error*/ , const char * description) { puts (description); } );
48- glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3 );
49- glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1 );
50- glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, true );
51-
52- // Create a context and, if valid, make it current
53- GLFWwindow * window = glfwCreateWindow (1024 , 768 , " " , fs ? glfwGetPrimaryMonitor () : NULL , NULL );
54- if (window == nullptr )
55- {
56- critical () << " Context creation failed. Terminate execution." ;
57-
58- glfwTerminate ();
59- exit (1 );
60- }
61- glfwMakeContextCurrent (window);
62-
63- // Create callback that when user presses ESC, the context should be destroyed and window closed
64- glfwSetKeyCallback (window, key_callback);
65-
66- // Initialize globjects (internally initializes glbinding, and registers the current context)
67- globjects::init ();
68-
69- // Do only on startup
70- if (!g_toggleFS)
71- {
72- // Dump information about context and graphics card
73- info () << std::endl
74- << " OpenGL Version: " << glbinding::ContextInfo::version () << std::endl
75- << " OpenGL Vendor: " << glbinding::ContextInfo::vendor () << std::endl
76- << " OpenGL Renderer: " << glbinding::ContextInfo::renderer () << std::endl;
77- }
78-
79- glClearColor (0 .2f , 0 .3f , 0 .4f , 1 .f );
27+ globjects::Texture * g_texture = nullptr ;
28+ ScreenAlignedQuad * g_quad = nullptr ;
8029
81- g_isFS = fs;
82- return window;
30+ auto g_size = glm::ivec2{};
8331}
8432
85- void destroyWindow (GLFWwindow * window)
86- {
87- globjects::detachAllObjects ();
88- glfwDestroyWindow (window);
89- }
9033
9134void initialize ()
9235{
@@ -104,7 +47,7 @@ void initialize()
10447 for (int i = 0 ; i < w * h * 4 ; ++i)
10548 data[i] = static_cast <unsigned char >(255 - static_cast <unsigned char >(r (generator) * 255 ));
10649
107- g_texture = Texture::createDefault (GL_TEXTURE_2D);
50+ g_texture = globjects:: Texture::createDefault (GL_TEXTURE_2D);
10851 g_texture->ref ();
10952 g_texture->image2D (0 , GL_RGBA8, w, h, 0 , GL_RGBA, GL_UNSIGNED_BYTE, data);
11053
@@ -123,37 +66,79 @@ void draw()
12366{
12467 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
12568
69+ glViewport (0 , 0 , g_size.x , g_size.y );
12670 g_quad->draw ();
12771}
12872
12973
74+ void error (int errnum, const char * errmsg)
75+ {
76+ globjects::critical () << errnum << " : " << errmsg << std::endl;
77+ }
78+
79+ void framebuffer_size_callback (GLFWwindow * /* window*/ , int width, int height)
80+ {
81+ g_size = glm::ivec2{ width, height };
82+ }
83+
84+ void key_callback (GLFWwindow * window, int key, int /* scancode*/ , int action, int /* modes*/ )
85+ {
86+ if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
87+ glfwSetWindowShouldClose (window, true );
88+
89+ if (key == GLFW_KEY_F5 && action == GLFW_RELEASE)
90+ globjects::File::reloadAll ();
91+ }
92+
93+
13094int main (int /* argc*/ , char * /* argv*/ [])
13195{
13296 // Initialize GLFW
133- glfwInit ();
97+ if (!glfwInit ())
98+ return 1 ;
99+
100+ glfwSetErrorCallback (error);
101+ glfwDefaultWindowHints ();
102+
103+ glfwSetErrorCallback ([](int /* error*/ , const char * description) { puts (description); });
104+ glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3 );
105+ glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1 );
106+ glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, true );
134107
135- GLFWwindow * window = createWindow ();
108+ // Create a context and, if valid, make it current
109+ GLFWwindow * window = glfwCreateWindow (640 , 480 , " globjects Texture" , NULL , NULL );
110+ if (window == nullptr )
111+ {
112+ globjects::critical () << " Context creation failed. Terminate execution." ;
113+
114+ glfwTerminate ();
115+ return -1 ;
116+ }
117+ glfwSetKeyCallback (window, key_callback);
118+ glfwSetFramebufferSizeCallback (window, framebuffer_size_callback);
119+
120+ glfwMakeContextCurrent (window);
121+
122+ // Initialize globjects (internally initializes glbinding, and registers the current context)
123+ globjects::init ();
124+
125+ std::cout << std::endl
126+ << " OpenGL Version: " << glbinding::ContextInfo::version () << std::endl
127+ << " OpenGL Vendor: " << glbinding::ContextInfo::vendor () << std::endl
128+ << " OpenGL Renderer: " << glbinding::ContextInfo::renderer () << std::endl << std::endl;
129+
130+ globjects::info () << " Press F5 to reload shaders." << std::endl << std::endl;
131+
132+ glfwGetFramebufferSize (window, &g_size[0 ], &g_size[1 ]);
136133 initialize ();
137134
138135 // Main loop
139136 while (!glfwWindowShouldClose (window))
140137 {
141138 glfwPollEvents ();
142-
143- if (g_toggleFS)
144- {
145- deinitialize ();
146- destroyWindow (window);
147- window = createWindow (!g_isFS);
148- initialize ();
149-
150- g_toggleFS = false ;
151- }
152-
153139 draw ();
154140 glfwSwapBuffers (window);
155141 }
156-
157142 deinitialize ();
158143
159144 // Properly shutdown GLFW
0 commit comments