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
|
#include "Platform.hpp"
#include "../core/jni_string.hpp"
#include "../../../../../base/logging.hpp"
#include "../../../../../std/algorithm.hpp"
#include "../../../../../std/cmath.hpp"
class Platform::PlatformImpl
{
public:
PlatformImpl(int densityDpi, int screenWidth, int screenHeight)
{ // Constants are taken from android.util.DisplayMetrics
/// ceiling screen sizes to the nearest power of two, and taking half of it as a tile size
double const log2 = log(2.0);
// LOG(LINFO, ("width:", screenWidth, ", height:", screenHeight));
size_t ceiledScreenWidth = static_cast<int>(pow(2.0, ceil(log(double(screenWidth)) / log2)));
size_t ceiledScreenHeight = static_cast<int>(pow(2.0, ceil(log(double(screenHeight)) / log2)));
size_t ceiledScreenSize = max(ceiledScreenWidth, ceiledScreenHeight);
// LOG(LINFO, ("ceiledScreenSize=", ceiledScreenSize));
m_tileSize = min(max(ceiledScreenSize / 2, (size_t)128), (size_t)512);
double k = (256.0 / m_tileSize) * (256.0 / m_tileSize);
LOG(LINFO, ("tileSize=", m_tileSize));
/// calculating how much tiles we need for the screen of such size
m_maxTilesCount = 0;
m_preCachingDepth = 3;
/// calculating for non-rotated screen
for (unsigned i = 0; i < m_preCachingDepth; ++i)
{
// LOG(LINFO, ("calculating", i, "level"));
/// minimum size of tile on the screen
float minTileSize = floor((m_tileSize << i) / 1.05 / 2.0) - 1;
// LOG(LINFO, ("minTileSize:", minTileSize));
int tilesOnXSide = ceil(screenWidth / minTileSize) + 1;
// LOG(LINFO, ("tilesOnXSide:", tilesOnXSide));
int tilesOnYSide = ceil(screenHeight / minTileSize) + 1;
// LOG(LINFO, ("tilesOnYSide:", tilesOnYSide));
/// tiles in the single screen
int singleScreenTilesCount = tilesOnXSide * tilesOnYSide;
// LOG(LINFO, ("singleScreenTilesCount:", singleScreenTilesCount));
int curLevelTilesCount = singleScreenTilesCount * 2;
// LOG(LINFO, ("curLevelTilesCount:", curLevelTilesCount));
m_maxTilesCount += curLevelTilesCount;
// LOG(LINFO, ("on", i, "depth we need", curLevelTilesCount, "tiles"));
}
LOG(LINFO, ("minimum amount of tiles needed is", m_maxTilesCount));
m_maxTilesCount = max(static_cast<int>(120 * k), m_maxTilesCount);
switch (densityDpi)
{
case 120:
m_visualScale = 0.75;
m_skinName = "basic_ldpi.skn";
LOG(LINFO, ("using LDPI resources"));
break;
case 160:
m_visualScale = 1.0;
m_skinName = "basic_mdpi.skn";
LOG(LINFO, ("using MDPI resources"));
break;
case 240:
m_visualScale = 1.5;
m_skinName = "basic_hdpi.skn";
LOG(LINFO, ("using HDPI resources"));
break;
default:
m_visualScale = 2.0;
m_skinName = "basic_xhdpi.skn";
LOG(LINFO, ("using XHDPI resources"));
break;
}
}
double m_visualScale;
string m_skinName;
int m_maxTilesCount;
size_t m_tileSize;
size_t m_preCachingDepth;
};
double Platform::VisualScale() const
{
return m_impl->m_visualScale;
}
string Platform::SkinName() const
{
return m_impl->m_skinName;
}
int Platform::MaxTilesCount() const
{
return m_impl->m_maxTilesCount;
}
int Platform::TileSize() const
{
return m_impl->m_tileSize;
}
int Platform::PreCachingDepth() const
{
return m_impl->m_preCachingDepth;
}
namespace android
{
Platform::~Platform()
{
delete m_impl;
}
void Platform::Initialize(JNIEnv * env,
jint densityDpi,
jint screenWidth,
jint screenHeight,
jstring apkPath,
jstring storagePath,
jstring tmpPath,
jstring extTmpPath,
jstring settingsPath)
{
if (m_impl)
delete m_impl;
m_impl = new PlatformImpl(densityDpi, screenWidth, screenHeight);
m_resourcesDir = jni::ToString(env, apkPath);
m_writableDir = jni::ToString(env, storagePath);
m_settingsDir = jni::ToString(env, settingsPath);
m_localTmpPath = jni::ToString(env, tmpPath);
m_externalTmpPath = jni::ToString(env, extTmpPath);
// By default use external temporary folder
m_tmpDir = m_externalTmpPath;
LOG(LDEBUG, ("Apk path = ", m_resourcesDir));
LOG(LDEBUG, ("Writable path = ", m_writableDir));
LOG(LDEBUG, ("Local tmp path = ", m_localTmpPath));
LOG(LDEBUG, ("External tmp path = ", m_externalTmpPath));
LOG(LDEBUG, ("Settings path = ", m_settingsDir));
}
void Platform::OnExternalStorageStatusChanged(bool isAvailable)
{
if (isAvailable)
m_tmpDir = m_externalTmpPath;
else
m_tmpDir = m_localTmpPath;
}
Platform & Platform::Instance()
{
static Platform platform;
return platform;
}
}
Platform & GetPlatform()
{
return android::Platform::Instance();
}
|