Good day to all!
There was a need to embed advertising in your game on Love2D. I decided to show the banner after the player won, and then there were difficulties: the banner is called by the Java code of the application, and the win is determined in the Lua code. It binds their C code, tutorials, how to add your methods to the engine was not, and I had to dig into the code myself. The Love2D repository for Android is right here .
I decided to start with the study of the method love.system.vibrate()
- the method that appears when using Love2D on android, which means it is added somewhere as well, as I want to add an advertisement.
If you open the GameActivity class, then you can find the vibrate method we are looking for, and here it’s worth explaining how the Java code is called from Lua.
When the application is just launched, for each Love2D module, an instance of the module class and an instance of the bundle class (the object that binds the Sishny object to the Lua variable) is created. The bundle object for each method that needs to be integrated into Lua has its own small method and a list indicating which method in Lua is associated with a sishna bundle. It looks like this:
// static const luaL_Reg functions[] = { { "getOS", w_getOS }, { "getProcessorCount", w_getProcessorCount }, { "setClipboardText", w_setClipboardText }, { "getClipboardText", w_getClipboardText }, { "getPowerInfo", w_getPowerInfo }, { "openURL", w_openURL }, { "vibrate", w_vibrate }, { 0, 0 } }; extern "C" int luaopen_love_system(lua_State *L) { // System *instance = instance(); if (instance == nullptr) { instance = new love::system::sdl::System(); } else instance->retain(); // WrappedModule w; w.module = instance; w.name = "system"; w.type = MODULE_ID; w.functions = functions; w.types = nullptr; return luax_register_module(L, w); }
Also note that the additional methods that are added to the android are stored in a separate class, which lies in the ./jni/love/src/common/
folder and is called android
.
First, create a static method in the GameActivity class:
private static GameActivity instance; @Override protected void onCreate(Bundle savedInstanceState) { instance = this; // ... } // ... // , , // public static void showAd() { Toast.makeText(instance, "Ad example", Toast.LENGTH_LONG).show(); }
Now create a method in the love.system
module. I chose this module because I’m implementing advertising, but it’s not necessary to use the system. You can even create your own module, depending on what you need.
First you need to write the main part of the method in the android
class. We declare:
// ./jni/love/src/common/android.h bool openURL(const std::string &url); void showAd(); void vibrate(double seconds);
And create:
// ./jni/love/src/common/android.cpp void showAd() { // Java JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv(); // , jclass activity = env->FindClass("org/love2d/android/GameActivity"); // jmethodID show_ad_method = env->GetStaticMethodID(activity, "showAd", "()V"); // env->CallStaticVoidMethod(activity, show_ad_method); env->DeleteLocalRef(activity); }
When the method identifier is received by the third parameter, we indicate what the method returns and what parameters we accept. If your method will take arguments, then you need to enumerate them in brackets. If your method returns a value, then you will also need to change V
to the corresponding letter. So, to describe this method:
bool isGreater(double a, double b) { return a > b; }
The following line will be used: (DD)Z
Read more here .
Add the showAd
method to the module itself:
// ./jni/love/src/modules/system/System.h /** * Shows ad */ virtual void showAd() const;
Well, the code itself:
// ./jni/love/src/modules/system/System.cpp void System::showAd() const { #ifdef LOVE_ANDROID love::android::showAd(); #endif }
Now it remains to combine all this with a bundle. Add the appropriate method to the bundle class:
int w_showAd(lua_State *L) { instance()->showAd(); return 0; } // static const luaL_Reg functions[] = { { "getOS", w_getOS }, { "getProcessorCount", w_getProcessorCount }, { "setClipboardText", w_setClipboardText }, { "getClipboardText", w_getClipboardText }, { "getPowerInfo", w_getPowerInfo }, { "openURL", w_openURL }, { "vibrate", w_vibrate }, { "showAd", w_showAd }, { 0, 0 } };
We collect all this by tutorial from the very beginning.
Add the code to the love.system.showAd()
) and check:
Voila
With the help of such manipulations, you can embed advertising, control the LED, the camera and generally do a lot of useful things with the help of the game, which sometimes is very useful for interesting game ideas.
Thanks for reading (:
Source: https://habr.com/ru/post/301330/
All Articles