Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Changes #43

Merged
merged 5 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/pong/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
</script>
</head>
<body>
<canvas id="twr_d2dcanvas" width="900" height="600"></canvas>
<canvas id="twr_d2dcanvas" width="900" height="600"></canvas><br>
<a id="control_text"></a>

<script type="module">
import {twrWasmModule, twrWasmModuleAsync} from "twr-wasm";
Expand Down
12 changes: 12 additions & 0 deletions examples/pong/jsEventsLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default class jsEventsLib extends twrLibrary {
registerAnimationLoop: {},
registerMousePressEvent: {},
registerMouseMoveEvent: {},

setElementText: {},
};

// every library should have this line
Expand Down Expand Up @@ -89,6 +91,16 @@ export default class jsEventsLib extends twrLibrary {
}
}

setElementText(mod:IWasmModule|IWasmModuleAsync, elementIDPtr: number, textPtr: number) {
const elementID = mod.wasmMem.getString(elementIDPtr);
const text = mod.wasmMem.getString(textPtr);

const element = document.getElementById(elementID)!;
if (!element) throw new Error(`setElementText was given an invalid ID (${elementID})`);

element.innerText = text;
}

}


7 changes: 7 additions & 0 deletions examples/pong/pong-menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ const colorRGB_t s_pong_background_color = 0xFFFFFF;
const colorRGB_t s_pong_paddle_color = 0xFF0000;
const colorRGB_t s_pong_ball_color = 0x00FF00;

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

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

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

default:
Expand Down
5 changes: 3 additions & 2 deletions examples/pong/pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void Pong::resetGame() {
this->paddle_x = this->width/2.0 - this->paddle_width/2.0;

const double start_speed = 200.0/1000.0;
double start_dir = rand()%360;
double start_dir = rand()%90 - 90;
double start_dir_rad = start_dir * M_PI/180;

this->ball_velocity_x = start_speed*cos(start_dir_rad);
Expand Down Expand Up @@ -44,7 +44,8 @@ Pong::Pong(double width, double height, colorRGB_t border_color, colorRGB_t back
assert(image_loaded);
#endif
//initialized random number generator
srand(time(NULL));

srand(twr_epoch_timems());

this->resetGame();
}
Expand Down
6 changes: 5 additions & 1 deletion examples/pong/two-player-pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TwoPlayerPong::TwoPlayerPong(double width, double height, bool hasAI) {
this->width = width;
this->height = height;
this->hasAI = hasAI;
this->bounce_noise = load_square_wave(493.883, 0.025, 48000);
this->bounce_noise = load_square_wave(493.883, 0.05, 48000);
this->score_noise = load_square_wave(440, 0.05, 48000);
srand(time(NULL));

Expand Down Expand Up @@ -274,6 +274,10 @@ void TwoPlayerPong::keyDownEvent(long keycode) {
paddleOne.dir = PaddleDir::DOWN;
break;

case KeyCode::enter:
if(!this->running)
this->resetGame();

default:
break;
}
Expand Down
24 changes: 12 additions & 12 deletions examples/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
from http import server # Python 3

class MyHTTPRequestHandler(server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_my_headers()
server.SimpleHTTPRequestHandler.end_headers(self)
def end_headers(self):
self.send_my_headers()
server.SimpleHTTPRequestHandler.end_headers(self)

def send_my_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
# Turn off caching
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
def send_my_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
# Turn off caching
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")

if __name__ == '__main__':
server.test(HandlerClass=MyHTTPRequestHandler)
server.test(HandlerClass=MyHTTPRequestHandler)