diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/kot.png | bin | 0 -> 7572 bytes | |||
| -rw-r--r-- | examples/rainbow.c | 5 | ||||
| -rw-r--r-- | examples/rects.c | 2 | ||||
| -rw-r--r-- | examples/texture.c | 65 |
4 files changed, 69 insertions, 3 deletions
diff --git a/examples/kot.png b/examples/kot.png Binary files differnew file mode 100644 index 0000000..784c809 --- /dev/null +++ b/examples/kot.png diff --git a/examples/rainbow.c b/examples/rainbow.c index 33cbb2d..31657ff 100644 --- a/examples/rainbow.c +++ b/examples/rainbow.c @@ -1,14 +1,13 @@ -#include <stdio.h> #include <math.h> - - #include "../sponge.h" #define PI ((float)3.14159265358979323846) #define SPEED (2.0f / 360.0f * 2 * PI) static float angle = 0.0f; +void init() {} + void draw_frame(sponge_Texture c) { angle += SPEED; diff --git a/examples/rects.c b/examples/rects.c index e78ac69..293f833 100644 --- a/examples/rects.c +++ b/examples/rects.c @@ -2,6 +2,8 @@ #include "../sponge.h" +void init() {} + void draw_frame(sponge_Texture c) { assert(c.width > 140 + 20); assert(c.height > 70 + 40); diff --git a/examples/texture.c b/examples/texture.c new file mode 100644 index 0000000..06428de --- /dev/null +++ b/examples/texture.c @@ -0,0 +1,65 @@ +#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) { + pos_x = c.width - texture.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) { + pos_y = c.height - texture.height; + speed_y = -SPEED_Y_ABS; + } + + sponge_draw_texture(c, pos_x, pos_y, texture); +} |
