前書き
Processingである程度重量のあるゲームなどを作っていて、setup内部で多くの画像や音楽などを読み込むようにしていると、読み込みに時間がかかり何も画面が出てこないため心配になることがある。
そこで、画面を表示させた後で別スレッドで読み込みを行い、その間はロード画面を表示することでそのような憂いをなくすことができるのではないかと考えた。
Before
void setup() { size(200, 100); //重い処理 for (int per = 0; per < 100; per++) { try { Thread.sleep(100l); } catch(InterruptedException e) { e.printStackTrace(); } } } char[] gtxt=new String("Game Main").toCharArray(); void draw() { background(255); fill(0); for (int i=0; i<gtxt.length; i++) { text(gtxt[i], width/2 + 6 * (sin(radians(frameCount)*3)+2) * (i - gtxt.length/2), height/2); } }
After
Load load; void setup() { size(200, 100); load = new Load(); } char[] ltxt=new String("NowLoading").toCharArray(); char[] gtxt=new String("Game Main").toCharArray(); void draw() { background(255); fill(0); if (load.isLoading()) {//ローディング画面 for (int i=0; i<ltxt.length; i++) { text(ltxt[i], width/2 + 12 * (i - ltxt.length/2), height/2 + sin(radians(frameCount+i*3)*3)*20); } text(nf(load.per, 2, 0)+"%", width-20, height); } else { for (int i=0; i<gtxt.length; i++) { text(gtxt[i], width/2 + 6 * (sin(radians(frameCount)*3)+2) * (i - gtxt.length/2), height/2); } } } class Load implements Runnable { public int per=0; public Load() { new Thread(this).start(); } public boolean isLoading() { return per<100; } public void run() { //重い処理 for (per = 0; per < 100; per++) { try { Thread.sleep(100l); } catch(InterruptedException e) { e.printStackTrace(); } } } }
あとがき
それに、NowLoading画面があった方がかっこいいとは思わないだろうか。実際以前もロード処理ではないが時間のかかる処理を行わせるときに読み込み中と表示させたところ中々に好評だった。
ゲームなど描画が見せ所になるとこにロードに時間がかかるときにはぜひNowLoadingを表示してみてはいかがだろうか。