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
b3d868fc
Commit
b3d868fc
authored
5 months ago
by
David Maul
Browse files
Options
Downloads
Patches
Plain Diff
add scaling factor
parent
a70e683d
No related branches found
No related tags found
1 merge request
!2
Basic spritesheet
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
src/level.cpp
+46
-37
46 additions, 37 deletions
src/level.cpp
src/main.cpp
+6
-4
6 additions, 4 deletions
src/main.cpp
src/spritesheet.cpp
+50
-43
50 additions, 43 deletions
src/spritesheet.cpp
with
104 additions
and
84 deletions
.gitignore
+
2
−
0
View file @
b3d868fc
...
...
@@ -54,6 +54,8 @@ Thumbs.db
*.gcov
coverage/
*.png
docs/generated/
bin/
...
...
This diff is collapsed.
Click to expand it.
src/level.cpp
+
46
−
37
View file @
b3d868fc
...
...
@@ -8,7 +8,8 @@
#include
<iostream>
#include
<string>
namespace
advanced_wars
{
namespace
advanced_wars
{
Level
::
Level
(
std
::
string
name
,
int
width
,
int
height
,
std
::
vector
<
Tile
>
tiles
,
std
::
vector
<
Building
>
buildings
,
std
::
vector
<
Unit
>
units
)
...
...
@@ -21,23 +22,30 @@ Level::Level(std::string name, int width, int height, std::vector<Tile> tiles,
*/
};
void
Level
::
render
(
Engine
&
engine
,
std
::
vector
<
SDL_Event
>
&
events
)
{
void
Level
::
render
(
Engine
&
engine
,
std
::
vector
<
SDL_Event
>
&
events
)
{
const
int
RENDERING_SCALE
=
3
;
// Iterate over all events
while
(
!
events
.
empty
())
{
while
(
!
events
.
empty
())
{
events
.
erase
(
events
.
begin
());
}
for
(
int
y
=
0
;
y
<
this
->
height
;
y
++
)
{
for
(
int
x
=
0
;
x
<
this
->
width
;
x
++
)
{
for
(
int
y
=
0
;
y
<
this
->
height
;
y
++
)
{
for
(
int
x
=
0
;
x
<
this
->
width
;
x
++
)
{
Spritesheet
*
spritesheet
=
engine
.
get_spritesheet
();
SDL_Rect
dst
;
dst
.
x
=
x
*
spritesheet
->
get_tile_width
();
dst
.
y
=
y
*
spritesheet
->
get_tile_height
();
dst
.
w
=
spritesheet
->
get_tile_width
();
dst
.
h
=
spritesheet
->
get_tile_height
();
dst
.
x
=
x
*
spritesheet
->
get_tile_width
()
*
RENDERING_SCALE
;
dst
.
y
=
y
*
spritesheet
->
get_tile_height
()
*
RENDERING_SCALE
;
dst
.
w
=
spritesheet
->
get_tile_width
()
*
RENDERING_SCALE
;
dst
.
h
=
spritesheet
->
get_tile_height
()
*
RENDERING_SCALE
;
if
(
spritesheet
->
render_tile
(
engine
.
renderer
(),
0
,
0
,
&
dst
)
!=
0
)
{
if
(
spritesheet
->
render_tile
(
engine
.
renderer
(),
0
,
0
,
&
dst
)
!=
0
)
{
throw
std
::
runtime_error
(
"error while rendering a tile: "
+
std
::
string
(
SDL_GetError
()));
}
...
...
@@ -45,7 +53,8 @@ void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
}
// Set background color for renderer
if
(
SDL_SetRenderDrawColor
(
engine
.
renderer
(),
255
,
0
,
0
,
0
))
{
if
(
SDL_SetRenderDrawColor
(
engine
.
renderer
(),
255
,
0
,
0
,
0
))
{
std
::
cout
<<
"Could not set render draw color: "
<<
SDL_GetError
()
<<
std
::
endl
;
}
...
...
This diff is collapsed.
Click to expand it.
src/main.cpp
+
6
−
4
View file @
b3d868fc
...
...
@@ -7,9 +7,10 @@
using
namespace
advanced_wars
;
int
main
()
{
int
main
()
{
Window
window
(
"Advanced Wars"
,
80
0
,
6
0
0
);
Window
window
(
"Advanced Wars"
,
96
0
,
9
60
);
Engine
engine
(
window
);
...
...
@@ -18,11 +19,12 @@ int main() {
engine
.
set_scene
(
level
);
Spritesheet
spritesheet
(
"
/media/data/cpp/cpp-project/t
es
t
.png"
,
engine
);
Spritesheet
spritesheet
(
"
../til
es.png"
,
engine
);
engine
.
set_spritesheet
(
spritesheet
);
while
(
!
engine
.
exited
())
{
while
(
!
engine
.
exited
())
{
engine
.
pump
();
engine
.
render
();
}
...
...
This diff is collapsed.
Click to expand it.
src/spritesheet.cpp
+
50
−
43
View file @
b3d868fc
...
...
@@ -6,18 +6,22 @@
#include
<SDL_image.h>
#include
<stdexcept>
namespace
advanced_wars
{
namespace
advanced_wars
{
Spritesheet
::
Spritesheet
(
std
::
string
path
,
Engine
&
engine
)
{
Spritesheet
::
Spritesheet
(
std
::
string
path
,
Engine
&
engine
)
{
SDL_Surface
*
loadedSurface
=
IMG_Load
(
path
.
c_str
());
if
(
loadedSurface
==
nullptr
)
{
if
(
loadedSurface
==
nullptr
)
{
throw
std
::
runtime_error
(
"Fehler beim Laden des Bildes "
+
path
+
": "
+
std
::
string
(
IMG_GetError
()));
}
texture
=
SDL_CreateTextureFromSurface
(
engine
.
renderer
(),
loadedSurface
);
if
(
texture
==
nullptr
)
{
if
(
texture
==
nullptr
)
{
throw
std
::
runtime_error
(
"Fehler beim Erstellen der Textur: "
+
std
::
string
(
SDL_GetError
()));
}
...
...
@@ -28,6 +32,7 @@ Spritesheet::Spritesheet(std::string path, Engine &engine) {
this
->
tile_width
=
16
;
this
->
tile_height
=
16
;
this
->
tiles
.
push_back
(
std
::
pair
(
0
,
1
));
this
->
tiles
.
push_back
(
std
::
pair
(
1
,
4
));
}
int
Spritesheet
::
get_tile_steps
(
int
tile
)
{
return
tiles
.
at
(
tile
).
second
;
}
...
...
@@ -37,8 +42,10 @@ int Spritesheet::get_tile_width() { return tile_width; }
int
Spritesheet
::
get_tile_height
()
{
return
tile_height
;
}
int
Spritesheet
::
render_tile
(
SDL_Renderer
*
renderer
,
int
tile
,
int
step
,
SDL_Rect
*
rect
)
{
if
(
step
>=
this
->
get_tile_steps
(
tile
)
||
step
<
0
)
{
SDL_Rect
*
rect
)
{
if
(
step
>=
this
->
get_tile_steps
(
tile
)
||
step
<
0
)
{
throw
std
::
runtime_error
(
"Tried to access step "
+
std
::
to_string
(
step
)
+
" for tile "
+
std
::
to_string
(
tile
));
}
...
...
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