Results 1 to 2 of 2

Thread: CL_Sprite question

  1. #1

    Default CL_Sprite question

    So I'm currently using ClanLib 2.3 to make a game. I'm using the CL_Sprite class to display players and etc. But now I'm wondering what's the most efficient way to have a lots of CL_Sprite with the same image. Because I have a couple of animation set I made for each direction. For now I saw you can't just call an empty CL_Sprite constructor and then call CL_Sprite::SetImageData(); to set the image of the sprite because no "new CL_Sprite_Impl()" is done like in other constructor. So then I just decided to pass the ressource manager and a default sprite in the ressource manager to construct the sprite, then I can just call SetImageData to switch animation since it's constructed. But I'm not sure about the efficiency of that, I read the ressource manager cache the sprite when they are loaded for the first time so that should prevent duplication of the same image I think but there was also an issue with that way, when creating a sprite that way from a different thread then the original thread the ressource manager was in it crash. Meaning the ressource manager isn't multi-thread compatible? I overcomed that issue by making a function to shedule sprite creating in first thread that fixed it but I'm still wondering if there would be a better way.

  2. #2
    Lesser Knight
    Join Date
    Nov 2010
    Location
    Winfield, BC Canada
    Posts
    32

    Default

    Create a grid image with all character directions and use an XML to load it. (sprites.xml)
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <sprite name="character">
        <image file="./sprites/character.png">
          <grid size="32,32" array="4,1" />
        </image>
        <rotation origin="center"/>
        <translation origin="center"/>
        <animation speed="50" loop="yes" pingpong="no" />
      </sprite>
    </resources>
    Create a an enum or array for each direction as they correspond to the grid.
    Code:
    enum {chr_down, chr_left, chr_up, chr_right};
    Create a class or struct to hold the character. You probably do this anyhow.
    Code:
    struct PLAYER
    {
        CL_Sprite mySprite;
    };
    Load the xml when your program inits'
    Code:
    CL_ResourceManager res_sprites("./assets/sprites.xml");
    When you create a character who has his own sprite and direction...
    Code:
    PLAYER newPlayer;
    newPlayer.mySprite = CL_Sprite(gc, "character", &resspritesui);
    When you want to change the characters orientation / facing direction use the enums to set the frame.
    Code:
    newPlayer.mySprite.iSprite_cursor_normal.set_frame(chr_left);
    I hope that helps, I'm no expert and I certainly didn't test this code.

    edit: I realize this isn't exactly what you wanted but you can just add x amount of sprites in the PLAYER struct for each direction.
    Last edited by Jarett Langton; 09-09-2011 at 03:26 AM.

Similar Threads

  1. Shaders and Cl_Sprite
    By Michaƫl Picovschi in forum Official ClanLib SDK Forums
    Replies: 11
    Last Post: 05-09-2011, 12:33 PM
  2. possibly bug in CL_Sprite and CL_CollisionOutline
    By nemcheg in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 06-20-2010, 06:17 PM
  3. CL_Sprite change to ClanLib 0.8 and 0.9
    By rombust in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 04-21-2009, 09:55 AM
  4. cl_sprite with multiple frames
    By dwune in forum Official ClanLib SDK Forums
    Replies: 7
    Last Post: 08-29-2008, 08:49 AM
  5. CL_Sprite problems
    By gmatt in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 06-29-2007, 05:38 PM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •