Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Window not showing!!!  (Read 39845 times)

0 Members and 4 Guests are viewing this topic.

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
SFML Window not showing!!!
« Reply #45 on: December 24, 2011, 08:20:34 pm »
Can you show me the code? Just the line where you set the subrect.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Window not showing!!!
« Reply #46 on: December 24, 2011, 09:00:33 pm »
Umm nvm i fixed the problem i had to do :  

Code: [Select]
sf::Sprite::SetSubRect(sf::IntRect(0,0,60,60));  

The int rect thingy. but now i have to understand the clock timer and hwo to make it switch frames. The only thing about switching frames i dont get is the "left" and "top" parameters. What does it mean by left or top?  Too general.  And also i need to understand the sf::Clock   functions. GetElapsedTime() and Reset(). I read the documentation but i got lost when it said you multiply the getelap.. by 1000 or something.

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
SFML Window not showing!!!
« Reply #47 on: December 24, 2011, 09:14:49 pm »
Oops, I forgot about sf::IntRect. Anyways, say you have a 100x100 square. You want to crop the image to a 10x10 sprite in the middle. You would do this:

Code: [Select]

sf::Image image;

if(!image.LoadFromFile("100x100.png")
{
    return 0;
}

sf::Sprite sprite(image);
sprite.SetSubRect(sf::IntRect(45, 45, 10, 10));



The sprite is a rectangle with a height and width of 10, and whose leftmost pixel is at column 45 of the image from the left and whose topmost pixel is at row 45 of the image from the top.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Window not showing!!!
« Reply #48 on: December 24, 2011, 09:25:20 pm »
Ok i see. Thanks!   And the Clock functions?  I don't understand them. When i had :          

Code: [Select]

float time = GetElapsedTime;
cout << time <<endl;




it just gave me a bunch of 1s and 2s. They werent even floats. So i have a few questions:  First, how do you control the time? So how do you code like if 2 seconds pass, go to next frame?  GetElapsedTime() is in seconds or milliseconds? And so far everyone has said that the GetElapsedTime() function gives you the amount of time AFTER the reset(). But what about the first loop?  Because look at this code:

Code: [Select]


sf::Clock clock;

while()
{

do all events and stuff..


clock.GetElapsedTime();
clock.Reset();

}


So after the Reset function it restarts. But what about when its the FIRST time? So the loop begins and sees the function GetElapsedTime().. but it hasnt seen reset yet so what happens?


Please clarify thanks!

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
SFML Window not showing!!!
« Reply #49 on: December 24, 2011, 09:35:43 pm »
Well, first, you don't time the frames. A frame is one run through the game loop. sf::Clock::GetElaspsedTime() gets the time between when you last reset (or created) the clock and when you call the function. GetElapsedTime() in SFML 1.6 is seconds (float), but in SFML 2.0 it's in milliseconds (UINT32).

EDIT: I mixed up SFML 1.6 and SFML 2.0; changed it.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Window not showing!!!
« Reply #50 on: December 24, 2011, 10:17:59 pm »
No..by frame i meant a frame in a spritesheet. So if i wanted to have 1 second before it switched (by using setsubrect) or half a second or so on. How do i manipulate the time?

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
SFML Window not showing!!!
« Reply #51 on: December 24, 2011, 11:11:56 pm »
Code: [Select]

if(clock.GetElapsedTime() >= 1)
{
    sprite.SetSubRect(sf::Intrect(frame * width, animation * height, width, height));
    ++frame;

    clock.Reset();
}



This would change the frame every second, but try to problem solve on your own sometimes; it's very important in programming. "animation" is for if you have multiple animations in one spritesheet. The first frame would be 0 and the first animation would also be 0.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
SFML Window not showing!!!
« Reply #52 on: December 25, 2011, 01:53:43 am »
The code Pyrius gave now is very similar to the code I gave you earlier, but mine was for SFML 2.0.

Are you reading the documentation? About all of your answers are there, waiting for you to read.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Window not showing!!!
« Reply #53 on: December 25, 2011, 04:21:06 am »
I dont like the documentation. Its too hard to find stuff. I find tutorials to be much more useful. And btw i tried the code but I got some messed up blinking really fast image. I tried changing the time but it failed. I didnt get a smooth animation. Here is my code:

Code: [Select]

 if(clock.GetElapsedTime() >= 20)
        {
            sprite.SetSubRect(sf::IntRect(frame * 68,frame2,68,68));
            ++frame;
            clock.Reset();
            if(frame > 5)
            {
                frame = 0;
                ++frame2;

            }
            Window.Draw(sprite);

        }


The "frame2" parameter was because the spritesheet had rows and colums. And then i put an inner if statement to check if the frames were greater than the number of "elements" in the spritesheet. So then it would reset back the frame to 0 and frame2 would be incremented because it was now on the second row, or third row and so on.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
SFML Window not showing!!!
« Reply #54 on: December 25, 2011, 10:36:08 am »
Your Draw call should be out of the ifs. The rest of the logic depends on which version of SFML you are using.

If you are using SFML2, one new frame every 20 milliseconds should be about 50 frames a second. I don't know if this is the speed you are after.

If you are using SFML 1.6, the frame will be changed each 20 seconds, what I find strange.

By the way, your logic was correct this time, except for the Draw part.

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
SFML Window not showing!!!
« Reply #55 on: December 25, 2011, 11:04:41 am »
You just wrote "frame2", so the animation will move down one pixel when you increment it. Also, it might be easier to write "frame %= totalFrames" and have each animation in the image take up just one row.

Use this to help you get started: http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition.aspx. Don't just copy and paste, though; I'd recommend making a different game and once the tutorials get more specific to the game he's creating, continue on your own.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
SFML Window not showing!!!
« Reply #56 on: December 25, 2011, 05:54:42 pm »
I havent saw that, sorry. Pyrius is right.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Window not showing!!!
« Reply #57 on: December 25, 2011, 06:39:16 pm »
Its still messed up when i set it to 1 second. The spritesheet walks(somewhat) but a black thing comes over the little box in the corner which is the subrect. What i mean is, a black thing keeps flickering through the animation. There is no black in the spritesheet so..

Anata

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Window not showing!!!
« Reply #58 on: December 25, 2011, 06:52:19 pm »
Smooth to false for this

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Window not showing!!!
« Reply #59 on: December 25, 2011, 08:55:57 pm »
Quote from: "Anata"
Smooth to false for this



What...?