migrat a SDL3
This commit is contained in:
55
CMakeLists.txt
Normal file
55
CMakeLists.txt
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
project(demo4_sprites)
|
||||||
|
|
||||||
|
# Establecer el estándar de C++
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
# Opciones comunes de compilación
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Os")
|
||||||
|
|
||||||
|
# Buscar SDL3 automáticamente
|
||||||
|
find_package(SDL3 REQUIRED)
|
||||||
|
|
||||||
|
# Si no se encuentra SDL3, generar un error
|
||||||
|
if (NOT SDL3_FOUND)
|
||||||
|
message(FATAL_ERROR "SDL3 no encontrado. Por favor, verifica su instalación.")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Archivos fuente
|
||||||
|
file(GLOB SOURCE_FILES source/*.cpp)
|
||||||
|
|
||||||
|
# Comprobar si se encontraron archivos fuente
|
||||||
|
if(NOT SOURCE_FILES)
|
||||||
|
message(FATAL_ERROR "No se encontraron archivos fuente en el directorio 'source/'. Verifica la ruta.")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Nombre del ejecutable
|
||||||
|
set(EXECUTABLE_NAME demo4_sprites)
|
||||||
|
|
||||||
|
# Detectar la plataforma y configuraciones específicas
|
||||||
|
if(WIN32)
|
||||||
|
set(PLATFORM windows)
|
||||||
|
set(LINK_LIBS ${SDL3_LIBRARIES} mingw32 ws2_32)
|
||||||
|
set(EXECUTABLE_EXTENSION ".exe")
|
||||||
|
elseif(UNIX AND NOT APPLE)
|
||||||
|
set(PLATFORM linux)
|
||||||
|
set(LINK_LIBS ${SDL3_LIBRARIES})
|
||||||
|
set(EXECUTABLE_EXTENSION ".out")
|
||||||
|
elseif(APPLE)
|
||||||
|
set(PLATFORM macos)
|
||||||
|
set(LINK_LIBS ${SDL3_LIBRARIES})
|
||||||
|
set(EXECUTABLE_EXTENSION ".out")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Incluir directorios de SDL3
|
||||||
|
include_directories(${SDL3_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
# Añadir el ejecutable
|
||||||
|
add_executable(${EXECUTABLE_NAME}${EXECUTABLE_EXTENSION} ${SOURCE_FILES})
|
||||||
|
|
||||||
|
# Especificar la ubicación del ejecutable (en la raíz del proyecto)
|
||||||
|
set_target_properties(${EXECUTABLE_NAME}${EXECUTABLE_EXTENSION} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
|
||||||
|
|
||||||
|
# Enlazar las bibliotecas necesarias
|
||||||
|
target_link_libraries(${EXECUTABLE_NAME}${EXECUTABLE_EXTENSION} ${LINK_LIBS})
|
||||||
25
Makefile
25
Makefile
@@ -1,31 +1,22 @@
|
|||||||
# Variables comunes
|
# Variables comunes
|
||||||
source := source/*.cpp
|
source := source/*.cpp
|
||||||
executable_name := demo_pelotas1
|
executable_name := demo4_sprites
|
||||||
CXX := g++ # Cambiar a clang++ si prefieres
|
CXXFLAGS := -std=c++20 -Wall -Os # Opciones comunes de compilación
|
||||||
CXXFLAGS := -std=c++11 -Wall # Opciones comunes de compilación
|
LDFLAGS := -lSDL2 # Flags de enlace comunes
|
||||||
LDFLAGS := -lSDL2 # Flags de enlace comunes
|
OUTPUT_EXT :=
|
||||||
|
|
||||||
# Detectar plataforma
|
# Detectar plataforma y configurar
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
PLATFORM := windows
|
|
||||||
LDFLAGS += -lmingw32 -lws2_32 -lSDL2main
|
LDFLAGS += -lmingw32 -lws2_32 -lSDL2main
|
||||||
OUTPUT_EXT := .exe
|
OUTPUT_EXT := .exe
|
||||||
else
|
else
|
||||||
PLATFORM := unix # Unificación para macOS y Linux
|
|
||||||
OUTPUT_EXT := .out
|
OUTPUT_EXT := .out
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Regla principal: compilar según la plataforma detectada
|
# Regla principal: compilar el ejecutable
|
||||||
all: $(PLATFORM)
|
all:
|
||||||
|
|
||||||
# Compilación para Windows
|
|
||||||
windows:
|
|
||||||
$(CXX) $(source) $(CXXFLAGS) $(LDFLAGS) -o $(executable_name)$(OUTPUT_EXT)
|
|
||||||
|
|
||||||
# Compilación para Unix (Linux y macOS)
|
|
||||||
unix:
|
|
||||||
$(CXX) $(source) $(CXXFLAGS) $(LDFLAGS) -o $(executable_name)$(OUTPUT_EXT)
|
$(CXX) $(source) $(CXXFLAGS) $(LDFLAGS) -o $(executable_name)$(OUTPUT_EXT)
|
||||||
|
|
||||||
# Regla para limpiar archivos generados
|
# Regla para limpiar archivos generados
|
||||||
clean:
|
clean:
|
||||||
rm -f $(executable_name)*
|
rm -f $(executable_name)$(OUTPUT_EXT)
|
||||||
@@ -12,7 +12,7 @@ Ball::Ball(int x, int y, int w, int h, int vx, int vy, Texture *texture)
|
|||||||
sprite = new Sprite(texture);
|
sprite = new Sprite(texture);
|
||||||
sprite->setPos({x, y});
|
sprite->setPos({x, y});
|
||||||
sprite->setSize(w, h);
|
sprite->setSize(w, h);
|
||||||
sprite->setClip({0, 0, w, h});
|
sprite->setClip({0, 0, static_cast<float>(w), static_cast<float>(h)});
|
||||||
color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32};
|
color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,9 +45,9 @@ void Ball::update()
|
|||||||
collision = true;
|
collision = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (x + w > SCREEN_WIDTH)
|
if (x + w > DEMO_WIDTH)
|
||||||
{
|
{
|
||||||
x = SCREEN_WIDTH - w;
|
x = DEMO_WIDTH - w;
|
||||||
vx = -luckyX;
|
vx = -luckyX;
|
||||||
collision = true;
|
collision = true;
|
||||||
}
|
}
|
||||||
@@ -59,9 +59,9 @@ void Ball::update()
|
|||||||
collision = true;
|
collision = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (y + h > SCREEN_HEIGHT)
|
if (y + h > DEMO_HEIGHT)
|
||||||
{
|
{
|
||||||
y = SCREEN_HEIGHT - h;
|
y = DEMO_HEIGHT - h;
|
||||||
vy = -luckyY;
|
vy = -luckyY;
|
||||||
collision = true;
|
collision = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ private:
|
|||||||
int w; // Ancho
|
int w; // Ancho
|
||||||
int h; // Alto
|
int h; // Alto
|
||||||
int vx, vy; // Velocidad
|
int vx, vy; // Velocidad
|
||||||
color_t color; // Color de la pelota
|
Color color; // Color de la pelota
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
|
|||||||
@@ -1,19 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "SDL2/SDL.h"
|
constexpr Uint64 DEMO_SPEED = 1000/60;
|
||||||
#include <stdlib.h> /* srand, rand */
|
|
||||||
|
|
||||||
#define SCREEN_WIDTH 480
|
constexpr int DEMO_WIDTH = 480;
|
||||||
#define SCREEN_HEIGHT 360
|
constexpr int DEMO_HEIGHT = 360;
|
||||||
#define NUM_BALLS 100
|
constexpr int WINDOW_ZOOM = 2;
|
||||||
#define BALL_SIZE 36
|
constexpr int NUM_BALLS = 100;
|
||||||
#define NUM_FRAMES 4
|
constexpr int BALL_SIZE = 36;
|
||||||
#define BG_R 96
|
constexpr int NUM_FRAMES = 4;
|
||||||
#define BG_G 96
|
|
||||||
#define BG_B 96
|
|
||||||
#define TEXTURE_FILE "resources/soccer_ball.png"
|
|
||||||
|
|
||||||
struct color_t
|
constexpr int BG_R = 96;
|
||||||
|
constexpr int BG_G = 96;
|
||||||
|
constexpr int BG_B = 96;
|
||||||
|
|
||||||
|
constexpr char TEXTURE_FILE[] = "resources/soccer_ball.png";
|
||||||
|
|
||||||
|
struct Color
|
||||||
{
|
{
|
||||||
int r, g, b;
|
int r, g, b;
|
||||||
};
|
};
|
||||||
@@ -2,15 +2,15 @@
|
|||||||
#include "ball.h"
|
#include "ball.h"
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
SDL_Window *window = NULL;
|
SDL_Window *window = nullptr;
|
||||||
SDL_Renderer *renderer = NULL;
|
SDL_Renderer *renderer = nullptr;
|
||||||
SDL_Event *event;
|
|
||||||
Texture *texture = nullptr;
|
Texture *texture = nullptr;
|
||||||
Ball *ball[NUM_BALLS];
|
Ball *ball[NUM_BALLS];
|
||||||
|
|
||||||
bool shouldExit = false;
|
bool should_exit = false;
|
||||||
Uint32 ticks = 0;
|
Uint64 ticks = 0;
|
||||||
|
|
||||||
bool init()
|
bool init()
|
||||||
{
|
{
|
||||||
@@ -18,7 +18,7 @@ bool init()
|
|||||||
bool success = true;
|
bool success = true;
|
||||||
|
|
||||||
// Initialize SDL
|
// Initialize SDL
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
if (SDL_Init(SDL_INIT_VIDEO))
|
||||||
{
|
{
|
||||||
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
|
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
|
||||||
success = false;
|
success = false;
|
||||||
@@ -26,7 +26,7 @@ bool init()
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Create window
|
// Create window
|
||||||
window = SDL_CreateWindow("demo_pelotas1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN);
|
window = SDL_CreateWindow("demo_pelotas1", DEMO_WIDTH * WINDOW_ZOOM, DEMO_HEIGHT * WINDOW_ZOOM, SDL_WINDOW_OPENGL);
|
||||||
if (window == NULL)
|
if (window == NULL)
|
||||||
{
|
{
|
||||||
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
||||||
@@ -35,7 +35,7 @@ bool init()
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Create renderer for window
|
// Create renderer for window
|
||||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
renderer = SDL_CreateRenderer(window, nullptr);
|
||||||
if (renderer == NULL)
|
if (renderer == NULL)
|
||||||
{
|
{
|
||||||
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
|
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
|
||||||
@@ -46,26 +46,24 @@ bool init()
|
|||||||
// Initialize renderer color
|
// Initialize renderer color
|
||||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||||
|
|
||||||
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
SDL_SetRenderLogicalPresentation(renderer, DEMO_WIDTH, DEMO_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
event = new SDL_Event();
|
|
||||||
|
|
||||||
texture = new Texture(renderer, TEXTURE_FILE);
|
texture = new Texture(renderer, TEXTURE_FILE);
|
||||||
for (int i = 0; i < NUM_BALLS; ++i)
|
for (int i = 0; i < NUM_BALLS; ++i)
|
||||||
{
|
{
|
||||||
const int vx = rand() % 2 == 0 ? 1 : -1;
|
const int VX = rand() % 2 == 0 ? 1 : -1;
|
||||||
const int vy = rand() % 2 == 0 ? 1 : -1;
|
const int VY = rand() % 2 == 0 ? 1 : -1;
|
||||||
const int x = rand() % SCREEN_WIDTH;
|
const int X = rand() % DEMO_WIDTH;
|
||||||
const int y = rand() % SCREEN_HEIGHT;
|
const int Y = rand() % DEMO_HEIGHT;
|
||||||
const int size = BALL_SIZE;
|
constexpr int SIZE = BALL_SIZE;
|
||||||
ball[i] = new Ball(x, y, size, size, vx, vy, texture);
|
ball[i] = new Ball(X, Y, SIZE, SIZE, VX, VY, texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
ticks = SDL_GetTicks();
|
ticks = SDL_GetTicks();
|
||||||
srand(time(NULL));
|
srand(time(nullptr));
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
@@ -75,13 +73,6 @@ void close()
|
|||||||
// Destroy window
|
// Destroy window
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
window = NULL;
|
|
||||||
renderer = NULL;
|
|
||||||
|
|
||||||
if (event)
|
|
||||||
{
|
|
||||||
delete event;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (texture)
|
if (texture)
|
||||||
{
|
{
|
||||||
@@ -102,22 +93,24 @@ void close()
|
|||||||
|
|
||||||
void checkEvents()
|
void checkEvents()
|
||||||
{
|
{
|
||||||
|
SDL_Event event;
|
||||||
|
|
||||||
// Comprueba los eventos que hay en la cola
|
// Comprueba los eventos que hay en la cola
|
||||||
while (SDL_PollEvent(event) != 0)
|
while (SDL_PollEvent(&event) != 0)
|
||||||
{
|
{
|
||||||
// Evento de salida de la aplicación
|
// Evento de salida de la aplicación
|
||||||
if (event->type == SDL_QUIT)
|
if (event.type == SDL_EVENT_QUIT)
|
||||||
{
|
{
|
||||||
shouldExit = true;
|
should_exit = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event->type == SDL_KEYDOWN && event->key.repeat == 0)
|
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
||||||
{
|
{
|
||||||
switch (event->key.keysym.sym)
|
switch (event.key.key)
|
||||||
{
|
{
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
shouldExit = true;
|
should_exit = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -129,7 +122,7 @@ void checkEvents()
|
|||||||
|
|
||||||
void update()
|
void update()
|
||||||
{
|
{
|
||||||
if (SDL_GetTicks() - ticks > 15)
|
if (SDL_GetTicks() - ticks > DEMO_SPEED)
|
||||||
{
|
{
|
||||||
ticks = SDL_GetTicks();
|
ticks = SDL_GetTicks();
|
||||||
|
|
||||||
@@ -157,7 +150,7 @@ int main(int argc, char *args[])
|
|||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
|
|
||||||
while (!shouldExit)
|
while (!should_exit)
|
||||||
{
|
{
|
||||||
update();
|
update();
|
||||||
checkEvents();
|
checkEvents();
|
||||||
|
|||||||
@@ -3,72 +3,67 @@
|
|||||||
// Constructor
|
// Constructor
|
||||||
Sprite::Sprite(Texture *texture)
|
Sprite::Sprite(Texture *texture)
|
||||||
{
|
{
|
||||||
this->texture = texture;
|
texture_ = texture;
|
||||||
pos = {0, 0, 0, 0};
|
pos_ = {0, 0, 0, 0};
|
||||||
clip = {0, 0, 0, 0};
|
clip_ = {0, 0, 0, 0};
|
||||||
frame = 0;
|
frame_ = 0;
|
||||||
numFrames = NUM_FRAMES;
|
num_frames_ = NUM_FRAMES;
|
||||||
animationSpeed = 20;
|
animation_speed_ = 20;
|
||||||
animationCounter = 0;
|
animation_counter_ = 0;
|
||||||
}
|
|
||||||
|
|
||||||
// Destructor
|
|
||||||
Sprite::~Sprite()
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece la posición del sprite
|
// Establece la posición del sprite
|
||||||
void Sprite::setPos(SDL_Point pos)
|
void Sprite::setPos(SDL_Point pos)
|
||||||
{
|
{
|
||||||
this->pos.x = pos.x;
|
pos_.x = pos.x;
|
||||||
this->pos.y = pos.y;
|
pos_.y = pos.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pinta el sprite
|
// Pinta el sprite
|
||||||
void Sprite::render()
|
void Sprite::render()
|
||||||
{
|
{
|
||||||
texture->render(&clip, &pos);
|
texture_->render(&clip_, &pos_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza la lógica de la clase
|
// Actualiza la lógica de la clase
|
||||||
void Sprite::update()
|
void Sprite::update()
|
||||||
{
|
{
|
||||||
animationCounter++;
|
animation_counter_++;
|
||||||
if (animationCounter % animationSpeed == 0)
|
if (animation_counter_ % animation_speed_ == 0)
|
||||||
{
|
{
|
||||||
animate();
|
animate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el rectangulo de la textura que se va a pintar
|
// Establece el rectangulo de la textura que se va a pintar
|
||||||
void Sprite::setClip(SDL_Rect clip)
|
void Sprite::setClip(SDL_FRect clip)
|
||||||
{
|
{
|
||||||
this->clip = clip;
|
clip_ = clip;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el tamaño del sprite
|
// Establece el tamaño del sprite
|
||||||
void Sprite::setSize(int w, int h)
|
void Sprite::setSize(int w, int h)
|
||||||
{
|
{
|
||||||
this->pos.w = w;
|
pos_.w = w;
|
||||||
this->pos.h = h;
|
pos_.h = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Anima el sprite
|
// Anima el sprite
|
||||||
void Sprite::animate()
|
void Sprite::animate()
|
||||||
{
|
{
|
||||||
frame = (frame + 1) % numFrames;
|
frame_ = (frame_ + 1) % num_frames_;
|
||||||
clip.x = frame * pos.w;
|
clip_.x = frame_ * pos_.w;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modulación de color
|
// Modulación de color
|
||||||
void Sprite::setColor(int r, int g, int b)
|
void Sprite::setColor(int r, int g, int b)
|
||||||
{
|
{
|
||||||
texture->setColor(r, g, b);
|
texture_->setColor(r, g, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cambia la velocidad de la animación
|
// Cambia la velocidad de la animación
|
||||||
void Sprite::setAnimationSpeed(int value)
|
void Sprite::setAnimationSpeed(int value)
|
||||||
{
|
{
|
||||||
animationSpeed = value;
|
animation_speed_ = value;
|
||||||
animationCounter = 0;
|
animation_counter_ = 0;
|
||||||
}
|
}
|
||||||
@@ -6,23 +6,23 @@
|
|||||||
class Sprite
|
class Sprite
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Texture *texture; // Textura con los gráficos del sprite
|
Texture *texture_; // Textura con los gráficos del sprite
|
||||||
SDL_Rect pos; // Posición y tamaño del sprite
|
SDL_FRect pos_; // Posición y tamaño del sprite
|
||||||
SDL_Rect clip; // Parte de la textura que se va a dibujar
|
SDL_FRect clip_; // Parte de la textura que se va a dibujar
|
||||||
int frame; // Frame a dibujar de la textura definido en clip
|
int frame_; // Frame a dibujar de la textura definido en clip
|
||||||
int numFrames; // Numero total de frames
|
int num_frames_; // Numero total de frames
|
||||||
int animationSpeed; // Velocidad de animación
|
int animation_speed_; // Velocidad de animación
|
||||||
int animationCounter; // Contador para la animación
|
int animation_counter_; // Contador para la animación
|
||||||
|
|
||||||
// Anima el sprite
|
// Anima el sprite
|
||||||
void animate();
|
void animate();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Sprite(Texture *texture);
|
explicit Sprite(Texture *texture);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Sprite();
|
~Sprite() = default;
|
||||||
|
|
||||||
// Establece la posición del sprite
|
// Establece la posición del sprite
|
||||||
void setPos(SDL_Point pos);
|
void setPos(SDL_Point pos);
|
||||||
@@ -34,7 +34,7 @@ public:
|
|||||||
void update();
|
void update();
|
||||||
|
|
||||||
// Establece el rectangulo de la textura que se va a pintar
|
// Establece el rectangulo de la textura que se va a pintar
|
||||||
void setClip(SDL_Rect clip);
|
void setClip(SDL_FRect clip);
|
||||||
|
|
||||||
// Establece el tamaño del sprite
|
// Establece el tamaño del sprite
|
||||||
void setSize(int w, int h);
|
void setSize(int w, int h);
|
||||||
|
|||||||
@@ -6,19 +6,19 @@
|
|||||||
|
|
||||||
Texture::Texture(SDL_Renderer *renderer)
|
Texture::Texture(SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
this->renderer = renderer;
|
renderer_ = renderer;
|
||||||
texture = NULL;
|
texture_ = nullptr;
|
||||||
width = 0;
|
width_ = 0;
|
||||||
height = 0;
|
height_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::Texture(SDL_Renderer *renderer, std::string filepath)
|
Texture::Texture(SDL_Renderer *renderer, std::string file_path)
|
||||||
{
|
{
|
||||||
this->renderer = renderer;
|
renderer_ = renderer;
|
||||||
texture = NULL;
|
texture_ = nullptr;
|
||||||
width = 0;
|
width_ = 0;
|
||||||
height = 0;
|
height_ = 0;
|
||||||
loadFromFile(filepath);
|
loadFromFile(file_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::~Texture()
|
Texture::~Texture()
|
||||||
@@ -26,12 +26,12 @@ Texture::~Texture()
|
|||||||
free();
|
free();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Texture::loadFromFile(std::string path)
|
bool Texture::loadFromFile(std::string file_path)
|
||||||
{
|
{
|
||||||
const std::string filename = path.substr(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(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("Loading image failed: %s", stbi_failure_reason());
|
||||||
@@ -39,20 +39,18 @@ bool Texture::loadFromFile(std::string path)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::cout << "Image loaded: " << filename.c_str() << std::endl;
|
std::cout << "Image loaded: " << filename.c_str() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int depth, pitch;
|
int pitch;
|
||||||
Uint32 pixel_format;
|
SDL_PixelFormat pixel_format;
|
||||||
if (req_format == STBI_rgb)
|
if (req_format == STBI_rgb)
|
||||||
{
|
{
|
||||||
depth = 24;
|
|
||||||
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
|
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
|
||||||
pixel_format = SDL_PIXELFORMAT_RGB24;
|
pixel_format = SDL_PIXELFORMAT_RGB24;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ // STBI_rgb_alpha (RGBA)
|
{ // STBI_rgb_alpha (RGBA)
|
||||||
depth = 32;
|
|
||||||
pitch = 4 * width;
|
pitch = 4 * width;
|
||||||
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
||||||
}
|
}
|
||||||
@@ -61,69 +59,69 @@ bool Texture::loadFromFile(std::string path)
|
|||||||
free();
|
free();
|
||||||
|
|
||||||
// La textura final
|
// La textura final
|
||||||
SDL_Texture *newTexture = nullptr;
|
SDL_Texture *new_texture = nullptr;
|
||||||
|
|
||||||
// Carga la imagen desde una ruta específica
|
// Carga la imagen desde una ruta específica
|
||||||
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
|
SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
|
||||||
if (loadedSurface == nullptr)
|
if (loaded_surface == nullptr)
|
||||||
{
|
{
|
||||||
std::cout << "Unable to load image " << path.c_str() << std::endl;
|
std::cout << "Unable to load image " << file_path.c_str() << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Crea la textura desde los pixels de la surface
|
// Crea la textura desde los pixels de la surface
|
||||||
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
|
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
||||||
if (newTexture == nullptr)
|
if (new_texture == nullptr)
|
||||||
{
|
{
|
||||||
std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
|
std::cout << "Unable to create texture from " << file_path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Obtiene las dimensiones de la imagen
|
// Obtiene las dimensiones de la imagen
|
||||||
this->width = loadedSurface->w;
|
width_ = loaded_surface->w;
|
||||||
this->height = loadedSurface->h;
|
height_ = loaded_surface->h;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Elimina la textura cargada
|
// Elimina la textura cargada
|
||||||
SDL_FreeSurface(loadedSurface);
|
SDL_DestroySurface(loaded_surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return success
|
// Return success
|
||||||
stbi_image_free(data);
|
stbi_image_free(data);
|
||||||
texture = newTexture;
|
texture_ = new_texture;
|
||||||
return texture != nullptr;
|
return texture_ != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::free()
|
void Texture::free()
|
||||||
{
|
{
|
||||||
// Free texture if it exists
|
// Free texture if it exists
|
||||||
if (texture != NULL)
|
if (texture_ != NULL)
|
||||||
{
|
{
|
||||||
SDL_DestroyTexture(texture);
|
SDL_DestroyTexture(texture_);
|
||||||
texture = NULL;
|
texture_ = NULL;
|
||||||
width = 0;
|
width_ = 0;
|
||||||
height = 0;
|
height_ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::render(SDL_Rect *src, SDL_Rect *dst)
|
void Texture::render(SDL_FRect *src, SDL_FRect *dst)
|
||||||
{
|
{
|
||||||
// Render to screen
|
// Render to screen
|
||||||
SDL_RenderCopy(renderer, texture, src, dst);
|
SDL_RenderTexture(renderer_, texture_, src, dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Texture::getWidth()
|
int Texture::getWidth()
|
||||||
{
|
{
|
||||||
return width;
|
return width_;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Texture::getHeight()
|
int Texture::getHeight()
|
||||||
{
|
{
|
||||||
return height;
|
return height_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modulación de color
|
// Modulación de color
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// Texture wrapper class
|
// Texture wrapper class
|
||||||
class Texture
|
class Texture
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
SDL_Renderer *renderer;
|
SDL_Renderer *renderer_;
|
||||||
SDL_Texture *texture;
|
SDL_Texture *texture_;
|
||||||
|
|
||||||
// Image dimensions
|
// Image dimensions
|
||||||
int width;
|
int width_;
|
||||||
int height;
|
int height_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Initializes variables
|
// Initializes variables
|
||||||
Texture(SDL_Renderer *renderer);
|
Texture(SDL_Renderer *renderer);
|
||||||
Texture(SDL_Renderer *renderer, std::string filepath);
|
Texture(SDL_Renderer *renderer, std::string file_path);
|
||||||
|
|
||||||
// Deallocates memory
|
// Deallocates memory
|
||||||
~Texture();
|
~Texture();
|
||||||
@@ -29,7 +29,7 @@ public:
|
|||||||
void free();
|
void free();
|
||||||
|
|
||||||
// Renders texture at given point
|
// Renders texture at given point
|
||||||
void render(SDL_Rect *src = nullptr, SDL_Rect *dst = nullptr);
|
void render(SDL_FRect *src = nullptr, SDL_FRect *dst = nullptr);
|
||||||
|
|
||||||
// Gets image dimensions
|
// Gets image dimensions
|
||||||
int getWidth();
|
int getWidth();
|
||||||
|
|||||||
Reference in New Issue
Block a user