LDC 1.40.0-beta3

ryuukk_ ryuukk.dev at gmail.com
Mon Sep 9 17:53:34 UTC 2024


On Monday, 9 September 2024 at 17:15:47 UTC, kinke wrote:
> Oh, I forgot an item:
> * Emscripten: The compiler now mimicks a musl Linux platform 
> wrt. extra predefined versions.
>
> This should pave the way for supporting Emscripten in druntime 
> & Phobos (via `version(Emscripten)` special cases), which is 
> probably much simpler with the musl libc interface than the 
> WASI interface directly.

Thanks! i got SDL + printf working

Follow what's bellow, then browse: http://0.0.0.0:8000/

The process could be simplified imo, but that's a promising start

```
$ ldc2 -mtriple=wasm32-emscripten-none -L-allow-undefined 
-relocation-model=pic app.d
$ mkdir dist/
$ emcc -v -O0 -s WASM=1 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s 
RELOCATABLE=1  -s ENVIRONMENT=web -o dist/index.html app.o
$ python -m http.server 8000 --directory dist/
```

```D
import core.stdc.stdio;

__gshared:
SDL_Window* window = null;
SDL_Renderer* renderer = null;
SDL_Surface* screen_surface = null;
SDL_Event* event = null;

bool is_running = true;

extern (C) void main()
{
     printf("hello from D\n");
     init_game();
}

extern (C) void mainloop()
{
     if (!is_running)
     {
         emscripten_cancel_main_loop();
         return;
     }
     SDL_FillRect(screen_surface, null, 0xFF0000);
     SDL_UpdateWindowSurface(window);

}

extern (C) void _start() {} // should not be needed


extern (C) void init_game()
{
     if (SDL_Init(SDL_INIT_VIDEO) < 0)
     {
         fprintf(stderr, "could not initialize sdl2: %s\n", 
SDL_GetError());
         emscripten_cancel_main_loop();
         return;
     }
     window = SDL_CreateWindow("hello_sdl2", 
SDL_WINDOWPOS_UNDEFINED,
             SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
     if (window == null)
     {
         fprintf(stderr, "could not create window: %s\n", 
SDL_GetError());
         emscripten_cancel_main_loop();
         return;
     }
     screen_surface = SDL_GetWindowSurface(window);

     emscripten_set_main_loop(&mainloop, 0, 1);
}

extern (C)
{

     // emscripten API
     alias em_callback_func = void function();
     void emscripten_set_main_loop(em_callback_func, int, int);
     void emscripten_cancel_main_loop();

     // SDL

     struct SDL_Rect
     {
         int x, y;
         int w, h;
     }

     alias SDL_Window = void;
     alias SDL_Renderer = void;
     alias SDL_Event = void;
     alias SDL_Surface = void;

     enum SDL_INIT_TIMER = 0x00000001u;
     enum SDL_INIT_AUDIO = 0x00000010u;
     enum SDL_INIT_VIDEO = 0x00000020u;
     enum SDL_INIT_JOYSTICK = 0x00000200u;
     enum SDL_INIT_HAPTIC = 0x00001000u;
     enum SDL_INIT_GAMEPAD = 0x00002000u;
     enum SDL_INIT_EVENTS = 0x00004000u;
     enum SDL_INIT_SENSOR = 0x00008000u;
     enum SDL_INIT_CAMERA = 0x00010000u;
     int SDL_Init(uint);

     enum SDL_WINDOWPOS_UNDEFINED_MASK = 0x1FFF0000u;
     enum SDL_WINDOWPOS_UNDEFINED = SDL_WINDOWPOS_UNDEFINED_MASK | 
0;
     enum SDL_WINDOW_SHOWN = 0x00000004;
     SDL_Window* SDL_CreateWindow(const char* title, int x, int y, 
int w, int h, uint flags);

     SDL_Surface* SDL_GetWindowSurface(SDL_Window*);

     int SDL_UpdateWindowSurface(SDL_Window*);

     int SDL_FillRect(SDL_Surface*, const(SDL_Rect)* rect, uint 
color);

     const(char)* SDL_GetError();
}
```


More information about the Digitalmars-d-announce mailing list