textures_blend_modes.c 4.3 KB

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