Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cpp-project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Maul
cpp-project
Commits
bdcd843d
Commit
bdcd843d
authored
6 months ago
by
fdai7466
Browse files
Options
Downloads
Patches
Plain Diff
add variable position for context menu
parent
3f727f13
Branches
Branches containing commit
No related tags found
1 merge request
!14
Refactoring CMakeLists.txt
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
src/level.cpp
+32
-4
32 additions, 4 deletions
src/level.cpp
src/level.hpp
+3
-0
3 additions, 0 deletions
src/level.hpp
src/main.cpp
+1
-1
1 addition, 1 deletion
src/main.cpp
src/ui/ContextMenu.cpp
+14
-9
14 additions, 9 deletions
src/ui/ContextMenu.cpp
src/ui/ContextMenu.hpp
+6
-0
6 additions, 0 deletions
src/ui/ContextMenu.hpp
with
56 additions
and
14 deletions
src/level.cpp
+
32
−
4
View file @
bdcd843d
...
...
@@ -5,13 +5,16 @@
#include
<iostream>
#include
<string>
#include
"ui/pausemenu.hpp"
#include
"ui/ContextMenu.hpp"
namespace
advanced_wars
{
Level
::
Level
(
std
::
string
name
,
int
width
,
int
height
,
std
::
vector
<
Tile
>
tiles
,
std
::
vector
<
Building
>
buildings
,
std
::
vector
<
Unit
>
units
)
:
name
(
name
),
width
(
width
),
height
(
height
),
buildings
(
buildings
),
units
(
units
)
{};
units
(
units
),
context_menu
(
ContextMenu
()),
context_menu_active
(
false
){
context_menu
.
setOptions
({
"Move"
,
"Info"
,
"Wait"
});
}
void
Level
::
render
(
Engine
*
engine
)
{
// Iterate over all events
...
...
@@ -20,10 +23,17 @@ void Level::render(Engine *engine) {
engine
->
events
().
pop_front
();
}
// Set background color for renderer
if
(
SDL_SetRenderDrawColor
(
engine
->
renderer
(),
255
,
0
,
0
,
0
))
{
if
(
SDL_SetRenderDrawColor
(
engine
->
renderer
(),
0
,
0
,
0
,
255
))
{
std
::
cout
<<
"Could not set render draw color: "
<<
SDL_GetError
()
<<
std
::
endl
;
}
SDL_RenderClear
(
engine
->
renderer
());
if
(
context_menu_active
)
{
context_menu
.
render
(
engine
);
}
}
void
Level
::
handleEvent
(
Engine
*
engine
,
SDL_Event
&
event
)
{
...
...
@@ -36,8 +46,26 @@ void Level::handleEvent(Engine *engine, SDL_Event &event) {
PauseMenu
pauseMenu
(
0
,
currentTexture
);
engine
->
push_scene
(
std
::
make_shared
<
PauseMenu
>
(
pauseMenu
));
}
if
(
context_menu_active
){
if
(
event
.
key
.
keysym
.
sym
==
SDLK_DOWN
)
{
context_menu
.
handleEvent
(
event
);
}
if
(
event
.
key
.
keysym
.
sym
==
SDLK_UP
)
{
context_menu
.
handleEvent
(
event
);
}
if
(
event
.
key
.
keysym
.
sym
==
SDLK_RETURN
)
{
if
(
context_menu
.
getSelectedOption
()
==
"Wait"
){
context_menu_active
=
false
;
}
}
}
}
if
(
event
.
type
==
SDL_MOUSEBUTTONDOWN
)
{
context_menu
.
update
(
event
.
button
.
x
,
event
.
button
.
y
);
context_menu_active
=
true
;
}
}
...
...
This diff is collapsed.
Click to expand it.
src/level.hpp
+
3
−
0
View file @
bdcd843d
...
...
@@ -7,6 +7,7 @@
#include
<SDL.h>
#include
<string>
#include
<vector>
#include
"ui/ContextMenu.hpp"
namespace
advanced_wars
{
...
...
@@ -27,6 +28,8 @@ private:
int
height
;
std
::
vector
<
Building
>
buildings
;
std
::
vector
<
Unit
>
units
;
ContextMenu
context_menu
;
bool
context_menu_active
;
};
}
// namespace advanced_wars
This diff is collapsed.
Click to expand it.
src/main.cpp
+
1
−
1
View file @
bdcd843d
...
...
@@ -25,7 +25,7 @@ int main() {
std
::
string
fullPath
=
basePath
+
relativePath
;
menu
->
loadBackground
(
engine
.
renderer
(),
fullPath
.
c_str
());
engine
.
push_scene
(
context_
menu
);
engine
.
push_scene
(
menu
);
/* Level level("Osnabrück", 20, 20, std::vector<Tile>(),
std::vector<Building>(), std::vector<Unit>());
...
...
This diff is collapsed.
Click to expand it.
src/ui/ContextMenu.cpp
+
14
−
9
View file @
bdcd843d
...
...
@@ -15,7 +15,6 @@ namespace advanced_wars {
}
void
ContextMenu
::
render
(
Engine
*
engine
)
{
if
(
!
options
.
empty
())
{
if
(
TTF_Init
()
==
-
1
)
{
std
::
cerr
<<
"Failed to initialize TTF: "
<<
TTF_GetError
()
<<
std
::
endl
;
...
...
@@ -32,14 +31,13 @@ namespace advanced_wars {
}
SDL_Color
white
=
{
255
,
255
,
255
,
255
};
SDL_Color
yellow
=
{
255
,
255
,
0
,
255
};
SDL_Color
yellow
=
{
192
,
255
,
0
,
255
};
int
startY
=
100
;
// Startposition für das Menü
int
spacing
=
20
;
// Abstand zwischen den Optionen
//box around options
SDL_SetRenderDrawColor
(
engine
->
renderer
(),
0
,
0
,
255
,
128
);
SDL_Rect
box
=
{
190
,
startY
-
3
,
50
,
static_cast
<
int
>
(
options
.
size
()
*
spacing
)};
SDL_SetRenderDrawColor
(
engine
->
renderer
(),
0
,
0
,
255
,
255
);
SDL_Rect
box
=
{
x
,
y
-
3
,
50
,
static_cast
<
int
>
(
options
.
size
()
*
spacing
)};
SDL_RenderFillRect
(
engine
->
renderer
(),
&
box
);
SDL_SetRenderDrawColor
(
engine
->
renderer
(),
0
,
0
,
0
,
255
);
...
...
@@ -51,7 +49,7 @@ namespace advanced_wars {
}
SDL_Texture
*
textTexture
=
SDL_CreateTextureFromSurface
(
engine
->
renderer
(),
textSurface
);
SDL_Rect
textRect
=
{
200
,
startY
+
static_cast
<
int
>
(
i
*
spacing
),
textSurface
->
w
,
textSurface
->
h
};
SDL_Rect
textRect
=
{
x
+
10
,
y
+
static_cast
<
int
>
(
i
*
spacing
),
textSurface
->
w
,
textSurface
->
h
};
SDL_RenderCopy
(
engine
->
renderer
(),
textTexture
,
nullptr
,
&
textRect
);
SDL_DestroyTexture
(
textTexture
);
...
...
@@ -69,10 +67,17 @@ namespace advanced_wars {
selectedOption
=
(
selectedOption
+
1
)
%
options
.
size
();
}
else
if
(
event
.
key
.
keysym
.
sym
==
SDLK_UP
)
{
selectedOption
=
(
selectedOption
-
1
+
options
.
size
())
%
options
.
size
();
}
else
if
(
event
.
key
.
keysym
.
sym
==
SDLK_RETURN
)
{
std
::
cout
<<
"Selected option: "
<<
options
[
selectedOption
]
<<
std
::
endl
;
}
}
}
std
::
string
ContextMenu
::
getSelectedOption
()
{
return
options
[
selectedOption
];
}
void
ContextMenu
::
update
(
int
x
,
int
y
)
{
this
->
x
=
x
;
this
->
y
=
y
;
}
}
This diff is collapsed.
Click to expand it.
src/ui/ContextMenu.hpp
+
6
−
0
View file @
bdcd843d
...
...
@@ -13,6 +13,8 @@ class ContextMenu : public Scene {
private:
size_t
selectedOption
;
std
::
vector
<
std
::
string
>
options
;
int
x
;
int
y
;
public:
ContextMenu
();
...
...
@@ -23,6 +25,10 @@ public:
void
handleEvent
(
SDL_Event
&
event
);
void
update
(
int
x
,
int
y
);
std
::
string
getSelectedOption
();
~
ContextMenu
();
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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