mirror of
https://github.com/raysan5/raylib.git
synced 2025-12-06 06:13:10 +00:00
Update textures_screen_buffer.c
This commit is contained in:
parent
c22b1d84d5
commit
12cce1766f
@ -17,16 +17,10 @@
|
|||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define MAX_COLORS 256
|
#include <stdlib.h> // Required for: calloc(), free()
|
||||||
#define SCREEN_WIDTH 800
|
|
||||||
#define SCREEN_HEIGHT 450
|
|
||||||
#define SCALE_FACTOR 2
|
|
||||||
// buffer size at least for screenImage pixel count
|
|
||||||
#define INDEX_BUFFER_SIZE ((SCREEN_WIDTH * SCREEN_HEIGHT) / SCALE_FACTOR)
|
|
||||||
#define FLAME_WIDTH (SCREEN_WIDTH / SCALE_FACTOR)
|
|
||||||
|
|
||||||
static void GeneretePalette(Color *palette);
|
#define MAX_COLORS 256
|
||||||
static void ClearIndexBuffer(unsigned char *buffer, int count);
|
#define SCALE_FACTOR 2
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Program main entry point
|
// Program main entry point
|
||||||
@ -35,22 +29,31 @@ int main(void)
|
|||||||
{
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
const int screenWidth = SCREEN_WIDTH;
|
const int screenWidth = 800;
|
||||||
const int screenHeight = SCREEN_HEIGHT;
|
const int screenHeight = 450;
|
||||||
const int pixelScale = SCALE_FACTOR;
|
|
||||||
const int imageWidth = screenWidth / pixelScale;
|
|
||||||
const int imageHeight = screenHeight / pixelScale;
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - screen buffer");
|
InitWindow(screenWidth, screenHeight, "raylib [textures] example - screen buffer");
|
||||||
|
|
||||||
Color palette[MAX_COLORS] = {0};
|
int imageWidth = screenWidth/SCALE_FACTOR;
|
||||||
unsigned char indexBuffer[INDEX_BUFFER_SIZE] = {0};
|
int imageHeight = screenHeight/SCALE_FACTOR;
|
||||||
unsigned char flameRootBuffer[FLAME_WIDTH] = {0};
|
int flameWidth = screenWidth/SCALE_FACTOR;
|
||||||
|
|
||||||
|
Color palette[MAX_COLORS] = { 0 };
|
||||||
|
unsigned char *indexBuffer = RL_CALLOC(imageWidth*imageWidth, sizeof(unsigned char));
|
||||||
|
unsigned char *flameRootBuffer = RL_CALLOC(flameWidth, sizeof(unsigned char));
|
||||||
|
|
||||||
Image screenImage = GenImageColor(imageWidth, imageHeight, BLACK);
|
Image screenImage = GenImageColor(imageWidth, imageHeight, BLACK);
|
||||||
Texture screenTexture = LoadTextureFromImage(screenImage);
|
Texture screenTexture = LoadTextureFromImage(screenImage);
|
||||||
GeneretePalette(palette);
|
|
||||||
ClearIndexBuffer(indexBuffer, INDEX_BUFFER_SIZE);
|
// Generate flame color palette
|
||||||
ClearIndexBuffer(flameRootBuffer, FLAME_WIDTH);
|
for (int i = 0; i < MAX_COLORS; i++)
|
||||||
|
{
|
||||||
|
float t = (float)i/(float)(MAX_COLORS - 1);
|
||||||
|
float hue = t*t;
|
||||||
|
float saturation = t;
|
||||||
|
float value = t;
|
||||||
|
palette[i] = ColorFromHSV(250.0f + 150.0f*hue, saturation, value);
|
||||||
|
}
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
@ -58,8 +61,10 @@ int main(void)
|
|||||||
// Main game loop
|
// Main game loop
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
{
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
// Grow flameRoot
|
// Grow flameRoot
|
||||||
for (int x = 2; x < FLAME_WIDTH; ++x)
|
for (int x = 2; x < flameWidth; x++)
|
||||||
{
|
{
|
||||||
unsigned short flame = flameRootBuffer[x];
|
unsigned short flame = flameRootBuffer[x];
|
||||||
if (flame == 255) continue;
|
if (flame == 255) continue;
|
||||||
@ -68,26 +73,26 @@ int main(void)
|
|||||||
flameRootBuffer[x] = flame;
|
flameRootBuffer[x] = flame;
|
||||||
}
|
}
|
||||||
|
|
||||||
// transfer flameRoot to indexBuffer
|
// Transfer flameRoot to indexBuffer
|
||||||
for (int x = 0; x < FLAME_WIDTH; ++x)
|
for (int x = 0; x < flameWidth; x++)
|
||||||
{
|
{
|
||||||
int i = x + (imageHeight - 1) * imageWidth;
|
int i = x + (imageHeight - 1)*imageWidth;
|
||||||
indexBuffer[i] = flameRootBuffer[x];
|
indexBuffer[i] = flameRootBuffer[x];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear top row, because it can't move any higher
|
// Clear top row, because it can't move any higher
|
||||||
for (int x = 0; x < imageWidth; ++x)
|
for (int x = 0; x < imageWidth; x++)
|
||||||
{
|
{
|
||||||
if (indexBuffer[x] == 0) continue;
|
if (indexBuffer[x] == 0) continue;
|
||||||
indexBuffer[x] = 0;
|
indexBuffer[x] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip top row, it is already cleared
|
// Skip top row, it is already cleared
|
||||||
for (int y = 1; y < imageHeight; ++y)
|
for (int y = 1; y < imageHeight; y++)
|
||||||
{
|
{
|
||||||
for (int x = 0; x < imageWidth; ++x)
|
for (int x = 0; x < imageWidth; x++)
|
||||||
{
|
{
|
||||||
unsigned i = x + y * imageWidth;
|
unsigned int i = x + y*imageWidth;
|
||||||
unsigned char colorIndex = indexBuffer[i];
|
unsigned char colorIndex = indexBuffer[i];
|
||||||
if (colorIndex == 0) continue;
|
if (colorIndex == 0) continue;
|
||||||
|
|
||||||
@ -97,19 +102,19 @@ int main(void)
|
|||||||
int newX = x + moveX;
|
int newX = x + moveX;
|
||||||
if (newX < 0 || newX >= imageWidth) continue;
|
if (newX < 0 || newX >= imageWidth) continue;
|
||||||
|
|
||||||
unsigned i_above = i - imageWidth + moveX;
|
unsigned int iabove = i - imageWidth + moveX;
|
||||||
int decay = GetRandomValue(0, 3);
|
int decay = GetRandomValue(0, 3);
|
||||||
colorIndex -= (decay < colorIndex) ? decay : colorIndex;
|
colorIndex -= (decay < colorIndex)? decay : colorIndex;
|
||||||
indexBuffer[i_above] = colorIndex;
|
indexBuffer[iabove] = colorIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update screenImage with palette colors
|
// Update screenImage with palette colors
|
||||||
for (int y = 1; y < imageHeight; ++y)
|
for (int y = 1; y < imageHeight; y++)
|
||||||
{
|
{
|
||||||
for (int x = 0; x < imageWidth; ++x)
|
for (int x = 0; x < imageWidth; x++)
|
||||||
{
|
{
|
||||||
unsigned i = x + y * imageWidth;
|
unsigned int i = x + y*imageWidth;
|
||||||
unsigned char colorIndex = indexBuffer[i];
|
unsigned char colorIndex = indexBuffer[i];
|
||||||
Color col = palette[colorIndex];
|
Color col = palette[colorIndex];
|
||||||
ImageDrawPixel(&screenImage, x, y, col);
|
ImageDrawPixel(&screenImage, x, y, col);
|
||||||
@ -117,19 +122,24 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
UpdateTexture(screenTexture, screenImage.data);
|
UpdateTexture(screenTexture, screenImage.data);
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
const Vector2 origin = (Vector2){0, 0};
|
|
||||||
const float rotation = 0.f;
|
ClearBackground(RAYWHITE);
|
||||||
DrawTextureEx(screenTexture, origin, rotation, pixelScale, WHITE);
|
|
||||||
|
DrawTextureEx(screenTexture, (Vector2){ 0, 0 }, 0.0f, 2.0f, WHITE);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
RL_FREE(indexBuffer);
|
||||||
|
RL_FREE(flameRootBuffer);
|
||||||
UnloadTexture(screenTexture);
|
UnloadTexture(screenTexture);
|
||||||
UnloadImage(screenImage);
|
UnloadImage(screenImage);
|
||||||
|
|
||||||
@ -138,24 +148,3 @@ int main(void)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void GeneretePalette(Color *palette)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < MAX_COLORS; ++i)
|
|
||||||
{
|
|
||||||
float t = (float)i/(float)(MAX_COLORS - 1);
|
|
||||||
float hue = t * t;
|
|
||||||
float saturation = t;
|
|
||||||
float value = t;
|
|
||||||
palette[i] = ColorFromHSV(250.f + 150.f * hue, saturation, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ClearIndexBuffer(unsigned char *buffer, int count)
|
|
||||||
{
|
|
||||||
// Use memset to set to ZERO, but for demonstration a plain for loop is used
|
|
||||||
for (int i = 0; i < count; ++i)
|
|
||||||
{
|
|
||||||
buffer[i] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user