ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Image.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for OpenGL, SDL2 and GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 */
17
18#include <assert.h>
19#include <string.h>
20
21#include <SDL_image.h>
22
23#include "Image.h"
24#include "Log.h"
25
26#define _Class _Image
27
28#pragma mark - Object
29
33static void dealloc(Object *self) {
34
35 Image *this = (Image *) self;
36
37 SDL_FreeSurface(this->surface);
38
39 super(Object, self, dealloc);
40}
41
42#pragma mark - Image
43
48static Image *imageWithBytes(const uint8_t *bytes, size_t length) {
49 return $(alloc(Image), initWithBytes, bytes, length);
50}
51
56static Image *imageWithData(const Data *data) {
57 return $(alloc(Image), initWithData, data);
58}
59
64static Image *imageWithResource(const Resource *resource) {
65 return $(alloc(Image), initWithResource, resource);
66}
67
72static Image *imageWithResourceName(const char *name) {
73 return $(alloc(Image), initWithResourceName, name);
74}
75
80static Image *imageWithSurface(SDL_Surface *surface) {
81 return $(alloc(Image), initWithSurface, surface);
82}
83
88static Image *initWithBytes(Image *self, const uint8_t *bytes, size_t length) {
89
90 SDL_RWops *ops = SDL_RWFromConstMem(bytes, (int) length);
91 if (ops) {
92 SDL_Surface *surface = IMG_LoadTyped_RW(ops, 0, self->type);
93 if (surface) {
94 self = $(self, initWithSurface, surface);
95 SDL_FreeSurface(surface);
96 } else {
97 self = release(self);
98 }
99 } else {
100 self = release(self);
101 }
102
103 SDL_FreeRW(ops);
104 return self;
105}
106
111static Image *initWithData(Image *self, const Data *data) {
112
113 if (data) {
114 self = $(self, initWithBytes, data->bytes, data->length);
115 } else {
116 self = release(self);
117 }
118
119 return self;
120}
121
126static Image *initWithResource(Image *self, const Resource *resource) {
127
128 if (resource) {
129 self->type = strrchr(resource->name, '.') ? strrchr(resource->name, '.') + 1 : NULL;
130 self = $(self, initWithData, resource->data);
131 } else {
132 self = release(self);
133 }
134
135 return self;
136}
137
142static Image *initWithResourceName(Image *self, const char *name) {
143
144 Resource *resource = $$(Resource, resourceWithName, name);
145
146 self = $(self, initWithResource, resource);
147
148 release(resource);
149
150 return self;
151}
152
157static Image *initWithSurface(Image *self, SDL_Surface *surface) {
158
159 self = (Image *) super(Object, self, init);
160 if (self) {
161
162 if (surface) {
163 if (surface->format->format != SDL_PIXELFORMAT_ABGR8888) {
164 self->surface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0);
165 } else {
166 self->surface = surface;
167 self->surface->refcount++;
168 }
169
170 assert(self->surface);
171 }
172 }
173
174 return self;
175}
176
181static SDL_Size size(const Image *self) {
182 return MakeSize(self->surface->w, self->surface->h);
183}
184
185#pragma mark - Class lifecycle
186
190static void initialize(Class *clazz) {
191
192 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
193
194 ((ImageInterface *) clazz->interface)->imageWithBytes = imageWithBytes;
195 ((ImageInterface *) clazz->interface)->imageWithData = imageWithData;
196 ((ImageInterface *) clazz->interface)->imageWithResource = imageWithResource;
197 ((ImageInterface *) clazz->interface)->imageWithResourceName = imageWithResourceName;
198 ((ImageInterface *) clazz->interface)->imageWithSurface = imageWithSurface;
199 ((ImageInterface *) clazz->interface)->initWithBytes = initWithBytes;
200 ((ImageInterface *) clazz->interface)->initWithData = initWithData;
201 ((ImageInterface *) clazz->interface)->initWithResource = initWithResource;
202 ((ImageInterface *) clazz->interface)->initWithResourceName = initWithResourceName;
203 ((ImageInterface *) clazz->interface)->initWithSurface = initWithSurface;
204 ((ImageInterface *) clazz->interface)->size = size;
205}
206
211Class *_Image(void) {
212 static Class *clazz;
213 static Once once;
214
215 do_once(&once, {
216 clazz = _initialize(&(const ClassDef) {
217 .name = "Image",
218 .superclass = _Object(),
219 .instanceSize = sizeof(Image),
220 .interfaceOffset = offsetof(Image, interface),
221 .interfaceSize = sizeof(ImageInterface),
223 });
224 });
225
226 return clazz;
227}
228
229#undef _Class
static void dealloc(Object *self)
Definition: Image.c:33
static void initialize(Class *clazz)
Definition: Image.c:190
Image loading.
View logging facilities via SDL_Log.
#define MakeSize(w, h)
Creates an SDL_Size with the given dimensions.
Definition: Types.h:79
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
Font * initWithData(Font *self, Data *data, int size, int index)
Data * data
The raw font data.
Definition: Font.h:79
Image loading.
Definition: Image.h:38
SDL_Size size(const Image *self)
Definition: Image.c:181
const char * type
The image type, inferred if instantiated with a Resource.
Definition: Image.h:59
Image * initWithData(Image *self, const Data *data)
Initializes this Image with the specified Data.
Definition: Image.c:111
Image * initWithResourceName(Image *self, const char *name)
Initializes this Image, loading the Resource by the given name.
Definition: Image.c:142
Image * initWithBytes(Image *self, const uint8_t *bytes, size_t length)
Initializes this Image with the specified bytes.
Definition: Image.c:88
Image * imageWithBytes(const uint8_t *bytes, size_t length)
Instantiates an Image with the specified bytes.
Definition: Image.c:48
Class * _Image(void)
The Image archetype.
Definition: Image.c:211
Image * imageWithResourceName(const char *name)
Instantiates an Image with the specified Resource name.
Definition: Image.c:72
Image * imageWithData(const Data *data)
Instantiates an Image with the specified Data.
Definition: Image.c:56
Image * initWithSurface(Image *self, SDL_Surface *surface)
Initializes this Image with the given surface.
Definition: Image.c:157
SDL_Surface * surface
The backing surface.
Definition: Image.h:54
Image * imageWithResource(const Resource *resource)
Instantiates an Image with the specified Resource.
Definition: Image.c:64
Image * imageWithSurface(SDL_Surface *surface)
Instantiates an Image with the specified surface.
Definition: Image.c:80
Image * initWithResource(Image *self, const Resource *resource)
Initializes this Image with the specified Resource.
Definition: Image.c:126
The SDL_Size type.
Definition: Types.h:62