Results 1 to 14 of 14

Thread: Clearer definition of how sprites work.

  1. #1
    Lesser Knight jmguy02's Avatar
    Join Date
    Apr 2009
    Location
    Georgia
    Posts
    43

    Default Clearer definition of how sprites work.

    I'm utilizing sprites, actually many sprites simultaneously (say about 20), and needing to control the movement, specifically the progression, very tightly. I'm not totally grasping the interaction between my sprite object and the xml resource description.

    For example:

    I have some code setup to show a sprite and everytime I press the letter "R" it progresses 0.01 milliseconds. My speed setting in the XML resource file is set to 1. So everytime I press "R" does that mean I'm stepping to the next frame of the sprite or what? I'd like to step from frame to frame ....to frame but I'm not sure I'm doing that. Seems like I may be getting larger progressions that I don't want.

    On another associated note: The reason I'm using sprites for this function is that my setPos(x, y) function won't work properly with multiple objects. This method was taken from SpriteRTS in the examples of ClanLib. If I try to move three objects that use the same TGA file then I graphically loose the other two. They just disappear while one object moves like it's suppose to. Not sure why this happens.

    Can anyone shed some light?

    I'm using ClanLib 0.8.1 with plans to move to 2.0.1 when I can get another computer to install it on.

  2. #2
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Bergen, Norway
    Posts
    588

    Default

    Its been along time since I looked at this, but if i recall correctly the speed settings in the xml is number of milliseconds. So if you enter 1 there, you need to call sprite->update(0.001); to increase it one frame.

    Regarding your second question, I have no idea.

  3. #3
    Lesser Knight jmguy02's Avatar
    Join Date
    Apr 2009
    Location
    Georgia
    Posts
    43

    Default

    Okay, yes that seems to work. Thanks.

    So what if I want to increase the speed in which an object moves but I still want to see each frame? I assume I need to increase the frame rate. How is that done, or can that be done with ClanLib?

  4. #4
    Knight
    Join Date
    Feb 2009
    Location
    I looked through my window, there are no signs ... but, its definitely planet Earth.
    Posts
    88

    Default

    I think that your approach is not good.

    I'm also using version 0.8.1 and I made several animations in Blender. Blender output was an array of images. I made a class that is using CL_Sprite to animate it based on delta time, because different machines have different speeds. In that way speed is maintained across different platforms. My class is using CL_Sprite::set_frame( unsigned int frame ) to update the animation and it works perfectly, because delta time is float and set_frame is integer so the animation is updated at exactly the same moment on all machines.

  5. #5
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Bergen, Norway
    Posts
    588

    Default

    CLanLib sprite does use delta-time, so I'm not sure what you did different alex. If you just call sprite->update() it calculates the delta time automatically. You can also feed it the delta time manually in update(), but normally that is not neccessary, unless you maybe want to run them in slow-mo or speeded up.

  6. #6
    Knight
    Join Date
    Feb 2009
    Location
    I looked through my window, there are no signs ... but, its definitely planet Earth.
    Posts
    88

    Default

    Oh, I didn't know that ) ... I have animation that is 25 frames per second, I'm not using time wait in main loop that waits if the machine is too fast. From the start I'm using delta time that is calculated and my wanted frame rate which is 25 frames per second. In movement formulas and all other formulas I multiply with that delta time. I was not able to find where the delta time is calculated by ClanLib and how I could fetch it during run-time. How could I fetch delta time calculated by ClanLib? If that can be done, then I could remove my redundant code.

  7. #7
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Bergen, Norway
    Posts
    588

    Default

    There is no builtin function that returns the deltatime, but the code is simple:

    void update()
    {
    static int lastTime = CL_System::get_time();
    int currentTime = CL_System::get_time();
    float deltaTime = (currentTime - lastTime) / 1000.0f;
    lastTime = currentTime;

    sprite->update(deltaTime);
    }

    or to have it done automatic:

    void update()
    {
    sprite->update();
    }


    Another reason for making the sprite::update taking in an optional deltatime is to make it possible to use fixed-time updates. (If you only update the animations every 25 frames like you do).

  8. #8
    Knight
    Join Date
    Feb 2009
    Location
    I looked through my window, there are no signs ... but, its definitely planet Earth.
    Posts
    88

    Default

    Yes that is very close to my code, so I did the right thing after all? I'm using delta time everywhere. Thanks.

  9. #9
    Lesser Knight jmguy02's Avatar
    Join Date
    Apr 2009
    Location
    Georgia
    Posts
    43

    Default

    I think I'm getting confused here. What I'm experiencing is that I have 10 frames of animation. I'm calculating deltatime as shown and using sprite->update() to step through the frames as discussed. I don't seem to be able to figure out how to specify my timing between the XML sprite definition and the update time. What I see is that the faster I want to go the more frames get skipped until the animation basically falls apart. I like the sprite->set_frame idea since I could specify to step through every frame at any given time.

    Here are some snippets of what I'm doing.

    <sprite name="car_roller_3" base_angle="0">
    <image file="Gfx/carsprites/car_3_roll_0.tga" />
    <image file="Gfx/carsprites/car_3_roll_1.tga" />
    <image file="Gfx/carsprites/car_3_roll_2.tga" />
    <image file="Gfx/carsprites/car_3_roll_3.tga" />
    <image file="Gfx/carsprites/car_3_roll_4.tga" />
    <image file="Gfx/carsprites/car_3_roll_5.tga" />
    <image file="Gfx/carsprites/car_3_roll_6.tga" />
    <image file="Gfx/carsprites/car_3_roll_7.tga" />
    <image file="Gfx/carsprites/car_3_roll_8.tga" />
    <image file="Gfx/carsprites/car_3_roll_9.tga" />
    <translation origin="left" />
    <rotation origin="center" />
    <animation pingpong="no" speed="1" />
    </sprite>

    float Game::calcTimeElapsed()
    {
    static float lastTime = 0;

    float newTime = (float)CL_System::get_time();
    if(lastTime == 0)
    lastTime = newTime;

    float deltaTime = (newTime - lastTime) / 1000.0f;
    lastTime = newTime;

    return deltaTime;
    }

    spriteBody->update(deltaTime returned from function);

    The thing I don't like about using deltaTime is that the animation becomes inconsistent for some reason and I don't get uniform animation every frame or at least I have a difficult time reaching that point. I can change the XML speed setting to all sorts of numbers but never get a consistent animation. I'm missing something....this can't be that hard.

  10. #10
    Lesser Knight jmguy02's Avatar
    Join Date
    Apr 2009
    Location
    Georgia
    Posts
    43

    Default

    I've made some progress to getting what I'm looking for out of the sprites now. I can take one sprite and move fast enough to meet my needs. The problem appears to be less on timing but more on object (sprite) count. As I add more objects (sprites) to be drawn things begin to slow down and I start getting these errors listing when I exit the game like

    [driAllocateTexture:636] unable to allocate texture

    Am I exceeding some limit on sprite size? My ideal sprite graphic is 45 frames and there are at least 12 sprites on the screen at the same time. Currently I'm only placing two or three to test and I get the errors by have only two.

    So, in short...with one sprite the action is good. With two sprites the action noticeably slows and unusable. I'm doing something wrong but don't know what it is. Anybody got any suggestions?

    What I'm trying to do is simulate rollers on a slot machine. Just blurring the object won't do. Need to actually see the objects fly by.

  11. #11
    Knight
    Join Date
    Feb 2009
    Location
    I looked through my window, there are no signs ... but, its definitely planet Earth.
    Posts
    88

    Default

    Actually, I'm using speed factor to multiply everything that depends on the frame rate with. Delta time is just a part of the formula. This works perfectly.

    CFPS::SpeedFactor = (( CL_System::get_time() - _lastTime ) / 1000.0f) * _wanted_fps;

    Its static so that I could access it from anywhere.

    I'm not that good in C++ so give me a break, I'm under stress.

  12. #12
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Quote Originally Posted by alexv1 View Post
    CFPS::SpeedFactor = (( CL_System::get_time() - _lastTime ) / 1000.0f) * _wanted_fps;

    Its static so that I could access it from anywhere.
    That is how I do it.

    Also keeping it static is more efficient, as CL_System::get_time() uses up cpu cycles. Calling get_time() multiple times in the same frame may give different values - giving unexpected results, depending on the application
    Last edited by rombust; 05-19-2009 at 06:06 PM. Reason: static comment

  13. #13
    Knight
    Join Date
    Feb 2009
    Location
    I looked through my window, there are no signs ... but, its definitely planet Earth.
    Posts
    88

    Default

    I'm displaying speed factor value on screen as well as wanted and real fps and the speed factor does not change most of the time and when it does its just a split of a second, almost unnoticeable, but its so rare, I'm not sure why I'm mentioning this. Naturally, with more and more calculations per frame, it will change and will show when the code becomes a bottleneck.

    Before, when I used CL_FramerateCounter::set_fps_limit, it was not going well, very inconsistent, speedups, slowdowns and when I switched to the above formula, everything became normal, both on slow machine and on fast one, exactly the same.

    I'm using 0.8.1, you probably solved the issues with new versions.

  14. #14
    Lesser Knight jmguy02's Avatar
    Join Date
    Apr 2009
    Location
    Georgia
    Posts
    43

    Default

    This sprite issue is taking an inordinate amount of time and I'm not getting it working as needed. Anyone willing to accept payment for helping out would be appreciated. I can send the whole project, which isn't very large at this point. This is the last major requirement necessary for me to get this thing moving forward. Anybody interested?

    Using Ubuntu 8.04 OS and ClanLib 0.8.1.

    Thanks in advance.

Similar Threads

  1. Sprites and more...
    By Ledorax in forum Novashell Game Creation System
    Replies: 8
    Last Post: 09-28-2009, 04:47 PM
  2. Problem: Using sprites after a gui.run
    By nokturn in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 05-16-2007, 06:44 PM
  3. Sprites
    By lixopmstp in forum Official ClanLib SDK Forums
    Replies: 4
    Last Post: 01-21-2007, 02:36 AM
  4. Sprites and Surfaces
    By ART_Adventures in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 09-22-2006, 10:03 AM
  5. sprites
    By seyah3 in forum Dink Smallwood HD
    Replies: 0
    Last Post: 02-03-2006, 03:05 AM

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
  •