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.

152 lines
5.7 KiB

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