2023-10-19 16:19:09 +02:00
|
|
|
/*
|
|
|
|
* main.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "mainwindow_moc.h"
|
2024-03-02 23:24:00 +02:00
|
|
|
#include "prepare.h"
|
2024-02-06 00:33:47 +02:00
|
|
|
|
|
|
|
#include "../lib/VCMIDirs.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
|
|
|
// Conan workaround https://github.com/conan-io/conan-center-index/issues/13332
|
|
|
|
#ifdef VCMI_IOS
|
2024-03-02 23:24:00 +02:00
|
|
|
# if __has_include("QIOSIntegrationPlugin.h")
|
|
|
|
# include "QIOSIntegrationPlugin.h"
|
|
|
|
# endif
|
2023-10-19 16:19:09 +02:00
|
|
|
int argcForClient;
|
|
|
|
char ** argvForClient;
|
2024-03-02 23:24:00 +02:00
|
|
|
#elif defined(VCMI_ANDROID)
|
|
|
|
# include <QAndroidJniObject>
|
|
|
|
# include <QtAndroid>
|
|
|
|
#else
|
|
|
|
# include <QMessageBox>
|
|
|
|
# include <QProcess>
|
|
|
|
#endif // VCMI_IOS
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-03-02 23:24:00 +02:00
|
|
|
// android must export main explicitly to make it visible in the shared library
|
|
|
|
#ifdef VCMI_ANDROID
|
|
|
|
# define MAIN_EXPORT ELF_VISIBILITY
|
|
|
|
#else
|
|
|
|
# define MAIN_EXPORT
|
|
|
|
#endif // VCMI_ANDROID
|
|
|
|
|
|
|
|
int MAIN_EXPORT main(int argc, char * argv[])
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
#ifdef VCMI_IOS
|
|
|
|
{
|
2024-04-29 13:00:16 +02:00
|
|
|
#endif
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
2023-10-19 16:19:09 +02:00
|
|
|
#endif
|
|
|
|
QApplication vcmilauncher(argc, argv);
|
|
|
|
|
2024-03-02 23:24:00 +02:00
|
|
|
launcher::prepare();
|
2024-02-06 00:33:47 +02:00
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
MainWindow mainWindow;
|
|
|
|
mainWindow.show();
|
2024-02-06 00:33:47 +02:00
|
|
|
|
2024-06-08 19:54:58 +02:00
|
|
|
#ifdef VCMI_ANDROID
|
|
|
|
// changing language causes window to increase size over the bounds, force it back to proper value
|
|
|
|
// TODO: check in Qt 6 if the hack is still needed
|
|
|
|
auto appWindow = vcmilauncher.focusWindow();
|
|
|
|
auto resizeWindowToScreen = [appWindow]{
|
|
|
|
appWindow->resize(appWindow->screen()->availableSize());
|
|
|
|
};
|
|
|
|
QObject::connect(appWindow, &QWindow::widthChanged, resizeWindowToScreen);
|
|
|
|
QObject::connect(appWindow, &QWindow::heightChanged, resizeWindowToScreen);
|
|
|
|
#endif
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
result = vcmilauncher.exec();
|
|
|
|
#ifdef VCMI_IOS
|
|
|
|
}
|
|
|
|
if (result == 0)
|
|
|
|
launchGame(argcForClient, argvForClient);
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void startGame(const QStringList & args)
|
|
|
|
{
|
|
|
|
logGlobal->warn("Starting game with the arguments: %s", args.join(" ").toStdString());
|
|
|
|
|
2024-03-02 23:24:00 +02:00
|
|
|
#ifdef VCMI_IOS
|
2023-10-19 16:19:09 +02:00
|
|
|
static const char clientName[] = "vcmiclient";
|
|
|
|
argcForClient = args.size() + 1; //first argument is omitted
|
|
|
|
argvForClient = new char*[argcForClient];
|
|
|
|
argvForClient[0] = new char[strlen(clientName)+1];
|
|
|
|
strcpy(argvForClient[0], clientName);
|
|
|
|
for(int i = 1; i < argcForClient; ++i)
|
|
|
|
{
|
2024-03-02 23:24:00 +02:00
|
|
|
std::string s = args.at(i - 1).toStdString();
|
|
|
|
argvForClient[i] = new char[s.size() + 1];
|
|
|
|
strcpy(argvForClient[i], s.c_str());
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
qApp->quit();
|
2024-03-02 23:24:00 +02:00
|
|
|
#elif defined(VCMI_ANDROID)
|
|
|
|
QtAndroid::androidActivity().callMethod<void>("onLaunchGameBtnPressed");
|
2023-10-19 16:19:09 +02:00
|
|
|
#else
|
|
|
|
startExecutable(pathToQString(VCMIDirs::get().clientPath()), args);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void startEditor(const QStringList & args)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_EDITOR
|
|
|
|
startExecutable(pathToQString(VCMIDirs::get().mapEditorPath()), args);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-03-02 23:24:00 +02:00
|
|
|
#ifndef VCMI_MOBILE
|
2023-10-19 16:19:09 +02:00
|
|
|
void startExecutable(QString name, const QStringList & args)
|
|
|
|
{
|
|
|
|
QProcess process;
|
|
|
|
|
|
|
|
// Start the executable
|
|
|
|
if(process.startDetached(name, args))
|
|
|
|
{
|
|
|
|
qApp->quit();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMessageBox::critical(qApp->activeWindow(),
|
2024-03-02 23:24:00 +02:00
|
|
|
QObject::tr("Error starting executable"),
|
|
|
|
QObject::tr("Failed to start %1\nReason: %2").arg(name, process.errorString())
|
|
|
|
);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|