Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C++ Abschlussprojekt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Leon Niklas Lux
C++ Abschlussprojekt
Commits
22df07bf
Unverified
Commit
22df07bf
authored
3 months ago
by
Leon Niklas Lux
Committed by
Leon Lux
3 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Entfernung von Renderable
parent
d0a0fe56
Branches
Branches containing commit
No related tags found
1 merge request
!55
Abgabe des Projektes
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
game/src/Renderable.cpp
+0
-88
0 additions, 88 deletions
game/src/Renderable.cpp
game/src/Renderable.hpp
+0
-115
0 additions, 115 deletions
game/src/Renderable.hpp
with
0 additions
and
203 deletions
game/src/Renderable.cpp
deleted
100644 → 0
+
0
−
88
View file @
d0a0fe56
#include
"Renderable.hpp"
#include
"GameWindow.hpp"
namespace
game
{
Renderable
::
Renderable
(
GameWindow
*
window
,
const
std
::
string
&
filePath
)
:
m_window
(
window
),
m_renderer
(
window
->
renderer
())
{
// check if filePath ends with .png (else its animated)
if
(
filePath
.
rfind
(
".png"
)
==
(
filePath
.
size
()
-
4
))
{
m_texture
=
LoadTexture
(
m_renderer
,
filePath
);
computeSourceRect
();
}
}
Renderable
::~
Renderable
()
{
if
(
m_texture
)
{
SDL_DestroyTexture
(
m_texture
);
m_texture
=
nullptr
;
}
}
SDL_Texture
*
Renderable
::
LoadTexture
(
SDL_Renderer
*
renderer
,
std
::
string
texFileName
)
{
// Check if SDL Image Support is available
if
(
!
(
IMG_Init
(
IMG_INIT_PNG
)
&
IMG_INIT_PNG
))
{
std
::
cout
<<
"SDL_image could not initialize PNG support! SDL_image Error: "
<<
IMG_GetError
()
<<
std
::
endl
;
}
// Load the texture
SDL_Texture
*
newTexture
=
IMG_LoadTexture
(
renderer
,
texFileName
.
c_str
());
if
(
newTexture
==
nullptr
)
{
std
::
cout
<<
"Failed to load PNG texture! SDL_image Error: "
<<
IMG_GetError
()
<<
std
::
endl
;
}
return
newTexture
;
}
void
Renderable
::
computeSourceRect
()
{
m_sourceRect
=
{
0
,
0
,
0
,
0
};
if
(
m_texture
)
{
Uint32
format
;
int
access
,
w
,
h
;
SDL_QueryTexture
(
m_texture
,
&
format
,
&
access
,
&
w
,
&
h
);
m_sourceRect
.
w
=
w
;
m_sourceRect
.
h
=
h
;
m_targetRect
.
w
=
w
;
m_targetRect
.
h
=
h
;
}
}
void
Renderable
::
render
()
{
SDL_RenderCopyEx
(
m_renderer
,
m_texture
,
&
m_sourceRect
,
&
m_targetRect
,
0
,
nullptr
,
SDL_FLIP_NONE
);
}
int
Renderable
::
w
()
const
{
return
m_targetRect
.
w
;
}
int
Renderable
::
h
()
const
{
return
m_targetRect
.
h
;
}
int
Renderable
::
x
()
const
{
// std::cout << m_position << std::endl;
return
m_targetRect
.
x
;
}
int
Renderable
::
y
()
const
{
return
m_targetRect
.
y
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
game/src/Renderable.hpp
deleted
100644 → 0
+
0
−
115
View file @
d0a0fe56
#pragma once
#include
"GameWindow.hpp"
#include
<SDL2/SDL.h>
#include
<cstdint>
#include
<fstream>
#include
<iostream>
#include
<string>
namespace
game
{
/**
* @class Renderable
* @brief A class representing an object that can be rendered to the screen.
*
* The `Renderable` class provides an interface for objects that need to be drawn to the screen. It handles the loading
* of textures, rendering the object, and computing the object's source and target rectangles for rendering.
*/
class
Renderable
{
public:
/**
* @brief Constructs a Renderable object with the given window and texture file path.
*
* This constructor initializes the renderable object, setting up the window and loading the texture for rendering.
*
* @param window The game window that will be used for rendering.
* @param filePath The path to the texture file to be loaded.
*/
Renderable
(
GameWindow
*
window
,
const
std
::
string
&
filePath
);
/**
* @brief Destructor for the Renderable class.
*
* This destructor cleans up resources associated with the renderable object, such as the SDL_Texture.
*/
~
Renderable
();
/**
* @brief Renders the object to the screen.
*
* This method performs the actual rendering of the object to the screen using the loaded texture and target
* rectangle. The method is virtual to allow for customization in derived classes.
*/
virtual
void
render
();
/**
* @brief Gets the width of the renderable object.
*
* This method retrieves the width of the renderable object's texture.
*
* @return The width of the texture.
*/
virtual
int
w
()
const
;
/**
* @brief Gets the height of the renderable object.
*
* This method retrieves the height of the renderable object's texture.
*
* @return The height of the texture.
*/
virtual
int
h
()
const
;
/**
* @brief Gets the x-coordinate of the renderable object's position.
*
* This method retrieves the x-coordinate of the object's position, used for rendering placement.
*
* @return The x-coordinate of the object's position.
*/
virtual
int
x
()
const
;
/**
* @brief Gets the y-coordinate of the renderable object's position.
*
* This method retrieves the y-coordinate of the object's position, used for rendering placement.
*
* @return The y-coordinate of the object's position.
*/
virtual
int
y
()
const
;
/**
* @brief Loads an SDL texture from a file.
*
* This method loads a texture from the specified file path, with the option to specify a transparency color (RGB).
* The loaded texture can then be used for rendering.
*
* @param renderer The SDL renderer used to create the texture.
* @param filename The path to the texture file.
* @param key_r Transparency color, red channel.
* @param key_g Transparency color, green channel.
* @param key_b Transparency color, blue channel.
*
* @return The loaded SDL_Texture, or NULL if an error occurs.
*/
SDL_Texture
*
LoadTexture
(
SDL_Renderer
*
renderer
,
std
::
string
filename
);
protected:
GameWindow
*
m_window
;
/**< A pointer to the game window used for rendering. */
SDL_Renderer
*
m_renderer
;
/**< A pointer to the SDL renderer used for rendering the texture. */
SDL_Texture
*
m_texture
;
/**< A pointer to the SDL texture used for rendering the object. */
SDL_Rect
m_sourceRect
;
/**< A rectangle representing the part of the texture to be rendered. */
SDL_Rect
m_targetRect
;
/**< A rectangle representing where the texture will be rendered on the screen. */
/**
* @brief Computes the source rectangle for texture rendering.
*
* This method calculates the source rectangle, which determines the portion of the texture that will be rendered.
* It is typically used when the texture is part of a larger sprite sheet.
*/
void
computeSourceRect
();
};
}
\ No newline at end of file
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