Added Uq-Ruan Masters game - it compiles but does not work (renders too slowly? Another problem?)
This commit is contained in:
2
project/jni/application/sc2/src/libs/callback/Makeinfo
Normal file
2
project/jni/application/sc2/src/libs/callback/Makeinfo
Normal file
@@ -0,0 +1,2 @@
|
||||
uqm_CFILES="alarm.c callback.c"
|
||||
|
||||
134
project/jni/application/sc2/src/libs/callback/alarm.c
Normal file
134
project/jni/application/sc2/src/libs/callback/alarm.c
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright 2006 Serge van den Boom <svdb@stack.nl>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "alarm.h"
|
||||
|
||||
#include "libs/heap.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
Heap *alarmHeap;
|
||||
|
||||
|
||||
static inline Alarm *
|
||||
Alarm_alloc(void) {
|
||||
return malloc(sizeof (Alarm));
|
||||
}
|
||||
|
||||
static inline void
|
||||
Alarm_free(Alarm *alarm) {
|
||||
free(alarm);
|
||||
}
|
||||
|
||||
static inline int
|
||||
AlarmTime_compare(const AlarmTime t1, const AlarmTime t2) {
|
||||
if (t1 < t2)
|
||||
return -1;
|
||||
if (t2 > t2)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
Alarm_compare(const Alarm *a1, const Alarm *a2) {
|
||||
return AlarmTime_compare(a1->time, a2->time);
|
||||
}
|
||||
|
||||
void
|
||||
Alarm_init(void) {
|
||||
assert(alarmHeap == NULL);
|
||||
alarmHeap = Heap_new((HeapValue_Comparator) Alarm_compare,
|
||||
4, 4, 0.8);
|
||||
}
|
||||
|
||||
void
|
||||
Alarm_uninit(void) {
|
||||
assert(alarmHeap != NULL);
|
||||
|
||||
while (Heap_hasMore(alarmHeap)) {
|
||||
Alarm *alarm = (Alarm *) Heap_pop(alarmHeap);
|
||||
Alarm_free(alarm);
|
||||
}
|
||||
Heap_delete(alarmHeap);
|
||||
alarmHeap = NULL;
|
||||
}
|
||||
|
||||
static inline AlarmTime
|
||||
AlarmTime_nowMS(void) {
|
||||
return SDL_GetTicks();
|
||||
}
|
||||
|
||||
Alarm *
|
||||
Alarm_addRelativeMs(Uint32 ms, AlarmCallback callback,
|
||||
AlarmCallbackArg arg) {
|
||||
Alarm *alarm;
|
||||
|
||||
assert(alarmHeap != NULL);
|
||||
|
||||
alarm = Alarm_alloc();
|
||||
alarm->time = AlarmTime_nowMS() + ms;
|
||||
alarm->callback = callback;
|
||||
alarm->arg = arg;
|
||||
|
||||
Heap_add(alarmHeap, (HeapValue *) alarm);
|
||||
|
||||
return alarm;
|
||||
}
|
||||
|
||||
void
|
||||
Alarm_remove(Alarm *alarm) {
|
||||
assert(alarmHeap != NULL);
|
||||
Heap_remove(alarmHeap, (HeapValue *) alarm);
|
||||
Alarm_free(alarm);
|
||||
}
|
||||
|
||||
// It is safe to call this function again from inside a callback function
|
||||
// that it called. It should not be called from multiple threads at once.
|
||||
void
|
||||
Alarm_process(void) {
|
||||
AlarmTime now;
|
||||
|
||||
assert(alarmHeap != NULL);
|
||||
|
||||
now = AlarmTime_nowMS();
|
||||
while (Heap_hasMore(alarmHeap)) {
|
||||
Alarm *alarm = (Alarm *) Heap_first(alarmHeap);
|
||||
|
||||
if (now < alarm->time)
|
||||
break;
|
||||
|
||||
Heap_pop(alarmHeap);
|
||||
alarm->callback(alarm->arg);
|
||||
Alarm_free(alarm);
|
||||
}
|
||||
}
|
||||
|
||||
Uint32
|
||||
Alarm_timeBeforeNextMs(void) {
|
||||
Alarm *alarm;
|
||||
|
||||
if (!Heap_hasMore(alarmHeap))
|
||||
return UINT32_MAX;
|
||||
|
||||
alarm = (Alarm *) Heap_first(alarmHeap);
|
||||
return alarmTimeToMsUint32(alarm->time);
|
||||
}
|
||||
|
||||
|
||||
54
project/jni/application/sc2/src/libs/callback/alarm.h
Normal file
54
project/jni/application/sc2/src/libs/callback/alarm.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2006 Serge van den Boom <svdb@stack.nl>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef _ALARM_H
|
||||
#define _ALARM_H
|
||||
|
||||
#include "port.h"
|
||||
#include "types.h"
|
||||
|
||||
#include SDL_INCLUDE(SDL.h)
|
||||
typedef Uint32 AlarmTime;
|
||||
static inline Uint32
|
||||
alarmTimeToMsUint32(AlarmTime time) {
|
||||
return (Uint32) time;
|
||||
}
|
||||
|
||||
typedef struct Alarm Alarm;
|
||||
typedef void *AlarmCallbackArg;
|
||||
typedef void (*AlarmCallback)(AlarmCallbackArg arg);
|
||||
|
||||
struct Alarm {
|
||||
size_t index;
|
||||
// For the HeapValue 'base struct'.
|
||||
|
||||
AlarmTime time;
|
||||
AlarmCallback callback;
|
||||
AlarmCallbackArg arg;
|
||||
};
|
||||
|
||||
void Alarm_init(void);
|
||||
void Alarm_uninit(void);
|
||||
Alarm *Alarm_addRelativeMs(Uint32 ms, AlarmCallback callback,
|
||||
AlarmCallbackArg arg);
|
||||
void Alarm_remove(Alarm *alarm);
|
||||
void Alarm_process(void);
|
||||
Uint32 Alarm_timeBeforeNextMs(void);
|
||||
|
||||
#endif /* _ALARM_H */
|
||||
|
||||
173
project/jni/application/sc2/src/libs/callback/callback.c
Normal file
173
project/jni/application/sc2/src/libs/callback/callback.c
Normal file
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* Copyright 2006 Serge van den Boom <svdb@stack.nl>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "port.h"
|
||||
#include "types.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct CallbackLink CallbackLink;
|
||||
|
||||
#define CALLBACK_INTERNAL
|
||||
#include "callback.h"
|
||||
|
||||
struct CallbackLink {
|
||||
CallbackLink *next;
|
||||
CallbackFunction callback;
|
||||
CallbackArg arg;
|
||||
};
|
||||
|
||||
static CallbackLink *callbacks;
|
||||
static CallbackLink **callbacksEnd;
|
||||
static CallbackLink *const *callbacksProcessEnd;
|
||||
|
||||
static inline void
|
||||
CallbackList_lock(void) {
|
||||
// TODO
|
||||
// Necessary for reentrant operation
|
||||
}
|
||||
|
||||
static inline void
|
||||
CallbackList_unlock(void) {
|
||||
// TODO
|
||||
// Necessary for reentrant operation
|
||||
}
|
||||
|
||||
#if 0
|
||||
static inline bool
|
||||
CallbackList_isLocked(void) {
|
||||
// TODO
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
Callback_init(void) {
|
||||
callbacks = NULL;
|
||||
callbacksEnd = &callbacks;
|
||||
callbacksProcessEnd = &callbacks;
|
||||
}
|
||||
|
||||
// Callbacks are guaranteed to be called in the order that they are queued.
|
||||
CallbackID
|
||||
Callback_add(CallbackFunction callback, CallbackArg arg) {
|
||||
CallbackLink *link = malloc(sizeof (CallbackLink));
|
||||
link->callback = callback;
|
||||
link->arg = arg;
|
||||
link->next = NULL;
|
||||
|
||||
CallbackList_lock();
|
||||
*callbacksEnd = link;
|
||||
callbacksEnd = &link->next;
|
||||
CallbackList_unlock();
|
||||
return (CallbackID) link;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
CallbackLink_delete(CallbackLink *link) {
|
||||
free(link);
|
||||
}
|
||||
|
||||
// Pre: CallbackList is locked.
|
||||
static CallbackLink **
|
||||
CallbackLink_find(CallbackLink *link) {
|
||||
CallbackLink **ptr;
|
||||
|
||||
//assert(CallbackList_isLocked());
|
||||
for (ptr = &callbacks; *ptr != NULL; ptr = &(*ptr)->next) {
|
||||
if (*ptr == link)
|
||||
return ptr;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool
|
||||
Callback_remove(CallbackID id) {
|
||||
CallbackLink *link = (CallbackLink *) id;
|
||||
CallbackLink **linkPtr;
|
||||
|
||||
CallbackList_lock();
|
||||
|
||||
linkPtr = CallbackLink_find(link);
|
||||
if (linkPtr == NULL) {
|
||||
CallbackList_unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (callbacksEnd == &(*linkPtr)->next)
|
||||
callbacksEnd = linkPtr;
|
||||
if (callbacksProcessEnd == &(*linkPtr)->next)
|
||||
callbacksProcessEnd = linkPtr;
|
||||
*linkPtr = (*linkPtr)->next;
|
||||
|
||||
CallbackList_unlock();
|
||||
|
||||
CallbackLink_delete(link);
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void
|
||||
CallbackLink_doCallback(CallbackLink *link) {
|
||||
(link->callback)(link->arg);
|
||||
}
|
||||
|
||||
// Call all queued callbacks currently in the queue. Callbacks queued
|
||||
// from inside the called functions will not be processed until the next
|
||||
// call of Callback_process().
|
||||
// It is allowed to remove callbacks from inside the called functions.
|
||||
// NB: Callback_process() must never be called from more than one thread
|
||||
// at the same time. It's the only sensible way to ensure that the
|
||||
// callbacks are called in the order in which they were queued.
|
||||
// It is however allowed to call Callback_process() from inside the
|
||||
// callback function called by Callback_process() itself.
|
||||
void
|
||||
Callback_process(void) {
|
||||
CallbackLink *link;
|
||||
|
||||
// We set 'callbacksProcessEnd' to callbacksEnd. Callbacks added
|
||||
// from inside a callback function will be placed after
|
||||
// callbacksProcessEnd, and will hence not be processed this
|
||||
// call of Callback_process().
|
||||
CallbackList_lock();
|
||||
callbacksProcessEnd = callbacksEnd;
|
||||
CallbackList_unlock();
|
||||
|
||||
for (;;) {
|
||||
CallbackList_lock();
|
||||
if (callbacksProcessEnd == &callbacks) {
|
||||
CallbackList_unlock();
|
||||
break;
|
||||
}
|
||||
assert(callbacks != NULL);
|
||||
// If callbacks == NULL, then callbacksProcessEnd == &callbacks
|
||||
link = callbacks;
|
||||
callbacks = link->next;
|
||||
if (callbacksEnd == &link->next)
|
||||
callbacksEnd = &callbacks;
|
||||
if (callbacksProcessEnd == &link->next)
|
||||
callbacksProcessEnd = &callbacks;
|
||||
CallbackList_unlock();
|
||||
|
||||
CallbackLink_doCallback(link);
|
||||
CallbackLink_delete(link);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
41
project/jni/application/sc2/src/libs/callback/callback.h
Normal file
41
project/jni/application/sc2/src/libs/callback/callback.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2006 Serge van den Boom <svdb@stack.nl>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef _CALLBACK_H
|
||||
#define _CALLBACK_H
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#ifdef CALLBACK_INTERNAL
|
||||
typedef CallbackLink *CallbackID;
|
||||
#else
|
||||
typedef void *CallbackID;
|
||||
// Uniquely identifies a queued callback.
|
||||
#endif
|
||||
#define CallbackID_invalid ((CallbackID ) NULL)
|
||||
|
||||
typedef void *CallbackArg;
|
||||
typedef void (*CallbackFunction)(CallbackArg arg);
|
||||
|
||||
void Callback_init(void);
|
||||
CallbackID Callback_add(CallbackFunction callback, CallbackArg arg);
|
||||
bool Callback_remove(CallbackID id);
|
||||
void Callback_process(void);
|
||||
|
||||
#endif /* _CALLBACK_H */
|
||||
|
||||
Reference in New Issue
Block a user