ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Macros | Functions
Image.c File Reference
#include <assert.h>
#include <string.h>
#include <SDL_image.h>
#include "Image.h"
#include "Log.h"

Go to the source code of this file.

Macros

#define _Class   _Image
 

Functions

Class * _Image (void)
 
static void dealloc (Object *self)
 
static ImageimageWithBytes (const uint8_t *bytes, size_t length)
 
static ImageimageWithData (const Data *data)
 
static ImageimageWithResource (const Resource *resource)
 
static ImageimageWithResourceName (const char *name)
 
static ImageimageWithSurface (SDL_Surface *surface)
 
static void initialize (Class *clazz)
 
static ImageinitWithBytes (Image *self, const uint8_t *bytes, size_t length)
 
static ImageinitWithData (Image *self, const Data *data)
 
static ImageinitWithResource (Image *self, const Resource *resource)
 
static ImageinitWithResourceName (Image *self, const char *name)
 
static ImageinitWithSurface (Image *self, SDL_Surface *surface)
 
static SDL_Size size (const Image *self)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Image

Definition at line 26 of file Image.c.

Function Documentation

◆ _Image()

Class * _Image ( void  )

Definition at line 211 of file Image.c.

211 {
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}
static void initialize(Class *clazz)
Definition: Image.c:190
Image loading.
Definition: Image.h:38

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 33 of file Image.c.

33 {
34
35 Image *this = (Image *) self;
36
37 SDL_FreeSurface(this->surface);
38
39 super(Object, self, dealloc);
40}
static void dealloc(Object *self)
Definition: Image.c:33

◆ imageWithBytes()

static Image * imageWithBytes ( const uint8_t *  bytes,
size_t  length 
)
static

Definition at line 48 of file Image.c.

48 {
49 return $(alloc(Image), initWithBytes, bytes, length);
50}
Image * initWithBytes(Image *self, const uint8_t *bytes, size_t length)
Initializes this Image with the specified bytes.
Definition: Image.c:88

◆ imageWithData()

static Image * imageWithData ( const Data *  data)
static

Definition at line 56 of file Image.c.

56 {
57 return $(alloc(Image), initWithData, data);
58}
Font * initWithData(Font *self, Data *data, int size, int index)

◆ imageWithResource()

static Image * imageWithResource ( const Resource *  resource)
static

Definition at line 64 of file Image.c.

64 {
65 return $(alloc(Image), initWithResource, resource);
66}
Image * initWithResource(Image *self, const Resource *resource)
Initializes this Image with the specified Resource.
Definition: Image.c:126

◆ imageWithResourceName()

static Image * imageWithResourceName ( const char *  name)
static

Definition at line 72 of file Image.c.

72 {
73 return $(alloc(Image), initWithResourceName, name);
74}
Image * initWithResourceName(Image *self, const char *name)
Initializes this Image, loading the Resource by the given name.
Definition: Image.c:142

◆ imageWithSurface()

static Image * imageWithSurface ( SDL_Surface *  surface)
static

Definition at line 80 of file Image.c.

80 {
81 return $(alloc(Image), initWithSurface, surface);
82}
Image * initWithSurface(Image *self, SDL_Surface *surface)
Initializes this Image with the given surface.
Definition: Image.c:157

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 190 of file Image.c.

190 {
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}
SDL_Size size(const Image *self)
Definition: Image.c:181
Image * initWithData(Image *self, const Data *data)
Initializes this Image with the specified Data.
Definition: Image.c:111
Image * imageWithBytes(const uint8_t *bytes, size_t length)
Instantiates an Image with the specified bytes.
Definition: Image.c:48
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 * 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

◆ initWithBytes()

static Image * initWithBytes ( Image self,
const uint8_t *  bytes,
size_t  length 
)
static

Definition at line 88 of file Image.c.

88 {
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}
const char * type
The image type, inferred if instantiated with a Resource.
Definition: Image.h:59

◆ initWithData()

static Image * initWithData ( Image self,
const Data *  data 
)
static

Definition at line 111 of file Image.c.

111 {
112
113 if (data) {
114 self = $(self, initWithBytes, data->bytes, data->length);
115 } else {
116 self = release(self);
117 }
118
119 return self;
120}

◆ initWithResource()

static Image * initWithResource ( Image self,
const Resource *  resource 
)
static

Definition at line 126 of file Image.c.

126 {
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}
Data * data
The raw font data.
Definition: Font.h:79

◆ initWithResourceName()

static Image * initWithResourceName ( Image self,
const char *  name 
)
static

Definition at line 142 of file Image.c.

142 {
143
144 Resource *resource = $$(Resource, resourceWithName, name);
145
146 self = $(self, initWithResource, resource);
147
148 release(resource);
149
150 return self;
151}

◆ initWithSurface()

static Image * initWithSurface ( Image self,
SDL_Surface *  surface 
)
static

Definition at line 157 of file Image.c.

157 {
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}
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
SDL_Surface * surface
The backing surface.
Definition: Image.h:54

◆ size()

static SDL_Size size ( const Image self)
static

Definition at line 181 of file Image.c.

181 {
182 return MakeSize(self->surface->w, self->surface->h);
183}
#define MakeSize(w, h)
Creates an SDL_Size with the given dimensions.
Definition: Types.h:79