summaryrefslogtreecommitdiff
path: root/examples/uv.c
blob: 937c4d5d45a1295bece17f3d1568a042882ae946 (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
#include <assert.h>

#include "../sponge.h"
#define SPONGE_EXAMPLE_IMPLEMENTATION
#define SPONGE_EXAMPLE_INIT_DEFINED
#define SPONGE_EXAMPLE_MOUSE_MOVE_DEFINED
#include "../example.h"
#include "../stb_image.h"

sponge_Texture texture;

int32_t mouse_x;
int32_t mouse_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_pixels = texture.width;
    texture.pixels = (sponge_Color32 *)data; // NOTE(kard): be careful if changing following loop

    // converting from stbi's RGBA
    for (size_t i = 0; i < texture.width * texture.height; i++) {
        sponge_Color32 result;
        result.r = data[i * 4 + 0];
        result.g = data[i * 4 + 1];
        result.b = data[i * 4 + 2];
        result.a = data[i * 4 + 3];
        texture.pixels[i] = result;
    }

    mouse_x = 100;
    mouse_y = 100;
}

void mouse_move(int32_t x, int32_t y) {
    mouse_x = x;
    mouse_y = y;
}

void draw_frame(sponge_Texture c) {
    sponge_clear(c, sponge_color32_make(0xFF000000));
    sponge_Vec2I v0 = sponge_vec2i_make(mouse_x, mouse_y);
    sponge_Vec2I v1 = sponge_vec2i_make(c.width - 100, 100);
    sponge_Vec2I v2 = sponge_vec2i_make(100, c.height - 100);
    sponge_Vec2I v3 = sponge_vec2i_make(c.width - 100, c.height - 100);
    sponge_Vec2 uv0 = sponge_vec2_make(0.0f, 0.0f);
    sponge_Vec2 uv1 = sponge_vec2_make(1.0f, 0.0f);
    sponge_Vec2 uv2 = sponge_vec2_make(0.0f, 1.0f);
    sponge_Vec2 uv3 = sponge_vec2_make(1.0f, 1.0f);

    sponge_draw_triangle_uv(c, v0, v1, v3, uv0, uv1, uv3, texture);
    sponge_draw_triangle_uv(c, v0, v3, v2, uv0, uv3, uv2, texture);
}