demo: Optimizado el calculo del efecto de fuego

This commit is contained in:
2023-05-25 22:16:59 +02:00
parent ab90709963
commit c12e8841b4

View File

@@ -52,15 +52,13 @@ int gradCurrentColor = 192; // Color actual más alto del degradado
int gradBreathDirection = 0; // Indica si gradCurrentColor crece o decrece int gradBreathDirection = 0; // Indica si gradCurrentColor crece o decrece
// Variables para el efecto de fuego // Variables para el efecto de fuego
const int fireScreenWidth = 320; // Ancho del efecto de fuego const int fireScreenWidth = 320; // Ancho del efecto de fuego
const int fireScreenHeight = 240; // Alto del efecto de fuego const int fireScreenHeight = 240; // Alto del efecto de fuego
Uint32 fire[fireScreenHeight][fireScreenWidth]; // Buffer con el fuego Uint8 fire[fireScreenHeight][fireScreenWidth]; // Buffer con el fuego
Uint32 palette[256]; // Paleta de color para el fuego Uint32 palette[256]; // Paleta de color para el fuego
Uint8 surface[fireScreenHeight * fireScreenWidth]; int fireModifier = 28; // Valor para modificar el efecto de fuego (0-30)
// const int fireDesp = 240 - fireScreenHeight + 3; // Fila donde comienza a dibujarse el efecto de fuego SDL_Texture *fireTexture; // Textura donde pintar los pixels del fuego
int fireModifier = 28; // Valor para modificar el efecto de fuego (0-30) Uint32 *fireBuffer; // Ubiación donde se guardan los pixeles del fuego
SDL_Texture *fireTexture; // Textura donde pintar los pixels del fuego
Uint32 *fireBuffer; // Ubiación donde se guardan los pixeles del fuego
int pitch; int pitch;
// Inicializa las opciones // Inicializa las opciones
@@ -263,7 +261,6 @@ void initFire()
fire[y][x] = 0; fire[y][x] = 0;
fireTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, fireScreenWidth, fireScreenHeight); fireTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, fireScreenWidth, fireScreenHeight);
//fireTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, fireScreenWidth, fireScreenHeight);
// Inicializa la paleta // Inicializa la paleta
for (int x = 0; x < 256; ++x) for (int x = 0; x < 256; ++x)
@@ -273,11 +270,8 @@ void initFire()
// Saturation is always the maximum: 255 // Saturation is always the maximum: 255
// Lightness is 0..255 for x=0..128, and 255 for x=128..255 // Lightness is 0..255 for x=0..128, and 255 for x=128..255
ColorRGB color = HSLtoRGB(ColorHSL(x / 3, 255, std::min(255, x * 2))); ColorRGB color = HSLtoRGB(ColorHSL(x / 3, 255, std::min(255, x * 2)));
// set the palette to the calculated RGB value // Pon en la paleta el color calculado
palette[x] = 16777216 * 255 + 65536 * color.r + 256 * color.g + color.b; palette[x] = (Uint32)16777216 * 255 + 65536 * color.r + 256 * color.g + color.b;
//palette[x] = 65536 * color.r + 256 * color.g + color.b;
// palette[x] = 0xFFFF0000;
// palette[x] = color;
} }
} }
@@ -455,27 +449,26 @@ void updateFire()
const int h = fireScreenHeight; const int h = fireScreenHeight;
const int mod = 157 - fireModifier; const int mod = 157 - fireModifier;
// Bloquea la textura para poder escribir en los pixeles
SDL_LockTexture(fireTexture, NULL, (void **)&fireBuffer, &pitch);
// Crea la fila inferior del fuego con pixels aleatorios // Crea la fila inferior del fuego con pixels aleatorios
for (int x = 0; x < w; ++x) for (int x = 0; x < w; ++x)
{
fire[h - 1][x] = rand() % 256; fire[h - 1][x] = rand() % 256;
fireBuffer[x + (h - 1) * 320] = palette[fire[h - 1][x]];
}
// Calcula los pixeles del efecto de fuego, desde la fila superior hasta la inferior // Calcula los pixeles del efecto de fuego, desde la fila superior hasta la inferior
for (int y = 0; y < h - 1; ++y) for (int y = 0; y < h - 1; ++y)
for (int x = 0; x < w; ++x)
fire[y][x] = ((fire[(y + 1) % h][(x - 1 + w) % w] + fire[(y + 1) % h][(x) % w] + fire[(y + 1) % h][(x + 1) % w] + fire[(y + 2) % h][(x) % w]) * 32) / mod;
SDL_LockTexture(fireTexture, NULL, (void **)&fireBuffer, &pitch);
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) for (int x = 0; x < w; ++x)
{ {
surface[x + y * 320] = fire[y][x]; fire[y][x] = ((fire[(y + 1) % h][(x - 1 + w) % w] + fire[(y + 1) % h][(x) % w] + fire[(y + 1) % h][(x + 1) % w] + fire[(y + 2) % h][(x) % w]) * 32) / mod;
fireBuffer[x + y * 320] = palette[surface[x + y * 320]]; fireBuffer[x + y * 320] = palette[fire[y][x]];
} }
SDL_UnlockTexture(fireTexture);
// surface[x + y * 320] = 0;
// for (int i = 0; i < 320 * 240; ++i) // Desbloquea la textura
// fireBuffer[i] = palette[surface[i]]; SDL_UnlockTexture(fireTexture);
} }
// Actualiza el efecto de fondo // Actualiza el efecto de fondo
@@ -553,7 +546,6 @@ void renderGradient()
// Dibuja el efecto de fuego // Dibuja el efecto de fuego
void renderFire() void renderFire()
{ {
SDL_RenderCopy(renderer, fireTexture, NULL, NULL); SDL_RenderCopy(renderer, fireTexture, NULL, NULL);
} }