Skip to content

Commit 90cf5a7

Browse files
committed
First commit of Defold extension and docs
1 parent 7a996b4 commit 90cf5a7

17 files changed

+1504
-2
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/.internal
2+
/build
3+
.externalToolBuilders
4+
.DS_Store
5+
Thumbs.db
6+
.lock-wscript
7+
*.pyc
8+
.project
9+
.cproject
10+
builtins

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Gamedistribution.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,105 @@
1-
# gd-defold
2-
GameDistribution Defold SDK
1+
# gd-sdk-defold
2+
The repository containing the GameDistribution SDK for Defold games. This allows you to display advertisements and other solutions available to games published within the GameDistribution network.
3+
4+
5+
## Registering as a developer
6+
Before you can start using GameDistribution you need to register as a developer:
7+
8+
https://developer.gamedistribution.com/register/developer/
9+
10+
When you have registered as a developer you also need to register your game in the GameDistribution developer portal:
11+
12+
https://developer.gamedistribution.com/games
13+
14+
When you have registered the game head to the Upload tab and copy the Game ID:
15+
16+
![](/docs/gameid.png)
17+
18+
19+
## Installation
20+
You can integrate GameDistribution in both new games and in existing games. Please follow the instructions below depending on your situation.
21+
22+
23+
### Creating a new game
24+
If you are creating a new game it is recommended that you use the GameDistribution project template from the Defold editor Welcome screen. The GameDistribution template includes the [GameDistribution SDK extension](https://github.com/GameDistribution/gd-defold):
25+
26+
![GameDistribution template](/docs/gamedistribution-template.png)
27+
28+
Add your Game ID to the GameDistribution section of the **game.project** file:
29+
30+
![Adding game id game.project](/docs/adding-gameid.png)
31+
32+
33+
### Configuring an existing game
34+
If you wish to use GameDistribution with an existing game you can open the **game.project** file and in the [Dependencies field in the Project section](https://defold.com/manuals/project-settings/#dependencies) add:
35+
36+
```
37+
https://github.com/GameDistribution/gd-defold/archive/master.zip
38+
```
39+
40+
Open the **game.project** file using a text editor and add a new section with your Game ID:
41+
42+
```
43+
[gamedistribution]
44+
game_id = ADD YOUR GAME ID HERE
45+
```
46+
47+
48+
## Usage
49+
To use the GameDistribution SDK in a Defold game you need to first set up a listener function to receive events from the GameDistribution SDK. The listener function will receive events when an ad is shown and the game should be paused and when an ad is closed and the game should be resumed.
50+
51+
```Lua
52+
gdsdk.set_listener(function(self, event, message)
53+
print(event, message)
54+
if event == gdsdk.SDK_GAME_PAUSE then
55+
-- pause your game
56+
elseif event == gdsdk.SDK_GAME_START then
57+
-- resume your game
58+
end
59+
end)
60+
```
61+
62+
Once you have the listener functions set up you can start showing ads. The GameDistribution SDK supports Rewarded Ads, Interstitial Ads and Display Ads.
63+
64+
65+
### Showing a Rewarded ad
66+
Use [Rewarded Ads](https://blog.gamedistribution.com/rewarded-ads-are-here/) to give the user premium content, extra lives, in-game currency and so on. You show a Rewarded Ad like this:
67+
68+
```
69+
gdsdk.show_rewarded_ad()
70+
```
71+
72+
73+
### Showing an Interstitial Ad
74+
Use Interstitial Ads during screen transitions, between sessions and in other situations where there is a natural pause in game play. You show an Interstitial Ad like this:
75+
76+
```
77+
gdsdk.show_interstitial_ad()
78+
```
79+
80+
81+
### Showing a Display Ad
82+
Use Display Ads (also known as a banner ad) at your discretion but make sure to not cover any of your game content. You need to manually add a `<div>` where the Display Ad will be shown. Example:
83+
84+
```html
85+
<!-- center and anchor to bottom of page -->
86+
<div style="position: absolute; bottom: 0px; left: 50%;">
87+
<div id="canvas-ad" style="width: 728px; height:90px; margin-left: -50%;"/>
88+
</div>
89+
```
90+
91+
Give the `<div>` a size that matches one of the [supported display ad sizes](https://github.com/GameDistribution/GD-HTML5/wiki/Display-Ads).
92+
93+
You show and hide a Display Ad like this:
94+
95+
```
96+
-- show it
97+
gdsdk.show_display_ad("canvas-ad")
98+
99+
-- hide it
100+
gdsdk.hide_display_ad("canvas-ad")
101+
```
102+
103+
104+
## Verify the SDK implementation
105+
To verify your SDK implementation; make sure to completely view an advertisement, while viewing your game through an iframe launched from the Upload tab of the GameDistribution developer portal page for you game.

docs/adding-gameid.png

8.95 KB
Loading

docs/gameid.png

65.1 KB
Loading

example/engine_template.html

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, minimal-ui, shrink-to-fit=no">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<!-- The above 4 meta tags *must* come first in the head; any other head content must come *after* these tags -->
9+
10+
<title>{{project.title}} {{project.version}}</title>
11+
<style type='text/css'>
12+
/* Disable user selection to avoid strange bug in Chrome on Windows:
13+
* Selecting a text outside the canvas, then clicking+draging would
14+
* drag the selected text but block mouse down/up events to the engine.
15+
*/
16+
body {
17+
{{^DEFOLD_SCALE_MODE_IS_NO_SCALE}}
18+
position: fixed; /* Prevent overscroll */
19+
{{/DEFOLD_SCALE_MODE_IS_NO_SCALE}}
20+
margin:0;
21+
padding:0;
22+
}
23+
24+
.canvas-app-container {
25+
width: 100%;
26+
height: 100%;
27+
position: absolute;
28+
align-items: center;
29+
justify-content: center;
30+
overflow: hidden;
31+
}
32+
33+
.canvas-app-container:-webkit-full-screen {
34+
/* Auto width and height in Safari/Chrome fullscreen. */
35+
width: auto;
36+
height: auto;
37+
}
38+
39+
#canvas {
40+
outline: none;
41+
border: 0;
42+
width: 100%;
43+
vertical-align: bottom;
44+
}
45+
46+
#canvas-container {
47+
position: relative;
48+
}
49+
50+
canvas:focus, canvas:active {
51+
outline: none;
52+
border: 0;
53+
ie-dummy: expression(this.hideFocus=true);
54+
-moz-outline-style: none;
55+
}
56+
57+
div {
58+
-webkit-tap-highlight-color: rgba(0,0,0,0);
59+
-webkit-touch-callout: none;
60+
-webkit-user-select: none;
61+
-khtml-user-select: none;
62+
-moz-user-select: none;
63+
-ms-user-select: none;
64+
user-select: none;
65+
}
66+
67+
{{{DEFOLD_CUSTOM_CSS_INLINE}}}
68+
</style>
69+
</head>
70+
71+
<body>
72+
<div id="app-container" class="canvas-app-container">
73+
<div id="canvas-container" class="canvas-app-canvas-container">
74+
<canvas id="canvas" class="canvas-app-canvas" tabindex="1" width="{{display.width}}" height="{{display.height}}"></canvas>
75+
<div style="position: absolute; bottom: 0px; left: 50%;">
76+
<div id="canvas-ad" style="width: 728px; height:90px; margin-left: -50%;"/>
77+
</div>
78+
</div>
79+
</div>
80+
<!-- -->
81+
<script id='engine-loader' type='text/javascript' src="dmloader.js"></script>
82+
<!-- -->
83+
<script id='engine-setup' type='text/javascript'>
84+
var extra_params = {
85+
archive_location_filter: function( path ) {
86+
return ("{{DEFOLD_ARCHIVE_LOCATION_PREFIX}}" + path + "{{DEFOLD_ARCHIVE_LOCATION_SUFFIX}}");
87+
},
88+
engine_arguments: [{{#DEFOLD_ENGINE_ARGUMENTS}}"{{.}}",{{/DEFOLD_ENGINE_ARGUMENTS}}],
89+
custom_heap_size: {{DEFOLD_HEAP_SIZE}},
90+
full_screen_container: "#canvas-container",
91+
disable_context_menu: true
92+
}
93+
94+
Module['onRuntimeInitialized'] = function() {
95+
Module.runApp("canvas", extra_params);
96+
};
97+
98+
Module["locateFile"] = function(path, scriptDirectory)
99+
{
100+
// dmengine*.wasm is hardcoded in the built JS loader for WASM,
101+
// we need to replace it here with the correct project name.
102+
if (path == "dmengine.wasm" || path == "dmengine_release.wasm" || path == "dmengine_headless.wasm") {
103+
path = "{{exe-name}}.wasm";
104+
}
105+
return scriptDirectory + path;
106+
};
107+
108+
var is_iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
109+
var buttonHeight = 0;
110+
{{#html5.show_made_with_defold}}
111+
buttonHeight = 42;
112+
{{/html5.show_made_with_defold}}
113+
{{#html5.show_fullscreen_button}}
114+
buttonHeight = 42;
115+
{{/html5.show_fullscreen_button}}
116+
// Resize on init, screen resize and orientation change
117+
function resize_game_canvas() {
118+
// Hack for iOS when exit from Fullscreen mode
119+
if (is_iOS) {
120+
window.scrollTo(0, 0);
121+
}
122+
123+
var app_container = document.getElementById('app-container');
124+
var game_canvas = document.getElementById('canvas');
125+
var innerWidth = window.innerWidth;
126+
var innerHeight = window.innerHeight - buttonHeight;
127+
var width = {{display.width}};
128+
var height = {{display.height}};
129+
var targetRatio = width / height;
130+
var actualRatio = innerWidth / innerHeight;
131+
{{#DEFOLD_SCALE_MODE_IS_DOWNSCALE_FIT}}
132+
//Downscale fit
133+
if (innerWidth < width || innerHeight < height) {
134+
if (actualRatio > targetRatio) {
135+
width = innerHeight * targetRatio;
136+
height = innerHeight;
137+
app_container.style.marginLeft = ((innerWidth - width) / 2) + "px";
138+
app_container.style.marginTop = "0px";
139+
}
140+
else {
141+
width = innerWidth;
142+
height = innerWidth / targetRatio;
143+
app_container.style.marginLeft = "0px";
144+
app_container.style.marginTop = ((innerHeight - height) / 2) + "px";
145+
}
146+
}
147+
else {
148+
app_container.style.marginLeft = ((innerWidth - width) / 2) + "px";
149+
app_container.style.marginTop = ((innerHeight - height) / 2) + "px";
150+
}
151+
{{/DEFOLD_SCALE_MODE_IS_DOWNSCALE_FIT}}
152+
{{#DEFOLD_SCALE_MODE_IS_STRETCH}}
153+
//Stretch
154+
width = innerWidth;
155+
height = innerHeight;
156+
{{/DEFOLD_SCALE_MODE_IS_STRETCH}}
157+
{{#DEFOLD_SCALE_MODE_IS_FIT}}
158+
//Fit
159+
if (actualRatio > targetRatio) {
160+
width = innerHeight * targetRatio;
161+
height = innerHeight;
162+
app_container.style.marginLeft = ((innerWidth - width) / 2) + "px";
163+
app_container.style.marginTop = "0px";
164+
}
165+
else {
166+
width = innerWidth;
167+
height = innerWidth / targetRatio;
168+
app_container.style.marginLeft = "0px";
169+
app_container.style.marginTop = ((innerHeight - height) / 2) + "px";
170+
}
171+
{{/DEFOLD_SCALE_MODE_IS_FIT}}
172+
{{#DEFOLD_SCALE_MODE_IS_NO_SCALE}}
173+
//No scale
174+
var margin_left = ((innerWidth - width) / 2);
175+
margin_left = margin_left > 0 ? margin_left : 0;
176+
var margin_top = ((innerHeight - height) / 2);
177+
margin_top = margin_top > 0 ? margin_top : 0;
178+
app_container.style.marginLeft = margin_left + "px";
179+
app_container.style.marginTop = margin_top + "px";
180+
{{/DEFOLD_SCALE_MODE_IS_NO_SCALE}}
181+
app_container.style.width = width + "px";
182+
app_container.style.height = height + buttonHeight + "px";
183+
game_canvas.width = width;
184+
game_canvas.height = height;
185+
}
186+
resize_game_canvas();
187+
window.addEventListener('resize', resize_game_canvas, false);
188+
window.addEventListener('orientationchange', resize_game_canvas, false);
189+
</script>
190+
191+
<script id='engine-start' type='text/javascript'>
192+
EngineLoader.load("canvas", "{{exe-name}}");
193+
</script>
194+
</body>
195+
</html>

example/example.collection

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: "example"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "example"
5+
data: "components {\n"
6+
" id: \"example\"\n"
7+
" component: \"/example/example.gui\"\n"
8+
" position {\n"
9+
" x: 0.0\n"
10+
" y: 0.0\n"
11+
" z: 0.0\n"
12+
" }\n"
13+
" rotation {\n"
14+
" x: 0.0\n"
15+
" y: 0.0\n"
16+
" z: 0.0\n"
17+
" w: 1.0\n"
18+
" }\n"
19+
"}\n"
20+
""
21+
position {
22+
x: 0.0
23+
y: 0.0
24+
z: 0.0
25+
}
26+
rotation {
27+
x: 0.0
28+
y: 0.0
29+
z: 0.0
30+
w: 1.0
31+
}
32+
scale3 {
33+
x: 1.0
34+
y: 1.0
35+
z: 1.0
36+
}
37+
}

example/example.font

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
font: "/builtins/fonts/vera_mo_bd.ttf"
2+
material: "/builtins/fonts/font.material"
3+
size: 15

0 commit comments

Comments
 (0)