raylib/ports/textures/textures_gif_player
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
textures_gif_player.odin ¶71 linesSource
1package main
2
3import rl "vendor:raylib"
4
5MAX_FRAME_DELAY :: 20
6MIN_FRAME_DELAY :: 1
7
8main :: proc() {
9 screen_width := i32(800)
10 screen_height := i32(450)
11
12 rl.InitWindow(screen_width, screen_height, "raylib [textures] example - gif playing")
13 defer rl.CloseWindow()
14
15 anim_frames: i32
16 im_scarfy_anim := rl.LoadImageAnim("resources/scarfy_run.gif", &anim_frames)
17 defer rl.UnloadImage(im_scarfy_anim)
18
19 tex_scarfy_anim := rl.LoadTextureFromImage(im_scarfy_anim)
20 defer rl.UnloadTexture(tex_scarfy_anim)
21
22 next_frame_data_offset, current_anim_frame, frame_counter: i32
23 frame_delay := i32(MAX_FRAME_DELAY/2)
24
25 rl.SetTargetFPS(60)
26
27 for !rl.WindowShouldClose() {
28 frame_counter += 1
29 if frame_counter >= frame_delay {
30 current_anim_frame = (current_anim_frame + 1) % anim_frames
31
32 next_frame_data_offset = im_scarfy_anim.width * im_scarfy_anim.height * 4 * current_anim_frame
33
34 rl.UpdateTexture(tex_scarfy_anim, ([^]byte)(im_scarfy_anim.data)[next_frame_data_offset:])
35
36 frame_counter = 0
37 }
38
39 if rl.IsKeyPressed(.RIGHT) {
40 frame_delay = min(frame_delay + 1, MAX_FRAME_DELAY)
41 } else if rl.IsKeyPressed(.LEFT) {
42 frame_delay = max(frame_delay - 1, MIN_FRAME_DELAY)
43 }
44
45 {
46 rl.BeginDrawing()
47 defer rl.EndDrawing()
48
49 rl.ClearBackground(rl.RAYWHITE)
50
51 rl.DrawText(rl.TextFormat("TOTAL GIF FRAMES: %02i", anim_frames), 50, 30, 20, rl.LIGHTGRAY)
52 rl.DrawText(rl.TextFormat("CURRENT FRAME: %02i", current_anim_frame), 50, 60, 20, rl.GRAY)
53 rl.DrawText(rl.TextFormat("CURRENT FRAME IMAGE.DATA OFFSET: %02i", next_frame_data_offset), 50, 90, 20, rl.GRAY)
54
55 rl.DrawText("FRAMES DELAY: ", 100, 305, 10, rl.DARKGRAY)
56 rl.DrawText(rl.TextFormat("%02i frames", frame_delay), 620, 305, 10, rl.DARKGRAY)
57 rl.DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 350, 10, rl.DARKGRAY)
58
59 for i in i32(0)..<MAX_FRAME_DELAY {
60 if i < frame_delay {
61 rl.DrawRectangle(190 + 21*i, 300, 20, 20, rl.RED)
62 }
63 rl.DrawRectangleLines(190 + 21*i, 300, 20, 20, rl.MAROON)
64 }
65
66 rl.DrawTexture(tex_scarfy_anim, rl.GetScreenWidth()/2 - tex_scarfy_anim.width/2, 140, rl.WHITE)
67
68 rl.DrawText("(c) Scarfy sprite by Eiden Marsal", screen_width - 200, screen_height - 20, 10, rl.GRAY)
69 }
70 }
71}
Assets 1
Declarations Used 26
- vendor:raylib BeginDrawing · ClearBackground · CloseWindow · DARKGRAY · DrawRectangle · DrawRectangleLines · DrawText · DrawTexture · EndDrawing · GetScreenWidth · GRAY · InitWindow · IsKeyPressed · LIGHTGRAY · LoadImageAnim · LoadTextureFromImage · MAROON · RAYWHITE · RED · SetTargetFPS · TextFormat · UnloadImage · UnloadTexture · UpdateTexture · WHITE · WindowShouldClose