aboutsummaryrefslogtreecommitdiff
path: root/src/rms.cpp
blob: a9bb166a1bb373c53e61b61a3309c03365930b31 (plain)
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
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "rms.hpp"

APP_CONTEXT *AppContext=nullptr;

void OnWindowResize(GLFWwindow* window, int width, int height){
	AppContext->renderer.AjustViewport(width,height);
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_R && action == GLFW_PRESS)
    	AppContext->renderer.RefreshShader();
}

int main(int argc, char *argv[])
{
   GLFWwindow* window;

	/* Initialize the library */
	if (!glfwInit())
		return -1;

	/* Create a windowed mode window and its OpenGL context */
	window = glfwCreateWindow(WIDTH, HEIGHT, "SFML/OpenGL Ray Marching", NULL, NULL);
	if (!window)
	{
		glfwTerminate();
		return -1;
	}

	/* Make the window's context current */
	glfwMakeContextCurrent(window);

	// Init Renderer/OpenGL
	APP_CONTEXT InitContext={
			Renderer(WIDTH,HEIGHT,"main.frag"),
			HUD(window)
	};
	AppContext=&InitContext;
	InitContext.renderer.RefreshShader();


	glfwSetWindowSizeCallback(window, OnWindowResize);
	glfwSetKeyCallback(window, key_callback);

	/* Loop until the user closes the window */
	double InitTime = glfwGetTime();
	double LastTime = InitTime;
	while (!glfwWindowShouldClose(window))
	{
		double CurrentTime = glfwGetTime();
		AppContext->hud.m_Context->time=CurrentTime-InitTime;

		/* Render here */
		glClear(GL_COLOR_BUFFER_BIT);

		if(CurrentTime-LastTime >= 1){
			AppContext->hud.m_Context->fps=AppContext->hud.m_Context->fps_frame_counter;
			AppContext->hud.m_Context->fps_frame_counter=0;
			LastTime=CurrentTime;
		}
		AppContext->renderer.Render();
		AppContext->hud.Render();

		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();
		AppContext->hud.m_Context->fps_frame_counter++;
	}

	glfwTerminate();
	return 0;

}