migrat a SDL3
This commit is contained in:
106
source/ball.cpp
106
source/ball.cpp
@@ -2,123 +2,121 @@
|
||||
#include "defines.h"
|
||||
|
||||
// Constructor
|
||||
Ball::Ball(float x, float vx, float vy, color_t color, Texture *texture)
|
||||
Ball::Ball(float x, float vx, float vy, Color color, Texture *texture)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = 0.0f;
|
||||
this->w = BALL_SIZE;
|
||||
this->h = BALL_SIZE;
|
||||
this->vx = vx;
|
||||
this->vy = vy;
|
||||
sprite = new Sprite(texture);
|
||||
sprite->setPos({(int)x, (int)y});
|
||||
sprite->setSize(BALL_SIZE, BALL_SIZE);
|
||||
sprite->setClip({0, 0, BALL_SIZE, BALL_SIZE});
|
||||
this->color = color;
|
||||
g = GRAVITY;
|
||||
onFloor = false;
|
||||
stopped = false;
|
||||
loss = ((rand() % 30) * 0.01f) + 0.6f;
|
||||
x_ = x;
|
||||
y_ = 0.0f;
|
||||
w_ = BALL_SIZE;
|
||||
h_ = BALL_SIZE;
|
||||
vx_ = vx;
|
||||
vy_ = vy;
|
||||
sprite_ = new Sprite(texture);
|
||||
sprite_->setPos({(int)x, (int)y_});
|
||||
sprite_->setSize(BALL_SIZE, BALL_SIZE);
|
||||
sprite_->setClip({0, 0, BALL_SIZE, BALL_SIZE});
|
||||
color_ = color;
|
||||
gravity_force_ = GRAVITY_FORCE;
|
||||
on_floor_ = false;
|
||||
stopped_ = false;
|
||||
loss_ = ((rand() % 30) * 0.01f) + 0.6f;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Ball::~Ball()
|
||||
{
|
||||
if (sprite)
|
||||
if (sprite_)
|
||||
{
|
||||
delete sprite;
|
||||
delete sprite_;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la lógica de la clase
|
||||
void Ball::update()
|
||||
{
|
||||
if (stopped)
|
||||
if (stopped_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Aplica la gravedad a la velocidad
|
||||
if (!onFloor && (y - SCREEN_HEIGHT) < BALL_SIZE * 2)
|
||||
if (!on_floor_ && (y_ - SCREEN_HEIGHT) < BALL_SIZE * 2)
|
||||
{
|
||||
vy += g;
|
||||
vy_ += gravity_force_;
|
||||
}
|
||||
|
||||
// Actualiza la posición en función de la velocidad
|
||||
x += vx;
|
||||
y += vy;
|
||||
x_ += vx_;
|
||||
y_ += vy_;
|
||||
|
||||
// Comprueba las colisiones con el lateral izquierdo
|
||||
if (x < 0)
|
||||
if (x_ < 0)
|
||||
{
|
||||
x = 0;
|
||||
vx = -vx;
|
||||
x_ = 0;
|
||||
vx_ = -vx_;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con el lateral derecho
|
||||
if (x + w > SCREEN_WIDTH)
|
||||
if (x_ + w_ > SCREEN_WIDTH)
|
||||
{
|
||||
x = SCREEN_WIDTH - w;
|
||||
vx = -vx;
|
||||
x_ = SCREEN_WIDTH - w_;
|
||||
vx_ = -vx_;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con la parte superior
|
||||
if (y < 0)
|
||||
if (y_ < 0)
|
||||
{
|
||||
y = 0;
|
||||
vy = -vy;
|
||||
//vy = -vy * loss;
|
||||
y_ = 0;
|
||||
vy_ = -vy_;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con la parte inferior
|
||||
if (y + h > SCREEN_HEIGHT)
|
||||
if (y_ + h_ > SCREEN_HEIGHT)
|
||||
{
|
||||
y = SCREEN_HEIGHT - h;
|
||||
vy = -vy * loss;
|
||||
if (abs(vy) < 0.1f)
|
||||
y_ = SCREEN_HEIGHT - h_;
|
||||
vy_ = -vy_ * loss_;
|
||||
if (abs(vy_) < 0.1f)
|
||||
{
|
||||
vy = 0.0f;
|
||||
onFloor = true;
|
||||
vy_ = 0.0f;
|
||||
on_floor_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica rozamiento al rodar por el suelo
|
||||
if (onFloor)
|
||||
if (on_floor_)
|
||||
{
|
||||
vx = vx * 0.97f;
|
||||
if (abs(vx) < 0.1f)
|
||||
vx_ = vx_ * 0.97f;
|
||||
if (abs(vx_) < 0.1f)
|
||||
{
|
||||
vx = 0.0f;
|
||||
stopped = true;
|
||||
vx_ = 0.0f;
|
||||
stopped_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
sprite->setPos({(int)x, (int)y});
|
||||
sprite->update();
|
||||
sprite_->setPos({(int)x_, (int)y_});
|
||||
}
|
||||
|
||||
// Pinta la clase
|
||||
void Ball::render()
|
||||
{
|
||||
sprite->setColor(color.r, color.g, color.b);
|
||||
sprite->render();
|
||||
sprite_->setColor(color_.r, color_.g, color_.b);
|
||||
sprite_->render();
|
||||
}
|
||||
|
||||
// Modifica la velocidad
|
||||
void Ball::modVel(float vx, float vy)
|
||||
{
|
||||
if (stopped)
|
||||
if (stopped_)
|
||||
{
|
||||
this->vx = this->vx + vx;
|
||||
vx_ = vx_ + vx;
|
||||
}
|
||||
this->vy = this->vy + vy;
|
||||
onFloor = false;
|
||||
stopped = false;
|
||||
vy_ = vy_ + vy;
|
||||
on_floor_ = false;
|
||||
stopped_ = false;
|
||||
}
|
||||
|
||||
// Cambia la gravedad
|
||||
void Ball::switchGravity()
|
||||
{
|
||||
g = g == 0.0f ? GRAVITY : 0.0f;
|
||||
gravity_force_ = gravity_force_ == 0.0f ? GRAVITY_FORCE : 0.0f;
|
||||
}
|
||||
@@ -7,21 +7,21 @@
|
||||
class Ball
|
||||
{
|
||||
private:
|
||||
Sprite *sprite; // Sprite para pintar la clase
|
||||
float x; // Posición x
|
||||
float y; // Posición y
|
||||
int w; // Ancho
|
||||
int h; // Alto
|
||||
float vx, vy; // Velocidad
|
||||
float g; // Gravedad
|
||||
color_t color; // Color de la pelota
|
||||
bool onFloor; // Indica si la pelota está ya en el suelo
|
||||
bool stopped; // Indica si la pelota ha terminado de moverse;
|
||||
float loss; // Coeficiente de rebote. Pérdida de energía en cada rebote
|
||||
Sprite *sprite_; // Sprite para pintar la clase
|
||||
float x_; // Posición x
|
||||
float y_; // Posición y
|
||||
int w_; // Ancho
|
||||
int h_; // Alto
|
||||
float vx_, vy_; // Velocidad
|
||||
float gravity_force_; // Gravedad
|
||||
Color color_; // Color de la pelota
|
||||
bool on_floor_; // Indica si la pelota está ya en el suelo
|
||||
bool stopped_; // Indica si la pelota ha terminado de moverse;
|
||||
float loss_; // Coeficiente de rebote. Pérdida de energía en cada rebote
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Ball(float x, float vx, float vy, color_t color, Texture *texture);
|
||||
Ball(float x, float vx, float vy, Color color, Texture *texture);
|
||||
|
||||
// Destructor
|
||||
~Ball();
|
||||
|
||||
@@ -1,37 +1,42 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
|
||||
namespace {
|
||||
SDL_Texture *dbg_tex = NULL;
|
||||
SDL_Renderer *dbg_ren = NULL;
|
||||
namespace
|
||||
{
|
||||
SDL_Texture *dbg_tex = nullptr;
|
||||
SDL_Renderer *dbg_ren = nullptr;
|
||||
}
|
||||
|
||||
void dbg_init(SDL_Renderer *renderer) {
|
||||
void dbg_init(SDL_Renderer *renderer)
|
||||
{
|
||||
dbg_ren = renderer;
|
||||
Uint8 font[448] = {0x42, 0x4D, 0xC0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0x01, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x18, 0xF3, 0x83, 0x83, 0xCF, 0x83, 0x87, 0x00, 0x00, 0xF3, 0x39, 0x39, 0xCF, 0x79, 0xF3, 0x00, 0x00, 0x01, 0xF9, 0x39, 0xCF, 0x61, 0xF9, 0x00, 0x00, 0x33, 0xF9, 0x03, 0xE7, 0x87, 0x81, 0x00, 0x00, 0x93, 0x03, 0x3F, 0xF3, 0x1B, 0x39, 0x00, 0x00, 0xC3, 0x3F, 0x9F, 0x39, 0x3B, 0x39, 0x00, 0x41, 0xE3, 0x03, 0xC3, 0x01, 0x87, 0x83, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xE7, 0x01, 0xC7, 0x81, 0x01, 0x83, 0x00, 0x00, 0xE7, 0x1F, 0x9B, 0xE7, 0x1F, 0x39, 0x00, 0x00, 0xE7, 0x8F, 0x39, 0xE7, 0x87, 0xF9, 0x00, 0x00, 0xC3, 0xC7, 0x39, 0xE7, 0xC3, 0xC3, 0x00, 0x00, 0x99, 0xE3, 0x39, 0xE7, 0xF1, 0xE7, 0x00, 0x00, 0x99, 0xF1, 0xB3, 0xC7, 0x39, 0xF3, 0x00, 0x00, 0x99, 0x01, 0xC7, 0xE7, 0x83, 0x81, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x83, 0xE7, 0x83, 0xEF, 0x39, 0x39, 0x00, 0x00, 0x39, 0xE7, 0x39, 0xC7, 0x11, 0x11, 0x00, 0x00, 0xF9, 0xE7, 0x39, 0x83, 0x01, 0x83, 0x00, 0x00, 0x83, 0xE7, 0x39, 0x11, 0x01, 0xC7, 0x00, 0x00, 0x3F, 0xE7, 0x39, 0x39, 0x29, 0x83, 0x00, 0x00, 0x33, 0xE7, 0x39, 0x39, 0x39, 0x11, 0x00, 0x00, 0x87, 0x81, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x39, 0x39, 0x83, 0x3F, 0x85, 0x31, 0x00, 0x00, 0x39, 0x31, 0x39, 0x3F, 0x33, 0x23, 0x00, 0x00, 0x29, 0x21, 0x39, 0x03, 0x21, 0x07, 0x00, 0x00, 0x01, 0x01, 0x39, 0x39, 0x39, 0x31, 0x00, 0x00, 0x01, 0x09, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0x11, 0x19, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0x39, 0x39, 0x83, 0x03, 0x83, 0x03, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xC1, 0x39, 0x81, 0x83, 0x31, 0x01, 0x00, 0x00, 0x99, 0x39, 0xE7, 0x39, 0x23, 0x3F, 0x00, 0x00, 0x39, 0x39, 0xE7, 0xF9, 0x07, 0x3F, 0x00, 0x00, 0x31, 0x01, 0xE7, 0xF9, 0x0F, 0x3F, 0x00, 0x00, 0x3F, 0x39, 0xE7, 0xF9, 0x27, 0x3F, 0x00, 0x00, 0x9F, 0x39, 0xE7, 0xF9, 0x33, 0x3F, 0x00, 0x00, 0xC1, 0x39, 0x81, 0xF9, 0x39, 0x3F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x39, 0x03, 0xC3, 0x07, 0x01, 0x3F, 0x00, 0x00, 0x39, 0x39, 0x99, 0x33, 0x3F, 0x3F, 0x00, 0x00, 0x01, 0x39, 0x3F, 0x39, 0x3F, 0x3F, 0x00, 0x00, 0x39, 0x03, 0x3F, 0x39, 0x03, 0x03, 0x00, 0x00, 0x39, 0x39, 0x3F, 0x39, 0x3F, 0x3F, 0x00, 0x00, 0x93, 0x39, 0x99, 0x33, 0x3F, 0x3F, 0x00, 0x00, 0xC7, 0x03, 0xC3, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
|
||||
dbg_tex = SDL_CreateTextureFromSurface(dbg_ren, SDL_LoadBMP_RW(SDL_RWFromMem(font, 448), 1));
|
||||
Uint8 font[448] = {0x42, 0x4D, 0xC0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0x01, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x18, 0xF3, 0x83, 0x83, 0xCF, 0x83, 0x87, 0x00, 0x00, 0xF3, 0x39, 0x39, 0xCF, 0x79, 0xF3, 0x00, 0x00, 0x01, 0xF9, 0x39, 0xCF, 0x61, 0xF9, 0x00, 0x00, 0x33, 0xF9, 0x03, 0xE7, 0x87, 0x81, 0x00, 0x00, 0x93, 0x03, 0x3F, 0xF3, 0x1B, 0x39, 0x00, 0x00, 0xC3, 0x3F, 0x9F, 0x39, 0x3B, 0x39, 0x00, 0x41, 0xE3, 0x03, 0xC3, 0x01, 0x87, 0x83, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xE7, 0x01, 0xC7, 0x81, 0x01, 0x83, 0x00, 0x00, 0xE7, 0x1F, 0x9B, 0xE7, 0x1F, 0x39, 0x00, 0x00, 0xE7, 0x8F, 0x39, 0xE7, 0x87, 0xF9, 0x00, 0x00, 0xC3, 0xC7, 0x39, 0xE7, 0xC3, 0xC3, 0x00, 0x00, 0x99, 0xE3, 0x39, 0xE7, 0xF1, 0xE7, 0x00, 0x00, 0x99, 0xF1, 0xB3, 0xC7, 0x39, 0xF3, 0x00, 0x00, 0x99, 0x01, 0xC7, 0xE7, 0x83, 0x81, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x83, 0xE7, 0x83, 0xEF, 0x39, 0x39, 0x00, 0x00, 0x39, 0xE7, 0x39, 0xC7, 0x11, 0x11, 0x00, 0x00, 0xF9, 0xE7, 0x39, 0x83, 0x01, 0x83, 0x00, 0x00, 0x83, 0xE7, 0x39, 0x11, 0x01, 0xC7, 0x00, 0x00, 0x3F, 0xE7, 0x39, 0x39, 0x29, 0x83, 0x00, 0x00, 0x33, 0xE7, 0x39, 0x39, 0x39, 0x11, 0x00, 0x00, 0x87, 0x81, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x39, 0x39, 0x83, 0x3F, 0x85, 0x31, 0x00, 0x00, 0x39, 0x31, 0x39, 0x3F, 0x33, 0x23, 0x00, 0x00, 0x29, 0x21, 0x39, 0x03, 0x21, 0x07, 0x00, 0x00, 0x01, 0x01, 0x39, 0x39, 0x39, 0x31, 0x00, 0x00, 0x01, 0x09, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0x11, 0x19, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0x39, 0x39, 0x83, 0x03, 0x83, 0x03, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xC1, 0x39, 0x81, 0x83, 0x31, 0x01, 0x00, 0x00, 0x99, 0x39, 0xE7, 0x39, 0x23, 0x3F, 0x00, 0x00, 0x39, 0x39, 0xE7, 0xF9, 0x07, 0x3F, 0x00, 0x00, 0x31, 0x01, 0xE7, 0xF9, 0x0F, 0x3F, 0x00, 0x00, 0x3F, 0x39, 0xE7, 0xF9, 0x27, 0x3F, 0x00, 0x00, 0x9F, 0x39, 0xE7, 0xF9, 0x33, 0x3F, 0x00, 0x00, 0xC1, 0x39, 0x81, 0xF9, 0x39, 0x3F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x39, 0x03, 0xC3, 0x07, 0x01, 0x3F, 0x00, 0x00, 0x39, 0x39, 0x99, 0x33, 0x3F, 0x3F, 0x00, 0x00, 0x01, 0x39, 0x3F, 0x39, 0x3F, 0x3F, 0x00, 0x00, 0x39, 0x03, 0x3F, 0x39, 0x03, 0x03, 0x00, 0x00, 0x39, 0x39, 0x3F, 0x39, 0x3F, 0x3F, 0x00, 0x00, 0x93, 0x39, 0x99, 0x33, 0x3F, 0x3F, 0x00, 0x00, 0xC7, 0x03, 0xC3, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
|
||||
dbg_tex = SDL_CreateTextureFromSurface(dbg_ren, SDL_LoadBMP_IO(SDL_IOFromMem(font, 448), 1));
|
||||
}
|
||||
|
||||
void dbg_print(int x, int y, const char* text, Uint8 r, Uint8 g, Uint8 b) {
|
||||
void dbg_print(int x, int y, const char *text, Uint8 r, Uint8 g, Uint8 b)
|
||||
{
|
||||
int cc = 0;
|
||||
SDL_SetTextureColorMod(dbg_tex, r, g, b);
|
||||
SDL_Rect src = {0, 0, 8, 8};
|
||||
SDL_Rect dst = {x, y, 8, 8};
|
||||
while (text[cc] != 0) {
|
||||
SDL_FRect src = {0, 0, 8, 8};
|
||||
SDL_FRect dst = {static_cast<float>(x), static_cast<float>(y), 8, 8};
|
||||
while (text[cc] != 0)
|
||||
{
|
||||
if (text[cc] != 32)
|
||||
{
|
||||
if (text[cc] >= 65)
|
||||
{
|
||||
src.x = ((text[cc]-65)%6)*8; src.y = ((text[cc]-65)/6)*8;
|
||||
src.x = ((text[cc] - 65) % 6) * 8;
|
||||
src.y = ((text[cc] - 65) / 6) * 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
src.x = ((text[cc]-22)%6)*8; src.y = ((text[cc]-22)/6)*8;
|
||||
src.x = ((text[cc] - 22) % 6) * 8;
|
||||
src.y = ((text[cc] - 22) / 6) * 8;
|
||||
}
|
||||
|
||||
SDL_RenderCopy(dbg_ren, dbg_tex, &src, &dst);
|
||||
|
||||
SDL_RenderTexture(dbg_ren, dbg_tex, &src, &dst);
|
||||
}
|
||||
cc++; dst.x+=8;
|
||||
cc++;
|
||||
dst.x += 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#define WINDOW_CAPTION "demo_pelotas2"
|
||||
#define SCREEN_WIDTH 320
|
||||
#define SCREEN_HEIGHT 240
|
||||
#define WINDOW_SIZE 3
|
||||
#define BALL_SIZE 8
|
||||
#define GRAVITY 0.2f
|
||||
#define TEXT_TIME 100
|
||||
constexpr char WINDOW_CAPTION[] = "demo5_sprites_bouncing";
|
||||
|
||||
struct color_t
|
||||
constexpr int SCREEN_WIDTH = 320;
|
||||
constexpr int SCREEN_HEIGHT = 240;
|
||||
constexpr int WINDOW_SIZE = 3;
|
||||
constexpr int BALL_SIZE = 8;
|
||||
constexpr float GRAVITY_FORCE = 0.2f;
|
||||
|
||||
constexpr Uint64 DEMO_SPEED = 1000 / 60;
|
||||
constexpr Uint64 TEXT_DURATION = 2000;
|
||||
|
||||
struct Color
|
||||
{
|
||||
int r,g,b;
|
||||
int r, g, b;
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include "texture.h"
|
||||
#include "ball.h"
|
||||
#include "defines.h"
|
||||
@@ -13,25 +13,25 @@ Texture *texture = nullptr;
|
||||
std::vector<Ball *> balls;
|
||||
int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000};
|
||||
|
||||
bool shouldExit = false; // Controla si la aplicación debe cerrarse
|
||||
Uint32 ticks = 0; // Tiempo en milisegundos para controlar la actualización
|
||||
int scenario = 0; // Escenario actual basado en el número de bolas
|
||||
std::string text = ""; // Texto a mostrar en pantalla
|
||||
int textPos = 0; // Posición del texto en la pantalla
|
||||
bool showText = true; // Determina si el texto se debe mostrar
|
||||
int counter = 0; // Temporizador para mostrar el texto
|
||||
bool should_exit = false; // Controla si la aplicación debe cerrarse
|
||||
Uint64 ticks = 0; // Tiempo en milisegundos para controlar la actualización
|
||||
int scenario = 0; // Escenario actual basado en el número de bolas
|
||||
std::string text = ""; // Texto a mostrar en pantalla
|
||||
int text_pos = 0; // Posición del texto en la pantalla
|
||||
bool show_text = true; // Determina si el texto se debe mostrar
|
||||
Uint64 text_init_time = 0; // Temporizador para mostrar el texto
|
||||
|
||||
void deleteBalls(); // Declaración de una función para eliminar bolas
|
||||
|
||||
// Establece el texto en pantalla mostrando el número de bolas actuales
|
||||
void setText()
|
||||
{
|
||||
const std::string text2 = test[scenario] == 1 ? " PELOTA" : " PELOTAS";
|
||||
text = std::to_string(test[scenario]) + text2;
|
||||
const int size = text.size() * 8;
|
||||
textPos = SCREEN_WIDTH / 2 - size / 2;
|
||||
counter = TEXT_TIME;
|
||||
showText = true;
|
||||
const std::string TEXT2 = test[scenario] == 1 ? " PELOTA" : " PELOTAS";
|
||||
text = std::to_string(test[scenario]) + TEXT2;
|
||||
const int SIZE = text.size() * 8;
|
||||
text_pos = SCREEN_WIDTH / 2 - SIZE / 2;
|
||||
text_init_time = SDL_GetTicks();
|
||||
show_text = true;
|
||||
}
|
||||
|
||||
// Inicializa las bolas según el escenario seleccionado
|
||||
@@ -40,12 +40,12 @@ void initBalls(int value)
|
||||
deleteBalls(); // Limpia las bolas actuales
|
||||
for (int i = 0; i < test[value]; ++i)
|
||||
{
|
||||
const int sign = ((rand() % 2) * 2) - 1; // Genera un signo aleatorio (+ o -)
|
||||
const float x = (rand() % (SCREEN_WIDTH / 2)) + (SCREEN_WIDTH / 4); // Posición inicial en X
|
||||
const float vx = (((rand() % 20) + 10) * 0.1f) * sign; // Velocidad en X
|
||||
const float vy = ((rand() % 60) - 30) * 0.1f; // Velocidad en Y
|
||||
const color_t color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32}; // Color aleatorio
|
||||
Ball *b = new Ball(x, vx, vy, color, texture);
|
||||
const int SIGN = ((rand() % 2) * 2) - 1; // Genera un signo aleatorio (+ o -)
|
||||
const float X = (rand() % (SCREEN_WIDTH / 2)) + (SCREEN_WIDTH / 4); // Posición inicial en X
|
||||
const float VX = (((rand() % 20) + 10) * 0.1f) * SIGN; // Velocidad en X
|
||||
const float VY = ((rand() % 60) - 30) * 0.1f; // Velocidad en Y
|
||||
const Color COLOR = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32}; // Color aleatorio
|
||||
Ball *b = new Ball(X, VX, VY, COLOR, texture);
|
||||
balls.push_back(b); // Añadir la nueva bola al vector
|
||||
}
|
||||
setText(); // Actualizar el texto
|
||||
@@ -56,10 +56,10 @@ void pushUpBalls()
|
||||
{
|
||||
for (auto ball : balls)
|
||||
{
|
||||
const int sign = ((rand() % 2) * 2) - 1;
|
||||
const float vx = (((rand() % 20) + 10) * 0.1f) * sign;
|
||||
const float vy = ((rand() % 40) * 0.1f) + 5;
|
||||
ball->modVel(vx, -vy); // Modifica la velocidad de la bola
|
||||
const int SIGN = ((rand() % 2) * 2) - 1;
|
||||
const float VX = (((rand() % 20) + 10) * 0.1f) * SIGN;
|
||||
const float VY = ((rand() % 40) * 0.1f) + 5;
|
||||
ball->modVel(VX, -VY); // Modifica la velocidad de la bola
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ bool init()
|
||||
bool success = true; // Bandera de éxito
|
||||
|
||||
// Inicializa SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
printf("¡SDL no se pudo inicializar! Error de SDL: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
@@ -103,7 +103,7 @@ bool init()
|
||||
else
|
||||
{
|
||||
// Crear ventana principal
|
||||
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * WINDOW_SIZE, SCREEN_HEIGHT * WINDOW_SIZE, SDL_WINDOW_SHOWN);
|
||||
window = SDL_CreateWindow(WINDOW_CAPTION, SCREEN_WIDTH * WINDOW_SIZE, SCREEN_HEIGHT * WINDOW_SIZE, SDL_WINDOW_OPENGL);
|
||||
if (window == nullptr)
|
||||
{
|
||||
printf("¡No se pudo crear la ventana! Error de SDL: %s\n", SDL_GetError());
|
||||
@@ -112,7 +112,7 @@ bool init()
|
||||
else
|
||||
{
|
||||
// Crear renderizador
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
renderer = SDL_CreateRenderer(window, nullptr);
|
||||
if (renderer == nullptr)
|
||||
{
|
||||
printf("¡No se pudo crear el renderizador! Error de SDL: %s\n", SDL_GetError());
|
||||
@@ -124,7 +124,7 @@ bool init()
|
||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
|
||||
// Establecer tamaño lógico para el renderizado
|
||||
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
SDL_SetRenderLogicalPresentation(renderer, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,26 +165,26 @@ void checkEvents()
|
||||
while (SDL_PollEvent(&event) != 0)
|
||||
{
|
||||
// Evento de salida
|
||||
if (event.type == SDL_QUIT)
|
||||
if (event.type == SDL_EVENT_QUIT)
|
||||
{
|
||||
shouldExit = true;
|
||||
should_exit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Procesar eventos de teclado
|
||||
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:
|
||||
shouldExit = true;
|
||||
should_exit = true;
|
||||
break;
|
||||
|
||||
case SDLK_SPACE:
|
||||
pushUpBalls();
|
||||
break;
|
||||
|
||||
case SDLK_g:
|
||||
case SDLK_G:
|
||||
switchBallsGravity();
|
||||
break;
|
||||
|
||||
@@ -235,7 +235,7 @@ void checkEvents()
|
||||
// Actualiza la lógica del juego
|
||||
void update()
|
||||
{
|
||||
if (SDL_GetTicks() - ticks > 15)
|
||||
if (SDL_GetTicks() - ticks > DEMO_SPEED)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
@@ -244,13 +244,9 @@ void update()
|
||||
ball->update();
|
||||
}
|
||||
|
||||
if (counter > 0)
|
||||
if (show_text)
|
||||
{
|
||||
counter--;
|
||||
}
|
||||
else
|
||||
{
|
||||
showText = false;
|
||||
show_text = !(SDL_GetTicks() - text_init_time > TEXT_DURATION);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -266,9 +262,9 @@ void render()
|
||||
ball->render();
|
||||
}
|
||||
|
||||
if (showText)
|
||||
if (show_text)
|
||||
{
|
||||
dbg_print(textPos, 8, text.c_str(), 255, 255, 255);
|
||||
dbg_print(text_pos, 8, text.c_str(), 255, 255, 255);
|
||||
}
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
@@ -279,7 +275,7 @@ int main(int argc, char *args[])
|
||||
{
|
||||
init();
|
||||
|
||||
while (!shouldExit)
|
||||
while (!should_exit)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
|
||||
@@ -2,51 +2,38 @@
|
||||
|
||||
// Constructor
|
||||
Sprite::Sprite(Texture *texture)
|
||||
{
|
||||
this->texture = texture;
|
||||
pos = {0, 0, 0, 0};
|
||||
clip = {0, 0, 0, 0};
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Sprite::~Sprite()
|
||||
{
|
||||
}
|
||||
: texture_(texture),
|
||||
pos_{0, 0, 0, 0},
|
||||
clip_{0, 0, 0, 0} {}
|
||||
|
||||
// Establece la posición del sprite
|
||||
void Sprite::setPos(SDL_Point pos)
|
||||
{
|
||||
this->pos.x = pos.x;
|
||||
this->pos.y = pos.y;
|
||||
pos_.x = pos.x;
|
||||
pos_.y = pos.y;
|
||||
}
|
||||
|
||||
// Pinta el sprite
|
||||
void Sprite::render()
|
||||
{
|
||||
texture->render(&clip, &pos);
|
||||
}
|
||||
|
||||
// Actualiza la lógica de la clase
|
||||
void Sprite::update()
|
||||
{
|
||||
|
||||
texture_->render(&clip_, &pos_);
|
||||
}
|
||||
|
||||
// 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
|
||||
void Sprite::setSize(int w, int h)
|
||||
{
|
||||
this->pos.w = w;
|
||||
this->pos.h = h;
|
||||
pos_.w = w;
|
||||
pos_.h = h;
|
||||
}
|
||||
|
||||
// Modulación de color
|
||||
void Sprite::setColor(int r, int g, int b)
|
||||
{
|
||||
texture->setColor(r, g, b);
|
||||
texture_->setColor(r, g, b);
|
||||
}
|
||||
@@ -1,21 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include "texture.h"
|
||||
|
||||
class Sprite
|
||||
{
|
||||
private:
|
||||
Texture *texture; // Textura con los gráficos del sprite
|
||||
SDL_Rect pos; // Posición y tamaño del sprite
|
||||
SDL_Rect clip; // Parte de la textura que se va a dibujar
|
||||
Texture *texture_; // Textura con los gráficos del sprite
|
||||
SDL_FRect pos_; // Posición y tamaño del sprite
|
||||
SDL_FRect clip_; // Parte de la textura que se va a dibujar
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Sprite(Texture *texture);
|
||||
explicit Sprite(Texture *texture);
|
||||
|
||||
// Destructor
|
||||
~Sprite();
|
||||
~Sprite() = default;
|
||||
|
||||
// Establece la posición del sprite
|
||||
void setPos(SDL_Point pos);
|
||||
@@ -23,11 +23,8 @@ public:
|
||||
// Pinta el sprite
|
||||
void render();
|
||||
|
||||
// Actualiza la lógica de la clase
|
||||
void update();
|
||||
|
||||
// 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
|
||||
void setSize(int w, int h);
|
||||
|
||||
@@ -1,24 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include "texture.h"
|
||||
|
||||
Texture::Texture(SDL_Renderer *renderer)
|
||||
{
|
||||
this->renderer = renderer;
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
}
|
||||
: renderer_(renderer), texture_(nullptr), width_(0), height_(0) {}
|
||||
|
||||
Texture::Texture(SDL_Renderer *renderer, std::string filepath)
|
||||
Texture::Texture(SDL_Renderer *renderer, std::string file_path)
|
||||
: renderer_(renderer), texture_(nullptr), width_(0), height_(0)
|
||||
{
|
||||
this->renderer = renderer;
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
loadFromFile(filepath);
|
||||
loadFromFile(file_path);
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
@@ -26,12 +18,12 @@ Texture::~Texture()
|
||||
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 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)
|
||||
{
|
||||
SDL_Log("Loading image failed: %s", stbi_failure_reason());
|
||||
@@ -39,20 +31,18 @@ bool Texture::loadFromFile(std::string path)
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Image loaded: " << filename.c_str() << std::endl;
|
||||
std::cout << "Image loaded: " << filename.c_str() << std::endl;
|
||||
}
|
||||
|
||||
int depth, pitch;
|
||||
Uint32 pixel_format;
|
||||
int pitch;
|
||||
SDL_PixelFormat pixel_format;
|
||||
if (req_format == STBI_rgb)
|
||||
{
|
||||
depth = 24;
|
||||
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
|
||||
pixel_format = SDL_PIXELFORMAT_RGB24;
|
||||
}
|
||||
else
|
||||
{ // STBI_rgb_alpha (RGBA)
|
||||
depth = 32;
|
||||
pitch = 4 * width;
|
||||
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
||||
}
|
||||
@@ -61,69 +51,69 @@ bool Texture::loadFromFile(std::string path)
|
||||
free();
|
||||
|
||||
// La textura final
|
||||
SDL_Texture *newTexture = nullptr;
|
||||
SDL_Texture *new_texture = nullptr;
|
||||
|
||||
// Carga la imagen desde una ruta específica
|
||||
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
|
||||
if (loadedSurface == nullptr)
|
||||
SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
|
||||
if (loaded_surface == nullptr)
|
||||
{
|
||||
std::cout << "Unable to load image " << path.c_str() << std::endl;
|
||||
std::cout << "Unable to load image " << file_path << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Crea la textura desde los pixels de la surface
|
||||
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
|
||||
if (newTexture == nullptr)
|
||||
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
||||
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 << "! SDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Obtiene las dimensiones de la imagen
|
||||
this->width = loadedSurface->w;
|
||||
this->height = loadedSurface->h;
|
||||
width_ = loaded_surface->w;
|
||||
height_ = loaded_surface->h;
|
||||
}
|
||||
|
||||
// Elimina la textura cargada
|
||||
SDL_FreeSurface(loadedSurface);
|
||||
SDL_DestroySurface(loaded_surface);
|
||||
}
|
||||
|
||||
// Return success
|
||||
stbi_image_free(data);
|
||||
texture = newTexture;
|
||||
return texture != nullptr;
|
||||
texture_ = new_texture;
|
||||
return texture_ != nullptr;
|
||||
}
|
||||
|
||||
void Texture::free()
|
||||
{
|
||||
// Free texture if it exists
|
||||
if (texture != nullptr)
|
||||
if (texture_ != NULL)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
SDL_DestroyTexture(texture_);
|
||||
texture_ = NULL;
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Texture::render(SDL_Rect *src, SDL_Rect *dst)
|
||||
void Texture::render(SDL_FRect *src, SDL_FRect *dst)
|
||||
{
|
||||
// Render to screen
|
||||
SDL_RenderCopy(renderer, texture, src, dst);
|
||||
SDL_RenderTexture(renderer_, texture_, src, dst);
|
||||
}
|
||||
|
||||
int Texture::getWidth()
|
||||
{
|
||||
return width;
|
||||
return width_;
|
||||
}
|
||||
|
||||
int Texture::getHeight()
|
||||
{
|
||||
return height;
|
||||
return height_;
|
||||
}
|
||||
|
||||
// Modulación de color
|
||||
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
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <iostream>
|
||||
|
||||
// Texture wrapper class
|
||||
class Texture
|
||||
{
|
||||
private:
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Texture *texture;
|
||||
SDL_Renderer *renderer_;
|
||||
SDL_Texture *texture_;
|
||||
|
||||
// Image dimensions
|
||||
int width;
|
||||
int height;
|
||||
int width_;
|
||||
int height_;
|
||||
|
||||
public:
|
||||
// Initializes variables
|
||||
Texture(SDL_Renderer *renderer);
|
||||
Texture(SDL_Renderer *renderer, std::string filepath);
|
||||
Texture(SDL_Renderer *renderer, std::string file_path);
|
||||
|
||||
// Deallocates memory
|
||||
~Texture();
|
||||
@@ -29,7 +29,7 @@ public:
|
||||
void free();
|
||||
|
||||
// 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
|
||||
int getWidth();
|
||||
|
||||
Reference in New Issue
Block a user