raylib/ports/shapes/shapes_logo_raylib_anim

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.
A screenshot of shapes_logo_raylib_anim

Code

shapes_logo_raylib_anim.odin ¶
155 linesSource

1package raylib_examples
2
3/*******************************************************************************************
4*
5*   raylib [shapes] example - raylib logo animation
6*
7*   This example was originally created in C with raylib 1.0, and updated with raylib 4.0
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_logo_raylib_anim
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
34SCREEN_WIDTH  :: 800
35SCREEN_HEIGHT :: 450
36
37State :: enum u8 {
38	STATE_0,
39	STATE_1,
40	STATE_2,
41	STATE_3,
42	STATE_4,
43}
44
45main :: proc() {
46	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - raylib logo animation")
47	defer rl.CloseWindow() // Close window and OpenGL context when leaving main.
48
49	logoPosX: i32 = SCREEN_WIDTH  / 2 - 128
50	logoPosY: i32 = SCREEN_HEIGHT / 2 - 128
51
52	framesCounter := 0
53	lettersCount: i32 = 0
54
55	topSideRecWidth:    i32 = 16
56	leftSideRecHeight:  i32 = 16
57	bottomSideRecWidth: i32 = 16
58	rightSideRecHeight: i32 = 16
59
60
61	state := State.STATE_0 // Track animation states.
62	alpha: f32 = 1.0       // Useful for fading.
63	rl.SetTargetFPS(60)    // Set frames per second.
64
65	// The primary loop.
66	for !rl.WindowShouldClose() { // Detect window close button or ESC key press.
67		switch state {
68		case .STATE_0:                 // State 0: Small box blinking.
69			framesCounter += 1
70			if framesCounter == 120 {
71				state = .STATE_1
72				framesCounter = 0      // Reset counter... will be used later...
73			}
74
75		case .STATE_1:                 // State 1: Top and left bars growing.
76			topSideRecWidth   += 4
77			leftSideRecHeight += 4
78			if topSideRecWidth == 256 {
79				state = .STATE_2
80			}
81
82		case .STATE_2:                 // State 2: Bottom and right bars growing.
83			bottomSideRecWidth += 4
84			rightSideRecHeight += 4
85			if bottomSideRecWidth == 256 {
86				state = .STATE_3
87			}
88
89		case .STATE_3:                 // State 3: Letters appearing (one by one).
90			framesCounter += 1
91			if framesCounter / 12 == 1 { // Every 12 frames, one more letter!
92				lettersCount += 1
93				framesCounter = 0
94			}
95
96			if lettersCount >= 10 { // When all letters have appeared, fade out everything.
97				alpha -= 0.02
98				if alpha <= 0 {
99					alpha = 0
100					state = .STATE_4
101				}
102			}
103
104		case .STATE_4:
105			if rl.IsKeyPressed(rl.KeyboardKey.R) {
106				framesCounter      = 0
107				lettersCount       = 0
108				topSideRecWidth    = 16
109				leftSideRecHeight  = 16
110				bottomSideRecWidth = 16
111				rightSideRecHeight = 16
112				alpha = 1.0
113				state = .STATE_0          // Return to State 0
114			}
115		}
116
117		// Start drawing.
118		rl.BeginDrawing()
119
120		rl.ClearBackground(rl.RAYWHITE)
121
122		switch state {
123		case .STATE_0:
124			if (framesCounter / 15) % 2 != 0 {
125				rl.DrawRectangle(logoPosX, logoPosY, 16, 16, rl.BLACK)
126			}
127
128		case .STATE_1:
129			rl.DrawRectangle(logoPosX, logoPosY, topSideRecWidth, 16, rl.BLACK)
130			rl.DrawRectangle(logoPosX, logoPosY, 16, leftSideRecHeight, rl.BLACK)
131
132		case .STATE_2:
133			rl.DrawRectangle(logoPosX, logoPosY, topSideRecWidth, 16, rl.BLACK)
134			rl.DrawRectangle(logoPosX, logoPosY, 16, leftSideRecHeight, rl.BLACK)
135
136			rl.DrawRectangle(logoPosX + 240, logoPosY, 16, rightSideRecHeight, rl.BLACK)
137			rl.DrawRectangle(logoPosX, logoPosY + 240, bottomSideRecWidth, 16, rl.BLACK)
138
139		case .STATE_3:
140			rl.DrawRectangle(logoPosX, logoPosY, topSideRecWidth, 16, rl.Fade(rl.BLACK, alpha))
141			rl.DrawRectangle(logoPosX, logoPosY + 16, 16, leftSideRecHeight - 32, rl.Fade(rl.BLACK, alpha))
142
143			rl.DrawRectangle(logoPosX + 240, logoPosY + 16, 16, rightSideRecHeight - 32, rl.Fade(rl.BLACK, alpha))
144			rl.DrawRectangle(logoPosX, logoPosY + 240, bottomSideRecWidth, 16, rl.Fade(rl.BLACK, alpha))
145
146			rl.DrawRectangle(rl.GetScreenWidth() / 2 - 112, rl.GetScreenHeight() / 2 - 112, 224, 224, rl.Fade(rl.RAYWHITE, alpha))
147
148			rl.DrawText(rl.TextSubtext("raylib", 0, lettersCount), rl.GetScreenWidth()/2 - 44, rl.GetScreenHeight() / 2 + 48, 50, rl.Fade(rl.BLACK, alpha))
149
150		case .STATE_4:
151			rl.DrawText("[R] REPLAY", 340, 200, 20, rl.GRAY)
152		}
153		rl.EndDrawing()
154	}
155}

Declarations Used 18