aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/sprites.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-13 17:41:24 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-13 17:41:24 -0500
commiteb30ab240acea4f1e338bb4fcc1ddadb5ccc5aee (patch)
tree4b8ed34c26598fe807c8af3206c17be4989a8c49 /src/sdl/sprites.c
parente4f70b11a2f91b47f474fa9e5e4e89978a57472a (diff)
downloadsok-eb30ab240acea4f1e338bb4fcc1ddadb5ccc5aee.tar.bz2
sok-eb30ab240acea4f1e338bb4fcc1ddadb5ccc5aee.zip
add assets, sprite-packer, sprite rendering (disabled, currently busted)
Diffstat (limited to 'src/sdl/sprites.c')
-rw-r--r--src/sdl/sprites.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/sdl/sprites.c b/src/sdl/sprites.c
new file mode 100644
index 0000000..cb127aa
--- /dev/null
+++ b/src/sdl/sprites.c
@@ -0,0 +1,64 @@
+#include <SDL.h>
+#define STB_IMAGE_IMPLEMENTATION
+#define STB_ASSERT(x)
+#define STB_ONLY_PNG
+#include "stb_image.h"
+#include "util.h"
+#include "sprites.h"
+
+typedef struct {
+ const Uint32 r,
+ g,
+ b,
+ a;
+} mask_t;
+
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+static const mask_t
+MASK = { 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff };
+#else
+static const mask_t
+MASK = { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 };
+#endif /* SDL_BYTEORDER */
+
+SDL_Texture *
+sprites_init(
+ SDL_Renderer * const renderer,
+ const char * const png_path
+) {
+ // load image
+ int x, y, n;
+ unsigned char *data = stbi_load(png_path, &x, &y, &n, 4);
+ if (!data) {
+ die("stbi_load()");
+ }
+
+ // create surface
+ SDL_Surface * const surface = SDL_CreateRGBSurfaceFrom(data, x, y, 32, 4 * x, MASK.r, MASK.g, MASK.b, MASK.a);
+ if (!surface) {
+ die("SDLCreateRGBSurfaceFrom(): %s", SDL_GetError());
+ }
+
+ // free image data
+ free(data);
+
+ // create texture
+ SDL_Texture *r = SDL_CreateTextureFromSurface(renderer, surface);
+ if (!r) {
+ die("SDLCreateTextureFromSurface(): %s", SDL_GetError());
+ }
+
+ // free surface
+ SDL_FreeSurface(surface);
+
+ // return texture
+ return r;
+}
+
+SDL_Rect
+sprites_get_rect(
+ const sprite_t sprite
+) {
+ const int x = ((sprite < SPRITE_LAST) ? sprite : SPRITE_NONE) * 32;
+ return (SDL_Rect) { x, 0, 32, 32 };
+}