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

Implement seeded random #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions examples/core_random_colors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "raylib.h"

void raylib_js_set_entry(void (*entry)(void));

unsigned int framesCounter = 0;
int r = 150;
int g = 150;
int b = 150;

void GameFrame()
{
BeginDrawing();
ClearBackground(CLITERAL(Color){r, g, b, 255});
framesCounter++;

if (((framesCounter / 120) % 2) == 1)
{
r = GetRandomValue(50, 255);
g = GetRandomValue(50, 255);
b = GetRandomValue(50, 255);
framesCounter = 0;
}

ClearBackground(CLITERAL(Color){r, g, b, 255});
DrawText("Every 2 seconds a new random color is generated", 130, 100, 20, BLACK);
EndDrawing();
}

int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "Random colors");

SetTargetFPS(60);
#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
while (!WindowShouldClose())
{
GameFrame();
}
CloseWindow();
#endif
return 0;
}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<script>
const wasmPaths = {
"tsoding": ["tsoding_ball", "tsoding_snake",],
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel","core_random_colors",],
"shapes": ["shapes_colors_palette"],
"text": ["text_writing_anim"],
"textures": ["textures_logo_raylib"],
Expand Down
5 changes: 5 additions & 0 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Example examples[] = {
.bin_path = "./build/text_writing_anim",
.wasm_path = "./wasm/text_writing_anim.wasm",
},
{
.src_path = "./examples/core_random_colors.c",
.bin_path = "./build/core_random_colors",
.wasm_path = "./wasm/core_random_colors.wasm",
},
};

bool build_native(void)
Expand Down
15 changes: 15 additions & 0 deletions raylib.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class RaylibJs {
this.ctx = undefined;
this.dt = undefined;
this.targetFPS = 60;
this.seed = undefined;
this.entryFunction = undefined;
this.prevPressedKeyState = new Set();
this.currentPressedKeyState = new Set();
Expand Down Expand Up @@ -111,6 +112,7 @@ class RaylibJs {
this.ctx.canvas.height = height;
const buffer = this.wasm.instance.exports.memory.buffer;
document.title = cstr_by_ptr(buffer, title_ptr);
this.seed = Date.now();
}

WindowShouldClose(){
Expand Down Expand Up @@ -344,6 +346,19 @@ class RaylibJs {
this.ctx.fillText(text, posX, posY + fontSize);
}

SetRandomSeed(seed) {
this.seed = seed;
}

// Here is the simple Park and Miller random number generator instead
// of SplitMix64 and Xoshiro128** combo used in raylib. (https://github.com/raysan5/raylib/blob/8a5fd3ac1d481ff978363b80298770ed8eeca9f8/src/external/rprand.h#L120)
// TODO: We'll need to implement the same algorithm in the future
GetRandomValue(min, max) {
if (min > max) [min, max] = [max, min];
this.seed = this.seed * 16807 % 2147483647;
return (this.seed % (max - min + 1)) + min;
}

raylib_js_set_entry(entry) {
this.entryFunction = this.wasm.instance.exports.__indirect_function_table.get(entry);
}
Expand Down