Skip to content

Commit 12e2ad8

Browse files
committed
All intensive tasks (load music, voices and ink) are now executed in a background thread.
1 parent f194104 commit 12e2ad8

File tree

7 files changed

+112
-75
lines changed

7 files changed

+112
-75
lines changed

blade-engine/src/com/bladecoder/engine/ink/InkManager.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,39 @@ public void newStory(InputStream is) throws Exception {
7070
externalFunctions.bindExternalFunctions(this);
7171
}
7272

73-
public void newStory(String storyName) throws Exception {
73+
public void newStory(final String name) throws Exception {
74+
new Thread() {
75+
@Override
76+
public void run() {
77+
loadStory(name, null);
78+
}
79+
}.start();
80+
}
81+
82+
synchronized private void loadStory(String name, String stateString) {
7483
FileHandle asset = EngineAssetManager.getInstance()
75-
.getAsset(EngineAssetManager.MODEL_DIR + storyName + EngineAssetManager.INK_EXT);
84+
.getAsset(EngineAssetManager.MODEL_DIR + name + EngineAssetManager.INK_EXT);
7685

7786
try {
7887
long initTime = System.currentTimeMillis();
7988
newStory(asset.read());
80-
EngineLogger.debug("INK STORY LOADING TIME (ms): " + (System.currentTimeMillis() - initTime));
8189

82-
this.storyName = storyName;
90+
storyName = name;
8391

8492
loadI18NBundle();
93+
94+
EngineLogger.debug("INK STORY LOADING TIME (ms): " + (System.currentTimeMillis() - initTime));
95+
96+
if (stateString != null) {
97+
initTime = System.currentTimeMillis();
98+
story.getState().loadJson(stateString);
99+
EngineLogger.debug("INK SAVED STATE LOADING TIME (ms): " + (System.currentTimeMillis() - initTime));
100+
}
101+
85102
} catch (Exception e) {
86-
EngineLogger.error("Cannot load Ink Story: " + storyName + " " + e.getMessage());
103+
EngineLogger.error("Cannot load Ink Story: " + name + " " + e.getMessage());
104+
story = null;
105+
storyName = null;
87106
}
88107
}
89108

@@ -96,12 +115,11 @@ public String translateLine(String line) {
96115
if (line.charAt(0) == I18N.PREFIX) {
97116
String key = line.substring(1);
98117

99-
100118
// In ink, several keys can be included in the same line.
101119
String[] keys = key.split("@");
102120

103121
String translated = "";
104-
122+
105123
for (String k : keys) {
106124
try {
107125
translated += i18n.getString(k);
@@ -110,7 +128,7 @@ public String translateLine(String line) {
110128
return key;
111129
}
112130
}
113-
131+
114132
return translated;
115133
}
116134

@@ -349,7 +367,7 @@ public Story getStory() {
349367
return story;
350368
}
351369

352-
public void runPath(String path, ActionCallback cb) throws Exception {
370+
synchronized public void runPath(String path, ActionCallback cb) throws Exception {
353371
if (story == null) {
354372
EngineLogger.error("Ink Story not loaded!");
355373
return;
@@ -466,10 +484,10 @@ public String getCurrentTarget() {
466484
@Override
467485
public void write(Json json) {
468486
json.writeValue("wasInCutmode", wasInCutmode);
469-
470-
if(cb == null && sCb != null)
487+
488+
if (cb == null && sCb != null)
471489
cb = ActionCallbackSerialization.find(sCb);
472-
490+
473491
json.writeValue("cb", ActionCallbackSerialization.find(cb));
474492

475493
// SAVE ACTIONS
@@ -538,15 +556,7 @@ public void read(Json json, JsonValue jsonData) {
538556
String storyName = json.readValue("storyName", String.class, jsonData);
539557
String storyString = json.readValue("story", String.class, jsonData);
540558
if (storyString != null) {
541-
try {
542-
newStory(storyName);
543-
544-
long initTime = System.currentTimeMillis();
545-
story.getState().loadJson(storyString);
546-
EngineLogger.debug("INK SAVED STATE LOADING TIME (ms): " + (System.currentTimeMillis() - initTime));
547-
} catch (Exception e) {
548-
EngineLogger.error(e.getMessage(), e);
549-
}
559+
loadStory(storyName, storyString);
550560
}
551561
}
552562
}

blade-engine/src/com/bladecoder/engine/model/MusicManager.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public class MusicManager implements Serializable, AssetConsumer {
3232

3333
public void playMusic() {
3434
if (music != null && !music.isPlaying()) {
35-
35+
3636
try {
3737
music.play();
3838
music.setLooping(desc.isLoop());
3939
music.setVolume(desc.getVolume());
40-
} catch(Exception e) {
40+
} catch (Exception e) {
4141
EngineLogger.error("Error Playing music: " + desc.getFilename(), e);
4242
}
4343
}
@@ -65,23 +65,30 @@ public void stopMusic() {
6565
public void setMusic(MusicDesc d) {
6666
EngineLogger.debug(">>>SETTING MUSIC.");
6767
stopMusic();
68+
volumeTween = null;
6869
currentMusicDelay = 0;
69-
volumeTween = null;
7070

7171
if (d != null) {
7272
if (desc != null)
7373
dispose();
7474

7575
desc = new MusicDesc(d);
7676

77-
retrieveAssets();
77+
// Load and play the voice file in a different Thread to avoid
78+
// blocking the UI
79+
new Thread() {
80+
@Override
81+
public void run() {
82+
retrieveAssets();
83+
}
84+
}.start();
7885
} else {
7986
dispose();
8087
desc = null;
8188
}
8289
}
8390

84-
public void setVolume(float volume) {
91+
public void setVolume(float volume) {
8592
if (desc != null)
8693
desc.setVolume(volume);
8794

@@ -138,7 +145,7 @@ public void update(float delta) {
138145
}
139146
}
140147
}
141-
148+
142149
if (volumeTween != null) {
143150
volumeTween.update(delta);
144151
if (volumeTween.isComplete()) {

blade-engine/src/com/bladecoder/engine/model/Scene.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public BaseActor getActor(String id, boolean searchInventory) {
257257
if (VAR_PLAYER.equals(id))
258258
return actors.get(player);
259259

260-
BaseActor a = actors.get(id);
260+
BaseActor a = id==null? null:actors.get(id);
261261

262262
if (a == null && searchInventory) {
263263
a = World.getInstance().getInventory().get(id);
@@ -737,7 +737,8 @@ public void read(Json json, JsonValue jsonData) {
737737
camera = json.readValue("camera", SceneCamera.class, jsonData);
738738
String followActorId = json.readValue("followActor", String.class, jsonData);
739739

740-
setCameraFollowActor((SpriteActor) actors.get(followActorId));
740+
if(followActor != null)
741+
setCameraFollowActor((SpriteActor) actors.get(followActorId));
741742

742743
soundManager.read(json, jsonData);
743744
}

blade-engine/src/com/bladecoder/engine/model/Verb.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,12 @@ public void cancel() {
187187
((VerbRunner) c).cancel();
188188
}
189189

190-
ip = actions.size();
190+
ip = actions.size() + 1;
191+
192+
EngineLogger.debug(">>> Verb CANCELLED: " + id);
193+
194+
if(cb != null)
195+
cb.resume();
191196
}
192197

193198
@Override

blade-engine/src/com/bladecoder/engine/model/VoiceManager.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
*/
2121
public class VoiceManager implements Serializable, AssetConsumer {
2222
transient private Music voice = null;
23-
23+
2424
String fileName = null;
2525

2626
private boolean isPlayingSer = false;
2727
private float voicePosSer = 0;
28-
28+
2929
// the master volume
3030
private float volume = 1.0f;
31-
31+
3232
transient private boolean isPaused = false;
3333
transient private TextManager textManager = null;
3434

@@ -60,37 +60,41 @@ public void stop() {
6060

6161
public void play(String fileName) {
6262
stop();
63-
63+
6464
this.fileName = fileName;
65-
66-
if (fileName != null) {
67-
retrieveAssets();
68-
69-
if(voice != null)
70-
voice.play();
65+
66+
if (fileName != null) {
67+
// Load and play the voice file in a different Thread to avoid
68+
// blocking the UI
69+
new Thread() {
70+
@Override
71+
public void run() {
72+
retrieveAssets();
73+
74+
if (voice != null)
75+
voice.play();
76+
}
77+
}.start();
7178
}
7279
}
73-
7480

7581
public void setVolume(float volume) {
7682
this.volume = volume;
77-
78-
if(voice != null)
83+
84+
if (voice != null)
7985
voice.setVolume(volume);
8086
}
8187

82-
83-
8488
@Override
8589
public void dispose() {
8690
if (voice != null) {
87-
88-
if(voice.isPlaying())
91+
92+
if (voice.isPlaying())
8993
voice.stop();
90-
94+
9195
EngineLogger.debug("DISPOSING VOICE: " + fileName);
9296
EngineAssetManager.getInstance().unload(EngineAssetManager.VOICE_DIR + fileName);
93-
97+
9498
voice = null;
9599
fileName = null;
96100
isPlayingSer = false;
@@ -109,10 +113,10 @@ public void loadAssets() {
109113
@Override
110114
public void retrieveAssets() {
111115
if (voice == null && fileName != null) {
112-
113-
if(!EngineAssetManager.getInstance().isLoaded(EngineAssetManager.VOICE_DIR + fileName)) {
116+
117+
if (!EngineAssetManager.getInstance().isLoaded(EngineAssetManager.VOICE_DIR + fileName)) {
114118
loadAssets();
115-
119+
116120
try {
117121
EngineAssetManager.getInstance().finishLoading();
118122
} catch (GdxRuntimeException e) {
@@ -123,25 +127,25 @@ public void retrieveAssets() {
123127
return;
124128
}
125129
}
126-
130+
127131
EngineLogger.debug("RETRIEVING VOICE: " + fileName);
128-
132+
129133
voice = EngineAssetManager.getInstance().get(EngineAssetManager.VOICE_DIR + fileName, Music.class);
130-
134+
131135
voice.setOnCompletionListener(new OnCompletionListener() {
132136
@Override
133137
public void onCompletion(Music music) {
134-
if(textManager.getCurrentText() != null)
138+
if (textManager.getCurrentText() != null)
135139
textManager.getCurrentText().setAutoTime();
136140
}
137141
});
138-
139-
if(voice != null)
142+
143+
if (voice != null)
140144
voice.setVolume(volume);
141145

142146
if (isPlayingSer) {
143147
voice.play();
144-
148+
145149
if (voice != null) {
146150
voice.setPosition(voicePosSer);
147151
}
@@ -155,8 +159,8 @@ public void onCompletion(Music music) {
155159
@Override
156160
public void write(Json json) {
157161
json.writeValue("fileName", fileName);
158-
json.writeValue("isPlaying", voice != null && (voice.isPlaying()|| isPaused));
159-
json.writeValue("musicPos", voice != null && (voice.isPlaying()|| isPaused) ? voice.getPosition() : 0f);
162+
json.writeValue("isPlaying", voice != null && (voice.isPlaying() || isPaused));
163+
json.writeValue("musicPos", voice != null && (voice.isPlaying() || isPaused) ? voice.getPosition() : 0f);
160164
}
161165

162166
@Override

blade-engine/src/com/bladecoder/engine/ui/CreditsScreen.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,25 @@ private float processCreditSpace(int height, float y, int i, String s) {
185185
return y;
186186
}
187187

188-
private void processCreditMusic(String s) {
188+
private void processCreditMusic(final String s) {
189189
if (music != null)
190190
music.dispose();
191191

192-
String sound = EngineAssetManager.getInstance().checkIOSSoundName("music/" + s);
193-
194-
music = Gdx.audio.newMusic(EngineAssetManager.getInstance().getAsset(sound));
195-
196-
try {
197-
music.play();
198-
} catch(Exception e) {
199-
// sometimes the play method fails on desktop.
200-
EngineLogger.error("Error Playing music: " + s, e);
201-
}
192+
final String sound = EngineAssetManager.getInstance().checkIOSSoundName("music/" + s);
193+
194+
new Thread() {
195+
@Override
196+
public void run() {
197+
music = Gdx.audio.newMusic(EngineAssetManager.getInstance().getAsset(sound));
198+
199+
try {
200+
music.play();
201+
} catch(Exception e) {
202+
// sometimes the play method fails on desktop.
203+
EngineLogger.error("Error Playing music: " + s, e);
204+
}
205+
}
206+
}.start();
202207
}
203208

204209
private float processCreditDefault(SpriteBatch batch, int width, int height, float y, int i, String s) {

blade-engine/src/com/bladecoder/engine/ui/MenuScreen.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,14 @@ public void clicked(InputEvent event, float x, float y) {
376376
Gdx.input.setInputProcessor(stage);
377377

378378
if (style.musicFile != null) {
379-
music = Gdx.audio.newMusic(EngineAssetManager.getInstance().getAsset(style.musicFile));
380-
music.setLooping(true);
381-
music.play();
379+
new Thread() {
380+
@Override
381+
public void run() {
382+
music = Gdx.audio.newMusic(EngineAssetManager.getInstance().getAsset(style.musicFile));
383+
music.setLooping(true);
384+
music.play();
385+
}
386+
}.start();
382387
}
383388
}
384389

0 commit comments

Comments
 (0)