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
eb5b3fd1
Unverified
Commit
eb5b3fd1
authored
6 months ago
by
David Hermann
Browse files
Options
Downloads
Patches
Plain Diff
Closing
#10
Implementing possibility to render background in main menu
parent
f42e8ccf
Branches
Branches containing commit
No related tags found
1 merge request
!14
Refactoring CMakeLists.txt
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/main.cpp
+5
-1
5 additions, 1 deletion
src/main.cpp
src/ui/Menu.cpp
+36
-6
36 additions, 6 deletions
src/ui/Menu.cpp
src/ui/Menu.hpp
+3
-0
3 additions, 0 deletions
src/ui/Menu.hpp
with
44 additions
and
7 deletions
src/main.cpp
+
5
−
1
View file @
eb5b3fd1
...
...
@@ -17,8 +17,12 @@ int main() {
MainMenu
menu
(
0
);
engine
.
set_scene
(
menu
);
std
::
string
basePath
=
SDL_GetBasePath
();
std
::
string
relativePath
=
"assets/main_background.png"
;
std
::
string
fullPath
=
basePath
+
relativePath
;
menu
.
loadBackground
(
engine
.
renderer
(),
fullPath
.
c_str
());
engine
.
set_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/Menu.cpp
+
36
−
6
View file @
eb5b3fd1
...
...
@@ -3,6 +3,7 @@
#include
<iostream>
#include
<vector>
#include
<string>
#include
<SDL_image.h>
#include
"Menu.hpp"
namespace
advanced_wars
...
...
@@ -10,13 +11,19 @@ namespace advanced_wars
MainMenu
::
MainMenu
(
int
selectedOption
)
:
selectedOption
(
selectedOption
),
options
({
"Start Game"
,
"Options"
,
"Exit"
})
options
({
"Start Game"
,
"Options"
,
"Exit"
}),
backgroundTexture
(
nullptr
)
{
if
(
!
(
IMG_Init
(
IMG_INIT_PNG
)
&
IMG_INIT_PNG
))
{
std
::
cerr
<<
"Failed to initialize SDL_image: "
<<
IMG_GetError
()
<<
std
::
endl
;
}
}
MainMenu
::~
MainMenu
()
{
if
(
backgroundTexture
)
{
SDL_DestroyTexture
(
backgroundTexture
);
}
IMG_Quit
();
};
void
MainMenu
::
render
(
SDL_Renderer
*
renderer
,
std
::
vector
<
SDL_Event
>
&
events
)
{
...
...
@@ -28,9 +35,14 @@ namespace advanced_wars
}
// Clear the screen with a background color
if
(
backgroundTexture
)
{
SDL_RenderCopy
(
renderer
,
backgroundTexture
,
nullptr
,
nullptr
);
}
else
{
std
::
cout
<<
"No background texture loaded"
<<
std
::
endl
;
// Falls kein Hintergrundbild vorhanden ist, cleare mit Schwarz
SDL_SetRenderDrawColor
(
renderer
,
0
,
0
,
0
,
255
);
SDL_RenderClear
(
renderer
);
}
// Initialize SDL_TTF if not already done
if
(
TTF_Init
()
==
-
1
)
{
...
...
@@ -101,4 +113,22 @@ namespace advanced_wars
}
}
void
MainMenu
::
loadBackground
(
SDL_Renderer
*
renderer
,
const
std
::
string
&
imagePath
)
{
// Lade das Hintergrundbild
SDL_Surface
*
backgroundSurface
=
IMG_Load
(
imagePath
.
c_str
());
if
(
!
backgroundSurface
)
{
std
::
cerr
<<
"Failed to load background image: "
<<
IMG_GetError
()
<<
std
::
endl
;
return
;
}
// Erstelle eine Textur aus der Oberfläche und speichere sie als Klassenmitglied
backgroundTexture
=
SDL_CreateTextureFromSurface
(
renderer
,
backgroundSurface
);
SDL_FreeSurface
(
backgroundSurface
);
// Oberfläche freigeben, da sie nicht mehr benötigt wird
if
(
!
backgroundTexture
)
{
std
::
cerr
<<
"Failed to create background texture: "
<<
SDL_GetError
()
<<
std
::
endl
;
}
}
}
This diff is collapsed.
Click to expand it.
src/ui/Menu.hpp
+
3
−
0
View file @
eb5b3fd1
...
...
@@ -20,6 +20,7 @@ class MainMenu : public Scene {
private:
size_t
selectedOption
;
std
::
array
<
std
::
string
,
3
>
options
;
SDL_Texture
*
backgroundTexture
;
public:
...
...
@@ -29,6 +30,8 @@ public:
void
handleEvent
(
SDL_Event
&
event
);
void
loadBackground
(
SDL_Renderer
*
renderer
,
const
std
::
string
&
imagePath
);
~
MainMenu
();
};
...
...
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