File: Main.h

package info (click to toggle)
mygui 3.4.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,792 kB
  • sloc: cpp: 133,849; ansic: 30,249; xml: 15,794; cs: 12,601; tcl: 776; python: 400; makefile: 35; sh: 4
file content (89 lines) | stat: -rw-r--r-- 1,700 bytes parent folder | download
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
/*!
	@file
	@author		Albert Semenov
	@date		08/2008
	@module
*/

#ifndef BASEMAIN_H_
#define BASEMAIN_H_

#include "Precompiled.h"

#if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
	#define WIN32_LEAN_AND_MEAN
	#include <windows.h>
	#include <direct.h>
#endif

#if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
	#define MYGUI_APP(cls) \
		INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT argc) \
		{ \
			return startApp<cls>(); \
		}
#else
	#define MYGUI_APP(cls) \
		int main(int argc, char** argv) \
		{ \
			return startApp<cls>(); \
		}
#endif

#ifdef EMSCRIPTEN
	#include <emscripten.h>

template<class AppClass>
void run(void* arg)
{
	reinterpret_cast<AppClass*>(arg)->run();
}
#endif

template<class AppClass>
int startApp()
{
	try
	{
#if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
		// set working directory to exe location
		LPSTR fileName = new CHAR[256];
		GetModuleFileName(nullptr, fileName, 256);
		std::string_view path = fileName;
		size_t path_directory_index = path.find_last_of('\\');
		std::string exedir{path.substr(0, path_directory_index + 1)};
		_chdir(exedir.c_str());
#endif

		AppClass* app = new AppClass();
		app->prepare();
		if (app->create())
		{
#ifdef EMSCRIPTEN
			emscripten_set_main_loop_arg(run<AppClass>, app, 120, true);
#else
			app->run();
#endif
			app->destroy();
		}
		delete app;
		app = nullptr;
	}
	catch (MyGUI::Exception& _e)
	{
#if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
		MessageBoxA(
			nullptr,
			_e.getFullDescription().c_str(),
			"An exception has occured",
			MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
		std::cerr << "An exception has occured"
				  << " : " << _e.getFullDescription().c_str();
#endif
		throw;
	}
	return 0;
}

#endif