OGLplus  (0.59.0) a C++ wrapper for rendering APIs

external/glfw3_glew_clear/main.cpp

Copyright Matus Chochlik. Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>
static void run_loop(GLFWwindow* window, int width, int height) {
using namespace eagine::oglp;
gl_api gl;
if(gl.operations().clear) {
gl.clear_color(0.3F, 0.3F, 0.9F, 0.0F);
gl.clear_depth(1);
while(true) {
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, 1);
break;
}
if(glfwWindowShouldClose(window)) {
break;
}
int new_width, new_height;
glfwGetWindowSize(window, &new_width, &new_height);
if((width != new_width) || (height != new_height)) {
width = new_width;
height = new_height;
}
gl.viewport(width, height);
gl().clear(gl.color_buffer_bit | gl.depth_buffer_bit);
glfwSwapBuffers(window);
}
} else {
std::cout << "missing required API" << std::endl;
}
}
static void init_and_run() {
if(!glfwInit()) {
throw std::runtime_error("GLFW initialization error");
} else {
auto ensure_glfw_cleanup = eagine::finally(glfwTerminate);
glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE);
glfwWindowHint(GLFW_RED_BITS, 8);
glfwWindowHint(GLFW_BLUE_BITS, 8);
glfwWindowHint(GLFW_GREEN_BITS, 8);
glfwWindowHint(GLFW_ALPHA_BITS, 0);
glfwWindowHint(GLFW_DEPTH_BITS, 24);
glfwWindowHint(GLFW_STENCIL_BITS, 0);
glfwWindowHint(GLFW_SAMPLES, GLFW_DONT_CARE);
int width = 800, height = 600;
GLFWwindow* window =
glfwCreateWindow(width, height, "OGLplus example", nullptr, nullptr);
if(!window) {
throw std::runtime_error("Error creating GLFW window");
} else {
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
GLenum init_result = glewInit();
glGetError();
if(init_result != GLEW_OK) {
throw std::runtime_error("OpenGL/GLEW initialization error.");
} else {
run_loop(window, width, height);
}
}
}
}
auto main() -> int {
try {
init_and_run();
return 0;
} catch(std::runtime_error& sre) {
std::cerr << "Runtime error: " << sre.what() << std::endl;
} catch(std::exception& se) {
std::cerr << "Unknown error: " << se.what() << std::endl;
}
return 1;
}
opt_c_api_constant< mp_list< buffer_clear_bit, buffer_blit_bit >, bitfield_type_i > depth_buffer_bit
Definition: constants.hpp:6149
static auto finally(Func func) -> func_on_scope_exit< Func >
Function constructing on-scope-exit actions.
Definition: scope_exit.hpp:144
Combined wrapper for the GL API operations and constants.
Definition: basic_gl_api.hpp:27
opt_c_api_constant< mp_list< buffer_clear_bit, buffer_blit_bit >, bitfield_type_i > color_buffer_bit
Definition: constants.hpp:6138
auto operations() noexcept -> basic_gl_operations< ApiTraits > &
Returns a reference to the wrapped operations.
Definition: basic_gl_api.hpp:52

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).