@BatVink
That's quite clever, I didn't think of that. I tried the ads in your game and they worked nicely. It's all semantics really as your method caused the web browser to open in front of the game and so the effect was identical. If fact it was more useful as I could immediately start browsing in my favoured web-browser rather than having to click on another button.
@baxslash
I've attached a zip file with my DBP library which I've added some comments to along with a template for the html page for your website. If you have a look, you'll see that it's mostly the same as you and BatVink posted earlier. As I say, I just added a couple of extra functions.
In case you don't want to download it, here is the DBP library:
REM *** Google AdSense Library ***
REMSTART
To use this library, either add it to your project OR add the line:
#INCLUDE "Google_AdSense.dba"
Ads will need to be directed to a web page setup as shown in the other document. This will require them to be
hosted on your website.
You will also need:
- BBB GUI
- Matrix1Utils
REMEND
type AdType
nID as integer
nDBPWindow as integer
nBannerWindow as integer
nBrowserWindow as integer
nX as integer
nY as integer
nWidth as integer
nHeight as integer
sURL as string
sCurrentURL as string
endtype
// Run this at the start of your program before you call any of the other functions in the library
// You need to run START BBB GUI at the same time before any other calls are made
// Remember to add END BBB GUI before your program ends
function InitializeAds()
global dim InAppAds(0) as AdType
empty array InAppAds(0)
endfunction
// Run this to create a new advert
// Point the URL to a page with only a Google Ad on it
function MakeAd(nID,nX,nY,nWidth,nHeight,sURL$)
for c = 0 to array count(InAppAds(0))
if InAppAds(c).nID = nID
report error "Advert " + str$(nID) + " already exists.","AdSense Error"
endif
next c
array insert at bottom InAppAds(0)
nIndex = array count(InAppAds(0))
InAppAds(nIndex).nID = nID
InAppAds(nIndex).nDBPWindow = get dbpro window()
InAppAds(nIndex).nX = nX
InAppAds(nIndex).nY = nY
InAppAds(nIndex).nWidth = nWidth
InAppAds(nIndex).nHeight = nHeight
InAppAds(nIndex).sURL = sURL$
InAppAds(nIndex).sCurrentURL = sURL$
InAppAds(nIndex).nBannerWindow = make window(InAppAds(nIndex).nX,InAppAds(nIndex).nY,InAppAds(nIndex).nWidth,InAppAds(nIndex).nHeight,"",InAppAds(nIndex).nDBPWindow,WS_CHILD)
InAppAds(nIndex).nBrowserWindow = make webbrowser(InAppAds(nIndex).sURL, InAppAds(nIndex).nBannerWindow)
endfunction
// Put this function in your main loop whenever any of the ads are displayed
// When an ad is clicked, it will open to fill the screen
// This function will attempt to pause your game by running it's own main loop so your true main loop effectively freezes
// This works well but can cause problems with timer based movement and so you will have to ensure you deal with these correctly
function HandleAds()
get event
nEvent = event get message()
nHandle = event get handle()
for c = 0 to array count(InAppAds(0))
if nHandle = InAppAds(c).nBannerWindow and nEvent = WM_MOUSEACTIVATE
for d = 0 to array count(InAppAds(0))
if d <> c
HideAd(InAppAds(d).nID)
endif
next d
bOpenedInNew as boolean = 0
window set size InAppAds(c).nBannerWindow,screen width(),screen height() - 20
window set position InAppAds(c).nBannerWindow,0,0
nButtonWindow = make window(0,screen height() - 20,screen width(),20,"",InAppAds(c).nDBPWindow,WS_CHILD)
nExitButton = make button(screen width() - 140,0,140,20,"Return To Game",nButtonWindow)
nOpenInNew = make button(screen width() - 290,0,140,20,"Open In Browser",nButtonWindow)
do
get event
webbrowser get event
nHandle = webbrowser get event handle()
nMessage = webbrowser get event message()
if nHandle = InAppAds(c).nBrowserWindow and nMessage = DISPID_BEFORENAVIGATE2
InAppAds(c).sCurrentURL = webbrowser get event string(c)
endif
if button clicked(nOpenInNew)
if bOpenedInNew = 0
bOpenedInNew = 15
execute file InAppAds(c).sCurrentURL,"",""
endif
else
if bOpenedInNew
bOpenedInNew = 0
endif
endif
if button clicked(nExitButton)
exit
endif
loop
delete window nExitButton
delete window nOpenInNew
delete window nButtonWindow
webbrowser navigate InAppAds(c).nBrowserWindow,InAppAds(c).sURL
window set size InAppAds(c).nBannerWindow,InAppAds(c).nWidth,InAppAds(c).nHeight
window set position InAppAds(c).nBannerWindow,InAppAds(c).nX,InAppAds(c).nY
InAppAds(c).sCurrentURL = InAppAds(c).sURL
for d = 0 to array count(InAppAds(0))
if d <> c
ShowAd(InAppAds(d).nID)
endif
next d
// You can add code to resolve issues with timer based movement here
endif
next c
endfunction
function PositionAd(nID,nX,nY)
for c = 0 to array count(InAppAds(0))
if InAppAds(c).nID = nID
InAppAds(c).nX = nX
InAppAds(c).nY = nY
window set position InAppAds(c).nBannerWindow,InAppAds(c).nX,InAppAds(c).nY
exit
endif
next c
endfunction
function ShowAd(nID)
for c = 0 to array count(InAppAds(0))
if InAppAds(c).nID = nID
//webbrowser refresh InAppAds(c).nBrowserWindow ` Un-comment this at your descretion. I do not refresh the browser for fear of
` generating "artificial clicks" but refreshing it would create more views and
` would show new ads to a customer each time it was shown.
show window InAppAds(c).nBannerWindow
exit
endif
next c
endfunction
function HideAd(nID)
for c = 0 to array count(InAppAds(0))
if InAppAds(c).nID = nID
hide window InAppAds(c).nBannerWindow
exit
endif
next c
endfunction
and here is the html page
<html>
<head>
<style type="text/css">html, body { overflow: hidden; margin: 0px;}</style>
<meta name="description" content="Description of your choice. Perhaps of the game?" />
<meta name="keywords" content="Key words to do with the game and games in general" />
<meta name="author" content="Your name" />
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Your title here</title>
</head>
<body>
<!-- SCRIPT FROM ADSENSE GOES HERE -->
</body>
</html>
Hope some of that is useful. The functions allow you to have multiple ads around the screen which you can hide and show as in baxslash's first example. The ads are managed by the HandleAds() function that should go somewhere in your main loop. It will "pause" your game when it needs to by running its own main loop but this can cause problems with timer based movement so watch out! Also for people not familiar with BBB GUI, it's worth remembering that positioning ads is the same as positioning BBB GUI Windows and so the position is based on the actual screen resolution; not the virtual one created by DBP. Thus DESKTOP WIDTH() and DESKTOP HEIGHT() can be useful for placing them.