You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
4.0 KiB

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