Skip to content

Commit 9b6da4a

Browse files
committed
Added notes below the canvas explaining how to move the paddles in Pong
1 parent 26a094d commit 9b6da4a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

examples/pong/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
</script>
1313
</head>
1414
<body>
15-
<canvas id="twr_d2dcanvas" width="900" height="600"></canvas>
15+
<canvas id="twr_d2dcanvas" width="900" height="600"></canvas><br>
16+
<a id="control_text"></a>
1617

1718
<script type="module">
1819
import {twrWasmModule, twrWasmModuleAsync} from "twr-wasm";

examples/pong/jsEventsLib.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export default class jsEventsLib extends twrLibrary {
1010
registerAnimationLoop: {},
1111
registerMousePressEvent: {},
1212
registerMouseMoveEvent: {},
13+
14+
setElementText: {},
1315
};
1416

1517
// every library should have this line
@@ -89,6 +91,16 @@ export default class jsEventsLib extends twrLibrary {
8991
}
9092
}
9193

94+
setElementText(mod:IWasmModule|IWasmModuleAsync, elementIDPtr: number, textPtr: number) {
95+
const elementID = mod.wasmMem.getString(elementIDPtr);
96+
const text = mod.wasmMem.getString(textPtr);
97+
98+
const element = document.getElementById(elementID)!;
99+
if (!element) throw new Error(`setElementText was given an invalid ID (${elementID})`);
100+
101+
element.innerText = text;
102+
}
103+
92104
}
93105

94106

examples/pong/pong-menu.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ const colorRGB_t s_pong_background_color = 0xFFFFFF;
199199
const colorRGB_t s_pong_paddle_color = 0xFF0000;
200200
const colorRGB_t s_pong_ball_color = 0x00FF00;
201201

202+
extern "C" {
203+
__attribute__((import_name("setElementText")))
204+
void set_element_text(const char* element_id, const char* text);
205+
}
202206
void Menu::tryButtonPress(long x, long y) {
203207
this->updateButtonSelections(x, y);
204208
for (LinkedList<MenuButton>* node = this->buttons.root; node; node = node->next) {
@@ -208,16 +212,19 @@ void Menu::tryButtonPress(long x, long y) {
208212
case 0:
209213
this->state = MenuState::SinglePlayerPong;
210214
this->s_pong = Pong(600, 600, s_pong_border_color, s_pong_background_color, s_pong_paddle_color, s_pong_ball_color);
215+
set_element_text("control_text", "Move the paddle using a and d or the left and right arrow keys.");
211216
break;
212217

213218
case 1:
214219
this->state = MenuState::TwoPlayerPong;
215220
this->t_pong = TwoPlayerPong(this->width, this->height, true);
221+
set_element_text("control_text", "Move the paddle using w and s or the up and down arrow keys.");
216222
break;
217223

218224
case 2:
219225
this->state = MenuState::TwoPlayerPong;
220226
this->t_pong = TwoPlayerPong(this->width, this->height, false);
227+
set_element_text("control_text", "Move the left paddle using w and s. Move the right one with the up and down arrow keys.");
221228
break;
222229

223230
default:

0 commit comments

Comments
 (0)