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