From 29b18e30b532778b4d7d16856cbe6b09bea40f0a Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 24 Mar 2025 13:44:46 +0100 Subject: [PATCH] canviat abs per fabs --- source/ball.cpp | 9 ++++++--- source/ball.h | 9 +++++---- source/main.cpp | 30 +++++++++++++++++++----------- source/sprite.cpp | 1 + source/sprite.h | 6 +++--- source/texture.cpp | 12 +++++++++--- source/texture.h | 22 +++++++++++----------- 7 files changed, 54 insertions(+), 35 deletions(-) diff --git a/source/ball.cpp b/source/ball.cpp index 68b5711..86ad0b6 100644 --- a/source/ball.cpp +++ b/source/ball.cpp @@ -1,5 +1,8 @@ #include "ball.h" -#include "defines.h" +#include // for rand +#include // 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) @@ -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; diff --git a/source/ball.h b/source/ball.h index de665e2..3158e7c 100644 --- a/source/ball.h +++ b/source/ball.h @@ -1,9 +1,10 @@ #pragma once -#include -#include "sprite.h" -#include "texture.h" -#include "defines.h" +#include // for SDL_FRect +#include // for shared_ptr, unique_ptr +#include "defines.h" // for Color +#include "sprite.h" // for Sprite +class Texture; class Ball { diff --git a/source/main.cpp b/source/main.cpp index d0fa8bf..987538b 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,14 +1,22 @@ -#include -#include "texture.h" -#include "ball.h" -#include "defines.h" -#include "dbgtxt.h" -#include -#include -#include -#include -#include -#include +#include // for SDL_GetError +#include // for SDL_EventType, SDL_PollEvent, SDL_Event +#include // for SDL_Init, SDL_Quit, SDL_INIT_VIDEO +#include // for SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5 +#include // for SDL_SetRenderDrawColor, SDL_CreateRend... +#include // for Uint64 +#include // for SDL_GetTicks +#include // for SDL_CreateWindow, SDL_DestroyWindow +#include // for array +#include // for rand, srand +#include // for time +#include // for basic_ostream, char_traits, operator<< +#include // for unique_ptr, shared_ptr, make_shared +#include // for operator+, string, to_string +#include // 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; diff --git a/source/sprite.cpp b/source/sprite.cpp index 810ffdc..e269f9f 100644 --- a/source/sprite.cpp +++ b/source/sprite.cpp @@ -1,4 +1,5 @@ #include "sprite.h" +#include "texture.h" // for Texture // Constructor Sprite::Sprite(std::shared_ptr texture) diff --git a/source/sprite.h b/source/sprite.h index 39aed1d..1caeeff 100644 --- a/source/sprite.h +++ b/source/sprite.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include "texture.h" +#include // for SDL_FRect, SDL_FPoint +#include // for shared_ptr +class Texture; class Sprite { diff --git a/source/texture.cpp b/source/texture.cpp index a13f8a3..3fc0332 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -1,8 +1,14 @@ #define STB_IMAGE_IMPLEMENTATION -#include "stb_image.h" -#include -#include #include "texture.h" +#include // for SDL_GetError +#include // for SDL_Log +#include // for SDL_PixelFormat +#include // for SDL_CreateSurfaceFrom, SDL_DestroySurface +#include // for NULL +#include // for exit +#include // for basic_ostream, char_traits, operator<< +#include // for operator<<, string +#include "stb_image.h" // for stbi_failure_reason, stbi_image_free Texture::Texture(SDL_Renderer *renderer) : renderer_(renderer), texture_(nullptr), width_(0), height_(0) {} diff --git a/source/texture.h b/source/texture.h index 4989369..d9f794d 100644 --- a/source/texture.h +++ b/source/texture.h @@ -1,40 +1,40 @@ #pragma once -#include -#include +#include // Para SDL_FRect +#include // Para SDL_Renderer, SDL_Texture +#include // 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 + // Inicializa las variables Texture(SDL_Renderer *renderer); Texture(SDL_Renderer *renderer, std::string file_path); - // Deallocates memory + // Libera la memoria ~Texture(); - // Loads image at specified path + // Carga una imagen desde la ruta especificada bool loadFromFile(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); };