2022-08-29 21:01:55 +02:00
# ifndef MAIN_CPP
2022-08-28 19:20:00 +02:00
# include <glad/glad.h>
2022-08-27 21:43:58 +02:00
# include <GLFW/glfw3.h>
2022-08-28 19:20:00 +02:00
2022-08-29 19:53:36 +02:00
# include <glad/glad.h>
# include <GLFW/glfw3.h>
# include <iostream>
2022-08-30 20:02:22 +02:00
# include <random> // I am using standart library RNG because I am lazy and wanted to create quick code snippet
// upgrade to this: https://arvid.io/2018/06/30/on-cxx-random-number-generator-quality/ whenever, if ever I feel like it
# include <chrono> // for std::chrono
2022-08-29 21:01:55 +02:00
# include "constants.hpp"
void configureGLFW ( ) {
// first argument tells us what option to configure
// second is to what we set the value of this option
// see: https://www.glfw.org/docs/latest/window.html#window_hints
2022-08-29 19:53:36 +02:00
glfwWindowHint ( GLFW_CONTEXT_VERSION_MAJOR , 3 ) ;
2022-08-28 19:20:00 +02:00
glfwWindowHint ( GLFW_CONTEXT_VERSION_MINOR , 3 ) ;
glfwWindowHint ( GLFW_OPENGL_PROFILE , GLFW_OPENGL_CORE_PROFILE ) ;
2022-08-29 21:01:55 +02:00
// we set GLFW to 3.3 CORE
// core profile gives us access to smaller subset of OGL without backwards compatible features
// if we are on Mac OS X we need this for our code to work
# ifdef __APPLE__
glfwWindowHint ( GLFW_OPENGL_FORWARD_COMPAT , GL_TRUE ) ;
# endif
}
2022-08-28 19:20:00 +02:00
2022-08-29 21:01:55 +02:00
void instantiateGLFWwindow ( ) {
// Initialize GLFW
glfwInit ( ) ;
configureGLFW ( ) ;
}
GLFWwindow * createWindowObject ( ) {
// First two arguments are width and height
// Third is the name of the window
// We ignore last two
GLFWwindow * window = glfwCreateWindow ( constants : : MAIN_WINDOW_WIDTH , constants : : MAIN_WINDOW_HEIGHT , constants : : MAIN_WINDOW_NAME , NULL , NULL ) ;
return window ;
}
int initializeGLAD ( )
2022-08-29 19:53:36 +02:00
{
2022-08-29 21:01:55 +02:00
// we load address of OGL OS-specific function pointers
if ( ! gladLoadGLLoader ( ( GLADloadproc ) glfwGetProcAddress ) )
{
std : : cout < < " Failed to initialize GLAD " < < std : : endl ;
return - 1 ;
}
return 0 ;
2022-08-28 19:20:00 +02:00
}
2022-08-27 21:43:58 +02:00
2022-08-29 21:01:55 +02:00
// resizes viewport when user resizes window
void framebuffer_size_callback ( GLFWwindow * window , int width , int height )
{
glViewport ( 0 , 0 , width , height ) ;
}
2022-08-29 19:53:36 +02:00
2022-08-29 21:01:55 +02:00
void viewPort ( GLFWwindow * window )
2022-08-28 19:20:00 +02:00
{
2022-08-29 21:01:55 +02:00
// We tell OGL size of rendering window
// First two define left corner of window
// 3th and 4th width and height of rendering window
// we could set them to be smaller than window dimension, ogl rendering will be then displayed in smaller window
glViewport ( 0 , 0 , constants : : MAIN_WINDOW_WIDTH , constants : : MAIN_WINDOW_HEIGHT ) ;
// processed coordinates are between -1 and 1 so here we map:
// (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH) and (0, constants::MAIN_WINDOW_HEIGHT)
glfwSetFramebufferSizeCallback ( window , framebuffer_size_callback ) ;
// we call framebuffer_size_callback on every window resize
}
2022-08-30 20:02:22 +02:00
void processInput ( GLFWwindow * window )
{
// glfwGetKey takes window and key as an input and checks is currently being pressed
// if the user pressed escape we close window
if ( glfwGetKey ( window , GLFW_KEY_ESCAPE ) = = GLFW_PRESS )
glfwSetWindowShouldClose ( window , true ) ;
if ( glfwGetKey ( window , GLFW_KEY_C ) = = GLFW_PRESS )
glfwSetWindowShouldClose ( window , true ) ;
}
const float padaczkaSimulator ( )
{
// Seed our Mersenne Twister using the
std : : mt19937 mt { static_cast < unsigned int > (
std : : chrono : : steady_clock : : now ( ) . time_since_epoch ( ) . count ( )
) } ;
// Create a reusable random number generator that generates uniform numbers between 1 and 6
std : : uniform_int_distribution ten { 1 , 10 } ; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 };
const float MT_FLOAT = static_cast < float > ( ten ( mt ) % 10 ) ;
return MT_FLOAT / 10 ;
}
2022-08-29 21:01:55 +02:00
void renderLoop ( GLFWwindow * window )
{
2022-08-30 20:02:22 +02:00
// glfwWindowShouldClose checks if GLFW was instructed to close
2022-08-29 21:01:55 +02:00
while ( ! glfwWindowShouldClose ( window ) )
{
2022-08-30 20:02:22 +02:00
// input
processInput ( window ) ;
const float FIRST_COLOR = padaczkaSimulator ( ) ;
const float SECOND_COLOR = padaczkaSimulator ( ) ;
const float THIRD_COLOR = padaczkaSimulator ( ) ;
const float FOUR_COLOR = padaczkaSimulator ( ) ;
2022-08-30 20:04:17 +02:00
2022-08-30 20:02:22 +02:00
glClearColor ( FIRST_COLOR , SECOND_COLOR , THIRD_COLOR , FOUR_COLOR ) ;
glClear ( GL_COLOR_BUFFER_BIT ) ;
// swaps buffer containing color values of each pixel in window
// there is front buffer (final image) and back buffer (where all rendering commands draw to)
// when back buffer is ready we swap it with front buffer to eliminate flickering
2022-08-29 21:01:55 +02:00
glfwSwapBuffers ( window ) ;
2022-08-30 20:02:22 +02:00
// glfwPollEvents checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods)
2022-08-29 21:01:55 +02:00
glfwPollEvents ( ) ;
2022-08-30 20:02:22 +02:00
2022-08-29 21:01:55 +02:00
}
}
int main ( )
{
instantiateGLFWwindow ( ) ;
GLFWwindow * window = createWindowObject ( ) ;
// function returns GLFWWindow object
if ( window = = NULL )
{
std : : cout < < " Failed to create GLFW window " < < std : : endl ;
glfwTerminate ( ) ;
return - 1 ;
}
// we make context of this window main context of current thread
glfwMakeContextCurrent ( window ) ;
if ( initializeGLAD ( ) = = - 1 ) return - 1 ;
viewPort ( window ) ;
renderLoop ( window ) ;
2022-08-30 20:02:22 +02:00
// clean GLFW resources
glfwTerminate ( ) ;
2022-08-28 19:20:00 +02:00
return 0 ;
2022-08-29 21:01:55 +02:00
}
# endif