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

Preprocessing and labels trick #59

Open
k00lagin opened this issue May 10, 2024 · 0 comments
Open

Preprocessing and labels trick #59

k00lagin opened this issue May 10, 2024 · 0 comments

Comments

@k00lagin
Copy link

k00lagin commented May 10, 2024

We could minimize or even completely ditch (in cases) editing existing raylib programms using some tricks. At least, the editing I suggest I see as less intrusive.

We can reuse main function as our frame generator. We will initialize the programm on the first main call, but will skip the loop by setting WindowShouldClose in js as always true. All subsequent calls we'll skip initialization and jump straight into frame update and draw.
Here is the simple example:

void main(int InitialRun)
{
  if (!InitialRun)
  {
    goto begin_frame;
  }
  else
  {
    // Initialization
    while (!WindowShouldClose())
    {
      begin_frame:
      // Update variables
      BeginDrawing();
      // Draw things
      EndDrawing();
      goto end_frame;
    }
    // De-Initialization
    CloseWindow();
    end_frame:
    return 0;
  }
}

By saying "less intrusive" I mean that it's just inserts or replacements, no or at least less moving existing stuff around. Which leads us to macros. In some simple programms we can factor all modifications out to raylib.h or other header and put it under #ifdef PLATFORM_WEB. If we can threat BeginDrawing and EndDrawing as loop brackets (no updates or anything in loop but outside them), and don't have anything meaningful after CloseWindow.

#ifdef PLATFORM_WEB
#define main() \
main(int InitialRun) { \
    if (!InitialRun) { \
        goto begin_frame; \
    } else
#define main(void) \
main(int InitialRun) { \
    if (!InitialRun) { \
        goto begin_frame; \
    } else
#define BeginDrawing(); begin_frame:BeginDrawing();
#define EndDrawing(); EndDrawing(); goto end_frame;
#define CloseWindow(); end_frame:; }
#endif

C macros are quite restricted, so as the next enhancement, to make it more robust we could implement our own preprocessing step. E.g. it would be better to automatically put begin_frame label right at the start of the loop. So I tryed a macro for this as well but no luck.

#define WindowShouldClose()) true) ; begin_frame: if (true)
@k00lagin k00lagin changed the title Preprocessing and labels Preprocessing and labels trick May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant