aboutsummaryrefslogtreecommitdiff
path: root/Src/tataki/bitmap/autobitmap.cpp
blob: 9c85c5864c3d0e17e99e97f909ef358a3fc58f24 (plain) (blame)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <tataki/api__tataki.h>
#include "autobitmap.h"
#include <bfc/assert.h>
#define TIMER_ID_RESET 0x1664

#ifdef DROP_BITMAP_ON_IDLE
// these are in seconds
#define DROP_MINDELAY 3
#define DROP_MAXDELAY 15
#define DROP_INITIALBUMP -5
#define DROP_MINDELAYSINCELASTUSE 7
#endif

/*
#ifdef _WIN32
extern HINSTANCE hInstance;
#endif
*/

AutoSkinBitmap::AutoSkinBitmap(const wchar_t *_name)
{
	bitmap = NULL;
	use = 0;
	id = 0;
	colorgroup = 0;
	name = 0;
	resamplingMode = RESAMPLING_MODE_NONE;
#ifdef WIN32
	myInstance = 0;
#endif
#ifdef DROP_BITMAP_ON_IDLE
	lastuse = 0;
#endif
	setBitmap(_name);
}

AutoSkinBitmap::~AutoSkinBitmap()
{
#ifdef DROP_BITMAP_ON_IDLE
	timerclient_killTimer(TIMER_ID_RESET);
#endif
	if (bitmap) bitmap->Release();
	ASSERT(WASABI_API_SYSCB != NULL);
	WASABI_API_SYSCB->syscb_deregisterCallback(this);
	free(colorgroup);
	free(name);
}

const wchar_t *AutoSkinBitmap::setBitmap(const wchar_t *_name)
{
	if (_name == NULL) return NULL;
	if (name == NULL || wcscmp(name, _name))
	{
		reset();
		free(name);
		name = _wcsdup(_name);
	}
	return name;
}

int AutoSkinBitmap::setBitmap(int _id)
{
	if (_id == 0) return 0;
	if (_id != id)
	{
		reset();
		id = _id;
	}
	return id;
}

#ifdef _WIN32
void AutoSkinBitmap::setHInstance(HINSTANCE hinstance)
{
	myInstance = hinstance;
}
#endif

void AutoSkinBitmap::reset()
{
	if (bitmap) bitmap->Release(); bitmap = NULL;
#ifdef DROP_BITMAP_ON_IDLE
	timerclient_killTimer(TIMER_ID_RESET);
#endif
}
static int modrandom(int max) 
{
  int ret = AGAVE_API_RANDOM->GetPositiveNumber();
  ret %= max;
  return ret;
}
SkinBitmap *AutoSkinBitmap::getBitmap()
{
	//FG  ASSERT(name != NULL);
	if ((name == NULL || (name != NULL && *name == NULL)) && id == NULL) return NULL;
	if (bitmap == NULL)
	{
		if (name)
		{
			switch (resamplingMode)
			{
			case RESAMPLING_MODE_SUPERSAMPLING:
				bitmap = new HQSkinBitmap(name);
				break;
			case RESAMPLING_MODE_NONE:
			default:
				bitmap = new SkinBitmap(name);
				break;
			}
		}
		else
		{
#ifdef WIN32
			switch (resamplingMode)
			{
			case RESAMPLING_MODE_SUPERSAMPLING:
				bitmap = new HQSkinBitmap(myInstance, id, colorgroup);
				break;
			case RESAMPLING_MODE_NONE:
			default:
				bitmap = new SkinBitmap(myInstance, id, colorgroup);
				break;
			}
#endif
		}
		ASSERT(WASABI_API_SYSCB != NULL);
		if (bitmap)
			WASABI_API_SYSCB->syscb_registerCallback(this);
#ifdef DROP_BITMAP_ON_IDLE
		if (bitmap)
		{
			lastuse = GetTickCount() + DROP_INITIALBUMP;
			timerclient_setTimer(TIMER_ID_RESET, DROP_MINDELAY*1000 + modrandom((DROP_MAXDELAY - DROP_MINDELAY)*1000));
			return bitmap;
		}
#endif

	}
#ifdef DROP_BITMAP_ON_IDLE
	if (bitmap) lastuse = GetTickCount();
#endif
	return bitmap;
}

const wchar_t *AutoSkinBitmap::getBitmapName()
{
	return name;
}

int AutoSkinBitmap::skincb_onReset()
{
	reset();
	return 1;
}

void AutoSkinBitmap::setHInstanceBitmapColorGroup(const wchar_t *_colorgroup) 
{ 
	free(colorgroup);
	if (_colorgroup)
		colorgroup = _wcsdup(_colorgroup);
	else
		colorgroup=0;
}

#ifdef DROP_BITMAP_ON_IDLE
void AutoSkinBitmap::timerclient_timerCallback(int id)
{
	if (id == TIMER_ID_RESET)
	{
		tryUnload();
	}
	else TimerClientDI::timerclient_timerCallback(id);
}

void AutoSkinBitmap::tryUnload()
{
	DWORD now = GetTickCount();
	if (now < lastuse + DROP_MINDELAYSINCELASTUSE*1000) return ;
	reset();
}
#endif

void AutoSkinBitmap::setResamplingMode(int mode)
{
	this->resamplingMode = mode;
	this->reset();
}

int AutoSkinBitmap::getResamplingMode()
{
	return this->resamplingMode;
}


#define CBCLASS AutoSkinBitmap
START_DISPATCH;
  CB(SYSCALLBACK_GETEVENTTYPE, getEventType);
  CB(SYSCALLBACK_NOTIFY, notify);
END_DISPATCH;
#undef CBCLASS