Implemented moving/hiding ad from C code, updated admob test project with moving ad
This commit is contained in:
@@ -422,38 +422,54 @@ public class MainActivity extends Activity
|
|||||||
mGLView.requestFocus();
|
mGLView.requestFocus();
|
||||||
};
|
};
|
||||||
|
|
||||||
public void setAdvertisementParams(int visible, int left, int top)
|
public void setAdvertisementPosition(int left, int top)
|
||||||
|
{
|
||||||
|
|
||||||
|
if( _ad.getView() != null )
|
||||||
|
{
|
||||||
|
final FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT);
|
||||||
|
layout.leftMargin = left;
|
||||||
|
layout.topMargin = top;
|
||||||
|
class Callback implements Runnable
|
||||||
|
{
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
_ad.getView().setLayoutParams(layout);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
runOnUiThread(new Callback());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void setAdvertisementVisible(final int visible)
|
||||||
{
|
{
|
||||||
if( _ad.getView() != null )
|
if( _ad.getView() != null )
|
||||||
{
|
{
|
||||||
if( visible == 0 )
|
class Callback implements Runnable
|
||||||
_ad.getView().setVisibility(View.GONE);
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT);
|
public void run()
|
||||||
layout.leftMargin = left;
|
{
|
||||||
layout.topMargin = top;
|
if( visible == 0 )
|
||||||
_ad.getView().setLayoutParams(layout);
|
_ad.getView().setVisibility(View.GONE);
|
||||||
_ad.getView().setVisibility(View.VISIBLE);
|
else
|
||||||
|
_ad.getView().setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
runOnUiThread(new Callback());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getAdvertisementParams(int size[])
|
public void getAdvertisementParams(int params[])
|
||||||
{
|
{
|
||||||
size[0] = 0;
|
for( int i = 0; i < 5; i++ )
|
||||||
size[1] = 0;
|
params[i] = 0;
|
||||||
size[2] = 0;
|
|
||||||
size[3] = 0;
|
|
||||||
size[4] = 0;
|
|
||||||
if( _ad.getView() != null )
|
if( _ad.getView() != null )
|
||||||
{
|
{
|
||||||
size[0] = _ad.getView().getMeasuredWidth();
|
params[0] = (_ad.getView().getVisibility() == View.VISIBLE) ? 1 : 0;
|
||||||
size[1] = _ad.getView().getMeasuredHeight();
|
|
||||||
size[2] = (_ad.getView().getVisibility() == View.VISIBLE) ? 1 : 0;
|
|
||||||
FrameLayout.LayoutParams layout = (FrameLayout.LayoutParams) _ad.getView().getLayoutParams();
|
FrameLayout.LayoutParams layout = (FrameLayout.LayoutParams) _ad.getView().getLayoutParams();
|
||||||
size[3] = layout.leftMargin;
|
params[1] = layout.leftMargin;
|
||||||
size[4] = layout.topMargin;
|
params[2] = layout.topMargin;
|
||||||
|
params[3] = _ad.getView().getMeasuredWidth();
|
||||||
|
params[4] = _ad.getView().getMeasuredHeight();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -561,7 +561,20 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer
|
|||||||
public void exitApp()
|
public void exitApp()
|
||||||
{
|
{
|
||||||
nativeDone();
|
nativeDone();
|
||||||
};
|
}
|
||||||
|
|
||||||
|
public void getAdvertisementParams(int params[])
|
||||||
|
{
|
||||||
|
context.getAdvertisementParams(params);
|
||||||
|
}
|
||||||
|
public void setAdvertisementVisible(int visible)
|
||||||
|
{
|
||||||
|
context.setAdvertisementVisible(visible);
|
||||||
|
}
|
||||||
|
public void setAdvertisementPosition(int left, int top)
|
||||||
|
{
|
||||||
|
context.setAdvertisementPosition(left, top);
|
||||||
|
}
|
||||||
|
|
||||||
private int PowerOf2(int i)
|
private int PowerOf2(int i)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "SDL_image.h"
|
#include "SDL_image.h"
|
||||||
|
#include "SDL_android.h"
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------------------------
|
/*----------------------------------------------------------
|
||||||
@@ -430,9 +431,18 @@ int main(int argc, char* argv[])
|
|||||||
int fps_count = 0;
|
int fps_count = 0;
|
||||||
int fps_start = 0;
|
int fps_start = 0;
|
||||||
float x_speed, y_speed, z_speed;
|
float x_speed, y_speed, z_speed;
|
||||||
|
SDL_Rect adSize;
|
||||||
|
int physicalW = 0, physicalH = 0;
|
||||||
|
int showAd = 0;
|
||||||
|
|
||||||
|
SDL_ANDROID_GetAdvertisementParams(NULL, &adSize);
|
||||||
|
__android_log_print(ANDROID_LOG_INFO, "Ballfield", "Advertisement size %dx%d pos %d:%d", (int)adSize.w, (int)adSize.h, (int)adSize.x, (int)adSize.y);
|
||||||
|
|
||||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
|
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
|
||||||
|
|
||||||
|
physicalW = SDL_GetVideoInfo()->current_w;
|
||||||
|
physicalH = SDL_GetVideoInfo()->current_h;
|
||||||
|
|
||||||
atexit(SDL_Quit);
|
atexit(SDL_Quit);
|
||||||
|
|
||||||
screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, bpp, flags);
|
screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, bpp, flags);
|
||||||
@@ -555,8 +565,22 @@ int main(int argc, char* argv[])
|
|||||||
fps = (float)fps_count * 1000.0 / (tick - fps_start);
|
fps = (float)fps_count * 1000.0 / (tick - fps_start);
|
||||||
fps_count = 0;
|
fps_count = 0;
|
||||||
fps_start = tick;
|
fps_start = tick;
|
||||||
|
showAd++;
|
||||||
|
if(showAd > 3)
|
||||||
|
showAd = 0;
|
||||||
|
SDL_ANDROID_SetAdvertisementVisible(showAd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int adX = abs(x_offs / 100) % (physicalW - adSize.w);
|
||||||
|
int adY = abs(y_offs / 80) % (physicalH - adSize.h);
|
||||||
|
SDL_ANDROID_SetAdvertisementPosition(adX, adY);
|
||||||
|
SDL_Rect adRect;
|
||||||
|
adRect.x = adX * SCREEN_W / physicalW;
|
||||||
|
adRect.w = adSize.w * SCREEN_W / physicalW;
|
||||||
|
adRect.y = adY * SCREEN_H / physicalH;
|
||||||
|
adRect.h = adSize.h * SCREEN_H / physicalH;
|
||||||
|
SDL_FillRect(screen, &adRect, 0xff0);
|
||||||
|
|
||||||
print_num(screen, font, screen->w-37, screen->h-12, fps);
|
print_num(screen, font, screen->w-37, screen->h-12, fps);
|
||||||
++fps_count;
|
++fps_count;
|
||||||
|
|
||||||
|
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.1 KiB |
@@ -45,8 +45,12 @@ extern DECLSPEC int SDLCALL SDL_ANDROID_SetApplicationPutToBackgroundCallback(
|
|||||||
extern DECLSPEC int SDLCALL SDL_ANDROID_PauseAudioPlayback(void);
|
extern DECLSPEC int SDLCALL SDL_ANDROID_PauseAudioPlayback(void);
|
||||||
extern DECLSPEC int SDLCALL SDL_ANDROID_ResumeAudioPlayback(void);
|
extern DECLSPEC int SDLCALL SDL_ANDROID_ResumeAudioPlayback(void);
|
||||||
|
|
||||||
|
/* Get the advertisement size, position and visibility */
|
||||||
extern DECLSPEC int SDLCALL SDL_ANDROID_GetAdvertisementParams(int * visible, SDL_Rect * position);
|
extern DECLSPEC int SDLCALL SDL_ANDROID_GetAdvertisementParams(int * visible, SDL_Rect * position);
|
||||||
extern DECLSPEC int SDLCALL SDL_ANDROID_SetAdvertisementParams(int visible, int left, int top);
|
/* Control the advertisement placement */
|
||||||
|
extern DECLSPEC int SDLCALL SDL_ANDROID_SetAdvertisementVisible(int visible);
|
||||||
|
/* Control the advertisement visibility */
|
||||||
|
extern DECLSPEC int SDLCALL SDL_ANDROID_SetAdvertisementPosition(int left, int top);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,8 @@ static jmethodID JavaSwapBuffers = NULL;
|
|||||||
static jmethodID JavaShowScreenKeyboard = NULL;
|
static jmethodID JavaShowScreenKeyboard = NULL;
|
||||||
static jmethodID JavaToggleScreenKeyboardWithoutTextInput = NULL;
|
static jmethodID JavaToggleScreenKeyboardWithoutTextInput = NULL;
|
||||||
static jmethodID JavaGetAdvertisementParams = NULL;
|
static jmethodID JavaGetAdvertisementParams = NULL;
|
||||||
static jmethodID JavaSetAdvertisementParams = NULL;
|
static jmethodID JavaSetAdvertisementVisible = NULL;
|
||||||
|
static jmethodID JavaSetAdvertisementPosition = NULL;
|
||||||
static int glContextLost = 0;
|
static int glContextLost = 0;
|
||||||
static int showScreenKeyboardDeferred = 0;
|
static int showScreenKeyboardDeferred = 0;
|
||||||
static const char * showScreenKeyboardOldText = "";
|
static const char * showScreenKeyboardOldText = "";
|
||||||
@@ -295,10 +296,9 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeInitJavaCallbacks) ( JNIEnv* env, jobject t
|
|||||||
JavaShowScreenKeyboard = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "showScreenKeyboard", "(Ljava/lang/String;I)V");
|
JavaShowScreenKeyboard = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "showScreenKeyboard", "(Ljava/lang/String;I)V");
|
||||||
JavaToggleScreenKeyboardWithoutTextInput = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "showScreenKeyboardWithoutTextInputField", "()V");
|
JavaToggleScreenKeyboardWithoutTextInput = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "showScreenKeyboardWithoutTextInputField", "()V");
|
||||||
// TODO: implement it
|
// TODO: implement it
|
||||||
/*
|
JavaGetAdvertisementParams = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "getAdvertisementParams", "([I)V");
|
||||||
JavaGetAdvertisementParams = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "GetAdvertisementParams", "()V");
|
JavaSetAdvertisementVisible = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setAdvertisementVisible", "(I)V");
|
||||||
JavaSetAdvertisementParams = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "SetAdvertisementParams", "(III)V");
|
JavaSetAdvertisementPosition = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setAdvertisementPosition", "(II)V");
|
||||||
*/
|
|
||||||
|
|
||||||
ANDROID_InitOSKeymap();
|
ANDROID_InitOSKeymap();
|
||||||
}
|
}
|
||||||
@@ -360,3 +360,35 @@ JAVA_EXPORT_NAME(Settings_nativeSetVideoDepth) (JNIEnv* env, jobject thiz, jint
|
|||||||
SDL_ANDROID_BYTESPERPIXEL = SDL_ANDROID_BITSPERPIXEL / 8;
|
SDL_ANDROID_BYTESPERPIXEL = SDL_ANDROID_BITSPERPIXEL / 8;
|
||||||
SDL_ANDROID_UseGles2 = UseGles2;
|
SDL_ANDROID_UseGles2 = UseGles2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SDLCALL SDL_ANDROID_GetAdvertisementParams(int * visible, SDL_Rect * position)
|
||||||
|
{
|
||||||
|
jint arr[5];
|
||||||
|
jintArray elemArr = (*JavaEnv)->NewIntArray(JavaEnv, 5);
|
||||||
|
if (elemArr == NULL)
|
||||||
|
return 0;
|
||||||
|
(*JavaEnv)->SetIntArrayRegion(JavaEnv, elemArr, 0, 5, arr);
|
||||||
|
(*JavaEnv)->CallVoidMethod(JavaEnv, JavaRenderer, JavaGetAdvertisementParams, elemArr);
|
||||||
|
(*JavaEnv)->GetIntArrayRegion(JavaEnv, elemArr, 0, 5, arr);
|
||||||
|
(*JavaEnv)->DeleteLocalRef(JavaEnv, elemArr);
|
||||||
|
if(visible)
|
||||||
|
*visible = arr[0];
|
||||||
|
if(position)
|
||||||
|
{
|
||||||
|
position->x = arr[1];
|
||||||
|
position->y = arr[2];
|
||||||
|
position->w = arr[3];
|
||||||
|
position->h = arr[4];
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int SDLCALL SDL_ANDROID_SetAdvertisementVisible(int visible)
|
||||||
|
{
|
||||||
|
(*JavaEnv)->CallVoidMethod( JavaEnv, JavaRenderer, JavaSetAdvertisementVisible, (jint)visible );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int SDLCALL SDL_ANDROID_SetAdvertisementPosition(int left, int top)
|
||||||
|
{
|
||||||
|
(*JavaEnv)->CallVoidMethod( JavaEnv, JavaRenderer, JavaSetAdvertisementPosition, (jint)left, (jint)top );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user