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.

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