summaryrefslogtreecommitdiff
path: root/examples/texture.c
blob: 1a4382ab292424dc3da887e23d1a4a195181d339 (plain)
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
#include <assert.h>

#include "../sponge.h"
#include "../stb_image.h"

#define SPEED_X_ABS 2
#define SPEED_Y_ABS 2

sponge_Texture texture;

int32_t pos_x;
int32_t pos_y;
int32_t speed_x;
int32_t speed_y;

void init() {
    unsigned char *data = stbi_load("examples/kot.png", &texture.width, &texture.height, NULL, 4);
    if (!data) {
        printf("%s\n", stbi_failure_reason());
        assert(0);
    }

    texture.stride = texture.width;
    texture.pixels = data; // NOTE(kard): be careful if changing following loop

    // converting from stbi's RGBA to ARGB
    for (size_t i = 0; i < texture.width * texture.height; i++) {
        uint32_t result = 0;
        result |= data[i * 4 + 0] << 16; // R
        result |= data[i * 4 + 1] << 8;  // G
        result |= data[i * 4 + 2] << 0;  // B
        result |= data[i * 4 + 3] << 24; // A
        texture.pixels[i] = result;
    }

    pos_x = 20;
    pos_y = 10;
    speed_x = SPEED_X_ABS;
    speed_y = SPEED_Y_ABS;
}

void draw_frame(sponge_Texture c) {
    sponge_clear(c, 0xFF000000);

    pos_x += speed_x;
    pos_y += speed_y;

    if (pos_x < 0) {
        pos_x = 0;
        speed_x = SPEED_X_ABS;
    } else if (pos_x + texture.width > c.width) {
        speed_x = -SPEED_X_ABS;
    }

    if (pos_y < 0) {
        pos_y = 0;
        speed_y = SPEED_X_ABS;
    } else if (pos_y + texture.height > c.height) {
        speed_y = -SPEED_Y_ABS;
    }

    sponge_draw_texture(c, pos_x, pos_y, texture);
}