Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active August 29, 2015 13:58
Show Gist options
  • Save kjunichi/10076523 to your computer and use it in GitHub Desktop.
Save kjunichi/10076523 to your computer and use it in GitHub Desktop.

Emscriptenでcursesなコードを動かすには

はじめに

PDCurses-emscriptenを使えば、白黒で動く。

slコマンドをEmscriptenで動かそうとして、悪戦苦闘のした結果のメモです。

まずbuild.shを編集して実行

emscriptenのインストール場所を自分の環境に合わせて書き直して、ビルド実行

./build.sh

自分のコードを動かすのに必要となるファイル

以下の2ファイルが以降の作業で必要となる。

  • pdcfont.bmp
  • libpdcurses.a

自分が動かしたいコードをPDCurses-emscriptenを使てビルドするには

メインループ相当の処理を関数にして切り出す

emscripten_set_main_loopに登録

切出した関数ポインタをemscripten_set_main_loopに登録する。

この作業により、Canvasに描画するタイミングをウェブブラウザに与えることが可能になる。 これをやらないと、ビルドできて、いざ表示しても真っ黒い画面という残念な結果になる。

pdcfont.bmpとlibpdcurses.aをコピーするなりして、持ってくる

~/local/emscripten/emcc -O2 -Wall -fPIC -I.  -o sl sl.c libpdcurses.a `sdl-config --libs`
cp sl sl.bc

ブラウザで動かすのに必要なHTMLファイルを生成

~/local/emscripten/emcc -o sl.html sl.bc --preload-file pdcfont.bmp

非jsなフツーにPDCursesを使うには

cc -O2 -Wall -I.. -Dmain=SDL_main -o sl ../demos/sl.c libpdcurses.a -L/usr/local/lib -lSDLmain -lSDL -Wl,-framework,Cocoa

エラー倉庫

06:47:22.692 ReferenceError: _SDL_RWFromMem is not defined sample.html:7815
06:36:53.879 "run() called, but dependencies remain, so not running" sample.html:61
06:38:12.770 "Cannot find preloaded image pdcicon.bmp" sample.html:61

メインスレッドで潜在的な無限ループの可能性があるから中断したと怒られる時の対処

SDL_Delay called on the main thread! Potential infinite loop, quitting.

こんなエラーがブラウザのコンソールに出力される。

Implementing an asynchronous main loop in C/C++を参考に、ループ部分を関数化して、その関数ポインタを emscripten_set_main_loopに登録すれば解決。

PDCurses-emscriptenのMakefileの一部

$(BUILD) $(DEMOFLAGS) -o $@ $< $(LDFLAGS)

BUILD	= $(CC) $(CFLAGS) -I$(PDCURSES_SRCDIR)

ifeq ($(DEBUG),Y)
CFLAGS = -g -Wall -DPDCDEBUG
else
CFLAGS = -O2 -Wall
endif

#wl
CFLAGS += -fPIC

ifndef PDCURSES_SRCDIR
PDCURSES_SRCDIR	= ..
endif

ifeq ($(shell uname),Darwin)
DEMOFLAGS = -Dmain=SDL_main
endif

LDFLAGS	= $(LIBCURSES) $(SLIBS)
LIBCURSES	= libpdcurses.a
SLIBS	= $(shell sdl-config --libs)

関連記事

関連

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment