raylib/ports/shapes/shapes_colors_palette
Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license.
Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event
will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you
wrote the original software. If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented
as being the original software.
3. This notice may not be removed or altered from any source distribution.
Code
shapes_colors_palette.odin ¶122 linesSource
1package raylib_examples
2
3/*******************************************************************************************
4*
5* raylib [shapes] example - Colors palette
6*
7* This example was originally created in C with raylib 1.0, and updated with raylib 2.5
8* before being translated to Odin.
9*
10* The original example in C can be found on raylib.com at:
11*
12* https://www.raylib.com/examples/shapes/loader.html?name=shapes_colors_palette
13*
14* raylib is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
15* BSD-like license that allows static linking with closed source software. The license
16* can be found in its entirety as part of the standard Odin distribution at:
17*
18* /vendor/raylib/LICENSE
19*
20* or online at:
21*
22* https://www.raylib.com/license.html
23*
24* This example is licensed under an unmodified zlib/libpng license, which is an
25* OSI-certified, BSD-like license that allows static linking with closed source software.
26*
27* Copyright (c) 2014-2023 Ramon Santamaria (@raysan5)
28* Copyright (c) 2023 Benjamin G. Thompson (@bg-thompson)
29*
30********************************************************************************************/
31
32import rl "vendor:raylib"
33
34Color_And_Name :: struct{
35 color: rl.Color,
36 name: cstring,
37}
38
39COLORS_AND_NAMES :: [?]Color_And_Name {
40 {rl.DARKGRAY, "DARKGRAY"},
41 {rl.MAROON, "MAROON"},
42 {rl.ORANGE, "ORANGE"},
43 {rl.DARKGREEN, "DARKGREEN"},
44 {rl.DARKBLUE, "DARKBLUE"},
45 {rl.DARKPURPLE, "DARKPURPLE"},
46 {rl.DARKBROWN, "DARKBROWN"},
47 {rl.GRAY, "GRAY"},
48 {rl.RED, "RED"},
49 {rl.GOLD, "GOLD"},
50 {rl.LIME, "LIME"},
51 {rl.BLUE, "BLUE"},
52 {rl.VIOLET, "VIOLET"},
53 {rl.BROWN, "BROWN"},
54 {rl.LIGHTGRAY, "LIGHTGRAY"},
55 {rl.PINK, "PINK"},
56 {rl.YELLOW, "YELLOW"},
57 {rl.GREEN, "GREEN"},
58 {rl.SKYBLUE, "SKYBLUE"},
59 {rl.PURPLE, "PURPLE"},
60 {rl.BEIGE, "BEIGE"},
61}
62
63NUMBER_OF_COLORS :: len(COLORS_AND_NAMES)
64
65SCREEN_WIDTH :: 800
66SCREEN_HEIGHT :: 450
67
68color_rects: [NUMBER_OF_COLORS]rl.Rectangle
69
70@(init) // Calls this proc once before main.
71initialize_colors_rects :: proc "contextless" () {
72 // Initial rectangle geometry data in colors_rects.
73 for i in 0..<len(color_rects) {
74 color_rects[i].x = 20 + 100 * f32(i % 7) + 10 * f32(i % 7)
75 color_rects[i].y = 80 + 100 * f32(i / 7) + 10 * f32(i / 7)
76 color_rects[i].width = 100
77 color_rects[i].height = 100
78 }
79}
80
81main :: proc() {
82 rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - colors palette")
83 defer rl.CloseWindow() // Close window and OpenGL context when leaving main.
84
85 color_state: [NUMBER_OF_COLORS]bool // Represents if the mouse is hovering over a rect.
86 mouse_pos: rl.Vector2
87 rl.SetTargetFPS(60) // Set frames per second.
88
89 // The primary loop.
90 for !rl.WindowShouldClose() { // Detect window close button or ESC key press.
91 mouse_pos = rl.GetMousePosition()
92
93 // Set mouse-hover information.
94 #assert(len(color_state) == len(color_rects))
95 for &state, i in color_state {
96 state = rl.CheckCollisionPointRec(mouse_pos, color_rects[i])
97 }
98
99 // Start drawing.
100 rl.BeginDrawing()
101
102 // Background and background text.
103 rl.ClearBackground(rl.RAYWHITE)
104 rl.DrawText("raylib colors palette", 28, 42, 20, rl.BLACK)
105 rl.DrawText("press SPACE to see all colors", rl.GetScreenWidth() - 180, rl.GetScreenHeight() - 40, 10, rl.GRAY)
106
107 // Draw rectangles.
108 #assert(len(COLORS_AND_NAMES) == len(color_rects))
109 #assert(len(COLORS_AND_NAMES) == len(color_state))
110 for color_and_name, i in COLORS_AND_NAMES {
111 color, name := color_and_name.color, color_and_name.name
112 rl.DrawRectangleRec(color_rects[i], rl.Fade(color, color_state[i] ? 0.6 : 1))
113 if rl.IsKeyDown(rl.KeyboardKey.SPACE) || color_state[i] {
114 crect := color_rects[i]
115 rl.DrawRectangle(i32(crect.x), i32(crect.y + crect.height) - 26, i32(crect.width), 20, rl.BLACK)
116 rl.DrawRectangleLinesEx(crect, 6, rl.Fade(rl.BLACK, 0.3))
117 rl.DrawText(name, i32(crect.x + crect.width) - rl.MeasureText(name, 10) - 12, i32(crect.y + crect.height) - 20, 10, color)
118 }
119 }
120 rl.EndDrawing()
121 }
122}
Declarations Used 45
- vendor:raylib BeginDrawing · BEIGE · BLACK · BLUE · BROWN · CheckCollisionPointRec · ClearBackground · CloseWindow · Color · DARKBLUE · DARKBROWN · DARKGRAY · DARKGREEN · DARKPURPLE · DrawRectangle · DrawRectangleLinesEx · DrawRectangleRec · DrawText · EndDrawing · Fade · GetMousePosition · GetScreenHeight · GetScreenWidth · GOLD · GRAY · GREEN · InitWindow · IsKeyDown · KeyboardKey · LIGHTGRAY · LIME · MAROON · MeasureText · ORANGE · PINK · PURPLE · RAYWHITE · Rectangle · RED · SetTargetFPS · SKYBLUE · Vector2 · VIOLET · WindowShouldClose · YELLOW