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.

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