Skip to content
Snippets Groups Projects
Unverified Commit 22df07bf authored by Leon Niklas Lux's avatar Leon Niklas Lux Committed by Leon Lux
Browse files

Entfernung von Renderable

parent d0a0fe56
Branches
No related tags found
1 merge request!55Abgabe des Projektes
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment