shaders_palette_switch.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - palette switch
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  8. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
  9. *
  10. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
  11. * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
  12. * raylib comes with shaders ready for both versions, check raylib/shaders install folder
  13. *
  14. * Example originally created with raylib 2.5, last time updated with raylib 3.7
  15. *
  16. * Example contributed by Marco Lizza (@MarcoLizza) and reviewed by Ramon Santamaria (@raysan5)
  17. *
  18. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  19. * BSD-like license that allows static linking with closed source software
  20. *
  21. * Copyright (c) 2019-2025 Marco Lizza (@MarcoLizza) and Ramon Santamaria (@raysan5)
  22. *
  23. ********************************************************************************************/
  24. #include "raylib.h"
  25. #if defined(PLATFORM_DESKTOP)
  26. #define GLSL_VERSION 330
  27. #else // PLATFORM_ANDROID, PLATFORM_WEB
  28. #define GLSL_VERSION 100
  29. #endif
  30. #define MAX_PALETTES 3
  31. #define COLORS_PER_PALETTE 8
  32. #define VALUES_PER_COLOR 3
  33. //------------------------------------------------------------------------------------
  34. // Global Variables Definition
  35. //------------------------------------------------------------------------------------
  36. static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = {
  37. { // 3-BIT RGB
  38. 0, 0, 0,
  39. 255, 0, 0,
  40. 0, 255, 0,
  41. 0, 0, 255,
  42. 0, 255, 255,
  43. 255, 0, 255,
  44. 255, 255, 0,
  45. 255, 255, 255,
  46. },
  47. { // AMMO-8 (GameBoy-like)
  48. 4, 12, 6,
  49. 17, 35, 24,
  50. 30, 58, 41,
  51. 48, 93, 66,
  52. 77, 128, 97,
  53. 137, 162, 87,
  54. 190, 220, 127,
  55. 238, 255, 204,
  56. },
  57. { // RKBV (2-strip film)
  58. 21, 25, 26,
  59. 138, 76, 88,
  60. 217, 98, 117,
  61. 230, 184, 193,
  62. 69, 107, 115,
  63. 75, 151, 166,
  64. 165, 189, 194,
  65. 255, 245, 247,
  66. }
  67. };
  68. static const char *paletteText[] = {
  69. "3-BIT RGB",
  70. "AMMO-8 (GameBoy-like)",
  71. "RKBV (2-strip film)"
  72. };
  73. //------------------------------------------------------------------------------------
  74. // Program main entry point
  75. //------------------------------------------------------------------------------------
  76. int main(void)
  77. {
  78. // Initialization
  79. //--------------------------------------------------------------------------------------
  80. const int screenWidth = 800;
  81. const int screenHeight = 450;
  82. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - palette switch");
  83. // Load shader to be used on some parts drawing
  84. // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
  85. // NOTE 2: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  86. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/palette_switch.fs", GLSL_VERSION));
  87. // Get variable (uniform) location on the shader to connect with the program
  88. // NOTE: If uniform variable could not be found in the shader, function returns -1
  89. int paletteLoc = GetShaderLocation(shader, "palette");
  90. int currentPalette = 0;
  91. int lineHeight = screenHeight/COLORS_PER_PALETTE;
  92. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  93. //--------------------------------------------------------------------------------------
  94. // Main game loop
  95. while (!WindowShouldClose()) // Detect window close button or ESC key
  96. {
  97. // Update
  98. //----------------------------------------------------------------------------------
  99. if (IsKeyPressed(KEY_RIGHT)) currentPalette++;
  100. else if (IsKeyPressed(KEY_LEFT)) currentPalette--;
  101. if (currentPalette >= MAX_PALETTES) currentPalette = 0;
  102. else if (currentPalette < 0) currentPalette = MAX_PALETTES - 1;
  103. // Send palette data to the shader to be used on drawing
  104. // NOTE: We are sending RGB triplets w/o the alpha channel
  105. SetShaderValueV(shader, paletteLoc, palettes[currentPalette], SHADER_UNIFORM_IVEC3, COLORS_PER_PALETTE);
  106. //----------------------------------------------------------------------------------
  107. // Draw
  108. //----------------------------------------------------------------------------------
  109. BeginDrawing();
  110. ClearBackground(RAYWHITE);
  111. BeginShaderMode(shader);
  112. for (int i = 0; i < COLORS_PER_PALETTE; i++)
  113. {
  114. // Draw horizontal screen-wide rectangles with increasing "palette index"
  115. // The used palette index is encoded in the RGB components of the pixel
  116. DrawRectangle(0, lineHeight*i, GetScreenWidth(), lineHeight, (Color){ i, i, i, 255 });
  117. }
  118. EndShaderMode();
  119. DrawText("< >", 10, 10, 30, DARKBLUE);
  120. DrawText("CURRENT PALETTE:", 60, 15, 20, RAYWHITE);
  121. DrawText(paletteText[currentPalette], 300, 15, 20, RED);
  122. DrawFPS(700, 15);
  123. EndDrawing();
  124. //----------------------------------------------------------------------------------
  125. }
  126. // De-Initialization
  127. //--------------------------------------------------------------------------------------
  128. UnloadShader(shader); // Unload shader
  129. CloseWindow(); // Close window and OpenGL context
  130. //--------------------------------------------------------------------------------------
  131. return 0;
  132. }