ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
AtlasImage.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for SDL3 and 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
19#include <assert.h>
20
21#include "AtlasImage.h"
22#include "ImageAtlas.h"
23
24#define _Class _AtlasImage
25
26#pragma mark - AtlasImage
27
33
34 assert(atlas);
35 assert(image);
36 assert(image->surface);
37
38 self = (AtlasImage *) super(Image, self, initWithSurface, image->surface);
39 if (self) {
40 self->image.scale = image->scale;
41 self->atlas = atlas;
42 self->rect = (SDL_Rect) { 0, 0, 0, 0 };
43 }
44
45 return self;
46}
47
52static SDL_FRect texcoords(const AtlasImage *self) {
53
54 const Image *sheet = self->atlas ? self->atlas->image : NULL;
55 if (sheet == NULL) {
56 return (SDL_FRect) { 0.f, 0.f, 0.f, 0.f };
57 }
58
59 const float w = (float) sheet->surface->w, h = (float) sheet->surface->h;
60
61 return (SDL_FRect) {
62 self->rect.x / w,
63 self->rect.y / h,
64 self->rect.w / w,
65 self->rect.h / h
66 };
67}
68
69#pragma mark - Class lifecycle
70
74static void initialize(Class *clazz) {
75
76 ((AtlasImageInterface *) clazz->interface)->initWithImage = initWithImage;
77 ((AtlasImageInterface *) clazz->interface)->texcoords = texcoords;
78}
79
84Class *_AtlasImage(void) {
85 static Class *clazz;
86 static Once once;
87
88 do_once(&once, {
89 clazz = _initialize(&(const ClassDef) {
90 .name = "AtlasImage",
91 .superclass = _Image(),
92 .instanceSize = sizeof(AtlasImage),
93 .interfaceSize = sizeof(AtlasImageInterface),
94 .initialize = initialize,
95 });
96 });
97
98 return clazz;
99}
100
101#undef _Class
static AtlasImage * initWithImage(AtlasImage *self, ImageAtlas *atlas, Image *image)
Definition AtlasImage.c:32
Class * _AtlasImage(void)
Definition AtlasImage.c:84
static SDL_FRect texcoords(const AtlasImage *self)
Definition AtlasImage.c:52
static void initialize(Class *clazz)
Definition AtlasImage.c:74
An Image packed into an ImageAtlas.
static Image * initWithSurface(Image *self, SDL_Surface *surface)
Definition Image.c:391
Class * _Image(void)
Definition Image.c:452
Packs many Images into one sheet, backed by one Texture.
static Image * image(Theme *self, const char *name, float pixelDensity)
Definition Theme.c:82
An Image packed into an ImageAtlas.
Definition AtlasImage.h:41
SDL_FRect texcoords(const AtlasImage *self)
Resolves this AtlasImage's normalized texture coordinates within the atlas Texture.
Definition AtlasImage.c:52
SDL_Rect rect
This AtlasImage's rectangle within the atlas sheet, in texels.
Definition AtlasImage.h:66
Image image
The superclass.
Definition AtlasImage.h:46
ImageAtlas * atlas
The ImageAtlas this AtlasImage belongs to, or NULL once that atlas has been released,...
Definition AtlasImage.h:59
Packs many Images into one sheet, backed by one Texture.
Definition ImageAtlas.h:49
Image * image
The compiled sheet, or NULL until ImageAtlas::compile.
Definition ImageAtlas.h:71
Image loading.
Definition Image.h:41
float scale
Pixels of surface per point: 1 for raster images, the requested scale for SVG.
Definition Image.h:57
SDL_Surface * surface
The backing surface.
Definition Image.h:62