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
3d65e85a
Commit
3d65e85a
authored
5 months ago
by
Lorenz Martin Diel
Browse files
Options
Downloads
Patches
Plain Diff
Changed look of info_menu
parent
cfab59a6
Branches
Branches containing commit
No related tags found
2 merge requests
!36
Unit info menu
,
!29
Merge main into box2d to implement physics
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/game/Unit.hpp
+1
-0
1 addition, 0 deletions
src/game/Unit.hpp
src/game/ui/UnitInfoMenu.cpp
+96
-62
96 additions, 62 deletions
src/game/ui/UnitInfoMenu.cpp
with
97 additions
and
62 deletions
src/game/Unit.hpp
+
1
−
0
View file @
3d65e85a
...
...
@@ -125,6 +125,7 @@ class Unit
int
getAmmo
()
const
{
return
m_ammo
;
}
int
getHealth
()
const
{
return
m_health
;
}
int
getCost
()
const
{
return
m_cost
;
}
UnitId
getId
()
const
{
return
m_id
;}
void
setState
(
UnitState
state
);
...
...
This diff is collapsed.
Click to expand it.
src/game/ui/UnitInfoMenu.cpp
+
96
−
62
View file @
3d65e85a
#include
"UnitInfoMenu.hpp"
#include
<SDL_ttf.h>
#include
<iostream>
#include
<unordered_map>
namespace
advanced_wars
{
// UnitInfoMenu::UnitInfoMenu() : m_currentUnit(nullptr), m_isVisible(false) {}
UnitInfoMenu
::
UnitInfoMenu
()
:
m_currentUnit
(
nullptr
)
{
}
UnitInfoMenu
::
UnitInfoMenu
()
:
m_currentUnit
(
nullptr
)
{}
std
::
string
UnitInfoMenu
::
getMovementTypeString
(
MovementType
type
)
{
switch
(
type
)
{
std
::
string
UnitInfoMenu
::
getMovementTypeString
(
MovementType
type
)
{
switch
(
type
)
{
case
MovementType
::
FOOT
:
return
"Foot"
;
case
MovementType
::
WHEELED
:
...
...
@@ -29,6 +31,28 @@ std::string UnitInfoMenu::getMovementTypeString(MovementType type) {
}
}
std
::
unordered_map
<
UnitId
,
std
::
string
>
unitDescriptions
=
{
{
UnitId
::
INFANTERY
,
"Infanterie Kostenguenstig und vielseitig einsetzbar"
},
{
UnitId
::
MECHANIZED_INFANTERY
,
"Mech-Infanterie Stark gegen Panzer langsam aber effizient"
},
{
UnitId
::
RECON
,
"Aufklaerung Schnell und ideal für frühe Aufklaerung"
},
{
UnitId
::
MEDIUM_TANK
,
"Mittlerer Panzer Guter Allrounder stark und ausgewogen"
},
{
UnitId
::
HEAVY_TANK
,
"Schwerer Panzer Langsam aber sehr stark und beschützend"
},
{
UnitId
::
NEO_TANK
,
"Neo Tank Einer der besten Panzer stark und vielseitig"
},
{
UnitId
::
APC
,
"Transporter Traeger fuer Infanterie keine Offensivkraefte"
},
{
UnitId
::
ANTI_AIR_TANK
,
"FlugabwehrPanzer Ideal zur Luftverteidigung"
},
{
UnitId
::
ARTILLERY
,
"Artillerie Kann aus Distanz zufuegen aber verletzbar im Nahkampf"
},
{
UnitId
::
ROCKET_ARTILLERY
,
"Raketenartillerie Grosse Reichweite ideal fuer defensive Taktiken"
},
{
UnitId
::
ANTI_AIR_MISSILE_LAUNCHER
,
"Raketenwerfer Kann Flugeinheiten auf grosse Distanz angreifen"
},
{
UnitId
::
FIGHTER
,
"Jaeger Ideal fuer Luftueberlegenheit"
},
{
UnitId
::
BOMBER
,
"Bomber Stark gegen Boden- und Seeziele aber verletzbar gegen Luft-und Flak"
},
{
UnitId
::
BATTLE_HELICOPTER
,
"Kampfhubschrauber Stark gegen Bodenfahrzeuge und Infanterie"
},
{
UnitId
::
TRANSPORT_HELICOPTER
,
"Transporthubschrauber Kann Einheiten schnell transportieren"
},
{
UnitId
::
BATTLESHIP
,
"Schlachtschiff Langreichweitenangriff auf See und Land"
},
{
UnitId
::
CRUISER
,
"Kreuzer Verteidigung gegen Luft und U-Boot-Einheiten"
},
{
UnitId
::
LANDER
,
"Landungsschiff Transport und Versorgung"
},
{
UnitId
::
SUBMARINE
,
"U-Boot Versteckt sich und kann Ueberwasserziele angreifen"
}
};
void
UnitInfoMenu
::
handleEvent
(
Engine
&
engine
,
SDL_Event
&
event
)
{
// Hier kannst du den Code hinzufügen, um die Ereignisse für das UnitInfoMenu zu behandeln
...
...
@@ -44,21 +68,13 @@ void UnitInfoMenu::setUnit(Unit& unit)
void
UnitInfoMenu
::
render
(
Engine
&
engine
)
{
// TTF Initialisierung
if
(
TTF_Init
()
==
-
1
)
{
std
::
cerr
<<
"TTF konnte nicht initialisiert werden: "
<<
TTF_GetError
()
<<
std
::
endl
;
return
;
}
if
(
!
m_currentUnit
||
!
m_isVisible
)
return
;
// TTF Initialisierung
if
(
TTF_Init
()
==
-
1
)
{
std
::
cerr
<<
"
Failed to initialize TTF
: "
<<
TTF_GetError
()
<<
std
::
endl
;
std
::
cerr
<<
"
TTF konnte nicht initialisiert werden
: "
<<
TTF_GetError
()
<<
std
::
endl
;
return
;
}
...
...
@@ -72,52 +88,70 @@ void UnitInfoMenu::render(Engine& engine)
return
;
}
SDL_Color
white
=
{
255
,
255
,
255
,
255
};
int
spacing
=
5
;
SDL_Color
yellow
=
{
255
,
255
,
0
,
255
};
// Gelb für den Text
int
spacing
=
10
;
// Abstand zwischen den Textzeilen
UnitId
unitId
=
m_currentUnit
->
getId
();
// Draw background box
SDL_SetRenderDrawColor
(
engine
.
renderer
(),
0
,
0
,
255
,
255
);
SDL_Rect
box
=
{
m_x
,
m_y
-
3
,
200
*
RENDERING_SCALE
,
120
*
RENDERING_SCALE
};
// Textzeilen, einschließlich der Beschreibung
std
::
vector
<
std
::
string
>
info_lines
=
{
"HP "
+
std
::
to_string
(
m_currentUnit
->
getHealth
()),
"Movement "
+
std
::
to_string
(
m_currentUnit
->
m_movementPoints
),
"Ammo "
+
std
::
to_string
(
m_currentUnit
->
getAmmo
()),
"Movement Type "
+
getMovementTypeString
(
m_currentUnit
->
m_movementType
),
"Cost "
+
std
::
to_string
(
m_currentUnit
->
getCost
()),
unitDescriptions
[
unitId
]
// Beschreibung einfügen
};
int
max_text_width
=
0
;
int
total_height
=
0
;
std
::
vector
<
SDL_Texture
*>
textures
;
for
(
const
auto
&
line
:
info_lines
)
{
SDL_Surface
*
textSurface
=
TTF_RenderText_Solid
(
font
,
line
.
c_str
(),
yellow
);
if
(
!
textSurface
)
continue
;
max_text_width
=
std
::
max
(
max_text_width
,
textSurface
->
w
);
total_height
+=
textSurface
->
h
+
spacing
;
SDL_Texture
*
textTexture
=
SDL_CreateTextureFromSurface
(
engine
.
renderer
(),
textSurface
);
textures
.
push_back
(
textTexture
);
SDL_FreeSurface
(
textSurface
);
}
int
width
=
std
::
max
(
max_text_width
+
20
*
RENDERING_SCALE
,
16
*
RENDERING_SCALE
+
20
);
int
height
=
total_height
+
30
*
RENDERING_SCALE
;
// Die Höhe anpassen, um alle Textzeilen zu integrieren
SDL_Rect
box
=
{
m_x
,
m_y
,
width
,
height
};
SDL_SetRenderDrawColor
(
engine
.
renderer
(),
75
,
87
,
219
,
255
);
// Schwarzes Hintergrundrechteck
SDL_RenderFillRect
(
engine
.
renderer
(),
&
box
);
SDL_SetRenderDrawColor
(
engine
.
renderer
(),
255
,
255
,
255
,
255
);
// Weißer Rahmen
SDL_RenderDrawRect
(
engine
.
renderer
(),
&
box
);
// Render unit sprite
Spritesheet
*
spritesheet
=
engine
.
getSpritesheet
();
SDL_Texture
*
unit_texture
=
spritesheet
->
getUnitTextures
()
.
at
(
static_cast
<
int
>
(
m_currentUnit
->
getFaction
()))
.
at
(
static_cast
<
int
>
(
m_currentUnit
->
getId
()
))
.
at
(
static_cast
<
int
>
(
unitId
))
.
at
(
static_cast
<
int
>
(
UnitState
::
IDLE
))
.
first
;
SDL_Rect
sprite_rect
=
{
m_x
+
10
*
RENDERING_SCALE
,
m_y
+
10
*
RENDERING_SCALE
,
16
*
RENDERING_SCALE
,
16
*
RENDERING_SCALE
};
SDL_Rect
sprite_rect
=
{
m_x
+
10
,
m_y
+
10
,
16
*
RENDERING_SCALE
,
16
*
RENDERING_SCALE
};
SDL_Rect
source_rect
=
{
0
,
0
,
16
,
16
};
SDL_RenderCopy
(
engine
.
renderer
(),
unit_texture
,
&
source_rect
,
&
sprite_rect
);
// Render unit information
std
::
vector
<
std
::
string
>
info_lines
=
{
"HP: "
+
std
::
to_string
(
m_currentUnit
->
getHealth
()),
"Movement: "
+
std
::
to_string
(
m_currentUnit
->
m_movementPoints
),
"Ammo: "
+
std
::
to_string
(
m_currentUnit
->
getAmmo
()),
"Movement Type: "
+
getMovementTypeString
(
m_currentUnit
->
m_movementType
),
"Cost: "
+
std
::
to_string
(
m_currentUnit
->
m_price
)};
int
text_y
=
m_y
+
50
*
RENDERING_SCALE
;
for
(
const
auto
&
line
:
info_lines
)
// Text zeichnen
int
text_y
=
m_y
+
20
*
RENDERING_SCALE
;
// Starte etwas unterhalb des Sprites
for
(
auto
*
texture
:
textures
)
{
SDL_Surface
*
textSurface
=
TTF_RenderText_Solid
(
font
,
line
.
c_str
(),
white
);
if
(
!
textSurface
)
continue
;
SDL_Texture
*
textTexture
=
SDL_CreateTextureFromSurface
(
engine
.
renderer
(),
textSurface
);
SDL_Rect
textRect
=
{
m_x
+
10
*
RENDERING_SCALE
,
text_y
,
textSurface
->
w
,
textSurface
->
h
};
SDL_RenderCopy
(
engine
.
renderer
(),
textTexture
,
nullptr
,
&
textRect
);
SDL_DestroyTexture
(
textTexture
);
SDL_FreeSurface
(
textSurface
);
text_y
+=
spacing
*
RENDERING_SCALE
;
int
w
,
h
;
SDL_QueryTexture
(
texture
,
nullptr
,
nullptr
,
&
w
,
&
h
);
SDL_Rect
textRect
=
{
m_x
+
10
,
text_y
,
w
,
h
};
SDL_RenderCopy
(
engine
.
renderer
(),
texture
,
nullptr
,
&
textRect
);
SDL_DestroyTexture
(
texture
);
text_y
+=
(
h
+
spacing
);
}
TTF_CloseFont
(
font
);
...
...
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