comparison src/libmlk-core/core/image.c @ 320:8f9937403749

misc: improve loading of data
author David Demelier <markand@malikania.fr>
date Fri, 01 Oct 2021 20:30:00 +0200
parents libmlk-core/core/image.c@d01e83210ca2
children 64aff0afc8ac
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * image.c -- basic image management
3 *
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20
21 #include <SDL_image.h>
22
23 #include "error.h"
24 #include "texture.h"
25 #include "window.h"
26 #include "window_p.h"
27
28 static void
29 dimensions(struct texture *tex)
30 {
31 int w, h;
32
33 if (SDL_QueryTexture(tex->handle, NULL, NULL, &w, &h) < 0)
34 tex->w = tex->h = 0;
35 else {
36 tex->w = w;
37 tex->h = h;
38 }
39 }
40
41 int
42 image_open(struct texture *tex, const char *path)
43 {
44 assert(tex);
45 assert(path);
46
47 if (!(tex->handle = IMG_LoadTexture(RENDERER(), path)))
48 return errorf("%s", SDL_GetError());
49
50 dimensions(tex);
51
52 return 0;
53 }
54
55 int
56 image_openmem(struct texture *tex, const void *buffer, size_t size)
57 {
58 assert(buffer);
59
60 SDL_RWops *ops = SDL_RWFromConstMem(buffer, size);
61
62 if (!ops || !(tex->handle = IMG_LoadTexture_RW(RENDERER(), ops, 1)))
63 return errorf("%s", SDL_GetError());
64
65 dimensions(tex);
66
67 return 0;
68 }