ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
SlideShowView.c File Reference
#include <assert.h>
#include <SDL3/SDL_timer.h>
#include "SlideShowView.h"

Go to the source code of this file.

Macros

#define _Class   _SlideShowView
 

Functions

Class * _SlideShowView (void)
 
static void addImage (SlideShowView *self, Image *image)
 
static void dealloc (Object *self)
 
static Viewinit (View *self)
 
static void initialize (Class *clazz)
 
static SlideShowViewinitWithFrame (SlideShowView *self, const SDL_Rect *frame)
 
static void render (View *self, Renderer *renderer)
 

Macro Definition Documentation

◆ _Class

#define _Class   _SlideShowView

Definition at line 30 of file SlideShowView.c.

Function Documentation

◆ _SlideShowView()

Class * _SlideShowView ( void  )

Definition at line 162 of file SlideShowView.c.

162 {
163 static Class *clazz;
164 static Once once;
165
166 do_once(&once, {
167 clazz = _initialize(&(const ClassDef) {
168 .name = "SlideShowView",
169 .superclass = _View(),
170 .instanceSize = sizeof(SlideShowView),
171 .interfaceOffset = offsetof(SlideShowView, interface),
172 .interfaceSize = sizeof(SlideShowViewInterface),
174 });
175 });
176
177 return clazz;
178}
static void initialize(Class *clazz)
Class * _View(void)
Definition View.c:2067
SlideShowView cycles through a collection of Images with crossfade transitions.

◆ addImage()

static void addImage ( SlideShowView self,
Image image 
)
static

Definition at line 106 of file SlideShowView.c.

106 {
107
108 assert(image);
109
110 $(self->images, addObject, image);
111}
Array * images
The images to display.

◆ dealloc()

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

Definition at line 37 of file SlideShowView.c.

37 {
38
39 SlideShowView *this = (SlideShowView *) self;
40
41 release(this->images);
42
43 super(Object, self, dealloc);
44}
static void dealloc(Object *self)

◆ init()

static View * init ( View self)
static
See also
View::init(View *)

Definition at line 51 of file SlideShowView.c.

51 {
52 return (View *) $((SlideShowView *) self, initWithFrame, NULL);
53}
static SlideShowView * initWithFrame(SlideShowView *self, const SDL_Rect *frame)
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134

◆ initialize()

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

Definition at line 147 of file SlideShowView.c.

147 {
148
149 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
150
151 ((ViewInterface *) clazz->interface)->render = render;
152 ((ViewInterface *) clazz->interface)->init = init;
153
154 ((SlideShowViewInterface *) clazz->interface)->addImage = addImage;
155 ((SlideShowViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
156}
static void addImage(SlideShowView *self, Image *image)
static View * init(View *self)
static void render(View *self, Renderer *renderer)
View * initWithFrame(View *self, const SDL_Rect *frame)
Initializes this View with the specified frame.
Definition View.c:989

◆ initWithFrame()

static SlideShowView * initWithFrame ( SlideShowView self,
const SDL_Rect *  frame 
)
static

Definition at line 117 of file SlideShowView.c.

117 {
118
119 self = (SlideShowView *) super(View, self, initWithFrame, frame);
120 if (self) {
121 self->images = $(alloc(Array), init);
122 assert(self->images);
123
124 self->slideDuration = 5000;
125 self->fadeDuration = 1000;
126
127 self->current = $(alloc(ImageView), initWithFrame, NULL);
128 assert(self->current);
129 $((View *) self, addSubview, (View *) self->current);
130 release(self->current);
131
132 self->next = $(alloc(ImageView), initWithFrame, NULL);
133 assert(self->next);
134 self->next->color.a = 0;
135 $((View *) self, addSubview, (View *) self->next);
136 release(self->next);
137 }
138
139 return self;
140}
static void addSubview(View *self, View *subview)
Definition PageView.c:49
ImageViews render an Image in the context of a View hierarchy.
Definition ImageView.h:45
ImageView * current
The bottom ImageView, showing the current slide.
Uint32 slideDuration
The duration of each slide in milliseconds, excluding the fade transition.
Uint32 fadeDuration
The duration of the fade transition in milliseconds.

◆ render()

static void render ( View self,
Renderer renderer 
)
static
See also
View::render(View *, Renderer *)

Definition at line 58 of file SlideShowView.c.

58 {
59
60 super(View, self, render, renderer);
61
62 SlideShowView *this = (SlideShowView *) self;
63
64 const Array *images = (Array *) this->images;
65 if (images->count == 0) {
66 return;
67 }
68
69 const Uint64 now = SDL_GetTicks();
70
71 if (this->current->image == NULL) {
72 Image *first = $(images, firstObject);
73 $(this->current, setImage, first);
74 this->fadeEndedAt = now;
75 return;
76 }
77
78 if (images->count < 2) {
79 return;
80 }
81
82 if (this->fadeStartedAt) {
83 const float frac = (float) (now - this->fadeStartedAt) / (float) this->fadeDuration;
84 this->next->color.a = (Uint8) (SDL_min(frac, 1.f) * 255.f);
85 if (frac >= 1.f) {
86 $(this->current, setImage, this->next->image);
87 $(this->next, setImage, NULL);
88 this->next->color.a = 0;
89 this->fadeStartedAt = 0;
90 this->fadeEndedAt = now;
91 }
92 } else if (now - this->fadeEndedAt >= this->slideDuration) {
93 this->index = (this->index + 1) % images->count;
94 Image *image = $(images, objectAtIndex, this->index);
95 $(this->next, setImage, image);
96 this->fadeStartedAt = now;
97 }
98}
static void setImage(ImageView *self, Image *image)
Definition ImageView.c:165
Image loading.
Definition Image.h:38