diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-01-13 20:24:06 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-01-13 20:24:06 -0500 |
commit | c90474ee7143a215ef3b3548024e025ce16ad6d3 (patch) | |
tree | c935b33186759432050ca8dc116117a989f7ff5b /src/sdl/sprites.c | |
parent | eb30ab240acea4f1e338bb4fcc1ddadb5ccc5aee (diff) | |
download | sok-c90474ee7143a215ef3b3548024e025ce16ad6d3.tar.bz2 sok-c90474ee7143a215ef3b3548024e025ce16ad6d3.zip |
working, with sprites
Diffstat (limited to 'src/sdl/sprites.c')
-rw-r--r-- | src/sdl/sprites.c | 69 |
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 }; } |