<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0,  comments - 6,  trackbacks - 0

    Sprites and Resource Management 精靈和資源管理

    The Sprite class  Sprite


    The sprite class will act as a wrapper round the standard java.awt.Image. However, its going to give us a place holder to expand things later on.

    譯:sprite類將作為一個包裝器包裝標準 java.awt.Image 類。這樣的設計是可擴展的。

    So heres the sprite class. Essentially it just takes and holds an image which can be drawn at a specified location onto a graphics context.

    譯:因此Sprite本質上只是需要,并有一個可以繪制到圖形上下文指定位置

    public class Sprite {

    /** The image to be drawn for this sprite 表示精靈的圖片*/

    private Image image;

    /**

     * Create a new sprite based on an image

     * 基于一個圖片創建一個精靈(Sprite實例)

     * @param image The image that is this sprite

     */

    public Sprite(Image image) {

    this.image = image;

    }

    /**

     * Get the width of the drawn sprite

     * 獲得表示該精靈的圖片的寬度,以像素為單位

     * @return The width in pixels of this sprite

     */

    public int getWidth() {

    return image.getWidth(null);

    }

    /**

     * Get the height of the drawn sprite

     * 獲得表示該精靈的圖片的高度,以像素為單位

     * @return The height in pixels of this sprite

     */

    public int getHeight() {

    return image.getHeight(null);

    }

    /**

     * Draw the sprite onto the graphics context provided

     * 利用參數提供的圖形上下文(g)萊繪制精靈,x和y是精靈的橫坐標和縱坐標

     * @param g The graphics context on which to draw the sprite

     * @param x The x location at which to draw the sprite

     * @param y The y location at which to draw the sprite

     */

    public void draw(Graphics g,int x,int y) {

    g.drawImage(image,x,y,null);

    }

    }

    Sprite Loading and Management 精靈的加載和管理


    An important part of any game is managing the resources that are used. In our case we're just looking at sprites. However, we do only want to load any single sprite once. It would also be handy if the collection of sprites existing in one central location. With this in mind we'll implement a SpriteStore object. This will be responsible for loading and caching sprites. To make it easy to get hold of the sprite store we'll implement it as a singleton.

    譯:管理用到的資源是任何一個游戲的重要組成部分,而我們主要關注精靈。我們希望每一個精靈只被加載一次。并且方便一群精靈出現在屏幕的中心位置。根據這種想法我們實現了一個 SpriteStore 對象。它負責加載和緩存精靈。為了易于控制精靈的存儲我們將 SpriteStore 設計成了單例。

    Implementing the singleton 實現單例


    A singleton simply means thats theres a single instance of the object available statically. In many cases this is done simply for convienience, however sometimes there are good design reasons for enforcing that there is only a single instance of the class ever created.

    譯:單例簡單來說就是一個類只有唯一的一個可用實例對象。很多情況下我們這么做是為了將問題簡化。但是有時候有更好的設計原因迫使我們限制一個類只可能被創建一個唯一的實例。

    Implementing this in our sprite store looks like this:

    譯:SpriteStore 的實現如下:

    /** The single instance of this class 當前類的唯一實例*/

    private static SpriteStore single = new SpriteStore();

    /**

     * Get the single instance of this class 

     * 獲得類的唯一實例

     * @return The single instance of this class

     */

    public static SpriteStore get() {

    return single;

    }

    The "single" is the single instance of the class ever created. The static get method gives us access to the single instance.

    譯:“單例”指一個類只能被創建的一個唯一的實例。靜態 get 方法使我們可以獲取該類唯一的實例。

    Loading the Sprites 加載精靈


    The first step is to retrieve the sprite image from disk. Generally in Java it makes sense to use the ClassLoader to find the image for you. This makes it much easier to deploy games with the resources packaged up. However, some people are more comfortable to use direct file access which is also perfectly viable. For this tutorial we'll stick to the ClassLoader since it makes webstart possible.

    譯:第一要做的是從硬盤上獲取精靈圖片。在Java中,我們通常可以使用ClassLoader來加載圖片(類加載器根據類路徑來加載資源,所有我們需要將圖片和類一起保存在包里)。這樣我們很容易將圖片資源和類一起打包發布。有些同志更愿意使用直接訪問文件這種可行的好辦法(使用標準I/O庫)。但在本文中我們堅持使用 ClassLoader ,因為它支持 webstart。(注意,加載精靈,緩存精靈都在 getSprite(String ref) 方法實現,ref為圖片路徑)

    The first step is locate the sprite:

    譯:第一步是定位精靈(其實就是定位精靈的顯示圖片):

    URL url = this.getClass().getClassLoader().getResource(ref);

    Its important to note that the chain of functions used to get to the class is only there to support specialised class loaders (like the one found in WebStart). The retrieved URL will point to the image specified in the string "ref" relative to the classpath.

    譯:重點注意上面代碼中的方法調用鏈是為了獲得當前類的專門類加載器(就像在 webstart中找到的一樣)。返回的URL 指向相對于類路徑根目錄的路徑字符串參數“ref”指定的圖片。

    The next step is to actually load in the image. In Java this is a simple matter of using the utlity class ImageIO. To load our image we use this code:

    譯:下一步要做的是加載圖片。在Java中,使用實用工具類 ImageIO來加載圖片是非常簡單的問題。我們使用下面的代碼來加載圖片:

    sourceImage = ImageIO.read(url);

    Finally, we need to allocate some accelerated graphics memory to store our image in. This will allow the image to be drawn without the CPU getting involved, we just want the graphics card to do the work for us.

    譯:最后,我們需要分配一些顯存用于存儲我們加載的圖片。這樣繪圖時就不會占用CPU資源,我們僅僅靠顯卡就可以完成繪圖工作了。

    Allocating the graphics memory is achieved like so:

    譯:我們這樣分配顯存:

    // create an accelerated image of the right size to store our sprite in

    //創建一個合適尺寸的加速圖形,將精靈保存在其中

    //GraphicsConfiguration 類描述圖形目標(如打印機或監視器)

    GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().

                               getDefaultScreenDevice().getDefaultConfiguration();

    Image image = gc.createCompatibleImage(sourceImage.getWidth(),

                                           sourceImage.getHeight(),

                                           Transparency.BITMASK);

    補充解釋:Transparency 接口定義用于實現類的通用透明模式。其 BITMASK 常量屬性表示完全不透明的圖像數據(alpha 值為 1.0)或完全透明的圖像數據(alpha 值為 0.0)。

    The final step is to draw our loaded image into our accelerated graphics image. This essentially creates our sprite:

    譯:最后一步是繪制我們加載的圖片到分配的顯存這實際上就創建了精靈:

    // draw our source image into the accelerated image

    //將源圖繪制入加速圖片

    image.getGraphics().drawImage(sourceImage,0,0,null);

    The only thing we have left to do is create our actual Sprite object.

    譯:我們需要做的唯一剩余的事情就是創建我們的精靈對象。

    Caching the Sprites 緩存精靈


    The other responsibility of our SpriteStore is to cahce the images that have been loaded. To achieve this we add a HashMap. This map will link the string references to images to the sprites that our loaded. Whenever we load a sprite we add it to the map like this:

    譯:SpriteStore 的另外一個責任是緩存已經加載的圖片。我們使用一個HashMap 來實現緩存圖片。這個 map 通過字符串來映射我們加載的精靈和精靈擁有的圖片。當我們加載一個精靈后,我們把它這樣添加到 map 里:

    // create a sprite, add it the cache then return it

    //創建一個精靈,把它添加到緩存中

    Sprite sprite = new Sprite(image);

    sprites.put(ref,sprite);

    Likewise, whenever a sprite is requested we check whether its already in the map. If it is, we return our cached copy instead of loading a new copy:

    譯:同樣地,每當需要一個精靈時我們都檢查它是否已經在這個map里了。如果存在,我們返回緩存的拷貝而不是重新加載一個新的拷貝。

    // if we've already got the sprite in the cache then just return the existing version 

    //如果我們想獲取的精靈在緩存中已經有了,則直接返回已存在的精靈

    if (sprites.get(ref) != null) {

    return (Sprite) sprites.get(ref);

    }

    學軟件開發,到蜂鳥科技!
    超強的師資力量 、完善的課程體系 、超低的培訓價格 、真實的企業項目。
    網址:www.ntcsoft.com
    電話:0371-63839606
    鄭州軟件開發興趣小組群:38236716
    posted on 2010-11-25 23:52 whistler 閱讀(441) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(2)

    我參與的團隊

    文章檔案(22)

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 国产激情免费视频在线观看| 亚洲国产成人精品无码区二本 | 亚洲精品视频在线看| 亚洲人成小说网站色| 久久久久av无码免费网| 激情内射亚洲一区二区三区| 性xxxxx大片免费视频| 亚洲精品在线不卡| 91久久成人免费| 亚洲国产精品一区二区三区在线观看| 91网站免费观看| 久久精品国产亚洲αv忘忧草| 日韩吃奶摸下AA片免费观看| 亚洲欧美成人一区二区三区| 国产zzjjzzjj视频全免费 | 亚洲七久久之综合七久久| 国产免费久久精品| 亚洲免费视频一区二区三区| 亚洲国产精品无码av| 91人人区免费区人人| 成人亚洲国产va天堂| 亚洲国产综合无码一区二区二三区 | 亚洲AV午夜成人影院老师机影院| 美女内射毛片在线看免费人动物| 久久久婷婷五月亚洲97号色| 在线观看的免费网站无遮挡| 亚洲AV无码乱码麻豆精品国产| 免费羞羞视频网站| 婷婷久久久亚洲欧洲日产国码AV | 毛片a级三毛片免费播放| 国产成人综合亚洲绿色| 国产L精品国产亚洲区久久| 永久免费AV无码网站国产 | 无码一区二区三区亚洲人妻| 亚洲国产免费综合| 小日子的在线观看免费| 亚洲熟妇无码一区二区三区| 亚洲欧洲中文日韩久久AV乱码| 日本免费在线中文字幕| 亚洲国产精品嫩草影院| 亚洲理论电影在线观看|