textures_blend_modes.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - blend modes
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  8. *
  9. * Example originally created with raylib 3.5, last time updated with raylib 3.5
  10. *
  11. * Example contributed by Karlo Licudine (@accidentalrebel) and reviewed by Ramon Santamaria (@raysan5)
  12. *
  13. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  14. * BSD-like license that allows static linking with closed source software
  15. *
  16. * Copyright (c) 2020-2025 Karlo Licudine (@accidentalrebel)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [textures] example - blend modes");
  30. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  31. Image bgImage = LoadImage("resources/cyberpunk_street_background.png"); // Loaded in CPU memory (RAM)
  32. Texture2D bgTexture = LoadTextureFromImage(bgImage); // Image converted to texture, GPU memory (VRAM)
  33. Image fgImage = LoadImage("resources/cyberpunk_street_foreground.png"); // Loaded in CPU memory (RAM)
  34. Texture2D fgTexture = LoadTextureFromImage(fgImage); // Image converted to texture, GPU memory (VRAM)
  35. // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  36. UnloadImage(bgImage);
  37. UnloadImage(fgImage);
  38. const int blendCountMax = 4;
  39. BlendMode blendMode = 0;
  40. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  41. //---------------------------------------------------------------------------------------
  42. // Main game loop
  43. while (!WindowShouldClose()) // Detect window close button or ESC key
  44. {
  45. // Update
  46. //----------------------------------------------------------------------------------
  47. if (IsKeyPressed(KEY_SPACE))
  48. {
  49. if (blendMode >= (blendCountMax - 1)) blendMode = 0;
  50. else blendMode++;
  51. }
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. DrawTexture(bgTexture, screenWidth/2 - bgTexture.width/2, screenHeight/2 - bgTexture.height/2, WHITE);
  58. // Apply the blend mode and then draw the foreground texture
  59. BeginBlendMode(blendMode);
  60. DrawTexture(fgTexture, screenWidth/2 - fgTexture.width/2, screenHeight/2 - fgTexture.height/2, WHITE);
  61. EndBlendMode();
  62. // Draw the texts
  63. DrawText("Press SPACE to change blend modes.", 310, 350, 10, GRAY);
  64. switch (blendMode)
  65. {
  66. case BLEND_ALPHA: DrawText("Current: BLEND_ALPHA", (screenWidth/2) - 60, 370, 10, GRAY); break;
  67. case BLEND_ADDITIVE: DrawText("Current: BLEND_ADDITIVE", (screenWidth/2) - 60, 370, 10, GRAY); break;
  68. case BLEND_MULTIPLIED: DrawText("Current: BLEND_MULTIPLIED", (screenWidth/2) - 60, 370, 10, GRAY); break;
  69. case BLEND_ADD_COLORS: DrawText("Current: BLEND_ADD_COLORS", (screenWidth/2) - 60, 370, 10, GRAY); break;
  70. default: break;
  71. }
  72. DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, GRAY);
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. UnloadTexture(fgTexture); // Unload foreground texture
  79. UnloadTexture(bgTexture); // Unload background texture
  80. CloseWindow(); // Close window and OpenGL context
  81. //--------------------------------------------------------------------------------------
  82. return 0;
  83. }