Optimizations

This commit is contained in:
Vladislav Khorev 2013-02-05 20:17:58 +00:00
parent 916a41e064
commit f2dca8749b
15 changed files with 126 additions and 260 deletions

View File

@ -15,7 +15,7 @@ public:
TApplicationInterface(); TApplicationInterface();
virtual ~TApplicationInterface() { } virtual ~TApplicationInterface();
virtual void OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight); virtual void OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight);

View File

@ -40,8 +40,6 @@ public:
TDataTriangleList Data; TDataTriangleList Data;
bool NeedRefreshBuffer;//Dummy for Android, but you need this for PC
virtual void RefreshBuffer() { } //Dummy for Android, but you need this for PC virtual void RefreshBuffer() { } //Dummy for Android, but you need this for PC
TTriangleListAncestor(); TTriangleListAncestor();
@ -78,12 +76,16 @@ public:
class TTriangleList : public TTriangleListAncestor //Implementation differs for Windows and Android class TTriangleList : public TTriangleListAncestor //Implementation differs for Windows and Android
{ {
public: protected:
virtual void InnerRefreshBuffer();
mutable std::map<std::string, std::shared_ptr<VBOObject> > VertBufferArr;
bool NeedPrepareBufferObjects; bool NeedPrepareBufferObjects;
bool NeedRefreshBuffer; //Dummy for Android, but you need this for PC
public:
mutable std::map<std::string, std::shared_ptr<VBOObject>> VertBufferArr;
TTriangleList(); TTriangleList();
~TTriangleList(); ~TTriangleList();

View File

@ -17,28 +17,11 @@ class TApplication : public TApplicationAncestor
{ {
protected: protected:
public: public:
int X, Y, Width, Height; //Window position and size
TApplication() : X(0), Y(0), Width(4), Height(4) { }
virtual void OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight);
virtual void InnerInit() = 0;
//To do on init
virtual void OuterDeinit();
virtual void InnerDeinit() = 0;
//To do on deinit
virtual void InnerDraw() = 0;
//What to draw
virtual void InnerUpdate(cardinal timer) = 0;
//To do on update
}; };
void CreateEngine();
void DestroyEngine();
} //namespace SE } //namespace SE
#include "include/GUIManager/WidgetTemplatesImpl.h" #include "include/GUIManager/WidgetTemplatesImpl.h"

View File

@ -109,12 +109,6 @@ protected:
public: public:
TApplicationAncestor();
virtual void OuterDeinit();
//To do on deinit
virtual void OuterDraw(); virtual void OuterDraw();
//What to draw //What to draw

View File

@ -17,47 +17,11 @@ class TApplication : public TApplicationAncestor
{ {
protected: protected:
public: public:
int X, Y, Width, Height; //Window position and size
TApplication() : X(0), Y(0), Width(4), Height(4) { }
virtual void OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight);
virtual void InnerInit() = 0;
//To do on init
virtual void OuterDeinit();
virtual void InnerDeinit() = 0;
//To do on deinit
//virtual void OuterDraw();
virtual void InnerDraw() = 0;
//What to draw
//virtual void OuterUpdate(cardinal timer);
virtual void InnerUpdate(cardinal timer) = 0;
//To do on update
virtual void UpdateQuick() { };
//To process input - this method is called more frequently than Update()
virtual void OnMouseMove(TMouseState& mouseState) { }
//To do on mouse move (with or without pressed buttons)
virtual void OnMouseDown(TMouseState& mouseState) { }
//To do on mouse up (with or without pressed buttons)
virtual void OnMouseUp(TMouseState& mouseState) { }
//To do on mouse down (with or without pressed buttons)
//Be careful - even when user "upped" button, in mouseState this button appears as pressed
virtual void OnMouseWheel(short int delta) { }
//To do on mouse wheel move
}; };
void CreateEngine();
void DestroyEngine();
} //namespace SE } //namespace SE

View File

@ -25,40 +25,24 @@ extern HDC hDC; //Device context
class TApplication : public TApplicationAncestor class TApplication : public TApplicationAncestor
{ {
protected:
std::string LogFilename; //Log file name
public: public:
int X, Y, Width, Height; //Window position and size
std::string WindowName; //Window name TApplication();
TApplication() : X(0), Y(0), Width(800), Height(600), WindowName("Salmon Engine"), LogFilename("log.txt") { } ~TApplication();
const std::string& GetLogFilename() { return LogFilename; }
virtual void InnerInit() = 0;
//To do on init
virtual void InnerDeinit() = 0;
//To do on deinit
virtual void InnerDraw() = 0;
//What to draw
virtual void InnerUpdate(cardinal timer) = 0;
//To do on update
virtual void UpdateQuick() { }; virtual void UpdateQuick() { };
//To process input - this method is called more frequently than Update() //To process input - this method is called more frequently than Update()
virtual void OnMouseWheel(short int delta) { } virtual void OnMouseWheel(short int delta) { }
//To do on mouse wheel move //To do on mouse wheel move
virtual void OnKeyPress(cardinal key); //Try not to override this. But if you need to override, call ancestor! virtual void OnKeyPress(cardinal key); //Try not to override this. But if you need to override, call ancestor!
}; };
int MainLoop(TApplication& application); bool CreateEngine(int width, int height, int x = 0, int y = 0, std::string windowName = "Salmon Engine App", std::string logFileName = "log.txt");
void MainLoop(TApplication* app);
void DestroyEngine();
} //namespace SE } //namespace SE

View File

@ -9,6 +9,10 @@ TApplicationInterface::TApplicationInterface()
{ {
} }
TApplicationInterface::~TApplicationInterface()
{
}
void TApplicationInterface::OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight) void TApplicationInterface::OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight)
{ {
ResourceManager->MainThreadId = boost::this_thread::get_id(); ResourceManager->MainThreadId = boost::this_thread::get_id();

View File

@ -94,15 +94,6 @@ void TGUIManager::Update(cardinal dt)
(*(i->SignalMap[CONST_HOLD_SIGNAL_NAME]))(TSignalParam(dt)); (*(i->SignalMap[CONST_HOLD_SIGNAL_NAME]))(TSignalParam(dt));
} }
for (TRenderPairList::iterator itr = i->Widget->TriangleListVector.begin(); itr != i->Widget->TriangleListVector.end(); ++itr)
{
if (itr->second.NeedRefreshBuffer)
{
itr->second.RefreshBuffer();
itr->second.NeedRefreshBuffer = false;
}
}
} }
for (TWidgetTrasfromTaskList::iterator i = WidgetTrasfromTaskList.begin(); i != WidgetTrasfromTaskList.end(); ) for (TWidgetTrasfromTaskList::iterator i = WidgetTrasfromTaskList.begin(); i != WidgetTrasfromTaskList.end(); )
@ -225,7 +216,7 @@ void TGUIManager::MoveWidgetByIterator(TWidgetArr::iterator widget, vec2 shift)
*i += vec3(shift, 0); *i += vec3(shift, 0);
} }
itr->second.NeedRefreshBuffer = true; itr->second.RefreshBuffer();
} }
} }

View File

@ -235,7 +235,7 @@ void THalibutExternalAnimObject::RefreshTriangleListVector()
RenderPairIter->second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB][i*6+5] = tex1; RenderPairIter->second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB][i*6+5] = tex1;
} }
RenderPairIter->second.NeedRefreshBuffer = true; RenderPairIter->second.RefreshBuffer();
} }

View File

@ -64,12 +64,10 @@ TDataTriangleList& TDataTriangleList::operator+=(const TDataTriangleList& dataTr
TTriangleListAncestor::TTriangleListAncestor() TTriangleListAncestor::TTriangleListAncestor()
: NeedRefreshBuffer(false)
{ {
} }
TTriangleListAncestor::TTriangleListAncestor(const TTriangleListAncestor& c) TTriangleListAncestor::TTriangleListAncestor(const TTriangleListAncestor& c)
: NeedRefreshBuffer(false)
{ {
Data = c.Data; Data = c.Data;
} }
@ -77,13 +75,11 @@ TTriangleListAncestor::TTriangleListAncestor(const TTriangleListAncestor& c)
TTriangleListAncestor& TTriangleListAncestor::operator=(const TTriangleListAncestor& c) TTriangleListAncestor& TTriangleListAncestor::operator=(const TTriangleListAncestor& c)
{ {
Data = c.Data; Data = c.Data;
NeedRefreshBuffer = c.NeedRefreshBuffer; RefreshBuffer();
return *this; return *this;
} }
TTriangleListAncestor::TTriangleListAncestor(const TDataTriangleList& dataTriangleList) TTriangleListAncestor::TTriangleListAncestor(const TDataTriangleList& dataTriangleList)
: NeedRefreshBuffer(false)
{ {
Data = dataTriangleList; Data = dataTriangleList;
} }
@ -91,7 +87,7 @@ TTriangleListAncestor::TTriangleListAncestor(const TDataTriangleList& dataTriang
TTriangleListAncestor& TTriangleListAncestor::operator=(const TDataTriangleList& dataTriangleList) TTriangleListAncestor& TTriangleListAncestor::operator=(const TDataTriangleList& dataTriangleList)
{ {
Data = dataTriangleList; Data = dataTriangleList;
NeedRefreshBuffer = true; RefreshBuffer();
return *this; return *this;
} }
@ -106,6 +102,7 @@ TTriangleListAncestor::~TTriangleListAncestor()
TTriangleList::TTriangleList() TTriangleList::TTriangleList()
: TTriangleListAncestor() : TTriangleListAncestor()
, NeedPrepareBufferObjects(true) , NeedPrepareBufferObjects(true)
, NeedRefreshBuffer(false)
{ {
} }
@ -113,11 +110,10 @@ TTriangleList::~TTriangleList()
{ {
} }
void TTriangleList::RefreshBuffer() void TTriangleList::InnerRefreshBuffer()
{ {
AssertIfInMainThread(); AssertIfInMainThread();
if (NeedPrepareBufferObjects && Data.Vec2CoordArr.size() > 0 && Data.Vec3CoordArr.size() > 0) if (NeedPrepareBufferObjects && Data.Vec2CoordArr.size() > 0 && Data.Vec3CoordArr.size() > 0)
{ {
@ -161,13 +157,10 @@ void TTriangleList::RefreshBuffer()
glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STATIC_DRAW);
} }
} }
} }
} }
BOOST_FOREACH(auto& i, Data.Vec2CoordArr) BOOST_FOREACH(auto& i, Data.Vec2CoordArr)
{ {
glBindBuffer(GL_ARRAY_BUFFER, VertBufferArr[i.first]->Buffer); glBindBuffer(GL_ARRAY_BUFFER, VertBufferArr[i.first]->Buffer);
@ -180,7 +173,17 @@ void TTriangleList::RefreshBuffer()
RefreshAttribBuffer3fv(i.first, Data.Vec3CoordArr); RefreshAttribBuffer3fv(i.first, Data.Vec3CoordArr);
} }
NeedRefreshBuffer = false;
}
void TTriangleList::RefreshBuffer()
{
if (!NeedRefreshBuffer)
{
NeedRefreshBuffer = true;
PerformInMainThreadAsync(boost::bind(&TTriangleList::InnerRefreshBuffer, this));
}
} }
#endif #endif

View File

@ -36,7 +36,7 @@ bool TSalmonRenderer::BindOpenGLFunctions()
char* glVersion = (char*)glGetString(GL_VERSION); char* glVersion = (char*)glGetString(GL_VERSION);
bool ok = true; bool ok = true;
*Console<<std::string("Render::OpenGL glVersion = ")+glVersion; *Console<<std::string("Render::OpenGL glVersion = ") + glVersion;
//Requires OpenGL 2.0 or above //Requires OpenGL 2.0 or above
if (glVersion[0]>='2') if (glVersion[0]>='2')

View File

@ -5,9 +5,10 @@
namespace SE namespace SE
{ {
void TApplication::OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight)
void CreateEngine()
{ {
OuterDeinit(); DestroyEngine();
Console = new TJavaConsole; Console = new TJavaConsole;
@ -15,30 +16,11 @@ void TApplication::OuterInit(int screenWidth, int screenHeight, float matrixWidt
ResourceManager = new TResourceManager; ResourceManager = new TResourceManager;
ResourceManager->MainThreadId = boost::this_thread::get_id();
Renderer = new TSalmonRendererAndroid; Renderer = new TSalmonRendererAndroid;
ResourceManager->ScriptManager.BindBasicFunctions();
Renderer->InitOpenGL(screenWidth, screenHeight, matrixWidth, matrixHeight);
InnerInit();
CheckGlError();
} }
void DestroyEngine()
void TApplication::OuterDeinit()
{ {
if (Console != NULL)
{
*Console<<"OuterDeinit";
}
InnerDeinit();
if (ResourceManager != NULL) if (ResourceManager != NULL)
{ {
delete ResourceManager; delete ResourceManager;
@ -60,14 +42,11 @@ void TApplication::OuterDeinit()
*Console<<"salmon render deleted"; *Console<<"salmon render deleted";
} }
if (Console != NULL) if (Console != NULL)
{ {
delete Console; delete Console;
Console = NULL; Console = NULL;
} }
} }
} //namespace SE } //namespace SE

View File

@ -61,17 +61,6 @@ TResourceManager::~TResourceManager()
} }
TApplicationAncestor::TApplicationAncestor()
{
}
void TApplicationAncestor::OuterDeinit()
{
InnerDeinit();
}
void TApplicationAncestor::OuterDraw() void TApplicationAncestor::OuterDraw()
{ {
TryUpdateMainThreadId(); TryUpdateMainThreadId();

View File

@ -6,10 +6,9 @@
namespace SE namespace SE
{ {
void TApplication::OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight) void CreateEngine()
{ {
DestroyEngine();
OuterDeinit();
Console = new TIosConsole; Console = new TIosConsole;
@ -17,30 +16,11 @@ void TApplication::OuterInit(int screenWidth, int screenHeight, float matrixWidt
ResourceManager = new TResourceManager; ResourceManager = new TResourceManager;
ResourceManager->MainThreadId = boost::this_thread::get_id();
Renderer = new TSalmonRendererIos; Renderer = new TSalmonRendererIos;
ResourceManager->ScriptManager.BindBasicFunctions();
Renderer->InitOpenGL(screenWidth, screenHeight, matrixWidth, matrixHeight);
InnerInit();
CheckGlError();
} }
void DestroyEngine()
void TApplication::OuterDeinit()
{ {
if (Console != NULL)
{
*Console<<"OuterDeinit";
}
InnerDeinit();
if (ResourceManager != NULL) if (ResourceManager != NULL)
{ {
delete ResourceManager; delete ResourceManager;
@ -62,26 +42,13 @@ void TApplication::OuterDeinit()
*Console<<"salmon render deleted"; *Console<<"salmon render deleted";
} }
if (Console != NULL) if (Console != NULL)
{ {
delete Console; delete Console;
Console = NULL; Console = NULL;
} }
} }
/*
void TApplication::OuterDraw()
{
InnerDraw();
}
void TApplication::OuterUpdate(cardinal timer)
{
InnerUpdate(timer);
}*/
} //namespace SE } //namespace SE

View File

@ -35,6 +35,15 @@ vec2 MouseTotalShift;
bool MouseButtonPressed = false; bool MouseButtonPressed = false;
bool MouseMoved = false; bool MouseMoved = false;
TApplication::TApplication()
{
}
TApplication::~TApplication()
{
}
void TApplication::OnKeyPress(cardinal key) void TApplication::OnKeyPress(cardinal key)
{ {
if (IsConsoleOut) if (IsConsoleOut)
@ -207,7 +216,7 @@ case WM_MOUSEMOVE:
if (MouseButtonPressed) if (MouseButtonPressed)
{ {
vec2 currentMousePos(static_cast<float>(mouseState.X), static_cast<float>(App->Height - mouseState.Y)); vec2 currentMousePos(static_cast<float>(mouseState.X), static_cast<float>(Renderer->GetScreenHeight() - mouseState.Y));
vec2 shift = (MouseButtonPos - currentMousePos); vec2 shift = (MouseButtonPos - currentMousePos);
//shift.v[1] = - shift.v[1]; //shift.v[1] = - shift.v[1];
App->OuterOnMove(shift); App->OuterOnMove(shift);
@ -232,7 +241,7 @@ case WM_MOUSEMOVE:
mouseState.MiddleButtonPressed = (wParam & MK_MBUTTON); mouseState.MiddleButtonPressed = (wParam & MK_MBUTTON);
mouseState.RightButtonPressed = (wParam & MK_RBUTTON); mouseState.RightButtonPressed = (wParam & MK_RBUTTON);
MouseButtonPos = vec2(static_cast<float>(mouseState.X), static_cast<float>(App->Height - mouseState.Y)); MouseButtonPos = vec2(static_cast<float>(mouseState.X), static_cast<float>(Renderer->GetScreenHeight() - mouseState.Y));
if (mouseState.LeftButtonPressed) if (mouseState.LeftButtonPressed)
{ {
@ -259,11 +268,11 @@ case WM_MOUSEMOVE:
if (MouseMoved) if (MouseMoved)
{ {
App->OuterOnTapUpAfterShift(vec2(static_cast<float>(mouseState.X), static_cast<float>(App->Height - mouseState.Y))); App->OuterOnTapUpAfterShift(vec2(static_cast<float>(mouseState.X), static_cast<float>(Renderer->GetScreenHeight() - mouseState.Y)));
} }
else else
{ {
App->OuterOnTapUp(vec2(static_cast<float>(mouseState.X), static_cast<float>(App->Height - mouseState.Y))); App->OuterOnTapUp(vec2(static_cast<float>(mouseState.X), static_cast<float>(Renderer->GetScreenHeight() - mouseState.Y)));
} }
MouseButtonPressed = false; MouseButtonPressed = false;
@ -348,53 +357,77 @@ bool CreateOpenGLWindow(const char* title, int x, int y, int width, int height,H
return true; return true;
} }
int MainLoop(TApplication& application) bool CreateEngine(int width, int height, int x, int y, std::string windowName, std::string logFileName)
{ {
MSG msg;
//Here console log is not created so we can not use ErrorCommon //Here console log is not created so we can not use ErrorCommon
try try
{ {
Console = new TFileConsole(application.GetLogFilename()); Console = new TFileConsole(logFileName);
} }
catch(...) catch(...)
{ {
exit(1); return false;
} }
//Here console log is already created //Here console log is already created
try
{
*Console<<"Log started"; *Console<<"Log started";
if (!CreateOpenGLWindow(application.WindowName.c_str(), application.X, application.Y, application.Width, application.Height,Hwnd,hDC,hRC)) if (!CreateOpenGLWindow(windowName.c_str(), x, y, width, height, Hwnd, hDC, hRC))
{ {
*Console<<"Unable to create OpenGL Window!"; *Console<<"Unable to create OpenGL Window!";
exit(1); return false;
} }
*Console<<"OpenGL Window created"; try
{
if (App != NULL)
throw ErrorCommon();
App = &application;
Renderer = new TSalmonRenderer; Renderer = new TSalmonRenderer;
ResourceManager = new TResourceManager; ResourceManager = new TResourceManager;
int width;
int height;
GetWindowWidthHeight(width, height); return Renderer->BindOpenGLFunctions();
}
catch (...)
{
return false;
}
}
if (Renderer->BindOpenGLFunctions()) void DestroyEngine()
{
try
{ {
App->OuterInit(width, height, static_cast<float>(width), static_cast<float>(height)); *Console<<"Program prepares to quit";
delete ResourceManager;
delete Renderer;
wglMakeCurrent(NULL, NULL);
ReleaseDC(Hwnd, hDC);
wglDeleteContext(hRC);
DestroyWindow(Hwnd);
}
catch(...)
{
//... let the ErrorTypes.h do the buisness
}
delete Console;
}
void MainLoop(TApplication* app)
{
App = app;
MSG msg;
ShowWindow(Hwnd, SW_SHOW); ShowWindow(Hwnd, SW_SHOW);
UpdateWindow(Hwnd); UpdateWindow(Hwnd);
@ -422,33 +455,6 @@ int MainLoop(TApplication& application)
ProcessTickCount(); ProcessTickCount();
} }
}
*Console<<"Program prepares to quit";
App->OuterDeinit();
delete ResourceManager;
delete Renderer;
wglMakeCurrent(NULL, NULL);
ReleaseDC(Hwnd,hDC);
wglDeleteContext(hRC);
DestroyWindow(Hwnd);
}
catch(...)
{
//... let the ErrorTypes.h do the buisness
}
delete Console;
return 0;
} }