Sprite Sheet - Galeria de Exemplos
Sprite Sheet

Descrição: Este exemplo demonstra como desenhar imagens armazenadas em uma tira de imagens (sprite sheet).

Autor: Edirlei Soares de Lima

Download: Exemplos11.zip

 

Código Fonte:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "Graphics.h"
 
#define SPRITE_SIZE 128
Graphics graphics;
struct Sprite{
    int x;
    int y;
    int width;
    int height;
};
Sprite sprite[4][4];
Image sprite_sheet;
float player_x = 350;  
float player_y = 250;  
float player_speed = 80;
int animation_clip = 1;
int animation_index = 0;
float time_next_frame = 0;
bool key_states[256];
void KeyboardInput(int key, int state, int x, int y)
{
    if (state == KEY_STATE_DOWN)
        key_states[key] = true;
    else if (state == KEY_STATE_UP)
        key_states[key] = false;
}
void MainLoop()
    bool next_frame = false;
    time_next_frame = time_next_frame + graphics.GetElapsedTime();
    if (key_states[KEY_LEFT])
    {
       animation_clip = 0;
       next_frame = true;     
       player_x -= player_speed * graphics.GetElapsedTime();
    }
    else if (key_states[KEY_RIGHT])
    {
      animation_clip = 1;
      next_frame = true;
      player_x += player_speed * graphics.GetElapsedTime();
    }
    else if (key_states[KEY_UP])
    {
        animation_clip = 3;
        next_frame = true;
        player_y += player_speed * graphics.GetElapsedTime();
    }
    else if (key_states[KEY_DOWN])
    {
        animation_clip = 2;
        next_frame = true;
        player_y -= player_speed * graphics.GetElapsedTime();
    }
    if (next_frame)
    {
        if (time_next_frame > 0.1)
        {
            animation_index++;
            time_next_frame = 0;
        }
        if (animation_index > 3)
            animation_index = 0;
    }
    graphics.DrawImage2D(player_x, player_y, SPRITE_SIZE, SPRITE_SIZE, sprite[animation_clip][animation_index].x, sprite[animation_clip][animation_index].y, sprite[animation_clip][animation_index].width, sprite[animation_clip][animation_index].height, sprite_sheet);
}
void InitSprites()
{
    int x = 0, y = 0;
    int spritex = 0;
    int spritey = 0;
    for (int w = 0; w < 16; w++)
    {
        sprite[x][y].x = spritex;
        sprite[x][y].y = spritey;
        sprite[x][y].width = SPRITE_SIZE;
        sprite[x][y].height = SPRITE_SIZE;
        spritex += SPRITE_SIZE;
        y++;
        if (spritex >= 512)
        {      
            spritex = 0;
            spritey += SPRITE_SIZE;
            x++;
            y = 0;
        }
    }
}
 
int main(void)
{
    graphics.CreateMainWindow(800, 600, "Exemplo 11 - Sprite Sheet");
    graphics.SetBackgroundColor(152,209,250);
    graphics.SetKeyboardInput(KeyboardInput);
    sprite_sheet.LoadPNGImage("Char_Sprite.png");
    InitSprites();
        
    graphics.SetMainLoop(MainLoop);
    graphics.StartMainLoop();
    return 0;
}