Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Дорожкин Дмитрий Михайлович
PAC-MAN
Commits
df67003c
Commit
df67003c
authored
3 weeks ago
by
Дорожкин Дмитрий Михайлович
Browse files
Options
Download
Patches
Plain Diff
well at least it somehow works
parent
ebc8f9e1
main
No related merge requests found
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
CMakeLists.txt
+2
-1
CMakeLists.txt
assets/MedodicaRegular.otf
+0
-0
assets/MedodicaRegular.otf
source/application.cpp
+29
-15
source/application.cpp
source/application.hpp
+4
-3
source/application.hpp
source/config.hpp
+48
-0
source/config.hpp
source/drawable.cpp
+27
-0
source/drawable.cpp
source/drawable.hpp
+46
-1
source/drawable.hpp
source/main.cpp
+2
-2
source/main.cpp
source/states.cpp
+28
-0
source/states.cpp
source/states.hpp
+9
-3
source/states.hpp
source/window.cpp
+2
-0
source/window.cpp
source/window.hpp
+5
-5
source/window.hpp
with
202 additions
and
30 deletions
+202
-30
CMakeLists.txt
+
2
−
1
View file @
df67003c
...
...
@@ -13,4 +13,5 @@ set(BUILD_SHARED_LIBS OFF) # Отключаем динамическую лин
FetchContent_Declare
(
SFML GIT_REPOSITORY https://github.com/SFML/SFML.git GIT_TAG 2.6.1
)
FetchContent_MakeAvailable
(
SFML
)
target_link_libraries
(
application PRIVATE sfml-window sfml-audio sfml-graphics sfml-system
)
\ No newline at end of file
target_link_libraries
(
application PRIVATE sfml-window sfml-audio sfml-graphics sfml-system
)
target_compile_definitions
(
application PUBLIC ASSETS_PATH=
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/assets/"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
assets/MedodicaRegular.otf
0 → 100644
+
0
−
0
View file @
df67003c
File added
This diff is collapsed.
Click to expand it.
source/application.cpp
+
29
−
15
View file @
df67003c
#include
"application.hpp"
#include
"config.hpp"
#include
<iostream>
// int Application::run() {
// try {
// while (m_ptr_state_current->do_step())
// apply_deffer_state_change();
// }
// catch (std::exception& ex) {
// std::cout << ex.what() << '\n';
// return 1;
// }
// catch (...) {
// std::cout << "Unknown exception\n";
// return 2;
// }
// return 0;
// }
\ No newline at end of file
Application
::
Application
()
:
m_ptr_state_current
(
std
::
make_unique
<
SelectState
>
(
*
this
,
config
::
SELECT_LEVEL_VIDEO_MODE
,
config
::
SELECT_LEVEL_TITLE
)){
}
int
Application
::
run
()
{
try
{
while
(
m_ptr_state_current
->
do_step
())
apply_deffer_state_change
();
}
catch
(
std
::
exception
&
ex
)
{
std
::
cout
<<
ex
.
what
()
<<
'\n'
;
return
1
;
}
catch
(...)
{
std
::
cout
<<
"Unknown exception
\n
"
;
return
2
;
}
return
0
;
}
void
Application
::
set_next_state
(
std
::
unique_ptr
<
IState
>
ptr_state
)
{
m_ptr_state_next
.
swap
(
ptr_state
);
}
void
Application
::
apply_deffer_state_change
()
{
if
(
m_ptr_state_next
)
{
m_ptr_state_current
=
std
::
move
(
m_ptr_state_next
);
}
}
This diff is collapsed.
Click to expand it.
source/application.hpp
+
4
−
3
View file @
df67003c
...
...
@@ -4,11 +4,12 @@
class
Application
:
public
IStateManager
{
public:
~
Application
()
override
;
~
Application
()
override
=
default
;
Application
();
int
run
();
private:
void
set_next_state
(
IState
*
ptr_state
)
override
;
void
set_next_state
(
std
::
unique_ptr
<
IState
>
ptr_state
)
override
;
void
apply_deffer_state_change
();
std
::
unique_ptr
<
IState
>
m_ptr_state_next
;
std
::
unique_ptr
<
IState
>
m_ptr_state_current
;
std
::
unique_ptr
<
IState
>
m_ptr_state_next
=
nullptr
;
};
This diff is collapsed.
Click to expand it.
source/config.hpp
0 → 100644
+
48
−
0
View file @
df67003c
#include
"SFML/Graphics.hpp"
namespace
config
{
// Общее:
const
unsigned
int
FRAME_RATE_LIMIT
=
60
;
// Меню:
const
sf
::
Vector2f
BUTTON_SIZE
=
{
250
,
100
};
const
size_t
BUTTON_FONT_SIZE
=
static_cast
<
size_t
>
(
BUTTON_SIZE
.
y
/
1.5
f
);
const
float
BUTTON_FRAME_THICKNESS
=
2.0
f
;
const
char
FONT_FILE
[]
=
"MedodicaRegular.otf"
;
const
char
SELECT_LEVEL_TITLE
[]
=
"Select Level"
;
const
sf
::
VideoMode
SELECT_LEVEL_VIDEO_MODE
{
400
,
600
};
const
char
BUTTON_TEXT_EASY
[]
=
"Easy"
;
const
char
BUTTON_TEXT_MEDIUM
[]
=
"Medium"
;
const
char
BUTTON_TEXT_HARD
[]
=
"Hard"
;
const
char
BUTTON_TEXT_EXIT
[]
=
"Exit"
;
// Игра:
const
sf
::
VideoMode
GAME_VIDEO_MODE
{
1080
,
720
};
const
char
EASY_GAME_TITLE
[]
=
"Level: Easy"
;
const
char
MEDIUM_GAME_TITLE
[]
=
"Level: Medium"
;
const
char
HARD_GAME_TITLE
[]
=
"Level: Hard"
;
const
float
EASY_GAME_ENEMY_RATIO
=
0.0
f
;
const
float
MEDIUM_GAME_ENEMY_RATIO
=
0.03
f
;
const
float
HARD_GAME_ENEMY_RATIO
=
0.07
f
;
const
float
ROOM_SIZE
=
50
;
const
float
GAME_ENEMY_SIZE
=
ROOM_SIZE
*
0.7
;
const
float
GAME_FOOD_SIZE
=
ROOM_SIZE
*
0.2
;
// Пакмэн:
const
float
GAME_PACMAN_SIZE
=
ROOM_SIZE
*
0.8
;
const
sf
::
Keyboard
::
Key
KEY_LEFT
=
sf
::
Keyboard
::
A
;
const
sf
::
Keyboard
::
Key
KEY_RIGHT
=
sf
::
Keyboard
::
D
;
const
sf
::
Keyboard
::
Key
KEY_UP
=
sf
::
Keyboard
::
W
;
const
sf
::
Keyboard
::
Key
KEY_DOWN
=
sf
::
Keyboard
::
S
;
// Цвета:
const
sf
::
Color
BUTTON_COLOR_TEXT
{
0
,
0
,
0
};
const
sf
::
Color
BUTTON_COLOR_FILL
{
180
,
180
,
180
};
const
sf
::
Color
BUTTON_COLOR_SELECTION
{
255
,
180
,
180
};
const
sf
::
Color
BUTTON_COLOR_FRAME
{
0
,
0
,
0
};
const
sf
::
Color
SELECT_LEVEL_BACKGROUND_COLOR
{
230
,
230
,
230
};
const
sf
::
Color
GAME_COLOR_BACKGROUND_INGAME
{
230
,
230
,
230
};
const
sf
::
Color
GAME_COLOR_BACKGROUND_WIN
{
0
,
255
,
0
};
const
sf
::
Color
GAME_COLOR_BACKGROUND_LOST
{
255
,
0
,
0
};
const
sf
::
Color
GAME_COLOR_PACMAN
{
250
,
150
,
0
};
const
sf
::
Color
GAME_COLOR_ROOM
{
255
,
255
,
255
};
const
sf
::
Color
GAME_COLOR_WALL
{
0
,
0
,
0
};
const
sf
::
Color
GAME_FOOD_COLOR
{
0
,
200
,
100
};
const
sf
::
Color
GAME_ENEMY_COLOR
{
255
,
50
,
0
};
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
source/drawable.cpp
+
27
−
0
View file @
df67003c
#include
"drawable.hpp"
#include
"config.hpp"
sf
::
Font
&
MyFont
::
Instance
()
{
static
MyFont
font
;
return
font
.
m_font
;
}
MyFont
::
MyFont
()
{
if
(
!
m_font
.
loadFromFile
(
ASSETS_PATH
+
std
::
string
(
config
::
FONT_FILE
)))
throw
std
::
runtime_error
(
"Failed to load font"
);
}
void
Button
::
set
(
sf
::
Vector2f
pos
,
sf
::
Vector2f
button_size
,
std
::
string
text
,
size_t
font_size
,
ISelectCommand
&
ptr_command
)
{
m_rectangle
.
setSize
(
button_size
);
m_rectangle
.
setOrigin
(
button_size
/
2.0
f
);
m_rectangle
.
setPosition
(
pos
);
m_rectangle
.
setOutlineThickness
(
config
::
BUTTON_FRAME_THICKNESS
);
m_text
.
setString
(
text
);
m_text
.
setFont
(
MyFont
::
Instance
());
auto
bounds
=
m_text
.
getLocalBounds
();
m_text
.
setOrigin
(
bounds
.
getPosition
()
+
bounds
.
getSize
()
/
2.0
f
);
m_text
.
setPosition
(
pos
);
}
This diff is collapsed.
Click to expand it.
source/drawable.hpp
+
46
−
1
View file @
df67003c
#pragma once
\ No newline at end of file
#pragma once
#include
<SFML/Graphics.hpp>
#include
<array>
#include
"states.hpp"
class
MyFont
{
public:
static
sf
::
Font
&
Instance
();
private:
sf
::
Font
m_font
;
MyFont
();
};
class
ISelectCommand
{
public:
virtual
~
ISelectCommand
()
=
default
;
virtual
void
execute
()
=
0
;
};
struct
IDrawable
{
virtual
void
draw_into
(
sf
::
RenderWindow
&
window
)
const
=
0
;
virtual
~
IDrawable
()
=
default
;
};
class
Button
{
public:
Button
();
void
set
(
sf
::
Vector2f
pos
,
sf
::
Vector2f
button_size
,
std
::
string
text
,
size_t
font_size
,
ISelectCommand
&
ptr_command
);
void
select
();
void
unselect
();
bool
is_position_in
(
sf
::
Vector2f
pos
);
void
push
();
private:
sf
::
RectangleShape
m_rectangle
;
sf
::
Text
m_text
;
ISelectCommand
*
m_command
;
};
class
Menu
:
public
IDrawable
{
public:
Menu
(
IStateManager
&
stateManager
);
void
process_mouse
(
sf
::
Vector2f
pos
,
bool
is_pressed
);
private:
std
::
array
<
Button
*
,
4
>
drawables
;
};
\ No newline at end of file
This diff is collapsed.
Click to expand it.
source/main.cpp
+
2
−
2
View file @
df67003c
...
...
@@ -2,6 +2,6 @@
#include
"application.hpp"
int
main
()
{
//
Application app;
//
return app.run();
Application
app
;
return
app
.
run
();
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
source/states.cpp
+
28
−
0
View file @
df67003c
#include
"states.hpp"
IState
::
IState
(
IStateManager
&
state_manager
)
:
m_state_manager
(
state_manager
)
{}
bool
SelectState
::
do_step
()
{
event_handling
();
update
();
render
();
return
true
;
}
SelectState
::
SelectState
(
IStateManager
&
state_manager
,
sf
::
VideoMode
video_mode
,
std
::
string
window_title
)
:
IWindowKeeper
(
video_mode
,
window_title
),
IState
(
state_manager
)
{}
void
SelectState
::
event_handling
()
{
sf
::
Event
event
;
while
(
m_window
.
pollEvent
(
event
))
{
}
}
void
SelectState
::
update
()
{
}
void
SelectState
::
render
()
{
}
This diff is collapsed.
Click to expand it.
source/states.hpp
+
9
−
3
View file @
df67003c
...
...
@@ -6,22 +6,28 @@ class IState;
class
IStateManager
{
public:
virtual
~
IStateManager
()
=
default
;
virtual
void
set_next_state
(
IState
*
state
);
virtual
void
set_next_state
(
std
::
unique_ptr
<
IState
>
state
)
=
0
;
};
class
IState
{
public:
virtual
~
IState
();
virtual
~
IState
()
=
default
;
IState
(
IStateManager
&
state_manager
);
virtual
bool
do_step
();
virtual
bool
do_step
()
=
0
;
protected:
IStateManager
&
m_state_manager
;
};
class
SelectState
:
public
IState
,
public
IWindowKeeper
{
public:
~
SelectState
()
override
=
default
;
bool
do_step
()
override
;
SelectState
(
IStateManager
&
state_manager
,
sf
::
VideoMode
video_mode
,
std
::
string
window_title
);
private:
void
event_handling
()
override
;
void
update
()
override
;
void
render
()
override
;
};
class
GameState
:
public
IState
,
public
IWindowKeeper
{
...
...
This diff is collapsed.
Click to expand it.
source/window.cpp
+
2
−
0
View file @
df67003c
#include
"window.hpp"
IWindowKeeper
::
IWindowKeeper
(
sf
::
VideoMode
video_mode
,
std
::
string
window_title
)
:
m_window
(
video_mode
,
window_title
)
{}
This diff is collapsed.
Click to expand it.
source/window.hpp
+
5
−
5
View file @
df67003c
...
...
@@ -4,11 +4,11 @@
class
IWindowKeeper
{
public:
virtual
~
IWindowKeeper
();
virtual
~
IWindowKeeper
()
=
default
;
IWindowKeeper
(
sf
::
VideoMode
video_mode
,
std
::
string
window_title
);
protected:
void
event_handling
();
void
update
();
void
render
();
sf
::
RenderWindow
window
;
virtual
void
event_handling
()
=
0
;
virtual
void
update
()
=
0
;
virtual
void
render
()
=
0
;
sf
::
RenderWindow
m_
window
;
};
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets