Commit f515d8ae authored by Gerhard Stein's avatar Gerhard Stein

Merge branch 'master' of gitlab.com:Dringgstein/Commander-Genius

parents f338e6c5 b5af21cb
# CMake file for development of Commander Genius (taken from OLX)
# This CMake file is used under Linux normally.
cmake_minimum_required(VERSION 3.7)
cmake_minimum_required(VERSION 3.5)
Project(CommanderGenius)
set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")
option(USE_VIRTUALPAD "Enable Onscreen Virtual Gamepad support " yes)
option(USE_VIRTUALPAD "Enable Onscreen Virtual Gamepad support " ON)
option(DISABLE_HOVER "Disables the hover over effect in the menus, useful for touch based devices" OFF)
if(DISABLE_HOVER)
ADD_DEFINITIONS(-DDISABLE_HOVER)
endif()
if(USE_VIRTUALPAD)
ADD_DEFINITIONS(-DVIRTUALPAD)
endif(USE_VIRTUALPAD)
# Use COmpile TIme REducer
include(cotire)
......
# CMake file for development of Commander Genius (taken from OLX)
# This CMake file is used under Linux normally.
cmake_minimum_required(VERSION 3.7)
cmake_minimum_required(VERSION 3.5)
Project(GsKit)
OPTION(USE_SDL2 "SDL2 support" Yes)
option(USE_PYTHON3 "Use Python3 scripts" yes)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
......@@ -79,17 +78,23 @@ endif(USE_SDL2)
# Use python3 for AI scripting and more!
IF(USE_PYTHON3)
# Look for Python (Version 3.0 or later is required)
FIND_PACKAGE(Python3 COMPONENTS Interpreter Development)
if(Python3_FOUND)
ADD_DEFINITIONS(-DUSE_PYTHON3=1)
INCLUDE_DIRECTORIES(${Python3_INCLUDE_DIRS})
LINK_LIBRARIES(${Python3_LIBRARIES})
message(STATUS "Python3_LIBRARIES = ${Python3_LIBRARIES}")
message(STATUS "Python3_INCLUDE_DIRS = ${Python3_INCLUDE_DIRS}")
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
FIND_PACKAGE(PythonLibs REQUIRED)
else()
message(FATAL_ERROR "Python3 not found!")
# Look for Python (Version 3.0 or later is required)
FIND_PACKAGE(Python3 COMPONENTS Interpreter Development)
if(Python3_FOUND)
ADD_DEFINITIONS(-DUSE_PYTHON3=1)
INCLUDE_DIRECTORIES(${Python3_INCLUDE_DIRS})
LINK_LIBRARIES(${Python3_LIBRARIES})
message(STATUS "Python3_LIBRARIES = ${Python3_LIBRARIES}")
message(STATUS "Python3_INCLUDE_DIRS = ${Python3_INCLUDE_DIRS}")
else()
message(FATAL_ERROR "Python3 not found!")
endif()
endif()
ENDIF()
......@@ -102,13 +107,16 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
if(USE_SDL2)
find_package(SDL2 REQUIRED)
if(SDL2_FOUND)
find_package(SDL2_image REQUIRED)
# find_package(SDL2_ttf REQUIRED)
message(STATUS "Using shared SDL Version 2 for Commander Genius")
include_directories(${SDL2_INCLUDE_DIRS})
include_directories(${SDL2_TTF_INCLUDE_DIRS})
include_directories(${SDL2_IMAGE_INCLUDE_DIRS})
endif(SDL2_FOUND)
......@@ -133,6 +141,7 @@ if(USE_SDL2)
Target_link_libraries(GsKit ${SDL2_LIBRARY})
Target_link_libraries(GsKit ${SDL2IMAGE_LIBRARIES})
Target_link_libraries(GsKit ${SDLIMAGE_LIBRARY})
Target_link_libraries(GsKit ${SDL2_TTF_LIBRARY})
else()
Target_link_libraries(GsKit ${SDL_LIBRARY})
Target_link_libraries(GsKit ${SDL_LIBRARIES})
......
......@@ -350,14 +350,14 @@ void CInput::render()
if(!mpVirtPad)
return;
if(!mpVirtPad->active())
return;
if(mVPadConfigState)
{
mpVirtPad->renderConfig();
return;
}
if(!mpVirtPad->active())
return;
GsWeakSurface blit(gVideoDriver.getBlitSurface());
mpVirtPad->render(blit);
......@@ -687,7 +687,6 @@ void CInput::pollEvents()
if(mpVirtPad && mVPadConfigState)
{
mpVirtPad->processConfig();
return;
}
#endif
......@@ -758,15 +757,15 @@ void CInput::pollEvents()
#if SDL_VERSION_ATLEAST(2, 0, 0)
case SDL_FINGERDOWN:
{
Vector2D<int> rotPt(Event.tfinger.x*float(activeArea.w),
Event.tfinger.y*float(activeArea.h));
#ifdef VIRTUALPAD
// If Virtual gamepad takes control...
if(gVideoDriver.VGamePadEnabled() && mpVirtPad &&
mpVirtPad->active() )
{
transMouseRelCoord(Pos, rotPt, activeArea, tiltedScreen);
Vector2D<int> rotPt(Event.tfinger.x*float(activeArea.w),
Event.tfinger.y*float(activeArea.h));
transMouseRelCoord(Pos, rotPt, activeArea, tiltedScreen);
if(!mpVirtPad->mouseFingerState(Pos, Event.tfinger, true))
{
......@@ -778,7 +777,9 @@ void CInput::pollEvents()
else
#endif
{
transMouseRelCoord(Pos, Event.tfinger, activeArea, tiltedScreen);
const Vector2D<int> rotPt(Event.tfinger.x,
Event.tfinger.y);
transMouseRelCoord(Pos, rotPt, activeArea, tiltedScreen);
m_EventList.add( new PointingDevEvent( Pos, PDE_BUTTONDOWN ) );
gPointDevice.mPointingState.mActionButton = 1;
gPointDevice.mPointingState.mPos = Pos;
......@@ -808,7 +809,10 @@ void CInput::pollEvents()
#endif
{
passSDLEventVec = true;
const Vector2D<int> rotPt(Event.tfinger.x, Event.tfinger.y);
Vector2D<int> rotPt(Event.tfinger.x*float(activeArea.w),
Event.tfinger.y*float(activeArea.h));
transMouseRelCoord(Pos, rotPt, activeArea, tiltedScreen);
m_EventList.add( new PointingDevEvent( Pos, PDE_BUTTONUP ) );
gPointDevice.mPointingState.mActionButton = 0;
......@@ -938,11 +942,29 @@ void CInput::pollEvents()
#endif
case SDL_MOUSEMOTION:
const Vector2D<int> rotPt(Event.motion.x, Event.motion.y);
transMouseRelCoord(Pos, Event.motion, activeArea, tiltedScreen);
m_EventList.add( new PointingDevEvent( Pos, PDE_MOVED ) );
gPointDevice.mPointingState.mPos = Pos;
break;
#ifdef VIRTUALPAD
if(gVideoDriver.VGamePadEnabled() && mpVirtPad &&
mpVirtPad->active())
{
if(!mpVirtPad->mouseUp(Pos))
{
passSDLEventVec = true;
m_EventList.add( new PointingDevEvent( Pos, PDE_BUTTONUP ) );
gPointDevice.mPointingState.mActionButton = 0;
gPointDevice.mPointingState.mPos = Pos;
}
}
else
#endif
{
m_EventList.add( new PointingDevEvent( Pos, PDE_MOVED ) );
gPointDevice.mPointingState.mPos = Pos;
break;
}
#endif
}
......
......@@ -305,9 +305,9 @@ public:
* @brief setInVPadConfigState Want to open the vpad configuration?
* Then open this!
*/
void setInVPadConfigState()
void setInVPadConfigState(const bool value)
{
mVPadConfigState = true;
mVPadConfigState = value;
}
#endif
......
......@@ -51,10 +51,10 @@ bool getBooleanArgument( int argc, char *argv[], const std::string& text )
GsApp::GsApp() :
mSink(this)
GsApp::GsApp()
{
gEventManager.regSink(&mSink);
mpSink.reset(new GsAppEventSink(this));
gEventManager.regSink(mpSink);
}
......@@ -70,12 +70,12 @@ mSink(this)
void GsApp::cleanup()
{
gInput.shutdown();
mpSink = nullptr;
//gInput.shutdown();
}
GsApp::~GsApp()
{
gEventManager.unregSink(&mSink);
}
......@@ -231,7 +231,11 @@ void GsApp::setEngine(GsEngine *engPtr)
void GsApp::runMainCycle()
{
// I hope the engine has been set. Otherwise quit the app
assert(mpCurEngine);
if(!mpCurEngine)
{
gLogging << "No Engine set. This should not happen. Please report this the developers";
return ;
}
mpCurEngine->start();
......
......@@ -63,7 +63,7 @@ public:
private:
std::unique_ptr<GsEngine> mpCurEngine;
GsAppEventSink mSink;
std::shared_ptr<GsAppEventSink> mpSink;
};
// It's a simple quit event which will force CG to close the App
......
......@@ -10,8 +10,8 @@
*
*/
#ifndef __GSENGINE_H_
#define __GSENGINE_H_
#ifndef GSENGINE_H
#define GSENGINE_H
#include <base/GsEvent.h>
#include <vector>
......@@ -58,4 +58,4 @@ struct SwitchEngineEvent : CEvent
};
#endif /* __GSENGINE_H_ */
#endif /* GSENGINE_H */
......@@ -16,7 +16,7 @@ void CEventContainer::processSinks()
// We don't need anything from this list anymore
m_EventList.clear();
for( GsEventSink* sink : mSinkPtrList )
for( auto sink : mSinkPtrList )
{
for( auto &event : mPumpEventPtrs )
{
......
......@@ -24,12 +24,12 @@ public:
* Otherwise app might crash.
* @param pSink pointer to the sink to be registered
*/
void regSink(GsEventSink *pSink)
void regSink(std::shared_ptr<GsEventSink> pSink)
{
mSinkPtrList.push_back(pSink);
}
void unregSink(GsEventSink *pSink)
void unregSink(std::shared_ptr<GsEventSink> pSink)
{
mSinkPtrList.remove(pSink);
}
......@@ -80,7 +80,7 @@ public:
void pop_Event() { m_EventList.pop_front(); }
private:
std::list< GsEventSink* > mSinkPtrList;
std::list< std::shared_ptr<GsEventSink> > mSinkPtrList;
std::deque< std::shared_ptr<CEvent> > m_EventList;
std::vector< std::shared_ptr<CEvent> > mPumpEventPtrs;
......
......@@ -55,6 +55,169 @@ void duplicateBackslashes(std::string &text)
#endif
}
GsPython::GsPython()
{
Py_Initialize();
}
GsPython::~GsPython()
{
Py_Finalize();
}
GsPythonModule::~GsPythonModule()
{
Py_XDECREF(mpModule);
}
bool
GsPythonModule::
loadIntegerFunc(const std::string &pyMethodStr,
int &value)
{
// pFunc is a new reference
PyObject *pFunc = PyObject_GetAttrString(mpModule, pyMethodStr.c_str());
if (pFunc && PyCallable_Check(pFunc))
{
PyObject *pValue = PyObject_CallObject(pFunc, nullptr);
if (pValue != nullptr)
{
value = PyLong_AsLong(pValue);
Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
PyErr_Print();
gLogging.ftextOut("Call failed\n");
return false;
}
}
else
{
if (PyErr_Occurred())
{
PyErr_Print();
}
gLogging.ftextOut("Cannot find function \"init\"\n");
return false;
}
Py_XDECREF(pFunc);
return true;
}
bool
GsPythonModule::
loadIntegerFunc(const std::string &pyMethodStr,
int &value,
const int param1)
{
// pFunc is a new reference
PyObject *pFunc = PyObject_GetAttrString(mpModule, pyMethodStr.c_str());
PyObject *pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, PyLong_FromLong(param1));
if (pFunc && PyCallable_Check(pFunc))
{
PyObject *pValue = PyObject_CallObject(pFunc, pArgs);
if (pValue != nullptr)
{
value = PyLong_AsLong(pValue);
Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
PyErr_Print();
gLogging.ftextOut("Call failed\n");
return false;
}
}
else
{
if (PyErr_Occurred())
{
PyErr_Print();
}
gLogging.ftextOut("Cannot find function \"%s\"\n", pyMethodStr.c_str());
return false;
}
Py_XDECREF(pArgs);
Py_XDECREF(pFunc);
return true;
}
bool
GsPythonModule::
loadBooleanFunc(const std::string &pyMethodStr,
bool &value)
{
// pFunc is a new reference
PyObject *pFunc = PyObject_GetAttrString(mpModule, pyMethodStr.c_str());
if (pFunc && PyCallable_Check(pFunc))
{
PyObject *pValue = PyObject_CallObject(pFunc, nullptr);
if (pValue != nullptr)
{
auto isBool = PyBool_Check(pValue);
if(isBool)
{
if(pValue == Py_False)
{
value = false;
}
else if(pValue == Py_True)
{
value = true;
}
}
Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
PyErr_Print();
gLogging.ftextOut("Call failed\n");
return false;
}
}
else
{
if (PyErr_Occurred())
{
PyErr_Print();
}
gLogging.ftextOut("Cannot find function \"init\"\n");
return false;
}
Py_XDECREF(pFunc);
return true;
}
bool
GsPythonModule::
load(const std::string &modName,
......@@ -65,6 +228,10 @@ load(const std::string &modName,
return (mpModule != nullptr) ? true : false;
}
GsPythonFunc::~GsPythonFunc()
{
Py_XDECREF(mpFunction);
}
bool
GsPythonFunc::
......@@ -95,30 +262,24 @@ load(GsPythonModule &module,
}
void
GsPythonFunc::call()
PyObject*
GsPythonFunc::call(PyObject* args)
{
/*PyObject *pValue = */PyObject_CallObject(mpFunction, nullptr);
/*if (pValue != nullptr)
if(!mpFunction)
{
if( PyObject_IsTrue(pValue) )
{
value = true;
}
else
{
value = false;
}
Py_DECREF(pValue);
gLogging.ftextOut("Python function did not get defined.");
return nullptr;
}
else
PyObject *pValue = PyObject_CallObject(mpFunction, args);
if (pValue == nullptr)
{
Py_DECREF(pFunc);
PyErr_Print();
gLogging.ftextOut("Call failed\n");
return false;
}*/
}
return pValue;
}
......@@ -142,7 +303,7 @@ PyObject *GsPython::loadModule(const std::string &scriptBaseName,
replaceSlashes(aidir);
duplicateBackslashes(aidir);
gLogging.ftextOut("calling Py_Initialize().\n", aiscriptPath.c_str() );
gLogging.ftextOut("calling Py_Initialize(): %s.\n", aiscriptPath.c_str() );
#ifdef ANDROID
......@@ -152,10 +313,7 @@ PyObject *GsPython::loadModule(const std::string &scriptBaseName,
const std::string pythonHome = JoinPaths(searchPath,"python3.5");
setenv("PYTHONHOME", pythonHome.c_str(), 1);
setenv("PYTHONPATH", pythonHome.c_str(), 1);
#endif
Py_Initialize();
#endif
PyObject* programName = PyUnicode_FromString(scriptBaseName.c_str());
......
......@@ -9,8 +9,45 @@
#define gPython GsPython::get()
/**
* @brief loadStrFunction Function that takes on integer and returns string
* @param pModule
* @param pyMethodStr
* @param value
* @param param1
* @return
*/
bool loadStrFunction(PyObject * pModule,
const std::string &pyMethodStr,
std::string &value,
const int param1);
bool loadStrFunction(PyObject * pModule,
const std::string &pyMethodStr,
std::string &value);
/**
* @brief loadIntegerGetter for a python function that has no arguments but returns an integer
* @param pModule
* @param pyMethodStr
* @param value
* @return
*/
bool loadIntegerFunc(PyObject * pModule,
const std::string &pyMethodStr,
int &value);
bool loadIntegerFunc(PyObject * pModule,
const std::string &pyMethodStr,
int &value,
const int param1);
bool loadBooleanFunc(PyObject * pModule,
const std::string &pyMethodStr,
bool &value);
class GsPythonModule
{
public:
......@@ -18,10 +55,7 @@ public:
bool load(const std::string &modName,
const std::string &dirPath);
~GsPythonModule()
{
Py_XDECREF(mpModule);
}
virtual ~GsPythonModule();
PyObject *rawPtr() const
{
......@@ -35,6 +69,17 @@ public:
}
bool loadIntegerFunc(const std::string &pyMethodStr,
int &value);
bool loadIntegerFunc(const std::string &pyMethodStr,
int &value,
const int param1);
bool loadBooleanFunc(const std::string &pyMethodStr,
bool &value);
private:
PyObject *mpModule = nullptr;
......@@ -45,6 +90,8 @@ class GsPythonFunc
public:
virtual ~GsPythonFunc();
bool load(GsPythonModule &module,
const std::string &funcName);
......@@ -55,12 +102,7 @@ public:
}
~GsPythonFunc()
{
Py_XDECREF(mpFunction);
}
void call();
PyObject *call(PyObject *args = nullptr);
private:
PyObject *mpFunction = nullptr;
......@@ -72,6 +114,10 @@ class GsPython : public GsSingleton<GsPython>
{
public:
GsPython();
virtual ~GsPython();
/**
* @brief loadModule Load the module and return as python object
* @param scriptBaseName name of script to load
......@@ -91,44 +137,7 @@ public:
void fixNewLine(std::string& str);
/**
* @brief loadStrFunction Function that takes on integer and returns string
* @param pModule
* @param pyMethodStr
* @param value
* @param param1
* @return
*/
bool loadStrFunction(PyObject * pModule,
const std::string &pyMethodStr,
std::string &value,
const int param1);