简体   繁体   English

流资源(Slick2D)

[英]Streaming resources (Slick2D)

I'm writing a game using the Slick2D library. 我正在使用Slick2D库编写游戏。 I recently added a loading screen using deferred loading. 我最近添加了使用延迟加载的加载屏幕。 I use the Music class to load OGG files to be used as music. 我使用Music类来加载要用作音乐的OGG文件。 These take an excessive amount of time to load. 这些会花费过多的时间来加载。 I looked on the wiki for more information on deferred loading. 我在Wiki上查看了有关延迟加载的更多信息。 I found: 我发现:

No one resource should take so long to load that it keeps the screen from refreshing for a significant amount of time (if it does, then the resource should probably be streamed anyway :)). 任何一个资源的加载时间都不会太长,以至于无法在相当长的时间内刷新屏幕(如果确实如此,则无论如何都应该对资源进行流式传输:)。

So I altered my menu class as follows (this is just an example and I'm leaving out quite a bit): 因此,我如下更改了菜单类(这只是一个示例,我遗漏了很多):

public class MainMenu extends BasicGameState{
private Music[] theme = new Music[1];
public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException{
    InputStream[] musicStream = {ResourceLoader.getResourceAsStream("/path/to/theme1.ogg")}
    try{
        theme[0] = new Music(musicStream[0],"path/to/theme1.ogg");
    }catch (Exception e){
        e.printStackTrace;
    }
}
(rest of class)

The music array is an array of org.newdawn.slick.Music 音乐数组是org.newdawn.slick.Music的数组

So I thought I'd done what would reduce loading time. 因此,我认为我已经做了可以减少加载时间的事情。 However, when loading, it took just as long, but instead of showing a file as the current resource, it showed the stream. 但是,加载时只花了很长时间,但没有显示文件作为当前资源,而是显示了流。

I'd like to know how to do one or both of the following: 我想知道如何执行以下一项或两项操作:

  1. Stream a resource to reduce loading time as the wiki suggests. 按照Wiki的建议,流化资源以减少加载时间。
  2. Load only selected resources and load more when changing states (load the game music only when entering game state, etc.). 在更改状态时仅加载选定的资源并加载更多(仅在进入游戏状态等时加载游戏音乐)。

I accidentally came across the answer. 我不小心碰到了答案。

public void init(GameContainer gc, StateBasedGame game) throws SlickException{
    try{
        theme[0] = new Music("path/to/theme1.ogg",true);
    }catch (Exception e){
        e.printStackTrace;
    }
}

Adding true after the file reference indicates that you want to stream the audio. 在文件引用之后添加true表示您要流式传输音频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM