Repository navigation
Expand file tree
/
Copy pathMain.cpp
More file actions
143 lines (119 loc) · 5.53 KB
/
Copy pathMain.cpp
File metadata and controls
143 lines (119 loc) · 5.53 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////// Author: Brennen Romo ////////////////
//////////////// Date: April 7th, 2023 ////////////////
//////////////// Project: Windows Renderer ////////////////
//////////////// Description: This Program creates a window, which we can resize, set the title of,Maximize/minimize with ////////////////
//////////////// the Space Bar ////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
void windowSizeCallback(GLFWwindow* window, int width, int height);
void windowCloseCallback(GLFWwindow* window);
void keyboardInput(GLFWwindow* window);
void framebufferSizeCallback(GLFWwindow* window, int width, int height);
// settings
const int screenHeight = 1200;
const int screenWidth = 1600;
int main()
{
//initialize GLFW
if (!glfwInit())
{
//close program if we've failed to run GLFW
std::cout << "failed to initialize GLFW";
return -1;
}
//Definitions for when we are using Apple device
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
//Set GLFW Window Parameters here (Using version 4.6)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
//using Core profile of OpenGL, (Compatibility profile is for legacy)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//////////////////////////create Fullscreen Window here////////////////////////
// GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "Render Window", glfwGetPrimaryMonitor(), NULL);
//////////////////////////create Windowed fullscreen here////////////////////////
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
GLFWwindow* window = glfwCreateWindow(mode->width, mode->height, "Render Window", NULL, NULL);
////////////////////////create window here////////////////////////////////
// GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "Render Window", NULL, NULL);
if (!window)
{
std::cout << "Error Creating our GLFW Window!" << std::endl;
//close down GLFW
glfwTerminate();
return -1;
}
// Make our Window the current Context
glfwMakeContextCurrent(window);
/////////////////////////////////////////////////////////////////////////////////
// /////////////////////////// Set Callback Functions Here ///////////////////////////
// /////////////////////////////////////////////////////////////////////////////////
//Sets the Callback function for when the frameBuffer is resized to our custom Callback function.
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
//Set callback Function for when Window is resized
glfwSetWindowSizeCallback(window, windowSizeCallback);
//set callback function for when we close glfw Window
glfwSetWindowCloseCallback(window, windowCloseCallback);
// Start glad and load all openGL function pointers (for whichever specific system and archritecture our program is running on)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// Start Render Loop here
do
{
//input
keyboardInput(window);
//clear the backbuffer to set colour
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Swap buffers
glfwSwapBuffers(window);
// poll windows events, call callback functions for events
glfwPollEvents();
} while (!glfwWindowShouldClose(window));
// close GLFW
glfwTerminate();
return 0;
}
// Function for Processing Inputs
void keyboardInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) //if Escape key is pressed, close Window
glfwSetWindowShouldClose(window, true);
else if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) //if Escape key is pressed, close Window
{
int maximized = glfwGetWindowAttrib(window, GLFW_MAXIMIZED);
if (!maximized)
glfwMaximizeWindow(window);
else
glfwRestoreWindow(window);
}
}
//Function callback for changing our Window's size
void windowSizeCallback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
std::cout << "Width: " << width << "\n" << "Height: " << height << "\n";
}
//Function callback for closing our Window
void windowCloseCallback(GLFWwindow* window)
{
std::cout << "Goodbye!";
}
//Function for when screen is resized
void framebufferSizeCallback(GLFWwindow* window, int width, int height)
{
//Set viewport size to new viewport size.
glViewport(0, 0, width, height);
}