demo: El efecto de fuego se ha optimizado pintando los pixels en una textura
This commit is contained in:
49
main.cpp
49
main.cpp
@@ -52,12 +52,16 @@ int gradCurrentColor = 192; // Color actual más alto del degradado
|
||||
int gradBreathDirection = 0; // Indica si gradCurrentColor crece o decrece
|
||||
|
||||
// Variables para el efecto de fuego
|
||||
const int fireScreenWidth = 320; // Ancho del efecto de fuego
|
||||
const int fireScreenHeight = 243; // Alto del efecto de fuego
|
||||
int fire[fireScreenHeight][fireScreenWidth]; // Buffer con el fuego
|
||||
ColorRGB palette[256]; // Paleta de color para el fuego
|
||||
const int fireDesp = 240 - fireScreenHeight + 3; // Fila donde comienza a dibujarse el efecto de fuego
|
||||
int fireModifier = 28; // Valor para modificar el efecto de fuego (0-30)
|
||||
const int fireScreenWidth = 320; // Ancho del efecto de fuego
|
||||
const int fireScreenHeight = 240; // Alto del efecto de fuego
|
||||
Uint32 fire[fireScreenHeight][fireScreenWidth]; // Buffer con el fuego
|
||||
Uint32 palette[256]; // Paleta de color para el fuego
|
||||
Uint8 surface[fireScreenHeight * fireScreenWidth];
|
||||
// const int fireDesp = 240 - fireScreenHeight + 3; // Fila donde comienza a dibujarse el efecto de fuego
|
||||
int fireModifier = 28; // Valor para modificar el efecto de fuego (0-30)
|
||||
SDL_Texture *fireTexture; // Textura donde pintar los pixels del fuego
|
||||
Uint32 *fireBuffer; // Ubiación donde se guardan los pixeles del fuego
|
||||
int pitch;
|
||||
|
||||
// Inicializa las opciones
|
||||
void initOptions();
|
||||
@@ -250,7 +254,7 @@ void initSprite()
|
||||
// Inicializa el efecto de fuego
|
||||
void initFire()
|
||||
{
|
||||
// Intializes random number generator
|
||||
// Inicializa el generador de numeros aleatorios
|
||||
srand((unsigned)SDL_GetTicks());
|
||||
|
||||
// Inicializa el buffer de fuego
|
||||
@@ -258,6 +262,9 @@ void initFire()
|
||||
for (int x = 0; x < fireScreenWidth; ++x)
|
||||
fire[y][x] = 0;
|
||||
|
||||
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
|
||||
for (int x = 0; x < 256; ++x)
|
||||
{
|
||||
@@ -267,8 +274,10 @@ void initFire()
|
||||
// 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)));
|
||||
// set the palette to the calculated RGB value
|
||||
// palette[x] = RGBtoINT(color);
|
||||
palette[x] = color;
|
||||
palette[x] = 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -454,6 +463,19 @@ void updateFire()
|
||||
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)
|
||||
{
|
||||
surface[x + y * 320] = fire[y][x];
|
||||
fireBuffer[x + y * 320] = palette[surface[x + y * 320]];
|
||||
}
|
||||
SDL_UnlockTexture(fireTexture);
|
||||
// surface[x + y * 320] = 0;
|
||||
|
||||
// for (int i = 0; i < 320 * 240; ++i)
|
||||
// fireBuffer[i] = palette[surface[i]];
|
||||
}
|
||||
|
||||
// Actualiza el efecto de fondo
|
||||
@@ -531,13 +553,8 @@ void renderGradient()
|
||||
// Dibuja el efecto de fuego
|
||||
void renderFire()
|
||||
{
|
||||
for (int y = 0; y < fireScreenHeight - 3; ++y)
|
||||
for (int x = 0; x < fireScreenWidth; ++x)
|
||||
{
|
||||
const ColorRGB c = palette[std::min(255, std::max(0, fire[y][x]))];
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 255);
|
||||
SDL_RenderDrawPoint(renderer, x, y + fireDesp);
|
||||
}
|
||||
|
||||
SDL_RenderCopy(renderer, fireTexture, NULL, NULL);
|
||||
}
|
||||
|
||||
// Dibuja el efecto de fondo
|
||||
|
||||
Reference in New Issue
Block a user