canviat abs per fabs

This commit is contained in:
2025-03-24 13:44:46 +01:00
parent e2e3b7c779
commit 29b18e30b5
7 changed files with 54 additions and 35 deletions

View File

@@ -1,5 +1,8 @@
#include "ball.h" #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 // 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)
@@ -62,7 +65,7 @@ void Ball::update()
{ {
pos_.y = SCREEN_HEIGHT - pos_.h; pos_.y = SCREEN_HEIGHT - pos_.h;
vy_ = -vy_ * loss_; vy_ = -vy_ * loss_;
if (abs(vy_) < 0.1f) if (std::fabs(vy_) < 0.1f)
{ {
vy_ = 0.0f; vy_ = 0.0f;
on_floor_ = true; on_floor_ = true;
@@ -73,7 +76,7 @@ void Ball::update()
if (on_floor_) if (on_floor_)
{ {
vx_ = vx_ * 0.97f; vx_ = vx_ * 0.97f;
if (abs(vx_) < 0.1f) if (std::fabs(vx_) < 0.1f)
{ {
vx_ = 0.0f; vx_ = 0.0f;
stopped_ = true; stopped_ = true;

View File

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

View File

@@ -1,14 +1,22 @@
#include <SDL3/SDL.h> #include <SDL3/SDL_error.h> // for SDL_GetError
#include "texture.h" #include <SDL3/SDL_events.h> // for SDL_EventType, SDL_PollEvent, SDL_Event
#include "ball.h" #include <SDL3/SDL_init.h> // for SDL_Init, SDL_Quit, SDL_INIT_VIDEO
#include "defines.h" #include <SDL3/SDL_keycode.h> // for SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5
#include "dbgtxt.h" #include <SDL3/SDL_render.h> // for SDL_SetRenderDrawColor, SDL_CreateRend...
#include <iostream> #include <SDL3/SDL_stdinc.h> // for Uint64
#include <vector> #include <SDL3/SDL_timer.h> // for SDL_GetTicks
#include <array> #include <SDL3/SDL_video.h> // for SDL_CreateWindow, SDL_DestroyWindow
#include <cstdlib> #include <array> // for array
#include <ctime> #include <cstdlib> // for rand, srand
#include <memory> #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 // Variables globales
SDL_Window *window = nullptr; SDL_Window *window = nullptr;

View File

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

View File

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

View File

@@ -1,8 +1,14 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <stdio.h>
#include <string>
#include "texture.h" #include "texture.h"
#include <SDL3/SDL_error.h> // for SDL_GetError
#include <SDL3/SDL_log.h> // for SDL_Log
#include <SDL3/SDL_pixels.h> // for SDL_PixelFormat
#include <SDL3/SDL_surface.h> // for SDL_CreateSurfaceFrom, SDL_DestroySurface
#include <stdio.h> // for NULL
#include <stdlib.h> // for exit
#include <iostream> // for basic_ostream, char_traits, operator<<
#include <string> // for operator<<, string
#include "stb_image.h" // for 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) {}

View File

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