Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Srb František
Cpp-Game
Commits
b1c2935a
Commit
b1c2935a
authored
Aug 30, 2021
by
Srb František
Browse files
Init divided into functions
Fix
#4
parent
652bf1ab
Changes
1
Show whitespace changes
Inline
Side-by-side
CppGame/main.cpp
View file @
b1c2935a
...
...
@@ -34,25 +34,30 @@ const char* fragmentShaderSource = "#version 330 core\n"
" FragColor = vertexColor;
\n
"
"}
\n\0
"
;
//Main that creates playable window
int
main
()
{
// glfw INIT
// --------------------
void
showHighscoreTable
(
long
player_time
,
int
player_level
)
{
auto
highscore_module
=
HighscoreModule
(
"highscore.txt"
);
highscore_module
.
Show
();
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
();
glfwWindowHint
(
GLFW_CONTEXT_VERSION_MAJOR
,
3
);
glfwWindowHint
(
GLFW_CONTEXT_VERSION_MINOR
,
3
);
glfwWindowHint
(
GLFW_OPENGL_PROFILE
,
GLFW_OPENGL_CORE_PROFILE
);
}
// glfw window creation
// --------------------
GLFWwindow
*
window
=
glfwCreateWindow
(
800
,
800
,
"LearnOpenGL"
,
NULL
,
NULL
);
GLFWwindow
*
createWindow
()
{
GLFWwindow
*
window
=
glfwCreateWindow
(
800
,
800
,
"Pod roukou tmy"
,
NULL
,
NULL
);
if
(
window
==
NULL
)
{
std
::
cout
<<
"Failed to create GLFW window"
<<
std
::
endl
;
glfwTerminate
();
return
-
1
;
return
NULL
;
}
glfwMakeContextCurrent
(
window
);
glfwSetFramebufferSizeCallback
(
window
,
framebuffer_size_callback
);
...
...
@@ -61,7 +66,9 @@ int main()
if
(
!
gladLoadGLLoader
((
GLADloadproc
)
glfwGetProcAddress
))
{
std
::
cout
<<
"Failed to initialise GLAD"
<<
std
::
endl
;
}
//Compile Vertex Shader
}
unsigned
int
createShaderProgram
()
{
unsigned
int
vertexShader
;
vertexShader
=
glCreateShader
(
GL_VERTEX_SHADER
);
glShaderSource
(
vertexShader
,
1
,
&
vertexShaderSource
,
NULL
);
...
...
@@ -95,6 +102,11 @@ int main()
glDeleteShader
(
vertexShader
);
glDeleteShader
(
fragmentShader
);
return
shaderProgram
;
}
unsigned
int
prepareOpenGLBuffers
()
{
unsigned
int
VAO
;
//vertex array object
glGenVertexArrays
(
1
,
&
VAO
);
unsigned
int
VBO
;
//vertex buffer object
...
...
@@ -110,11 +122,25 @@ int main()
glVertexAttribPointer
(
1
,
3
,
GL_FLOAT
,
GL_FALSE
,
5
*
sizeof
(
float
),
(
void
*
)(
2
*
sizeof
(
float
)));
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)
glfwSetInputMode
(
window
,
GLFW_STICKY_KEYS
,
GLFW_TRUE
);
unsigned
int
VAO
=
prepareOpenGLBuffers
();
Display
display
(
VAO
,
shaderProgram
);
// runtime variables
...
...
@@ -126,7 +152,7 @@ int main()
bool
game_over
=
false
;
time_t
start_time
=
time
(
NULL
);
//sets start clock in
long
runtime_s
=
0
;
auto
highscore_module
=
HighscoreModule
(
"highscore.txt"
);
//main game loop
while
(
!
glfwWindowShouldClose
(
window
))
{
...
...
@@ -171,11 +197,7 @@ int main()
//highscore
if
(
runtime_s
==
0
)
runtime_s
=
(
long
)
difftime
(
time
(
NULL
),
start_time
);
highscore_module
.
Show
();
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
);
showHighscoreTable
(
runtime_s
,
level_counter
);
return
0
;
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment