arreglos d'estil
This commit is contained in:
@@ -1,16 +1,16 @@
|
|||||||
#include "ball.h"
|
#include "ball.h"
|
||||||
#include <stdlib.h> // for rand
|
#include <stdlib.h> // for rand
|
||||||
#include <cmath> // for fabs
|
#include <cmath> // for fabs
|
||||||
#include "defines.h" // for BALL_SIZE, Color, SCREEN_HEIGHT, GRAVITY_FORCE
|
#include "defines.h" // for BALL_SIZE, Color, SCREEN_HEIGHT, GRAVITY_FORCE
|
||||||
class Texture;
|
class Texture;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Ball::Ball(float x, float vx, float vy, Color color, std::shared_ptr<Texture> texture)
|
Ball::Ball(float x, float vx, float vy, Color color, std::shared_ptr<Texture> texture)
|
||||||
|
: sprite_(std::make_unique<Sprite>(texture)),
|
||||||
|
pos_({x, 0.0f, BALL_SIZE, BALL_SIZE})
|
||||||
{
|
{
|
||||||
pos_ = {x, 0.0f, BALL_SIZE, BALL_SIZE};
|
|
||||||
vx_ = vx;
|
vx_ = vx;
|
||||||
vy_ = vy;
|
vy_ = vy;
|
||||||
sprite_ = std::make_unique<Sprite>(texture);
|
|
||||||
sprite_->setPos({pos_.x, pos_.y});
|
sprite_->setPos({pos_.x, pos_.y});
|
||||||
sprite_->setSize(BALL_SIZE, BALL_SIZE);
|
sprite_->setSize(BALL_SIZE, BALL_SIZE);
|
||||||
sprite_->setClip({0, 0, BALL_SIZE, BALL_SIZE});
|
sprite_->setClip({0, 0, BALL_SIZE, BALL_SIZE});
|
||||||
|
|||||||
@@ -1,125 +1,128 @@
|
|||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
#include <SDL3/SDL_error.h> // for SDL_GetError
|
#include <SDL3/SDL_error.h> // Para SDL_GetError
|
||||||
#include <SDL3/SDL_log.h> // for SDL_Log
|
#include <SDL3/SDL_log.h> // Para SDL_Log
|
||||||
#include <SDL3/SDL_pixels.h> // for SDL_PixelFormat
|
#include <SDL3/SDL_pixels.h> // Para SDL_PixelFormat
|
||||||
#include <SDL3/SDL_surface.h> // for SDL_CreateSurfaceFrom, SDL_DestroySurface
|
#include <SDL3/SDL_surface.h> // Para SDL_CreateSurfaceFrom, SDL_DestroySurface
|
||||||
#include <stdio.h> // for NULL
|
#include <stdio.h> // Para NULL
|
||||||
#include <stdlib.h> // for exit
|
#include <stdlib.h> // Para exit
|
||||||
#include <iostream> // for basic_ostream, char_traits, operator<<
|
#include <iostream> // Para basic_ostream, char_traits, operator<<
|
||||||
#include <string> // for operator<<, string
|
#include <string> // Para operator<<, string
|
||||||
#include "stb_image.h" // for stbi_failure_reason, stbi_image_free
|
#include "stb_image.h" // Para stbi_failure_reason, stbi_image_free
|
||||||
|
|
||||||
Texture::Texture(SDL_Renderer *renderer)
|
Texture::Texture(SDL_Renderer *renderer)
|
||||||
: renderer_(renderer), texture_(nullptr), width_(0), height_(0) {}
|
: renderer_(renderer), texture_(nullptr), width_(0), height_(0) {}
|
||||||
|
|
||||||
Texture::Texture(SDL_Renderer *renderer, std::string file_path)
|
Texture::Texture(SDL_Renderer *renderer, const std::string &file_path)
|
||||||
: renderer_(renderer), texture_(nullptr), width_(0), height_(0)
|
: renderer_(renderer), texture_(nullptr), width_(0), height_(0)
|
||||||
{
|
{
|
||||||
loadFromFile(file_path);
|
loadFromFile(file_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::~Texture()
|
Texture::~Texture()
|
||||||
{
|
{
|
||||||
free();
|
free();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Texture::loadFromFile(std::string file_path)
|
// Carga la imagen desde una ruta especificada
|
||||||
|
bool Texture::loadFromFile(const std::string &file_path)
|
||||||
{
|
{
|
||||||
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||||
int req_format = STBI_rgb_alpha;
|
int req_format = STBI_rgb_alpha;
|
||||||
int width, height, orig_format;
|
int width, height, orig_format;
|
||||||
unsigned char *data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
|
unsigned char *data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
|
||||||
if (data == nullptr)
|
if (data == nullptr)
|
||||||
{
|
{
|
||||||
SDL_Log("Loading image failed: %s", stbi_failure_reason());
|
SDL_Log("Error al cargar la imagen: %s", stbi_failure_reason());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::cout << "Image loaded: " << filename.c_str() << std::endl;
|
std::cout << "Imagen cargada: " << filename.c_str() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pitch;
|
int pitch;
|
||||||
SDL_PixelFormat pixel_format;
|
SDL_PixelFormat pixel_format;
|
||||||
if (req_format == STBI_rgb)
|
if (req_format == STBI_rgb)
|
||||||
{
|
{
|
||||||
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
|
pitch = 3 * width; // 3 bytes por pixel * pixels por línea
|
||||||
pixel_format = SDL_PIXELFORMAT_RGB24;
|
pixel_format = SDL_PIXELFORMAT_RGB24;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ // STBI_rgb_alpha (RGBA)
|
{ // STBI_rgb_alpha (RGBA)
|
||||||
pitch = 4 * width;
|
pitch = 4 * width;
|
||||||
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Limpia
|
// Libera la memoria previa
|
||||||
free();
|
free();
|
||||||
|
|
||||||
// La textura final
|
// La textura final
|
||||||
SDL_Texture *new_texture = nullptr;
|
SDL_Texture *new_texture = nullptr;
|
||||||
|
|
||||||
// Carga la imagen desde una ruta específica
|
// Crea la superficie de la imagen desde los datos cargados
|
||||||
SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
|
SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
|
||||||
if (loaded_surface == nullptr)
|
if (loaded_surface == nullptr)
|
||||||
{
|
{
|
||||||
std::cout << "Unable to load image " << file_path << std::endl;
|
std::cout << "No se pudo cargar la imagen " << file_path << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Crea la textura desde los pixels de la surface
|
// Crea la textura desde los píxeles de la superficie
|
||||||
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
||||||
if (new_texture == nullptr)
|
if (new_texture == nullptr)
|
||||||
{
|
{
|
||||||
std::cout << "Unable to create texture from " << file_path << "! SDL Error: " << SDL_GetError() << std::endl;
|
std::cout << "No se pudo crear la textura desde " << file_path << "! Error de SDL: " << SDL_GetError() << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Obtiene las dimensiones de la imagen
|
// Obtiene las dimensiones de la imagen
|
||||||
width_ = loaded_surface->w;
|
width_ = loaded_surface->w;
|
||||||
height_ = loaded_surface->h;
|
height_ = loaded_surface->h;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Elimina la textura cargada
|
// Destruye la superficie cargada
|
||||||
SDL_DestroySurface(loaded_surface);
|
SDL_DestroySurface(loaded_surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return success
|
// Devuelve el resultado del proceso
|
||||||
stbi_image_free(data);
|
stbi_image_free(data);
|
||||||
texture_ = new_texture;
|
texture_ = new_texture;
|
||||||
return texture_ != nullptr;
|
return texture_ != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Libera la textura si existe
|
||||||
void Texture::free()
|
void Texture::free()
|
||||||
{
|
{
|
||||||
// Free texture if it exists
|
if (texture_ != nullptr)
|
||||||
if (texture_ != NULL)
|
{
|
||||||
{
|
SDL_DestroyTexture(texture_);
|
||||||
SDL_DestroyTexture(texture_);
|
texture_ = nullptr;
|
||||||
texture_ = NULL;
|
width_ = 0;
|
||||||
width_ = 0;
|
height_ = 0;
|
||||||
height_ = 0;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Renderiza la textura en pantalla
|
||||||
void Texture::render(SDL_FRect *src, SDL_FRect *dst)
|
void Texture::render(SDL_FRect *src, SDL_FRect *dst)
|
||||||
{
|
{
|
||||||
// Render to screen
|
SDL_RenderTexture(renderer_, texture_, src, dst);
|
||||||
SDL_RenderTexture(renderer_, texture_, src, dst);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Obtiene el ancho de la imagen
|
||||||
int Texture::getWidth()
|
int Texture::getWidth()
|
||||||
{
|
{
|
||||||
return width_;
|
return width_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Obtiene la altura de la imagen
|
||||||
int Texture::getHeight()
|
int Texture::getHeight()
|
||||||
{
|
{
|
||||||
return height_;
|
return height_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modulación de color
|
// Modula el color de la textura
|
||||||
void Texture::setColor(int r, int g, int b)
|
void Texture::setColor(int r, int g, int b)
|
||||||
{
|
{
|
||||||
SDL_SetTextureColorMod(texture_, r, g, b);
|
SDL_SetTextureColorMod(texture_, r, g, b);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Inicializa las variables
|
// Inicializa las variables
|
||||||
Texture(SDL_Renderer *renderer);
|
explicit Texture(SDL_Renderer *renderer);
|
||||||
Texture(SDL_Renderer *renderer, std::string file_path);
|
Texture(SDL_Renderer *renderer, const std::string &file_path);
|
||||||
|
|
||||||
// Libera la memoria
|
// Libera la memoria
|
||||||
~Texture();
|
~Texture();
|
||||||
|
|
||||||
// Carga una imagen desde la ruta especificada
|
// Carga una imagen desde la ruta especificada
|
||||||
bool loadFromFile(std::string path);
|
bool loadFromFile(const std::string &path);
|
||||||
|
|
||||||
// Libera la textura
|
// Libera la textura
|
||||||
void free();
|
void free();
|
||||||
|
|||||||
Reference in New Issue
Block a user