ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Public Member Functions | Data Fields | Protected Attributes
Image Struct Reference

Image loading. More...

#include <Image.h>

Inheritance diagram for Image:

Public Member Functions

Class * _Image (void)
 The Image archetype. More...
 
ImageimageWithBytes (const uint8_t *bytes, size_t length)
 Instantiates an Image with the specified bytes. More...
 
ImageimageWithData (const Data *data)
 Instantiates an Image with the specified Data. More...
 
ImageimageWithResource (const Resource *resource)
 Instantiates an Image with the specified Resource. More...
 
ImageimageWithResourceName (const char *name)
 Instantiates an Image with the specified Resource name. More...
 
ImageimageWithSurface (SDL_Surface *surface)
 Instantiates an Image with the specified surface. More...
 
ImageinitWithBytes (Image *self, const uint8_t *bytes, size_t length)
 Initializes this Image with the specified bytes. More...
 
ImageinitWithData (Image *self, const Data *data)
 Initializes this Image with the specified Data. More...
 
ImageinitWithResource (Image *self, const Resource *resource)
 Initializes this Image with the specified Resource. More...
 
ImageinitWithResourceName (Image *self, const char *name)
 Initializes this Image, loading the Resource by the given name. More...
 
ImageinitWithSurface (Image *self, SDL_Surface *surface)
 Initializes this Image with the given surface. More...
 
SDL_Size size (const Image *self)
 

Data Fields

Object object
 The superclass. More...
 
SDL_Surface * surface
 The backing surface. More...
 
const char * type
 The image type, inferred if instantiated with a Resource. More...
 

Protected Attributes

ImageInterface * interface
 The interface. More...
 

Detailed Description

Image loading.

Definition at line 38 of file Image.h.

Member Function Documentation

◆ _Image()

Class * _Image ( void  )

The Image archetype.

Returns
The Image Class.

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
ImageInterface * interface
The interface.
Definition: Image.h:49

◆ imageWithBytes()

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

Instantiates an Image with the specified bytes.

Parameters
bytesThe encoded image bytes.
lengthThe length of bytes.
Returns
The new Image, or NULL on error.

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()

Image * imageWithData ( const Data *  data)

Instantiates an Image with the specified Data.

Parameters
dataThe encoded image Data.
Returns
The new Image, or NULL on error.

Definition at line 56 of file Image.c.

56 {
57 return $(alloc(Image), initWithData, data);
58}
Image * initWithData(Image *self, const Data *data)
Initializes this Image with the specified Data.
Definition: Image.c:111

◆ imageWithResource()

Image * imageWithResource ( const Resource *  resource)

Instantiates an Image with the specified Resource.

Parameters
resourceThe Resource containing encoded image data.
Returns
The new Image, or NULL on error.

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()

Image * imageWithResourceName ( const char *  name)

Instantiates an Image with the specified Resource name.

Parameters
nameThe name of a Resource containing encoded image data.
Returns
The new Image, or NULL on error.

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()

Image * imageWithSurface ( SDL_Surface *  surface)

Instantiates an Image with the specified surface.

Parameters
surfaceThe surface.
Returns
The new Image, or NULL on error.

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
SDL_Surface * surface
The backing surface.
Definition: Image.h:54

◆ initWithBytes()

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

Initializes this Image with the specified bytes.

Parameters
selfThe Image.
bytesThe encoded image bytes.
lengthThe length of bytes.
Returns
The initialized Image, or NULL on error.

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()

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

Initializes this Image with the specified Data.

Parameters
selfThe Image.
dataThe encoded image Data.
Returns
The initialized Image, or NULL on error.

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()

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

Initializes this Image with the specified Resource.

Parameters
selfThe Image.
resourceThe Resource containing encoded image data.
Returns
The initialized Image, or NULL on error.

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}

◆ initWithResourceName()

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

Initializes this Image, loading the Resource by the given name.

Parameters
selfThe Image.
nameThe Resource name.
Returns
The initialized Image, or NULL on error.

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()

Image * initWithSurface ( Image self,
SDL_Surface *  surface 
)

Initializes this Image with the given surface.

Parameters
selfThe Image.
surfaceThe backing surface.
Returns
The initialized Image, or NULL on error.
Remarks
The surface's reference count is incremented, so that you can (and should) call SDL_FreeSurface when you no longer need it. The Image will also free the surface on dealloc.
Designated initializer.

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}
static View * init(View *self)
Definition: Box.c:67

◆ size()

SDL_Size size ( const Image self)
Parameters
selfThe Image.
Returns
The Image size.

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

Field Documentation

◆ interface

ImageInterface* Image::interface
protected

The interface.

Definition at line 49 of file Image.h.

◆ object

Object Image::object

The superclass.

Definition at line 43 of file Image.h.

◆ surface

SDL_Surface* Image::surface

The backing surface.

Definition at line 54 of file Image.h.

◆ type

const char* Image::type

The image type, inferred if instantiated with a Resource.

Definition at line 59 of file Image.h.


The documentation for this struct was generated from the following files: