Skip to content

Preprocessing and labels trick #59

@k00lagin

Description

@k00lagin

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions