First blood

This commit is contained in:
vladislav.khorev committed 2013-01-19 20:02:34 +00:00
1 parent 04f270f322
commit 5720f8322b
250 files changed
+33215

No files matched your search

+243
View File
@@ -0,0 +1,243 @@
#include "include/Engine.h"
#include "boost/date_time.hpp"
namespace SE
{
const cardinal CONST_MAX_ROWS_IN_HISTORY = 20;
const float CONST_CONSOLE_PART_OF_SCREEN = 0.35f;
const int CONST_ROW_WIDTH = 14;
const std::string CONST_CONSOLE_TEX_NAME = "console_bkg.bmp";
void TSimpleConsole::Draw()
{
RenderUniform1f("Transparency", 1.f);
if (HistoryTriangeList.get() == 0)
{
HistoryTriangeList = std::shared_ptr<TTriangleList>(new TTriangleList);
}
float screenWidth = Renderer->GetMatrixWidth();
float screenHeight = Renderer->GetMatrixHeight();
glBindTexture(GL_TEXTURE_2D, ResourceManager->TexList[CONST_CONSOLE_TEX_NAME]);
Renderer->PushProjectionMatrix(static_cast<float>(screenWidth), static_cast<float>(screenHeight));
Renderer->PushMatrix();
Renderer->LoadIdentity();
Renderer->DrawRect(vec2(0.f, screenHeight*(1.f - CONST_CONSOLE_PART_OF_SCREEN)), vec2(screenWidth, screenHeight));
if (TextSavedInTriangleList != History)
{
TextSavedInTriangleList = History;
*HistoryTriangeList = ResourceManager->FontManager.DrawTextInBoxToVBO(vec2(0.f, screenHeight * (1.f - CONST_CONSOLE_PART_OF_SCREEN)), vec2(static_cast<float>(screenWidth), static_cast<float>(screenHeight)), CONST_ROW_WIDTH, TextSavedInTriangleList);
}
TTriangleList consoleInputTriangleList = ResourceManager->FontManager.DrawTextInBoxToVBO(vec2(0.f, screenHeight * (1.f - CONST_CONSOLE_PART_OF_SCREEN)), vec2(static_cast<float>(screenWidth), static_cast<float>(screenHeight)), CONST_ROW_WIDTH, ">"+ConsoleInput);
glBindTexture(GL_TEXTURE_2D, ResourceManager->TexList[ResourceManager->FontManager.GetCurrentFontTextureName()]);
Renderer->DrawTriangleList(*HistoryTriangeList);
Renderer->DrawTriangleList(consoleInputTriangleList);
std::string ConsoleInputAtCursor = std::string((">"+ConsoleInput), 0, ConsoleCursor+1);
float advance = ResourceManager->FontManager.GetTextAdvance(ConsoleInputAtCursor);
TTriangleList consoleCursorTriangleList = ResourceManager->FontManager.DrawTextInBoxToVBO(vec2(advance, screenHeight*(1.f - CONST_CONSOLE_PART_OF_SCREEN)), vec2(static_cast<float>(screenWidth), static_cast<float>(screenHeight)), 14, "|");
Renderer->DrawTriangleList(consoleCursorTriangleList);
Renderer->PopMatrix();
Renderer->PopProjectionMatrix();
}
void TSimpleConsole::CutHistory()
{
cardinal i = 0;
cardinal pos = std::string::npos;
do
{
if (pos != std::string::npos && pos > 0)
{
pos--;
}
pos = History.find_last_of('\n', pos);
i++;
}
while (pos != std::string::npos && i != CONST_MAX_ROWS_IN_HISTORY);
if (pos != std::string::npos)
{
History = History.substr(pos);
}
}
std::string TSimpleConsole::GetHistory()
{
return History;
}
#ifdef TARGET_WIN32
TFileConsole::TFileConsole()
: filename("conlog.txt")
, f("conlog.txt")
{
}
TFileConsole::TFileConsole(const std::string& Afilename)
: filename(Afilename)
, f(Afilename)
{
}
TFileConsole::~TFileConsole()
{
f.close();
}
TFileConsole& TFileConsole::operator<<(const std::string& s)
{
boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
std::string string_with_time_mark;
string_with_time_mark = to_simple_string(t)+": "+s;
PrintImmediate(string_with_time_mark);
History += string_with_time_mark+endl;
CutHistory();
return *this;
}
void TFileConsole::PrintImmediate(const std::string& s)
{
f<<s+endl;
f.flush();
OutputDebugString( (s+endl).c_str() );
}
#endif
#ifdef TARGET_ANDROID
#include "include/Utils/JniApi/JniApi.h"
TJavaConsole::TJavaConsole()
{
boost::mutex::scoped_lock scoped_lock(ConsoleMutex);
AppDir = JniGetApplicationDir();
LogFilename = AppDir + "/fishrungames-log.txt";
FILE* file = fopen(LogFilename.c_str(), "w");
if (file != NULL)
{
fclose(file);
}
}
TJavaConsole& TJavaConsole::operator<<(const std::string& s)
{
boost::mutex::scoped_lock scoped_lock(ConsoleMutex);
boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
std::string string_with_time_mark;
string_with_time_mark = boost::posix_time::to_simple_string(t)+": "+s;
PrintImmediate(string_with_time_mark);
History += string_with_time_mark+endl;
CutHistory();
return *this;
}
void TJavaConsole::PrintImmediate(const std::string& s)
{
FILE* file = fopen(LogFilename.c_str(), "a");
if (file != NULL)
{
fputs(std::string(s+endl).c_str(), file);
fflush(file);
fclose(file);
}
}
#endif
#ifdef TARGET_IOS
TIosConsole::TIosConsole()
{
}
TIosConsole::~TIosConsole()
{
}
TIosConsole& TIosConsole::operator<<(const std::string& s)
{
boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
std::string string_with_time_mark;
string_with_time_mark = to_simple_string(t)+": "+s;
PrintImmediate(string_with_time_mark);
History += string_with_time_mark+endl;
CutHistory();
return *this;
}
void TIosConsole::PrintImmediate(const std::string& s)
{
printf("%s", std::string(s+endl).c_str());
}
#endif
} //namespace SE
+479
View File
@@ -0,0 +1,479 @@
#include "include/Utils/DataTypes/DataTypes.h"
#include "include/Utils/ErrorTypes/ErrorTypes.h"
#include "boost/algorithm/string.hpp"
namespace SE
{
mat4 InverseModelViewMatrix(const mat4& m)
{
mat4 r;
r.m[0] = m.m[0];
r.m[1] = m.m[4];
r.m[2] = m.m[8];
r.m[3] = 0.0f;
r.m[4] = m.m[1];
r.m[5] = m.m[5];
r.m[6] = m.m[9];
r.m[7] = 0.0f;
r.m[8] = m.m[2];
r.m[9] = m.m[6];
r.m[10] = m.m[10];
r.m[11] = 0.0f;
r.m[12] = - (m.m[12]*r.m[0] + m.m[13]*r.m[4] + m.m[14]*r.m[8]);
r.m[13] = - (m.m[12]*r.m[1] + m.m[13]*r.m[5] + m.m[14]*r.m[9]);
r.m[14] = - (m.m[12]*r.m[2] + m.m[13]*r.m[6] + m.m[14]*r.m[10]);
r.m[15] = 1.0f;
return r;
}
mat4 SetToNormalMatrix(const mat4& m)
{
mat4 r;
r.m[0] = m.m[0];
r.m[1] = m.m[4];
r.m[2] = m.m[8];
r.m[3] = 0.0f;
r.m[4] = m.m[1];
r.m[5] = m.m[5];
r.m[6] = m.m[9];
r.m[7] = 0.0f;
r.m[8] = m.m[2];
r.m[9] = m.m[6];
r.m[10] = m.m[10];
r.m[11] = 0.0f;
r.m[12] = 0.0f;
r.m[13] = 0.0f;
r.m[14] = 0.0f;
r.m[15] = 1.0f;
return r;
}
mat4 MakeOrthoMatrix(float width, float height)
{
mat4 r;
r.m[0] = 2.f / width;
r.m[1] = 0;
r.m[2] = 0;
r.m[3] = 0;
r.m[4] = 0;
r.m[5] = 2.f / height;
r.m[6] = 0;
r.m[7] = 0;
r.m[8] = 0;
r.m[9] = 0;
r.m[10] = -1;
r.m[11] = 0;
r.m[12] = -1;
r.m[13] = -1;
r.m[14] = 0;
r.m[15] = 1;
return r;
}
mat4 MakeFrustumMatrix(float left, float right, float bottom, float top, float nearVal, float farVal)
{
mat4 r;
float a = (right + left) / (right - left);
float b = (top + bottom) / (top - bottom);
float c = - (farVal + nearVal) / (farVal - nearVal);
float d = - (2.f * farVal * nearVal) / (farVal - nearVal);
r.m[0] = 2.f*nearVal / (right - left); r.m[4] = 0.f; r.m[8] = a; r.m[12] = 0.f;
r.m[1] = 0.f; r.m[5] = 2.f*nearVal / (top - bottom); r.m[9] = b; r.m[13] = 0.f;
r.m[2] = 0.f; r.m[6] = 0.f; r.m[10] = c; r.m[14] = d;
r.m[3] = 0.f; r.m[7] = 0.f; r.m[11] = -1; r.m[15] = 0.f;
return r;
}
mat4 MakePerspectiveMatrix(float angle, float aspect, float zNear, float zFar)
{
//angle = angle * pi / 180.f;
float top = zNear * tan(angle / 2.f);
float bottom = -top;
float left = bottom * aspect;
float right = top * aspect;
return MakeFrustumMatrix(left, right, bottom, top, zNear, zFar);
}
mat4 MultMatrixMatrix(const mat4& m1, const mat4& m2)
{
mat4 r;
r.m[0] = m1.m[0]*m2.m[0] + m1.m[4]*m2.m[1] + m1.m[8]*m2.m[2] + m1.m[12]*m2.m[3];
r.m[1] = m1.m[1]*m2.m[0] + m1.m[5]*m2.m[1] + m1.m[9]*m2.m[2] + m1.m[13]*m2.m[3];
r.m[2] = m1.m[2]*m2.m[0] + m1.m[6]*m2.m[1] + m1.m[10]*m2.m[2] + m1.m[14]*m2.m[3];
r.m[3] = m1.m[3]*m2.m[0] + m1.m[7]*m2.m[1] + m1.m[11]*m2.m[2] + m1.m[15]*m2.m[3];
r.m[4] = m1.m[0]*m2.m[4] + m1.m[4]*m2.m[5] + m1.m[8]*m2.m[6] + m1.m[12]*m2.m[7];
r.m[5] = m1.m[1]*m2.m[4] + m1.m[5]*m2.m[5] + m1.m[9]*m2.m[6] + m1.m[13]*m2.m[7];
r.m[6] = m1.m[2]*m2.m[4] + m1.m[6]*m2.m[5] + m1.m[10]*m2.m[6] + m1.m[14]*m2.m[7];
r.m[7] = m1.m[3]*m2.m[4] + m1.m[7]*m2.m[5] + m1.m[11]*m2.m[6] + m1.m[15]*m2.m[7];
r.m[8] = m1.m[0]*m2.m[8] + m1.m[4]*m2.m[9] + m1.m[8]*m2.m[10] + m1.m[12]*m2.m[11];
r.m[9] = m1.m[1]*m2.m[8] + m1.m[5]*m2.m[9] + m1.m[9]*m2.m[10] + m1.m[13]*m2.m[11];
r.m[10] = m1.m[2]*m2.m[8] + m1.m[6]*m2.m[9] + m1.m[10]*m2.m[10] + m1.m[14]*m2.m[11];
r.m[11] = m1.m[3]*m2.m[8] + m1.m[7]*m2.m[9] + m1.m[11]*m2.m[10] + m1.m[15]*m2.m[11];
r.m[12] = m1.m[0]*m2.m[12] + m1.m[4]*m2.m[13] + m1.m[8]*m2.m[14] + m1.m[12]*m2.m[15];
r.m[13] = m1.m[1]*m2.m[12] + m1.m[5]*m2.m[13] + m1.m[9]*m2.m[14] + m1.m[13]*m2.m[15];
r.m[14] = m1.m[2]*m2.m[12] + m1.m[6]*m2.m[13] + m1.m[10]*m2.m[14] + m1.m[14]*m2.m[15];
r.m[15] = m1.m[3]*m2.m[12] + m1.m[7]*m2.m[13] + m1.m[11]*m2.m[14] + m1.m[15]*m2.m[15];
return r;
}
bool IsFloatEqual(float a, float b)
{
return (fabs(b-a) <= CONST_EPSILON) ? true : false;
}
int LineCrossTriangle(const vec3& a, const vec3& b, const TSmpTriangle& tr)
{
vec3 dummy;
return LineCrossTriangle(a, b, tr, dummy);
}
int LineCrossTriangle(const vec3& a, const vec3& b, const TSmpTriangle& tr, vec3& CrossPoint)
{
vec3 h, q, t, vx;
mat3 M;
h = b - a;
if (IsFloatEqual(DotProduct(tr.n, h), 0.0f))
{
return -1;
}
M.m[0] = h.v[0];
M.m[3] = h.v[1];
M.m[6] = h.v[2];
M.m[1] = -tr.v.v[0];
M.m[4] = -tr.v.v[1];
M.m[7] = -tr.v.v[2];
M.m[2] = -tr.w.v[0];
M.m[5] = -tr.w.v[1];
M.m[8] = -tr.w.v[2];
M = InverseMatrix(M);
vx = tr.p[0] - a;
t = vx * M;
if ((t.v[0]<0)||(t.v[0]>1))
{
return 0;
}
q = a + h * t.v[0];
CrossPoint = q;
return PointInTriangle(q, tr);
}
int PointInTriangle(const vec3& q, const TSmpTriangle& tr)
{
vec3 v,h;
float nf2[3];
vec4 quat, inv_quat, p;
TSmpTriangle trx;
float sina,cosa,cos2a;
int i;
h = vec3(0.0f, 1.0f, 0.0f);
if (IsFloatEqual(tr.n.v[0],0.0f) && (!IsFloatEqual(tr.n.v[1],0.0f)) && IsFloatEqual(tr.n.v[2],0.0f))
{
quat = vec4(1.0f, 0.0f, 0.0f, 0.0f);
}
else
{
v = CrossProduct(tr.n,h);
cos2a = DotProduct(Normalize(tr.n), h);
cos2a *= 0.5f;
cosa = sqrtf(0.5f + cos2a);
sina = sqrtf(0.5f - cos2a);
v = Normalize(v);
quat.v[0] = v.v[0]*sina;
quat.v[1] = v.v[1]*sina;
quat.v[2] = v.v[2]*sina;
quat.v[3] = cosa;
}
inv_quat.v[0] = - quat.v[0];
inv_quat.v[1] = - quat.v[1];
inv_quat.v[2] = - quat.v[2];
inv_quat.v[3] = quat.v[3];
for (i=0; i<3; i++)
{
p = vec4(tr.p[i]);
p = quat * p * inv_quat;
trx.p[i] = vec3(p);
};
p.v[0] = q.v[0];
p.v[1] = q.v[1];
p.v[2] = q.v[2];
p.v[3] = 0.0f;
p = quat * p * inv_quat;
for (i=0;i<2;i++)
nf2[i] = (p.v[0]-trx.p[i].v[0])*(trx.p[i+1].v[2]-trx.p[i].v[2])-(p.v[2]-trx.p[i].v[2])*(trx.p[i+1].v[0]-trx.p[i].v[0]);
//Not to go over array:
nf2[2] = (p.v[0]-trx.p[2].v[0])*(trx.p[0].v[2]-trx.p[2].v[2])-(p.v[2]-trx.p[2].v[2])*(trx.p[0].v[0]-trx.p[2].v[0]);
if (((nf2[0]>=0.0f) && (nf2[1]>=0.0f) && (nf2[2]>=0.0f))
|| ((nf2[0]<=0.0f) && (nf2[1]<=0.0f) && (nf2[2]<=0.0f)))
{
return 1;
}
else
{
return 0;
}
}
float roundf(float r)
{
return (r > 0.0f) ? floorf(r + 0.5f) : ceilf(r - 0.5f);
}
cardinal GetGreaterPower2(cardinal x)
{
if (x > 65536)
{
throw ErrorToLog("GetGreaterPower2: x("+tostr(x)+") is bigger than 65536");
}
cardinal result = 1;
while (x > result)
{
result *= 2;
}
return result;
}
vec2 StringToVec2(std::string str)
{
boost::erase_all(str, " ");
std::vector<std::string> splitVec;
boost::split( splitVec, str, boost::is_any_of(",;"), boost::token_compress_on );
if (splitVec.size() < 2)
{
throw ErrorToLog("StringToVec2 - vector is too short: "+str);
}
vec2 result;
result.v[0] = boost::lexical_cast<float>(splitVec[0]);
result.v[1] = boost::lexical_cast<float>(splitVec[1]);
return result;
}
vec3 StringToVec3(std::string str)
{
boost::erase_all(str, " ");
std::vector<std::string> splitVec;
boost::split( splitVec, str, boost::is_any_of(",;"), boost::token_compress_on );
if (splitVec.size() < 3)
{
throw ErrorToLog("StringToVec3 - vector is too short: "+str);
}
vec3 result;
result.v[0] = boost::lexical_cast<float>(splitVec[0]);
result.v[1] = boost::lexical_cast<float>(splitVec[1]);
result.v[2] = boost::lexical_cast<float>(splitVec[2]);
return result;
}
vec4 StringToVec4(std::string str)
{
boost::erase_all(str, " ");
std::vector<std::string> splitVec;
boost::split( splitVec, str, boost::is_any_of(",;"), boost::token_compress_on );
if (splitVec.size() < 4)
{
throw ErrorToLog("StringToVec4 - vector is too short: "+str);
}
vec4 result;
result.v[0] = boost::lexical_cast<float>(splitVec[0]);
result.v[1] = boost::lexical_cast<float>(splitVec[1]);
result.v[2] = boost::lexical_cast<float>(splitVec[2]);
result.v[3] = boost::lexical_cast<float>(splitVec[3]);
return result;
}
// ================================================
// ========== wide strings and UTF8 strings =======
// ================================================
void utf8toWStr(std::wstring& dest, const std::string& src){
dest.clear();
wchar_t w = 0;
int bytes = 0;
wchar_t err = 0xFFFD;
for (size_t i = 0; i < src.size(); i++){
unsigned char c = (unsigned char)src[i];
if (c <= 0x7f){//first byte
if (bytes){
dest.push_back(err);
bytes = 0;
}
dest.push_back((wchar_t)c);
}
else if (c <= 0xbf){//second/third/etc byte
if (bytes){
w = ((w << 6)|(c & 0x3f));
bytes--;
if (bytes == 0)
dest.push_back(w);
}
else
dest.push_back(err);
}
else if (c <= 0xdf){//2byte sequence start
bytes = 1;
w = c & 0x1f;
}
else if (c <= 0xef){//3byte sequence start
bytes = 2;
w = c & 0x0f;
}
else if (c <= 0xf7){//3byte sequence start
bytes = 3;
w = c & 0x07;
}
else{
dest.push_back(err);
bytes = 0;
}
}
if (bytes)
dest.push_back(err);
}
void wstrToUtf8(std::string& dest, const std::wstring& src){
dest.clear();
for (size_t i = 0; i < src.size(); i++){
wchar_t w = src[i];
if (w <= 0x7f)
dest.push_back((char)w);
else if (w <= 0x7ff){
dest.push_back(0xc0 | ((w >> 6)& 0x1f));
dest.push_back(0x80| (w & 0x3f));
}
else if (w <= 0xffff){
dest.push_back(0xe0 | ((w >> 12)& 0x0f));
dest.push_back(0x80| ((w >> 6) & 0x3f));
dest.push_back(0x80| (w & 0x3f));
}
else if (w <= 0x10ffff){
dest.push_back(0xf0 | ((w >> 18)& 0x07));
dest.push_back(0x80| ((w >> 12) & 0x3f));
dest.push_back(0x80| ((w >> 6) & 0x3f));
dest.push_back(0x80| (w & 0x3f));
}
else
dest.push_back('?');
}
}
std::string wstrToUtf8(const std::wstring& str){
std::string result;
wstrToUtf8(result, str);
return result;
}
std::wstring utf8toWStr(const std::string& str){
std::wstring result;
utf8toWStr(result, str);
return result;
}
std::ostream& operator<<(std::ostream& f, const std::wstring& s){
std::string s1;
wstrToUtf8(s1, s);
f << s1;
return f;
}
std::istream& operator>>(std::istream& f, std::wstring& s){
std::string s1;
f >> s1;
utf8toWStr(s, s1);
return f;
}
} //namespace SE
+57
View File
@@ -0,0 +1,57 @@
#include "include/Utils/DataTypes/NewDataTypes.h"
#include "include/Utils/DataTypes/DataTypes.h"
#include "include/Utils/ErrorTypes/ErrorTypes.h"
namespace SE
{
mat3 TransposeMatrix(const mat3& m)
{
mat3 r;
r.m[0] = m.m[0];
r.m[1] = m.m[3];
r.m[2] = m.m[6];
r.m[3] = m.m[1];
r.m[4] = m.m[4];
r.m[5] = m.m[7];
r.m[6] = m.m[2];
r.m[7] = m.m[5];
r.m[8] = m.m[8];
return r;
}
mat3 InverseMatrix(const mat3& m)
{
float d;
mat3 r;
d = m.m[0]*(m.m[4]*m.m[8] - m.m[5]*m.m[7]);
d -= m.m[1]*(m.m[3]*m.m[8] - m.m[6]*m.m[5]);
d += m.m[2]*(m.m[3]*m.m[7] - m.m[6]*m.m[4]);
if (IsFloatEqual(d,0.0f))
{
throw ErrorToLog("Error: matrix cannot be inversed!");
}
else
{
r.m[0] = (m.m[4]*m.m[8] - m.m[5]*m.m[7])/d;
r.m[1] = - (m.m[1]*m.m[8] - m.m[2]*m.m[7])/d;
r.m[2] = (m.m[1]*m.m[5] - m.m[2]*m.m[4])/d;
r.m[3] = - (m.m[3]*m.m[8] - m.m[5]*m.m[6])/d;
r.m[4] = (m.m[0]*m.m[8] - m.m[2]*m.m[6])/d;
r.m[5] = - (m.m[0]*m.m[5] - m.m[2]*m.m[3])/d;
r.m[6] = (m.m[3]*m.m[7] - m.m[6]*m.m[4])/d;
r.m[7] = - (m.m[0]*m.m[7] - m.m[6]*m.m[1])/d;
r.m[8] = (m.m[0]*m.m[4] - m.m[1]*m.m[3])/d;
};
return r;
}
} //namespace SE
+91
View File
@@ -0,0 +1,91 @@
#include "include/Utils/FileUtils/FileUtils.h"
#include "include/Utils/Utils.h"
#include "include/Engine.h"
#include <string>
namespace SE
{
bool findString(char* in, char* list)
{
int thisLength = strlen(in);
while (*list != 0)
{
int length = strcspn(list," ");
if( thisLength == length )
if (!strncmp(in,list,length))
return true;
list += length + 1;
}
return false;
}
std::string GetFilePath(const std::string& filename)
{
//any symbol - "/" or "\"
int i = filename.find_last_of("/\\");
if (i==filename.npos)
{
return "";
}
std::string r = filename.substr(0, i+1);
return r;
}
bool IsFileExistsInUserData(const std::string& filename)
{
std::string realFileName = GetFilePathUserData(filename);
FILE* f;
f = fopen(realFileName.c_str(), "r");
if (f == NULL)
{
return false;
}
fclose(f);
return true;
}
std::string GetFilePathUserData(const std::string& filename)
{
#ifdef TARGET_WIN32
std::string realFileName = filename;
#endif
#ifdef TARGET_ANDROID
std::string realFileName = JniGetApplicationDir()+"/" + filename;
#endif
#ifdef TARGET_IOS
std::string realFileName = IosGetFilePathUserData(filename);
#endif
return realFileName;
}
#ifdef TARGET_IOS
//Special for IOS -> Foundation.h conflicts with sq_plus.h
std::string GetPathToResources()
{
return ResourceManager->PathToResources;
}
#endif
} //namespace SE
+51
View File
@@ -0,0 +1,51 @@
//
// Halibut_Engine.m
// Halibut Engine
//
// Created by vvv ооо on 13.07.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#include "include/Utils/Utils.h"
namespace SE
{
std::string IosGetFileReadPath(const std::string& filename)
{
NSString* nsFullFileName = [NSString stringWithCString:filename.c_str() encoding:NSUTF8StringEncoding];
NSString* nsFileShortName = [nsFullFileName lastPathComponent];
NSString* nsFileShortNameWithoutExt = [nsFileShortName stringByDeletingPathExtension];
NSString* nsFileExt = [nsFullFileName pathExtension];
NSString* nsFilePath = [nsFullFileName stringByDeletingLastPathComponent];
NSString *filePath = [[NSBundle mainBundle] pathForResource:nsFileShortNameWithoutExt ofType:nsFileExt inDirectory:nsFilePath];
return std::string([filePath UTF8String]);
}
std::string IosGetFilePathUserData(const std::string& filename)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* docDir = [paths objectAtIndex:0];
docDir = [docDir stringByAppendingString:@"/"];
NSString* file = [docDir stringByAppendingString:[NSString stringWithCString:filename.c_str() encoding:NSUTF8StringEncoding]];
return std::string([[NSFileManager defaultManager] fileSystemRepresentationWithPath:file]);
}
} //namespace SE
+389
View File
@@ -0,0 +1,389 @@
#include "include/Utils/JniApi/JniApi.h"
#include "include/Utils/Utils.h"
namespace SE
{
JavaVM* JavaVirtualMachine = 0;
const std::string CONST_JAVA_FILESYSTEM_CLASS_NAME = "fishrungames/engine/FileWrapper";
//Private data
//All 3 deprecated
cardinal* FileArr = NULL;
int FileSize = 0;
int filePointer;
std::string ApkFilePath;
//Private methods
jclass JniTryFindClass(const std::string& className);
jmethodID JniTryFindStaticMethod(jclass clazz, const std::string& methodId, const std::string methodSignature);
void CheckCommonExceptionWithMessage(const std::string& message);
//Deprecated
void JniCallOpenFile(const std::string& fileName);
//==================================================
//=============== IMPLEMENTATION ===================
//==================================================
JNIEXPORT void JNICALL Java_fishrungames_engine_FileWrapper_SetupEnviroment(JNIEnv* env, jobject thiz)
{
int JVMResult;
JVMResult = env->GetJavaVM(&JavaVirtualMachine);
//TODO: Check if jmvresult != 0
}
JNIEXPORT void JNICALL Java_fishrungames_engine_FileWrapper_SetupApkFilePath(JNIEnv* env, jobject thiz, jstring s)
{
const char *nativeString = env->GetStringUTFChars(s, 0);
ApkFilePath = std::string(nativeString);
env->ReleaseStringUTFChars(s, nativeString);
}
JNIEXPORT void JNICALL Java_fishrungames_engine_FileWrapper_ConsoleOut(JNIEnv* env, jobject thiz, jstring s)
{
const char *nativeString = env->GetStringUTFChars(s, 0);
*Console<<std::string("OUT OF JAVA: ")+nativeString;
env->ReleaseStringUTFChars(s, nativeString);
}
//Deprecated
JNIEXPORT void JNICALL Java_fishrungames_engine_FileWrapper_CreateFile(JNIEnv* env, jobject thiz, int fileSize)
{
FileSize = fileSize;
FileArr = new cardinal [fileSize % 4 == 0 ? fileSize/4 : fileSize/4 + 1];
filePointer = 0;
}
//Deprecated
JNIEXPORT void JNICALL Java_fishrungames_engine_FileWrapper_WriteToFile(JNIEnv* env, jobject thiz, jbyteArray buffer, int bufferSize)
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jenv->GetByteArrayRegion(buffer, 0, bufferSize, (jbyte*)((char*)FileArr + filePointer));
filePointer += bufferSize;
}
//===================================================
//============== Private methods ====================
//===================================================
jclass JniTryFindClass(const std::string& className)
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = jenv->FindClass(className.c_str());
if (jenv->ExceptionOccurred() != 0)
{
jenv->ExceptionClear();
throw ErrorToLog("JNI: failed to find class: "+className);
}
return clazz;
}
jmethodID JniTryFindStaticMethod(jclass clazz, const std::string& methodId, const std::string methodSignature)
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jmethodID mid = jenv->GetStaticMethodID(clazz, methodId.c_str(), methodSignature.c_str());
if (jenv->ExceptionOccurred() != 0)
{
jenv->ExceptionClear();
throw ErrorToLog("JNI: failed to find static method: "+methodId+" with signature: "+methodSignature);
}
return mid;
}
void CheckCommonExceptionWithMessage(const std::string& message)
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
if (jenv->ExceptionOccurred() != 0)
{
jenv->ExceptionClear();
throw ErrorToLog(message);
}
}
//Deprecated
void JniCallOpenFile(const std::string& fileName)
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "OpenFile", "(Ljava/lang/String;)I");
jstring jstr = jenv->NewStringUTF(fileName.c_str());
CheckCommonExceptionWithMessage("Fail in JniCallOpenFile\n");
//Any problem? - see console log
if (jenv->CallStaticIntMethod(clazz, mid, jstr) != 1)
throw ErrorToLog("ERROR: Could not open file: " + fileName);
}
std::string JniGetApplicationDir()
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "GetApplicationDir", "()Ljava/lang/String;");
jstring jstr = (jstring)jenv->CallStaticObjectMethod(clazz, mid);
const char *nativeString = jenv->GetStringUTFChars(jstr, 0);
std::string s(nativeString);
jenv->ReleaseStringUTFChars(jstr, nativeString);
return s;
}
void JniLoadSound(const std::string& fileName)
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "LoadSound", "(Ljava/lang/String;)V");
jstring jstr = jenv->NewStringUTF(fileName.c_str());
CheckCommonExceptionWithMessage("Fail in JniLoadSound");
//Any problem? - see console log
jenv->CallStaticVoidMethod(clazz, mid, jstr);
}
void JniPlaySound(const std::string& soundName)
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "PlaySound", "(Ljava/lang/String;)V");
jstring jstr = jenv->NewStringUTF(soundName.c_str());
CheckCommonExceptionWithMessage("Fail in JniPlaySound");
//Any problem? - see console log
jenv->CallStaticVoidMethod(clazz, mid, jstr);
}
void JniPlayMusic(const std::string& fileName)
{
*Console<<"In play music\n";
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "PlayMusic", "(Ljava/lang/String;)V");
jstring jstr = jenv->NewStringUTF(fileName.c_str());
CheckCommonExceptionWithMessage("Fail in JniPlayMusic");
//Any problem? - see console log
jenv->CallStaticVoidMethod(clazz, mid, jstr);
}
void JniPlayMusicLooped(const std::string& fileName)
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "PlayMusicLooped", "(Ljava/lang/String;)V");
jstring jstr = jenv->NewStringUTF(fileName.c_str());
CheckCommonExceptionWithMessage("Fail in JniPlayMusicLooped");
//Any problem? - see console log
jenv->CallStaticVoidMethod(clazz, mid, jstr);
}
void JniStopMusic()
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "StopMusic", "()V");
CheckCommonExceptionWithMessage("Fail in JniPlayMusicLooped");
//Any problem? - see console log
jenv->CallStaticVoidMethod(clazz, mid);
}
void JniReleaseAllSoundsAndMusic()
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "ReleaseAllSoundsAndMusic", "()V");
CheckCommonExceptionWithMessage("Fail in JniReleaseAllSoundsAndMusic");
//Any problem? - see console log
jenv->CallStaticVoidMethod(clazz, mid);
}
void JniPauseAllSoundsAndMusic()
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "PauseAllSoundsAndMusic", "()V");
CheckCommonExceptionWithMessage("Fail in JniPauseAllSoundsAndMusic");
//Any problem? - see console log
jenv->CallStaticVoidMethod(clazz, mid);
}
void JniResumeAllSoundsAndMusic()
{
JNIEnv* jenv;
JavaVirtualMachine->GetEnv((void**)&jenv, JNI_VERSION_1_4);
jclass clazz = JniTryFindClass(CONST_JAVA_FILESYSTEM_CLASS_NAME);
jmethodID mid = JniTryFindStaticMethod(clazz, "ResumeAllSoundsAndMusic", "()V");
CheckCommonExceptionWithMessage("Fail in JniResumeAllSoundsAndMusic");
//Any problem? - see console log
jenv->CallStaticVoidMethod(clazz, mid);
}
void PrintApkContent()
{
zip* APKArchive = zip_open(ApkFilePath.c_str(), 0, NULL);
if (APKArchive == NULL)
{
throw ErrorToLog("Error in loadAPK");
}
//Just for debug, print APK contents
int numFiles = zip_get_num_files(APKArchive);
for (int i=0; i<numFiles; i++)
{
const char* name = zip_get_name(APKArchive, i, 0);
if (name == NULL)
{
*Console<<"Error reading zip file name at index "+tostr(i)+" "+std::string(name);
throw ErrorToLog("Error in loadAPK");
}
*Console<<"Ok reading zip file name at index "+tostr(i)+" "+std::string(name);
}
zip_close(APKArchive);
}
void LoadApk(const std::string& fileName, std::vector<char>& returnData)
{
//PrintApkContent();
if (ApkFilePath == "")
{
throw ErrorToLog("Apk file path is not set!");
}
//*Console<<"ApkFilePath = "+ApkFilePath+"\n";
zip* APKArchive = zip_open(ApkFilePath.c_str(), 0, NULL);
if (APKArchive == NULL)
{
throw ErrorToLog("Error in loadAPK, apk archive is not valid");
}
zip_file *zfile = zip_fopen(APKArchive, ("assets/" + fileName).c_str(), 0);
char buf[4096];
if (zfile != NULL)
{
size_t bytesRead;
do
{
bytesRead = zip_fread(zfile, &buf[0], 4096);
returnData.insert(returnData.end(), buf, buf + bytesRead);
}
while (bytesRead == 4096);
zip_fclose(zfile);
}
else
{
throw ErrorToLog("Error in loadAPK, data in apk archive is broken: "+fileName);
}
zip_close(APKArchive);
}
} //namespace SE
+133
View File
@@ -0,0 +1,133 @@
#include "include/Utils/PngHelper.h"
#include "include/Utils/Utils.h"
namespace SE
{
#ifdef TARGET_ANDROID
typedef PNG_CONST png_byte FAR * png_const_bytep;
#endif
struct TPngReadStruct
{
const boost::shared_array<char>& FileArr;
cardinal FileSize;
int CurrentPointer;
TPngReadStruct(const boost::shared_array<char>& fileArr, cardinal fileSize)
: FileArr(fileArr)
, FileSize(fileSize)
, CurrentPointer(8)
{
}
};
void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
png_voidp a = png_get_io_ptr(png_ptr);
TPngReadStruct* pngReadStruct = reinterpret_cast<TPngReadStruct*>(a);
if (pngReadStruct->CurrentPointer + length >= pngReadStruct->FileSize)
{
throw ErrorToLog("PNG pointer gone too far!");
}
for (cardinal i = 0; i < length; i++)
{
data[i] = pngReadStruct->FileArr[pngReadStruct->CurrentPointer + i];
}
pngReadStruct->CurrentPointer += length;
}
TPngDataStruct read_png_file(const boost::shared_array<char>& fileArr, cardinal fileSize)
{
TPngDataStruct result;
TPngReadStruct readStruct(fileArr, fileSize);
char header[8]; // 8 is the maximum size that can be checked
for (cardinal i=0; i<8; i++)
{
header[i] = fileArr[i];
}
if (png_sig_cmp(reinterpret_cast<png_bytep>(header), 0, 8))
{
throw ErrorToLog("Incorrect PNG file (1)!");
}
/* initialize stuff */
result.PngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!result.PngPtr)
{
throw ErrorToLog("Incorrect PNG file (2)!");
}
result.InfoPtr = png_create_info_struct(result.PngPtr);
if (!result.InfoPtr)
{
throw ErrorToLog("Incorrect PNG file (3)!");
}
if (setjmp(png_jmpbuf(result.PngPtr)))
{
throw ErrorToLog("Incorrect PNG file (4)!");
}
png_set_read_fn(result.PngPtr, (png_voidp)&readStruct, user_read_data);
png_set_sig_bytes(result.PngPtr, 8);
png_read_info(result.PngPtr, result.InfoPtr);
result.Width = png_get_image_width(result.PngPtr, result.InfoPtr);
result.Height = png_get_image_height(result.PngPtr, result.InfoPtr);
result.ColorType = png_get_color_type(result.PngPtr, result.InfoPtr);
result.BitDepth = png_get_bit_depth(result.PngPtr, result.InfoPtr);
if (result.BitDepth != 8)
{
throw ErrorToLog("Incorrect bit depth in png file!");
}
if (result.ColorType != PNG_COLOR_TYPE_RGB && result.ColorType != PNG_COLOR_TYPE_RGBA)
{
throw ErrorToLog("Incorrect color type in png file!");
}
result.NumberOfPasses = png_set_interlace_handling(result.PngPtr);
png_read_update_info(result.PngPtr, result.InfoPtr);
if (setjmp(png_jmpbuf(result.PngPtr)))
{
throw ErrorToLog("Incorrect PNG file (5)!");
}
result.RowPointers = (png_bytep*) malloc(sizeof(png_bytep) * result.Height);
for (int y=0; y<result.Height; y++)
result.RowPointers[y] = (png_byte*) malloc(png_get_rowbytes(result.PngPtr, result.InfoPtr));
png_read_image(result.PngPtr, result.RowPointers);
return result;
}
} //namespace SE
@@ -0,0 +1,153 @@
#include "include/Utils/SerializeInterface/SerializeInterface.h"
#include "include/Engine.h"
#include <boost/algorithm/string.hpp>
namespace SE
{
void ReplaceText(boost::property_tree::ptree& propertyTree, std::map<std::string, std::string>& replaceMap)
{
for (boost::property_tree::ptree::iterator i = propertyTree.begin(); i != propertyTree.end(); )
{
BOOST_FOREACH(auto& itr, replaceMap)
{
std::string param = i->second.get<std::string>("");
if (param != "")
{
boost::algorithm::replace_all(param, itr.first, itr.second);
i->second.put("", param);
}
}
if (i != propertyTree.end())
{
i++;
}
}
}
boost::property_tree::ptree::iterator ReplaceIncludeFile(boost::property_tree::ptree& propertyTree, boost::property_tree::ptree::iterator propertyTreeItr, std::map<std::string, std::string> replaceMap)
{
boost::shared_array<char> xmlFileArr;
cardinal xmlFileSize;
std::string fileName = propertyTreeItr->second.get<std::string>("<xmlattr>.file");
for (boost::property_tree::ptree::iterator i = propertyTreeItr->second.begin(); i != propertyTreeItr->second.end(); ++i)
{
if (i->first == "Param")
{
std::string paramName = i->second.get<std::string>("<xmlattr>.name");
std::string paramValue = i->second.get<std::string>("<xmlattr>.value");
//If value already consist template that can be expanded:
while (replaceMap.count(paramValue) != 0)
{
paramValue = replaceMap[paramValue];
}
replaceMap[paramName] = paramValue;
}
}
xmlFileArr = CreateMemFromFile<char>(ResourceManager->PathToResources + fileName, xmlFileSize);
std::string xmlString = std::string(&xmlFileArr[0], &xmlFileArr[xmlFileSize]);
std::shared_ptr<boost::property_tree::ptree> includeFileTree = StringToPropertyTree(xmlString, replaceMap);
boost::property_tree::ptree::iterator newItr = propertyTree.erase(propertyTreeItr);
propertyTree.insert(newItr, includeFileTree->begin(), includeFileTree->end());
if (newItr == propertyTree.begin())
{
throw ErrorToLog("Property tree logic error - don't know how to handle :(");
}
newItr--;
return newItr;
}
void GetReplaceMapRecoursively(boost::property_tree::ptree& propertyTree, std::map<std::string, std::string> replaceMap)
{
ReplaceText(propertyTree, replaceMap);
for (boost::property_tree::ptree::iterator i = propertyTree.begin(); i != propertyTree.end(); )
{
std::map<std::string, std::string> localMap = replaceMap;
if (i->first == "Include")
{
i = ReplaceIncludeFile(propertyTree, i, localMap);
}
else
{
GetReplaceMapRecoursively(i->second, localMap);
}
BOOST_FOREACH(auto& itr, localMap)
{
if (localMap.count(itr.second) != 0)
{
localMap[itr.first] = localMap[itr.second];
}
}
if (i != propertyTree.end())
{
i++;
}
}
}
std::shared_ptr<boost::property_tree::ptree> StringToPropertyTree(std::string xmlCode, std::map<std::string, std::string> replaceMap)
{
std::shared_ptr<boost::property_tree::ptree> propertyTree(new boost::property_tree::ptree);
std::stringstream stream(xmlCode);
try
{
boost::property_tree::read_xml(stream, *propertyTree);
//Parsing <include/> tag
GetReplaceMapRecoursively(*propertyTree, replaceMap);
}
catch(boost::property_tree::xml_parser_error)
{
throw ErrorToLog("ERROR: xml parse error");
}
return propertyTree;
}
std::shared_ptr<boost::property_tree::ptree> FileToPropertyTree(boost::shared_array<char> xmlFileArr, cardinal xmlFileSize, std::map<std::string, std::string> replaceMap)
{
std::string xmlCode = std::string(&xmlFileArr[0], &xmlFileArr[xmlFileSize]);
return StringToPropertyTree(xmlCode, replaceMap);
}
std::shared_ptr<boost::property_tree::ptree> FileToPropertyTree(const std::string& fileName, std::map<std::string, std::string> replaceMap)
{
cardinal byteCount;
boost::shared_array<char> file = CreateMemFromFile<char>(ResourceManager->PathToResources + fileName, byteCount);
return FileToPropertyTree(file, byteCount, replaceMap);
}
} //namespace SE
+37
View File
@@ -0,0 +1,37 @@
#include "include/Utils/SimpleTimer.h"
namespace SE
{
TSimpleTimer::TSimpleTimer()
: Timer(0)
{
}
void TSimpleTimer::SetTimer(int timer)
{
Timer = timer;
}
void TSimpleTimer::Update(cardinal dt)
{
if (Timer > 0)
{
Timer = max(Timer - static_cast<int>(dt), 0);
}
}
bool TSimpleTimer::IsOver()
{
return Timer == 0;
}
boost::posix_time::time_duration TSimpleTimer::GetPosixTime()
{
return boost::posix_time::seconds(Timer / 1000);
}
} //namespace SE
+36
View File
@@ -0,0 +1,36 @@
#include "include/Engine.h"
namespace SE
{
/*
Here goes all functions that are platform-specific
When I make iOS/Mac/Linux port, I will make same API pair h/cpp
*/
void GetWindowWidthHeight(int& width, int& height)
{
RECT rc;
GetClientRect(Hwnd, &rc);
width = rc.right;
height = rc.bottom;
}
int GetWindowWidth()
{
RECT rc;
GetClientRect(Hwnd, &rc);
return rc.right;
}
int GetWindowHeight()
{
RECT rc;
GetClientRect(Hwnd, &rc);
return rc.bottom;
}
} //namespace SE