Compare commits

...

3 Commits

Author SHA1 Message Date
e36a555038 actualitzat makefile 2025-03-24 17:36:20 +01:00
9bc129e9b0 arreglos d'estil 2025-03-24 14:08:26 +01:00
29b18e30b5 canviat abs per fabs 2025-03-24 13:44:46 +01:00
8 changed files with 138 additions and 118 deletions

View File

@@ -9,8 +9,6 @@ OUTPUT_EXT :=
ifeq ($(OS),Windows_NT)
LDFLAGS += -lmingw32 -lws2_32
OUTPUT_EXT := .exe
else
OUTPUT_EXT := .out
endif
# Regla principal: compilar el ejecutable

View File

@@ -1,13 +1,16 @@
#include "ball.h"
#include "defines.h"
#include <stdlib.h> // for rand
#include <cmath> // for fabs
#include "defines.h" // for BALL_SIZE, Color, SCREEN_HEIGHT, GRAVITY_FORCE
class Texture;
// Constructor
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;
vy_ = vy;
sprite_ = std::make_unique<Sprite>(texture);
sprite_->setPos({pos_.x, pos_.y});
sprite_->setSize(BALL_SIZE, BALL_SIZE);
sprite_->setClip({0, 0, BALL_SIZE, BALL_SIZE});
@@ -62,7 +65,7 @@ void Ball::update()
{
pos_.y = SCREEN_HEIGHT - pos_.h;
vy_ = -vy_ * loss_;
if (abs(vy_) < 0.1f)
if (std::fabs(vy_) < 0.1f)
{
vy_ = 0.0f;
on_floor_ = true;
@@ -73,7 +76,7 @@ void Ball::update()
if (on_floor_)
{
vx_ = vx_ * 0.97f;
if (abs(vx_) < 0.1f)
if (std::fabs(vx_) < 0.1f)
{
vx_ = 0.0f;
stopped_ = true;

View File

@@ -1,9 +1,10 @@
#pragma once
#include <memory>
#include "sprite.h"
#include "texture.h"
#include "defines.h"
#include <SDL3/SDL_rect.h> // for SDL_FRect
#include <memory> // for shared_ptr, unique_ptr
#include "defines.h" // for Color
#include "sprite.h" // for Sprite
class Texture;
class Ball
{

View File

@@ -1,14 +1,22 @@
#include <SDL3/SDL.h>
#include "texture.h"
#include "ball.h"
#include "defines.h"
#include "dbgtxt.h"
#include <iostream>
#include <vector>
#include <array>
#include <cstdlib>
#include <ctime>
#include <memory>
#include <SDL3/SDL_error.h> // for SDL_GetError
#include <SDL3/SDL_events.h> // for SDL_EventType, SDL_PollEvent, SDL_Event
#include <SDL3/SDL_init.h> // for SDL_Init, SDL_Quit, SDL_INIT_VIDEO
#include <SDL3/SDL_keycode.h> // for SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5
#include <SDL3/SDL_render.h> // for SDL_SetRenderDrawColor, SDL_CreateRend...
#include <SDL3/SDL_stdinc.h> // for Uint64
#include <SDL3/SDL_timer.h> // for SDL_GetTicks
#include <SDL3/SDL_video.h> // for SDL_CreateWindow, SDL_DestroyWindow
#include <array> // for array
#include <cstdlib> // for rand, srand
#include <ctime> // for time
#include <iostream> // for basic_ostream, char_traits, operator<<
#include <memory> // for unique_ptr, shared_ptr, make_shared
#include <string> // for operator+, string, to_string
#include <vector> // for vector
#include "ball.h" // for Ball
#include "dbgtxt.h" // for dbg_init, dbg_print
#include "defines.h" // for SCREEN_WIDTH, SCREEN_HEIGHT, WINDOW_SIZE
#include "texture.h" // for Texture
// Variables globales
SDL_Window *window = nullptr;

View File

@@ -1,4 +1,5 @@
#include "sprite.h"
#include "texture.h" // for Texture
// Constructor
Sprite::Sprite(std::shared_ptr<Texture> texture)

View File

@@ -1,8 +1,8 @@
#pragma once
#include <SDL3/SDL.h>
#include <memory>
#include "texture.h"
#include <SDL3/SDL_rect.h> // for SDL_FRect, SDL_FPoint
#include <memory> // for shared_ptr
class Texture;
class Sprite
{

View File

@@ -1,119 +1,128 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <stdio.h>
#include <string>
#include "texture.h"
#include <SDL3/SDL_error.h> // Para SDL_GetError
#include <SDL3/SDL_log.h> // Para SDL_Log
#include <SDL3/SDL_pixels.h> // Para SDL_PixelFormat
#include <SDL3/SDL_surface.h> // Para SDL_CreateSurfaceFrom, SDL_DestroySurface
#include <stdio.h> // Para NULL
#include <stdlib.h> // Para exit
#include <iostream> // Para basic_ostream, char_traits, operator<<
#include <string> // Para operator<<, string
#include "stb_image.h" // Para stbi_failure_reason, stbi_image_free
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)
: renderer_(renderer), texture_(nullptr), width_(0), height_(0)
Texture::Texture(SDL_Renderer *renderer, const std::string &file_path)
: renderer_(renderer), texture_(nullptr), width_(0), height_(0)
{
loadFromFile(file_path);
loadFromFile(file_path);
}
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);
int req_format = STBI_rgb_alpha;
int width, height, orig_format;
unsigned char *data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
if (data == nullptr)
{
SDL_Log("Loading image failed: %s", stbi_failure_reason());
exit(1);
}
else
{
std::cout << "Image loaded: " << filename.c_str() << std::endl;
}
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
int req_format = STBI_rgb_alpha;
int width, height, orig_format;
unsigned char *data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
if (data == nullptr)
{
SDL_Log("Error al cargar la imagen: %s", stbi_failure_reason());
exit(1);
}
else
{
std::cout << "Imagen cargada: " << filename.c_str() << std::endl;
}
int pitch;
SDL_PixelFormat pixel_format;
if (req_format == STBI_rgb)
{
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else
{ // STBI_rgb_alpha (RGBA)
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
int pitch;
SDL_PixelFormat pixel_format;
if (req_format == STBI_rgb)
{
pitch = 3 * width; // 3 bytes por pixel * pixels por línea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else
{ // STBI_rgb_alpha (RGBA)
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Limpia
free();
// Libera la memoria previa
free();
// La textura final
SDL_Texture *new_texture = nullptr;
// La textura final
SDL_Texture *new_texture = nullptr;
// Carga la imagen desde una ruta específica
SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
if (loaded_surface == nullptr)
{
std::cout << "Unable to load image " << file_path << std::endl;
}
else
{
// Crea la textura desde los pixels de la surface
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
if (new_texture == nullptr)
{
std::cout << "Unable to create texture from " << file_path << "! SDL Error: " << SDL_GetError() << std::endl;
}
else
{
// Obtiene las dimensiones de la imagen
width_ = loaded_surface->w;
height_ = loaded_surface->h;
}
// Crea la superficie de la imagen desde los datos cargados
SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
if (loaded_surface == nullptr)
{
std::cout << "No se pudo cargar la imagen " << file_path << std::endl;
}
else
{
// Crea la textura desde los píxeles de la superficie
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
if (new_texture == nullptr)
{
std::cout << "No se pudo crear la textura desde " << file_path << "! Error de SDL: " << SDL_GetError() << std::endl;
}
else
{
// Obtiene las dimensiones de la imagen
width_ = loaded_surface->w;
height_ = loaded_surface->h;
}
// Elimina la textura cargada
SDL_DestroySurface(loaded_surface);
}
// Destruye la superficie cargada
SDL_DestroySurface(loaded_surface);
}
// Return success
stbi_image_free(data);
texture_ = new_texture;
return texture_ != nullptr;
// Devuelve el resultado del proceso
stbi_image_free(data);
texture_ = new_texture;
return texture_ != nullptr;
}
// Libera la textura si existe
void Texture::free()
{
// Free texture if it exists
if (texture_ != NULL)
{
SDL_DestroyTexture(texture_);
texture_ = NULL;
width_ = 0;
height_ = 0;
}
if (texture_ != nullptr)
{
SDL_DestroyTexture(texture_);
texture_ = nullptr;
width_ = 0;
height_ = 0;
}
}
// Renderiza la textura en pantalla
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()
{
return width_;
return width_;
}
// Obtiene la altura de la imagen
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)
{
SDL_SetTextureColorMod(texture_, r, g, b);
SDL_SetTextureColorMod(texture_, r, g, b);
}

View File

@@ -1,40 +1,40 @@
#pragma once
#include <SDL3/SDL.h>
#include <iostream>
#include <SDL3/SDL_rect.h> // Para SDL_FRect
#include <SDL3/SDL_render.h> // Para SDL_Renderer, SDL_Texture
#include <string> // Para std::string
// Texture wrapper class
class Texture
{
private:
SDL_Renderer *renderer_;
SDL_Texture *texture_;
// Image dimensions
// Dimensiones de la imagen
int width_;
int height_;
public:
// Initializes variables
Texture(SDL_Renderer *renderer);
Texture(SDL_Renderer *renderer, std::string file_path);
// Inicializa las variables
explicit Texture(SDL_Renderer *renderer);
Texture(SDL_Renderer *renderer, const std::string &file_path);
// Deallocates memory
// Libera la memoria
~Texture();
// Loads image at specified path
bool loadFromFile(std::string path);
// Carga una imagen desde la ruta especificada
bool loadFromFile(const std::string &path);
// Deallocates texture
// Libera la textura
void free();
// Renders texture at given point
// Renderiza la textura en el punto especificado
void render(SDL_FRect *src = nullptr, SDL_FRect *dst = nullptr);
// Gets image dimensions
// Obtiene las dimensiones de la imagen
int getWidth();
int getHeight();
// Modulación de color
// Modula el color de la textura
void setColor(int r, int g, int b);
};