/* Copyright (c) 2014 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #ifndef __FixedIntervalRunLoop_H__ #define __FixedIntervalRunLoop_H__ #include "stemobject/StemObject.h" typedef struct FixedIntervalRunLoop FixedIntervalRunLoop; #define FixedIntervalRunLoop_superclass StemObject typedef void (* FixedIntervalRunLoopCallback)(void * context); #define FixedIntervalRunLoop_ivars \ StemObject_ivars \ \ double stepInterval; \ double tolerance; \ FixedIntervalRunLoopCallback stepCallback; \ void * stepContext; \ double slop; \ bool * private_ivar(disposed); #define FixedIntervalRunLoop_vtable(self_type) \ StemObject_vtable(self_type) \ stemobject_declare(FixedIntervalRunLoop) // stepInterval specifies the nominal amount of time expected to pass between each call to stepCallback. // tolerance can be set to a nonzero value to specify an amount of time deviation allowed in either direction // in an attempt to invoke stepCallback exactly once per call to FixedIntervalRunLoop_advance(). // Behavior may be unpredictable for tolerance values greater than half of stepInterval or less than zero. FixedIntervalRunLoop * FixedIntervalRunLoop_create(double stepInterval, double tolerance, FixedIntervalRunLoopCallback stepCallback, void * stepContext); bool FixedIntervalRunLoop_init(FixedIntervalRunLoop * self, double stepInterval, double tolerance, FixedIntervalRunLoopCallback stepCallback, void * stepContext); void FixedIntervalRunLoop_dispose(FixedIntervalRunLoop * self); // Invokes stepCallback zero or more times based on the specified time interval since the last call. // Returns the number of times stepCallback was invoked. // Note: It is safe to dispose a run loop from a callback invoked by FixedIntervalRunLoop_advance(). unsigned int FixedIntervalRunLoop_advance(FixedIntervalRunLoop * self, double timeDelta); #endif