Commit b1c2935a authored by Srb František's avatar Srb František
Browse files

Init divided into functions

Fix #4
parent 652bf1ab
...@@ -34,25 +34,30 @@ const char* fragmentShaderSource = "#version 330 core\n" ...@@ -34,25 +34,30 @@ const char* fragmentShaderSource = "#version 330 core\n"
" FragColor = vertexColor;\n" " FragColor = vertexColor;\n"
"}\n\0"; "}\n\0";
//Main that creates playable window void showHighscoreTable(long player_time,int player_level) {
int main() auto highscore_module = HighscoreModule("highscore.txt");
{ highscore_module.Show();
// glfw INIT std::cout << "You finished at level " << player_time << " in " << player_level << " seconds! Enter your name: ";
// -------------------- std::string name;
std::cin >> name;
highscore_module.Store(player_time, player_level, name);
}
void myGlfwInit() {
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
// glfw window creation GLFWwindow* createWindow()
// -------------------- {
GLFWwindow* window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL); GLFWwindow* window = glfwCreateWindow(800, 800, "Pod roukou tmy", NULL, NULL);
if (window == NULL) if (window == NULL)
{ {
std::cout << "Failed to create GLFW window" << std::endl; std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
return -1; return NULL;
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
...@@ -61,7 +66,9 @@ int main() ...@@ -61,7 +66,9 @@ int main()
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialise GLAD" << std::endl; std::cout << "Failed to initialise GLAD" << std::endl;
} }
//Compile Vertex Shader }
unsigned int createShaderProgram() {
unsigned int vertexShader; unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER); vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
...@@ -95,13 +102,18 @@ int main() ...@@ -95,13 +102,18 @@ int main()
glDeleteShader(vertexShader); glDeleteShader(vertexShader);
glDeleteShader(fragmentShader); glDeleteShader(fragmentShader);
return shaderProgram;
}
unsigned int prepareOpenGLBuffers()
{
unsigned int VAO; //vertex array object unsigned int VAO; //vertex array object
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
unsigned int VBO; //vertex buffer object unsigned int VBO; //vertex buffer object
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(VAO); glBindVertexArray(VAO);
//Tell VAO how to read given arrays //Tell VAO how to read given arrays
// 2 floats for coords (x,y) // 2 floats for coords (x,y)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
...@@ -110,11 +122,25 @@ int main() ...@@ -110,11 +122,25 @@ int main()
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
return VAO;
}
//Main that creates playable window
int main()
{
myGlfwInit();
// glfw window creation
// --------------------
GLFWwindow* window = createWindow();
if (window == NULL)
return -1;
unsigned int shaderProgram = createShaderProgram();
//set glfw input mode to stick keys (stay pressed until polling) //set glfw input mode to stick keys (stay pressed until polling)
glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE); glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE);
unsigned int VAO = prepareOpenGLBuffers();
Display display(VAO,shaderProgram); Display display(VAO,shaderProgram);
// runtime variables // runtime variables
...@@ -126,7 +152,7 @@ int main() ...@@ -126,7 +152,7 @@ int main()
bool game_over=false; bool game_over=false;
time_t start_time= time(NULL); //sets start clock in time_t start_time= time(NULL); //sets start clock in
long runtime_s = 0; long runtime_s = 0;
auto highscore_module = HighscoreModule("highscore.txt");
//main game loop //main game loop
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
...@@ -171,11 +197,7 @@ int main() ...@@ -171,11 +197,7 @@ int main()
//highscore //highscore
if(runtime_s==0) if(runtime_s==0)
runtime_s = (long)difftime(time(NULL), start_time); runtime_s = (long)difftime(time(NULL), start_time);
highscore_module.Show(); showHighscoreTable(runtime_s,level_counter);
std::cout << "You finished at level " << level_counter << " in " << runtime_s << " seconds! Enter your name: ";
std::string name;
std::cin >> name;
highscore_module.Store(runtime_s, level_counter, name);
return 0; return 0;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment