aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/sprites.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sdl/sprites.c')
-rw-r--r--src/sdl/sprites.c69
1 files changed, 35 insertions, 34 deletions
diff --git a/src/sdl/sprites.c b/src/sdl/sprites.c
index cb127aa..9b9b42f 100644
--- a/src/sdl/sprites.c
+++ b/src/sdl/sprites.c
@@ -6,53 +6,54 @@
#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 *
+void
sprites_init(
SDL_Renderer * const renderer,
- const char * const png_path
+ const char * const png_path,
+ SDL_Texture **sprites
) {
// load image
- int x, y, n;
- unsigned char *data = stbi_load(png_path, &x, &y, &n, 4);
- if (!data) {
- die("stbi_load()");
+ int im_w, im_h;
+ unsigned char *im_data = stbi_load(png_path, &im_w, &im_h, NULL, 4);
+ if (!im_data) {
+ die("stbi_load(): %s", stbi_failure_reason());
}
// create surface
- SDL_Surface * const surface = SDL_CreateRGBSurfaceFrom(data, x, y, 32, 4 * x, MASK.r, MASK.g, MASK.b, MASK.a);
- if (!surface) {
+ SDL_Surface * const src = SDL_CreateRGBSurfaceWithFormatFrom(im_data, im_w, im_h, 32, 4 * im_w, SDL_PIXELFORMAT_RGBA32);
+ if (!src) {
die("SDLCreateRGBSurfaceFrom(): %s", SDL_GetError());
}
- // free image data
- free(data);
+ // create sprite textures
+ for (size_t i = 0; i < SPRITE_LAST; i++) {
+ // create surface
+ SDL_Surface *s = SDL_CreateRGBSurfaceWithFormat(0, 32, 32, 32, SDL_PIXELFORMAT_RGBA32);
+ if (!s) {
+ die("SDL_CreateRGBSurfaceWithFormat(): %s", SDL_GetError());
+ }
- // create texture
- SDL_Texture *r = SDL_CreateTextureFromSurface(renderer, surface);
- if (!r) {
- die("SDLCreateTextureFromSurface(): %s", SDL_GetError());
+ // copy from main surface
+ const SDL_Rect src_rect = { i * 32, 0, 32, 32 };
+ if (SDL_BlitScaled(src, &src_rect, s, NULL)) {
+ die("SDL_BlitScaled(): %s", SDL_GetError());
+ }
+
+ // create texture
+ sprites[i] = SDL_CreateTextureFromSurface(renderer, s);
+ if (!sprites[i]) {
+ die("SDLCreateTextureFromSurface(): %s", SDL_GetError());
+ }
+
+ // free surface
+ SDL_FreeSurface(s);
}
- // free surface
- SDL_FreeSurface(surface);
+ // free main surface
+ SDL_FreeSurface(src);
- // return texture
- return r;
+ // free image data
+ stbi_image_free(im_data);
}
SDL_Rect
@@ -60,5 +61,5 @@ sprites_get_rect(
const sprite_t sprite
) {
const int x = ((sprite < SPRITE_LAST) ? sprite : SPRITE_NONE) * 32;
- return (SDL_Rect) { x, 0, 32, 32 };
+ return (SDL_Rect) { .x = x, .y = 0, .w = 32, .h = 32 };
}