-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathactorhandler.cpp
More file actions
155 lines (126 loc) · 3.82 KB
/
actorhandler.cpp
File metadata and controls
155 lines (126 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "prism/actorhandler.h"
#include "prism/datastructures.h"
#include "prism/log.h"
#include "prism/memoryhandler.h"
#include "prism/wrapper.h"
namespace prism {
typedef struct {
ActorBlueprint mBP;
ListIterator mListEntry;
int mID;
void* mData;
int mIsOwned;
int mIsPausable;
} Actor;
static struct {
IntMap mActors;
List mSequentialActorList;
int mIsInitialized;
} gPrismActorHandlerData;
ActorBlueprint makeActorBlueprint(LoadActorFunction tLoad, UnloadActorFunction tUnload, UpdateActorFunction tUpdate, DrawActorFunction tDraw, IsActorActiveFunction tIsActive) {
ActorBlueprint ret;
ret.mLoad = tLoad;
ret.mUnload = tUnload;
ret.mUpdate = tUpdate;
ret.mDraw = tDraw;
ret.mIsActive = tIsActive;
return ret;
}
void setupActorHandler()
{
setProfilingSectionMarkerCurrentFunction();
if (gPrismActorHandlerData.mIsInitialized) {
logWarning("Actor handling already initialized.");
shutdownActorHandler();
}
gPrismActorHandlerData.mActors = new_int_map();
gPrismActorHandlerData.mSequentialActorList = new_list();
gPrismActorHandlerData.mIsInitialized = 1;
}
static void unloadActor(Actor* e) {
if (e->mBP.mUnload) e->mBP.mUnload(e->mData);
if (e->mIsOwned) {
freeMemory(e->mData);
}
int_map_remove(&gPrismActorHandlerData.mActors, e->mID);
}
static int removeActorCB(void* tCaller, void* tData) {
(void)tCaller;
Actor* e = (Actor*)tData;
unloadActor(e);
return 1;
}
void shutdownActorHandler()
{
setProfilingSectionMarkerCurrentFunction();
list_remove_predicate_inverted(&gPrismActorHandlerData.mSequentialActorList, removeActorCB, NULL);
delete_int_map(&gPrismActorHandlerData.mActors);
delete_list(&gPrismActorHandlerData.mSequentialActorList);
gPrismActorHandlerData.mIsInitialized = 0;
}
static int updateSingleActor(void* tCaller, void* tData) {
(void)tCaller;
Actor* e = (Actor*)tData;
if (isWrapperPaused() && e->mIsPausable) return 0;
int isActive = e->mBP.mIsActive == NULL || e->mBP.mIsActive(e->mData);
if (!isActive) {
unloadActor(e);
return 1;
}
if (e->mBP.mUpdate) e->mBP.mUpdate(e->mData);
return 0;
}
void updateActorHandler()
{
setProfilingSectionMarkerCurrentFunction();
list_remove_predicate(&gPrismActorHandlerData.mSequentialActorList, updateSingleActor, NULL);
}
static void drawSingleActor(void* tCaller, void* tData) {
(void)tCaller;
Actor* e = (Actor*)tData;
if (e->mBP.mDraw) e->mBP.mDraw(e->mData);
}
void drawActorHandler()
{
setProfilingSectionMarkerCurrentFunction();
list_map(&gPrismActorHandlerData.mSequentialActorList, drawSingleActor, NULL);
}
int instantiateActor(const ActorBlueprint& tBP)
{
return instantiateActorWithData(tBP, NULL, 0);
}
int instantiateActorWithData(const ActorBlueprint& tBP, void* tData, int tIsOwned)
{
Actor* e = (Actor*)allocMemory(sizeof(Actor));
e->mBP = tBP;
e->mData = tData;
e->mIsOwned = tIsOwned;
e->mIsPausable = 1;
if (e->mBP.mLoad) e->mBP.mLoad(e->mData);
list_push_back_owned(&gPrismActorHandlerData.mSequentialActorList, e);
e->mListEntry = list_iterator_end(&gPrismActorHandlerData.mSequentialActorList);
e->mID = int_map_push_back(&gPrismActorHandlerData.mActors, e);
return e->mID;
}
void performOnActor(int tID, ActorInteractionFunction tFunc, void* tCaller)
{
Actor* e = (Actor*)int_map_get(&gPrismActorHandlerData.mActors, tID);
tFunc(tCaller, e->mData);
}
void setActorUnpausable(int tID)
{
Actor* e = (Actor*)int_map_get(&gPrismActorHandlerData.mActors, tID);
e->mIsPausable = 0;
}
void removeActor(int tID)
{
Actor* e = (Actor*)int_map_get(&gPrismActorHandlerData.mActors, tID);
unloadActor(e);
list_iterator_remove(&gPrismActorHandlerData.mSequentialActorList, e->mListEntry);
}
void* getActorData(int tID)
{
Actor* e = (Actor*)int_map_get(&gPrismActorHandlerData.mActors, tID);
return e->mData;
}
}