raylib/examples/textures/textures_blend_modes.c

97 lines
4.2 KiB
C
Raw Normal View History

2020-05-22 03:30:23 +03:00
/*******************************************************************************************
*
* raylib [textures] example - blend modes
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
2020-11-01 15:39:48 +03:00
* This example has been created using raylib 3.5 (www.raylib.com)
2020-05-22 03:30:23 +03:00
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
2020-11-01 15:39:48 +03:00
* Example contributed by Karlo Licudine (@accidentalrebel) and reviewed by Ramon Santamaria (@raysan5)
*
2020-05-22 03:30:23 +03:00
* Copyright (c) 2020 Karlo Licudine (@accidentalrebel)
*
********************************************************************************************/
#include "raylib.h"
2022-06-21 20:53:18 +03:00
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
2020-05-22 03:30:23 +03:00
int main(void)
{
2020-11-01 15:39:48 +03:00
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
InitWindow(screenWidth, screenHeight, "raylib [textures] example - blend modes");
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image bgImage = LoadImage("resources/cyberpunk_street_background.png"); // Loaded in CPU memory (RAM)
Texture2D bgTexture = LoadTextureFromImage(bgImage); // Image converted to texture, GPU memory (VRAM)
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
Image fgImage = LoadImage("resources/cyberpunk_street_foreground.png"); // Loaded in CPU memory (RAM)
Texture2D fgTexture = LoadTextureFromImage(fgImage); // Image converted to texture, GPU memory (VRAM)
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
// Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
2021-04-22 19:55:24 +03:00
UnloadImage(bgImage);
2020-11-01 15:39:48 +03:00
UnloadImage(fgImage);
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
const int blendCountMax = 4;
BlendMode blendMode = 0;
2021-04-22 19:55:24 +03:00
2020-11-01 15:39:48 +03:00
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE))
{
if (blendMode >= (blendCountMax - 1)) blendMode = 0;
else blendMode++;
}
//----------------------------------------------------------------------------------
2021-04-22 19:55:24 +03:00
2020-11-01 15:39:48 +03:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
ClearBackground(RAYWHITE);
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
DrawTexture(bgTexture, screenWidth/2 - bgTexture.width/2, screenHeight/2 - bgTexture.height/2, WHITE);
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
// Apply the blend mode and then draw the foreground texture
BeginBlendMode(blendMode);
DrawTexture(fgTexture, screenWidth/2 - fgTexture.width/2, screenHeight/2 - fgTexture.height/2, WHITE);
EndBlendMode();
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
// Draw the texts
DrawText("Press SPACE to change blend modes.", 310, 350, 10, GRAY);
2021-04-22 19:55:24 +03:00
2020-11-01 15:39:48 +03:00
switch (blendMode)
{
case BLEND_ALPHA: DrawText("Current: BLEND_ALPHA", (screenWidth / 2) - 60, 370, 10, GRAY); break;
case BLEND_ADDITIVE: DrawText("Current: BLEND_ADDITIVE", (screenWidth / 2) - 60, 370, 10, GRAY); break;
case BLEND_MULTIPLIED: DrawText("Current: BLEND_MULTIPLIED", (screenWidth / 2) - 60, 370, 10, GRAY); break;
case BLEND_ADD_COLORS: DrawText("Current: BLEND_ADD_COLORS", (screenWidth / 2) - 60, 370, 10, GRAY); break;
default: break;
}
2021-04-22 19:55:24 +03:00
2020-11-01 15:39:48 +03:00
DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, GRAY);
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
EndDrawing();
//----------------------------------------------------------------------------------
}
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(fgTexture); // Unload foreground texture
UnloadTexture(bgTexture); // Unload background texture
2020-05-22 03:30:23 +03:00
2020-11-01 15:39:48 +03:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
2021-04-22 19:55:24 +03:00
2020-11-01 15:39:48 +03:00
return 0;
2020-05-22 03:30:23 +03:00
}