Eliminada paleta.cpp i mig adaptada la surface que la gastava

This commit is contained in:
2025-03-07 14:12:24 +01:00
parent ce88596d35
commit 529bfb5e3a
6 changed files with 22 additions and 289 deletions

View File

@@ -1,218 +0,0 @@
#include "paleta.h"
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_ARGB8888
#include <SDL2/SDL_rect.h> // Para SDL_Rect
#include <fcntl.h> // Para SEEK_END, SEEK_SET
#include <stdio.h> // Para NULL, fseek, fclose, fopen, fread, ftell
#include <stdlib.h> // Para malloc, free
#include "gif.h" // Para LoadGif, LoadPalette
struct jSurface_s
{
Uint8 *data;
Uint16 w, h;
};
static SDL_Texture *jTex = NULL;
static jSurface jScreen;
static jSurface jDestSurf;
static jSurface jSourceSurf = NULL;
static Uint32 paleta[256];
static int jWidth = 256;
static int jHeight = 128;
static int transparentColor = 255;
void pInit(SDL_Renderer *renderer, int w, int h)
{
jWidth = w;
jHeight = h;
jTex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h);
jScreen = pNewSurface(w, h);
jDestSurf = jScreen;
}
jSurface pNewSurface(int w, int h)
{
jSurface surf = (jSurface)malloc(sizeof(jSurface_s));
surf->w = w;
surf->h = h;
surf->data = (Uint8 *)malloc(w * h);
return surf;
}
void pDeleteSurface(jSurface surf)
{
if (surf == NULL)
return;
if (surf->data != NULL)
free(surf->data);
free(surf);
}
void pSetDest(jSurface surf)
{
if (surf == NULL)
jDestSurf = jScreen;
else
jDestSurf = surf;
}
void pSetSource(jSurface surf)
{
jSourceSurf = surf;
}
void pBlit(int dx, int dy, int sx, int sy, int w, int h)
{
if (jSourceSurf == NULL)
return;
for (int iy = 0; iy < h; ++iy)
{
for (int ix = 0; ix < w; ++ix)
pPutPixel(dx + ix, dy + iy, pGetPixel(sx + ix, sy + iy));
}
}
jSurface pLoadSurface(const char *filename)
{
FILE *f = fopen(filename, "rb");
if (!f)
return NULL;
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = (Uint8 *)malloc(size);
if (!buffer) // Verificar que la memoria se asignó correctamente
{
fclose(f);
return NULL;
}
// Verificar el retorno de fread
if (fread(buffer, size, 1, f) != 1)
{
free(buffer); // Liberar memoria si falla la lectura
fclose(f);
return NULL;
}
fclose(f);
Uint16 w, h;
Uint8 *pixels = LoadGif(buffer, &w, &h);
free(buffer); // Liberar memoria después de usar el buffer
if (pixels == NULL)
{
return NULL;
}
jSurface surf = (jSurface)malloc(sizeof(jSurface_s));
if (!surf) // Verificar que la memoria se asignó correctamente
{
free(pixels); // Liberar los píxeles si falla
return NULL;
}
surf->w = w;
surf->h = h;
surf->data = pixels;
return surf;
}
void pLoadPal(const char *filename)
{
FILE *f = fopen(filename, "rb");
if (!f)
return;
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = (Uint8 *)malloc(size);
if (!buffer) // Verificar que la asignación de memoria fue exitosa
{
fclose(f);
return;
}
// Verificar el resultado de fread
if (fread(buffer, size, 1, f) != 1)
{
free(buffer); // Liberar memoria si falla la lectura
fclose(f);
return;
}
fclose(f);
Uint32 *pal = LoadPalette(buffer);
free(buffer); // Liberar memoria después de usar el buffer
if (pal == NULL)
{
return;
}
for (int i = 0; i < 256; ++i)
{
paleta[i] = pal[i];
}
}
void pSetPal(int index, Uint32 color)
{
paleta[index] = color;
}
void pCls(Uint8 color)
{
for (int i = 0; i < jDestSurf->w * jDestSurf->h; ++i)
jDestSurf->data[i] = color;
}
void pFlip(SDL_Renderer *renderer)
{
Uint32 *pixels;
int pitch;
SDL_LockTexture(jTex, NULL, (void **)&pixels, &pitch);
for (int i = 0; i < jWidth * jHeight; ++i)
pixels[i] = paleta[jScreen->data[i]];
SDL_UnlockTexture(jTex);
SDL_Rect rect = {0, 64, 256, 128};
SDL_RenderCopy(renderer, jTex, NULL, &rect);
}
void pPutPixel(int x, int y, Uint8 color)
{
if (x < 0 || y < 0 || x >= jDestSurf->w || y >= jDestSurf->h || color == transparentColor)
return;
jDestSurf->data[x + y * jDestSurf->w] = color;
}
Uint8 pGetPixel(int x, int y)
{
return jSourceSurf->data[x + y * jSourceSurf->w];
}
bool pFadePal()
{
// Colores pares
for (int i = 18; i > 0; i = i - 2)
{
paleta[i] = paleta[i - 2];
}
// Colores impares
for (int i = 17; i > 1; i = i - 2)
{
paleta[i] = paleta[i - 2];
}
paleta[1] = paleta[0];
return paleta[15] == paleta[0] ? true : false;
}

View File

@@ -1,29 +0,0 @@
#pragma once
#include <SDL2/SDL_render.h> // Para SDL_Renderer
#include <SDL2/SDL_stdinc.h> // Para Uint8, Uint32
typedef struct jSurface_s *jSurface;
jSurface pNewSurface(int w, int h);
void pDeleteSurface(jSurface surf);
void pSetDest(jSurface surf);
void pSetSource(jSurface surf);
jSurface pLoadSurface(const char *filename);
void pPutPixel(int x, int y, Uint8 color);
Uint8 pGetPixel(int x, int y);
void pBlit(int dx, int dy, int sx, int sy, int w, int h);
void pInit(SDL_Renderer *renderer, int w, int h);
void pSetPal(int index, Uint32 color);
void pLoadPal(const char *filename);
void pCls(Uint8 color);
void pFlip(SDL_Renderer *renderer);
bool pFadePal();

View File

@@ -55,7 +55,7 @@ Palette loadPalette(const std::string &file_path)
}
// Carga una paleta desde un archivo .pal
Palette readPalFile(const std::string& file_path)
Palette readPalFile(const std::string &file_path)
{
Palette palette{};
palette.fill(0); // Inicializar todo con 0 (transparente por defecto)
@@ -101,7 +101,6 @@ Palette readPalFile(const std::string& file_path)
return palette;
}
// Constructor
Surface::Surface(int w, int h)
: surface_data_(std::make_shared<SurfaceData>(w, h)),
@@ -186,10 +185,7 @@ void Surface::putPixel(int x, int y, Uint8 color)
}
// Obtiene el color de un pixel de la surface_data
Uint8 Surface::getPixel(int x, int y)
{
return surface_data_->data[x + y * surface_data_->width];
}
Uint8 Surface::getPixel(int x, int y) { return surface_data_->data[x + y * surface_data_->width]; }
// Dibuja un rectangulo relleno
void Surface::fillRect(SDL_Rect *rect, Uint8 color)
@@ -320,7 +316,7 @@ void Surface::render(int dx, int dy, int sx, int sy, int w, int h)
void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
{
auto surface_data = Screen::get()->getRendererSurface()->getSurfaceData();
auto surface_data_dest = Screen::get()->getRendererSurface()->getSurfaceData();
// Determina la región de origen (clip) a renderizar
int sx = (srcRect) ? srcRect->x : 0;
@@ -331,12 +327,12 @@ void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
// Limitar la región para evitar accesos fuera de rango en origen
w = std::min(w, surface_data_->width - sx);
h = std::min(h, surface_data_->height - sy);
w = std::min(w, surface_data->width - x);
h = std::min(h, surface_data->height - y);
w = std::min(w, surface_data_dest->width - x);
h = std::min(h, surface_data_dest->height - y);
// Limitar la región para evitar accesos fuera de rango en destino
w = std::min(w, surface_data->width - x);
h = std::min(h, surface_data->height - y);
w = std::min(w, surface_data_dest->width - x);
h = std::min(h, surface_data_dest->height - y);
// Renderiza píxel por píxel aplicando el flip si es necesario
for (int iy = 0; iy < h; ++iy)
@@ -352,13 +348,13 @@ void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
int dest_y = y + iy;
// Verificar que las coordenadas de destino están dentro de los límites
if (dest_x >= 0 && dest_x < surface_data->width && dest_y >= 0 && dest_y < surface_data->height)
if (dest_x >= 0 && dest_x < surface_data_dest->width && dest_y >= 0 && dest_y < surface_data_dest->height)
{
// Copia el píxel si no es transparente
Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width];
if (color != transparent_color_)
{
surface_data->data[dest_x + dest_y * surface_data->width] = color;
surface_data_dest->data[dest_x + dest_y * surface_data_dest->width] = color;
}
}
}

View File

@@ -19,20 +19,15 @@
#include "text.h" // for Text, TEXT_CENTER, TEXT_COLOR
#include "surface.h" // for Surface
#include "utils.h" // for Color, stringToColor, Palette
#include "paleta.h"
// Constructor
Title::Title()
: title_logo_surface_(Resource::get()->getSurface("title_logo.gif")),
title_logo_sprite_(std::make_shared<SSprite>(title_logo_surface_, 0, 0, title_logo_surface_->getWidth(), title_logo_surface_->getHeight())),
loading_screen_surface_(Resource::get()->getSurface("title_loading.gif")),
loading_screen_sprite_(std::make_shared<SSprite>(loading_screen_surface_, 0, options.game.height - loading_screen_surface_->getHeight(), loading_screen_surface_->getWidth(), loading_screen_surface_->getHeight())),
bg_surface_(std::make_shared<Surface>(options.game.width, options.game.height))
{
// Carga la surface con los gráficos de la pantalla de carga
pInit(Screen::get()->getRenderer(), 256, 128);
loading_screen_ = pLoadSurface(Asset::get()->get("loading_screen_color.gif").c_str());
pLoadPal(Asset::get()->get("loading_screen_color.gif").c_str());
pSetSource(loading_screen_);
// Inicializa variables
state_ = options.section.subsection == Subsection::TITLE_WITH_LOADING_SCREEN ? TitleState::SHOW_LOADING_SCREEN : TitleState::SHOW_MENU;
options.section.section = Section::TITLE;
@@ -52,12 +47,6 @@ Title::Title()
playMusic("title.ogg");
}
// Destructor
Title::~Title()
{
pDeleteSurface(loading_screen_);
}
// Inicializa la marquesina
void Title::initMarquee()
{
@@ -164,7 +153,7 @@ void Title::updateMarquee()
// Comprueba si ha terminado la marquesina y la reinicia
if (letters_[letters_.size() - 1].x < -10)
{
{
// Inicializa la marquesina
initMarquee();
}
@@ -213,7 +202,7 @@ void Title::update()
case TitleState::FADE_LOADING_SCREEN:
if (counter_ % 4 == 0)
{
if (pFadePal())
if (true) // poner aqui la condicion de "fin de fade = true"
{
counter_ = 0;
state_ = TitleState::SHOW_MENU;
@@ -267,12 +256,7 @@ void Title::render()
case TitleState::SHOW_LOADING_SCREEN:
case TitleState::FADE_LOADING_SCREEN:
// Dibuja la pantalla de carga
pCls(4);
pBlit(0, 0, 0, 0, 256, 128);
pFlip(Screen::get()->getRenderer());
// Dibuja el logo del título
loading_screen_sprite_->render();
title_logo_sprite_->render();
break;

View File

@@ -6,7 +6,6 @@
#include <memory> // for shared_ptr
#include <string> // for string
#include <vector> // for vector
#include "paleta.h" // for jSurface
class SSprite; // lines 13-13
class Surface; // lines 15-15
@@ -28,11 +27,13 @@ private:
};
// Objetos y punteros
std::shared_ptr<Surface> title_logo_surface_; // Textura con los graficos
std::shared_ptr<SSprite> title_logo_sprite_; // SSprite para manejar la surface
std::shared_ptr<Surface> bg_surface_; // Textura para dibujar el fondo de la pantalla
std::shared_ptr<Surface> cheevos_surface_; // Textura con la lista de logros
std::shared_ptr<SSprite> cheevos_sprite_; // SSprite para manejar la surface con la lista de logros
std::shared_ptr<Surface> title_logo_surface_; // Textura con los graficos
std::shared_ptr<SSprite> title_logo_sprite_; // SSprite para manejar la surface
std::shared_ptr<Surface> loading_screen_surface_; // Surface con los gráficos de la pantalla de carga
std::shared_ptr<SSprite> loading_screen_sprite_; // SSprite con los gráficos de la pantalla de carga
std::shared_ptr<Surface> bg_surface_; // Textura para dibujar el fondo de la pantalla
std::shared_ptr<Surface> cheevos_surface_; // Textura con la lista de logros
std::shared_ptr<SSprite> cheevos_sprite_; // SSprite para manejar la surface con la lista de logros
// Variables
int counter_ = 0; // Contador
@@ -43,7 +44,6 @@ private:
bool show_cheevos_ = false; // Indica si se muestra por pantalla el listado de logros
SDL_Rect cheevos_surface_view_; // Zona visible de la surface con el listado de logros
TitleState state_; // Estado en el que se encuentra el bucle principal
jSurface loading_screen_; // Surface con los gráficos de la pantalla de carga
// Actualiza las variables
void update();
@@ -83,7 +83,7 @@ public:
Title();
// Destructor
~Title();
~Title() = default;
// Bucle principal
void run();