Closed abarichello closed 4 years ago
Thanks for reporting this!
Could you please help me understand what is the example you are talking about? Is it one of: https://arthursonzogni.github.io/smk/examples/?
I don't see any "draw a circle" here. Maybe "shape_2d" ?
I am surprised you see _nanosleep being called. It shouldn't happen when webassembly is being used. Maybe I need to build and update the examples.
Using this example:
#include <smk/Color.hpp>
#include <smk/Shape.hpp>
#include <smk/Window.hpp>
int main() {
auto window = smk::Window(640, 480, "test");
auto circle = smk::Shape::Circle(200);
circle.SetColor(smk::Color::Red);
circle.SetPosition(320, 240);
while (!window.input().IsKeyPressed(GLFW_KEY_ESCAPE)) {
window.PoolEvents();
window.Clear(smk::Color::Blue);
window.Draw(circle);
window.Display();
window.LimitFrameRate(60);
}
return 0;
}
The webpage loads but it's really inconvenient to wait around10seconds with a frozen tab to test the wasm result.
I see! Your code works in a desktop environment, but it won't in a web browser environment.
You are doing a infinite "while" loop. It means your "javascript" task will take forever and block the page. Instead you must set a callback that will be called periodically by the web browser using emscripten_set_main_loop.
You should start from the starter project: smk-starter or any other examples in /examples.
Your example will look like:
#include <smk/Color.hpp>
#include <smk/Shape.hpp>
#include <smk/Window.hpp>
#include "./util.hpp" // ExecuteMainLoop
int main() {
auto window = smk::Window(640, 480, "test");
auto circle = smk::Shape::Circle(200);
circle.SetColor(smk::Color::Red);
circle.SetPosition(320, 240);
ExecuteMainLoop(window, [&] {
window.PoolEvents();
window.Clear(smk::Color::Blue);
window.Draw(circle);
window.Display();
});
return EXIT_SUCCESS;
}
std::function<void()> main_loop;
void MainLoop() {
main_loop();
}
void ExecuteMainLoop(smk::Window& window, std::function<void()> loop) {
main_loop = loop;
#ifdef __EMSCRIPTEN__
(void)window; // unused parameter.
emscripten_set_main_loop(&MainLoop, 0, 1);
#else
while (!window.input().IsKeyPressed(GLFW_KEY_ESCAPE)) {
MainLoop();
window.LimitFrameRate(60.0f);
}
#endif
}
Do you think I should make "ExecuteMainLoop" part of the library?
I added an helper function for you in commit 36f90f0. You can now use:
#include <smk/Color.hpp>
#include <smk/Shape.hpp>
#include <smk/Window.hpp>
int main() {
auto window = smk::Window(640, 480, "test");
auto circle = smk::Shape::Circle(200);
circle.SetColor(smk::Color::Red);
circle.SetPosition(320, 240);
window.ExecuteMainLoop([&]{
window.PoolEvents();
window.Clear(smk::Color::Blue);
window.Draw(circle);
window.Display();
}
return 0;
}
Does it addresses you issue?
Thanks! The abstraction being included in the lib is really handy.
Using Firefox with the sample 'draw a circle' code freezes the tab for seconds, only loads the webpage after it prompts you to stop the unresponsible script.
Console output: