diff --git a/include/ApplicationInterface.h b/include/ApplicationInterface.h index 1e8737d..d14a39a 100644 --- a/include/ApplicationInterface.h +++ b/include/ApplicationInterface.h @@ -15,7 +15,7 @@ public: TApplicationInterface(); - virtual ~TApplicationInterface() { } + virtual ~TApplicationInterface(); virtual void OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight); diff --git a/include/Render/RenderMisc.h b/include/Render/RenderMisc.h index cdecca5..31b16d2 100644 --- a/include/Render/RenderMisc.h +++ b/include/Render/RenderMisc.h @@ -40,8 +40,6 @@ public: 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 TTriangleListAncestor(); @@ -78,12 +76,16 @@ public: class TTriangleList : public TTriangleListAncestor //Implementation differs for Windows and Android { -public: - - mutable std::map > VertBufferArr; +protected: + virtual void InnerRefreshBuffer(); bool NeedPrepareBufferObjects; + bool NeedRefreshBuffer; //Dummy for Android, but you need this for PC +public: + + mutable std::map> VertBufferArr; + TTriangleList(); ~TTriangleList(); diff --git a/include/SalmonEngineAndroid.h b/include/SalmonEngineAndroid.h index 3ea232f..3677d7f 100644 --- a/include/SalmonEngineAndroid.h +++ b/include/SalmonEngineAndroid.h @@ -17,28 +17,11 @@ class TApplication : public TApplicationAncestor { protected: 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 #include "include/GUIManager/WidgetTemplatesImpl.h" diff --git a/include/SalmonEngineInterface.h b/include/SalmonEngineInterface.h index af33af0..5ff79d7 100644 --- a/include/SalmonEngineInterface.h +++ b/include/SalmonEngineInterface.h @@ -109,12 +109,6 @@ protected: public: - TApplicationAncestor(); - - - virtual void OuterDeinit(); - //To do on deinit - virtual void OuterDraw(); //What to draw diff --git a/include/SalmonEngineIos.h b/include/SalmonEngineIos.h index eace46d..b779e45 100644 --- a/include/SalmonEngineIos.h +++ b/include/SalmonEngineIos.h @@ -17,47 +17,11 @@ class TApplication : public TApplicationAncestor { protected: 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 diff --git a/include/SalmonEngineWindows.h b/include/SalmonEngineWindows.h index cec4f2d..592d667 100644 --- a/include/SalmonEngineWindows.h +++ b/include/SalmonEngineWindows.h @@ -25,40 +25,24 @@ extern HDC hDC; //Device context class TApplication : public TApplicationAncestor { -protected: - std::string LogFilename; //Log file name 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") { } - - 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 + ~TApplication(); virtual void UpdateQuick() { }; //To process input - this method is called more frequently than Update() - virtual void OnMouseWheel(short int delta) { } //To do on mouse wheel move 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 diff --git a/src/ApplicationInterface.cpp b/src/ApplicationInterface.cpp index 1cea9b9..78c6dbb 100644 --- a/src/ApplicationInterface.cpp +++ b/src/ApplicationInterface.cpp @@ -9,6 +9,10 @@ TApplicationInterface::TApplicationInterface() { } +TApplicationInterface::~TApplicationInterface() +{ +} + void TApplicationInterface::OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight) { ResourceManager->MainThreadId = boost::this_thread::get_id(); diff --git a/src/GUIManager/GUIManager.cpp b/src/GUIManager/GUIManager.cpp index 06e94c8..49cb076 100644 --- a/src/GUIManager/GUIManager.cpp +++ b/src/GUIManager/GUIManager.cpp @@ -94,15 +94,6 @@ void TGUIManager::Update(cardinal 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(); ) @@ -225,7 +216,7 @@ void TGUIManager::MoveWidgetByIterator(TWidgetArr::iterator widget, vec2 shift) *i += vec3(shift, 0); } - itr->second.NeedRefreshBuffer = true; + itr->second.RefreshBuffer(); } } diff --git a/src/HalibutAnimation/HalibutAnimation.cpp b/src/HalibutAnimation/HalibutAnimation.cpp index b29cef4..e31d31b 100644 --- a/src/HalibutAnimation/HalibutAnimation.cpp +++ b/src/HalibutAnimation/HalibutAnimation.cpp @@ -235,7 +235,7 @@ void THalibutExternalAnimObject::RefreshTriangleListVector() RenderPairIter->second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB][i*6+5] = tex1; } - RenderPairIter->second.NeedRefreshBuffer = true; + RenderPairIter->second.RefreshBuffer(); } diff --git a/src/Render/RenderMisc.cpp b/src/Render/RenderMisc.cpp index 9157da3..1646d57 100644 --- a/src/Render/RenderMisc.cpp +++ b/src/Render/RenderMisc.cpp @@ -64,12 +64,10 @@ TDataTriangleList& TDataTriangleList::operator+=(const TDataTriangleList& dataTr TTriangleListAncestor::TTriangleListAncestor() - : NeedRefreshBuffer(false) { } TTriangleListAncestor::TTriangleListAncestor(const TTriangleListAncestor& c) - : NeedRefreshBuffer(false) { Data = c.Data; } @@ -77,13 +75,11 @@ TTriangleListAncestor::TTriangleListAncestor(const TTriangleListAncestor& c) TTriangleListAncestor& TTriangleListAncestor::operator=(const TTriangleListAncestor& c) { Data = c.Data; - NeedRefreshBuffer = c.NeedRefreshBuffer; - + RefreshBuffer(); return *this; } TTriangleListAncestor::TTriangleListAncestor(const TDataTriangleList& dataTriangleList) - : NeedRefreshBuffer(false) { Data = dataTriangleList; } @@ -91,7 +87,7 @@ TTriangleListAncestor::TTriangleListAncestor(const TDataTriangleList& dataTriang TTriangleListAncestor& TTriangleListAncestor::operator=(const TDataTriangleList& dataTriangleList) { Data = dataTriangleList; - NeedRefreshBuffer = true; + RefreshBuffer(); return *this; } @@ -106,6 +102,7 @@ TTriangleListAncestor::~TTriangleListAncestor() TTriangleList::TTriangleList() : TTriangleListAncestor() , NeedPrepareBufferObjects(true) + , NeedRefreshBuffer(false) { } @@ -113,11 +110,10 @@ TTriangleList::~TTriangleList() { } -void TTriangleList::RefreshBuffer() +void TTriangleList::InnerRefreshBuffer() { AssertIfInMainThread(); - 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); } } - - } } - BOOST_FOREACH(auto& i, Data.Vec2CoordArr) { glBindBuffer(GL_ARRAY_BUFFER, VertBufferArr[i.first]->Buffer); @@ -180,7 +173,17 @@ void TTriangleList::RefreshBuffer() RefreshAttribBuffer3fv(i.first, Data.Vec3CoordArr); } + NeedRefreshBuffer = false; +} + +void TTriangleList::RefreshBuffer() +{ + if (!NeedRefreshBuffer) + { + NeedRefreshBuffer = true; + PerformInMainThreadAsync(boost::bind(&TTriangleList::InnerRefreshBuffer, this)); + } } #endif diff --git a/src/Render/SalmonRender/SalmonRenderWindows.cpp b/src/Render/SalmonRender/SalmonRenderWindows.cpp index 8b8ab9b..1f13f83 100644 --- a/src/Render/SalmonRender/SalmonRenderWindows.cpp +++ b/src/Render/SalmonRender/SalmonRenderWindows.cpp @@ -36,7 +36,7 @@ bool TSalmonRenderer::BindOpenGLFunctions() char* glVersion = (char*)glGetString(GL_VERSION); bool ok = true; - *Console<MainThreadId = boost::this_thread::get_id(); - Renderer = new TSalmonRendererAndroid; - - ResourceManager->ScriptManager.BindBasicFunctions(); - - Renderer->InitOpenGL(screenWidth, screenHeight, matrixWidth, matrixHeight); - - InnerInit(); - - CheckGlError(); } - -void TApplication::OuterDeinit() +void DestroyEngine() { - - if (Console != NULL) - { - *Console<<"OuterDeinit"; - } - - InnerDeinit(); - if (ResourceManager != NULL) { delete ResourceManager; @@ -47,7 +29,7 @@ void TApplication::OuterDeinit() if (Console != NULL) { - *Console<<"Resource manager deleted, deleting salmon render"; + *Console<<"Resource manager deleted, deleting salmon render"; } if (Renderer != NULL) @@ -57,17 +39,14 @@ void TApplication::OuterDeinit() } if (Console != NULL) { - *Console<<"salmon render deleted"; + *Console<<"salmon render deleted"; } - - - + if (Console != NULL) { delete Console; Console = NULL; } - } } //namespace SE \ No newline at end of file diff --git a/src/SalmonEngineInterface.cpp b/src/SalmonEngineInterface.cpp index f9d1540..af68424 100644 --- a/src/SalmonEngineInterface.cpp +++ b/src/SalmonEngineInterface.cpp @@ -61,17 +61,6 @@ TResourceManager::~TResourceManager() } -TApplicationAncestor::TApplicationAncestor() -{ - -} - -void TApplicationAncestor::OuterDeinit() -{ - InnerDeinit(); -} - - void TApplicationAncestor::OuterDraw() { TryUpdateMainThreadId(); diff --git a/src/SalmonEngineIos.cpp b/src/SalmonEngineIos.cpp index 1d5f26c..d9982ad 100644 --- a/src/SalmonEngineIos.cpp +++ b/src/SalmonEngineIos.cpp @@ -6,41 +6,21 @@ namespace SE { -void TApplication::OuterInit(int screenWidth, int screenHeight, float matrixWidth, float matrixHeight) +void CreateEngine() { - - OuterDeinit(); - - Console = new TIosConsole; + DestroyEngine(); + + Console = new TIosConsole; *Console<MainThreadId = boost::this_thread::get_id(); - - Renderer = new TSalmonRendererIos; - - ResourceManager->ScriptManager.BindBasicFunctions(); - - Renderer->InitOpenGL(screenWidth, screenHeight, matrixWidth, matrixHeight); - - InnerInit(); - CheckGlError(); + Renderer = new TSalmonRendererIos; } - -void TApplication::OuterDeinit() +void DestroyEngine() { - - if (Console != NULL) - { - *Console<<"OuterDeinit"; - } - - InnerDeinit(); - if (ResourceManager != NULL) { delete ResourceManager; @@ -49,7 +29,7 @@ void TApplication::OuterDeinit() if (Console != NULL) { - *Console<<"Resource manager deleted, deleting salmon render"; + *Console<<"Resource manager deleted, deleting salmon render"; } if (Renderer != NULL) @@ -59,29 +39,16 @@ void TApplication::OuterDeinit() } if (Console != NULL) { - *Console<<"salmon render deleted"; + *Console<<"salmon render deleted"; } - - - + if (Console != NULL) { delete Console; Console = NULL; } - } -/* -void TApplication::OuterDraw() -{ - InnerDraw(); -} - -void TApplication::OuterUpdate(cardinal timer) -{ - InnerUpdate(timer); -}*/ } //namespace SE \ No newline at end of file diff --git a/src/SalmonEngineWindows.cpp b/src/SalmonEngineWindows.cpp index be3e545..6a653d8 100644 --- a/src/SalmonEngineWindows.cpp +++ b/src/SalmonEngineWindows.cpp @@ -35,6 +35,15 @@ vec2 MouseTotalShift; bool MouseButtonPressed = false; bool MouseMoved = false; + +TApplication::TApplication() +{ +} + +TApplication::~TApplication() +{ +} + void TApplication::OnKeyPress(cardinal key) { if (IsConsoleOut) @@ -207,7 +216,7 @@ case WM_MOUSEMOVE: if (MouseButtonPressed) { - vec2 currentMousePos(static_cast(mouseState.X), static_cast(App->Height - mouseState.Y)); + vec2 currentMousePos(static_cast(mouseState.X), static_cast(Renderer->GetScreenHeight() - mouseState.Y)); vec2 shift = (MouseButtonPos - currentMousePos); //shift.v[1] = - shift.v[1]; App->OuterOnMove(shift); @@ -232,7 +241,7 @@ case WM_MOUSEMOVE: mouseState.MiddleButtonPressed = (wParam & MK_MBUTTON); mouseState.RightButtonPressed = (wParam & MK_RBUTTON); - MouseButtonPos = vec2(static_cast(mouseState.X), static_cast(App->Height - mouseState.Y)); + MouseButtonPos = vec2(static_cast(mouseState.X), static_cast(Renderer->GetScreenHeight() - mouseState.Y)); if (mouseState.LeftButtonPressed) { @@ -259,11 +268,11 @@ case WM_MOUSEMOVE: if (MouseMoved) { - App->OuterOnTapUpAfterShift(vec2(static_cast(mouseState.X), static_cast(App->Height - mouseState.Y))); + App->OuterOnTapUpAfterShift(vec2(static_cast(mouseState.X), static_cast(Renderer->GetScreenHeight() - mouseState.Y))); } else { - App->OuterOnTapUp(vec2(static_cast(mouseState.X), static_cast(App->Height - mouseState.Y))); + App->OuterOnTapUp(vec2(static_cast(mouseState.X), static_cast(Renderer->GetScreenHeight() - mouseState.Y))); } MouseButtonPressed = false; @@ -346,95 +355,60 @@ bool CreateOpenGLWindow(const char* title, int x, int y, int width, int height,H wglMakeCurrent( hDC, hRC ); 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 try { - Console = new TFileConsole(application.GetLogFilename()); + Console = new TFileConsole(logFileName); } catch(...) { - exit(1); + return false; } //Here console log is already created - try - { *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!"; - exit(1); + return false; } - *Console<<"OpenGL Window created"; - - - if (App != NULL) - throw ErrorCommon(); - - App = &application; + try + { Renderer = new TSalmonRenderer; ResourceManager = new TResourceManager; - int width; - int height; - GetWindowWidthHeight(width, height); + return Renderer->BindOpenGLFunctions(); + } + catch (...) + { + return false; + } +} - if (Renderer->BindOpenGLFunctions()) - { - - App->OuterInit(width, height, static_cast(width), static_cast(height)); - - ShowWindow(Hwnd, SW_SHOW); - UpdateWindow(Hwnd); - - NewTickCount = GetTickCount(); - LastTickCount = NewTickCount; - - bool StayIn = true; - - while (StayIn) - { - while(PeekMessage(&msg, Hwnd, 0, 0, PM_NOREMOVE)) - { - if(GetMessage(&msg, Hwnd, 0, 0)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - else - { - StayIn = false; - } - } - DrawScene(); - - ProcessTickCount(); - } - - } +void DestroyEngine() +{ + try + { *Console<<"Program prepares to quit"; - App->OuterDeinit(); - delete ResourceManager; delete Renderer; wglMakeCurrent(NULL, NULL); - ReleaseDC(Hwnd,hDC); + ReleaseDC(Hwnd, hDC); wglDeleteContext(hRC); DestroyWindow(Hwnd); @@ -447,8 +421,40 @@ int MainLoop(TApplication& application) delete Console; - - return 0; +} + +void MainLoop(TApplication* app) +{ + App = app; + + MSG msg; + + ShowWindow(Hwnd, SW_SHOW); + UpdateWindow(Hwnd); + + NewTickCount = GetTickCount(); + LastTickCount = NewTickCount; + + bool StayIn = true; + + while (StayIn) + { + while(PeekMessage(&msg, Hwnd, 0, 0, PM_NOREMOVE)) + { + if(GetMessage(&msg, Hwnd, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + else + { + StayIn = false; + } + } + DrawScene(); + + ProcessTickCount(); + } }