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

Author Topic: Beginner Code Problems  (Read 6293 times)

0 Members and 1 Guest are viewing this topic.

Friend

  • Newbie
  • *
  • Posts: 12
    • View Profile
Beginner Code Problems
« on: September 22, 2010, 08:58:30 pm »
Hi, I'm new to SFML and new to programming in general. I have started self learning C++ a few days ago as well as SFML. I have read through and did most of the beginner tutorials on this website.

I've been trying to test out how to display something which has the ability to fade in and fade out and such. I tried several things, including the following code which I thought was going to work:
Code: [Select]

while (true)
{
    for (int j = 0; j <255 ; ++j)
    {
        Sprite.SetColor(sf::Color(255, 255, 255, j));
        App.Draw(Sprite);
    }
    for (int k = 255; k !=0 ; --k)
    {
        Sprite.SetColor(sf::Color(255, 255, 255, k));
        App.Draw(Sprite);
    }
}


But, it doesn't. I assume I'm using the Draw() function completely wrong. I read through the documentations about it but it's really hard to make sense of how that all works as a beginner.

Any help would be appreciated. If I'm just dumb and you don't feel like answering, if there's any resources that I can read, feel free to link it so I can learn. Thanks in advance!

Beliar

  • Newbie
  • *
  • Posts: 27
    • View Profile
Beginner Code Problems
« Reply #1 on: September 22, 2010, 09:24:30 pm »
Well, since nothng is drawn until App.Display() is called this won't, of course, work that way.*

What i would do is have a variable for the alpha value, change that each frame - either by a fixed value or based on the frame time - and then set the color of the Sprite. (And then draw the sprite and call App.Display() of course)

I leave the actual implementation to you.

*This assumes that this block is how it acually appears in your code.
Debuggers don't remove bugs. They only show them in slow motion.

Friend

  • Newbie
  • *
  • Posts: 12
    • View Profile
Beginner Code Problems
« Reply #2 on: September 22, 2010, 09:26:07 pm »
Quote from: "Beliar"
What i would do is have a variable for the alpha value, change that each frame


Hmm, do you know how I can do that? I'm not too clear on how frames work and I haven't found any tutorials/guides as to practicing working with them.

Beliar

  • Newbie
  • *
  • Posts: 27
    • View Profile
Beginner Code Problems
« Reply #3 on: September 22, 2010, 09:31:35 pm »
here, this code block is from a tutorial:

Code: [Select]
   
int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
   
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}



Each iteration of the code inside the "while (App.IsOpened())" loop is basically a frame.
Debuggers don't remove bugs. They only show them in slow motion.

Friend

  • Newbie
  • *
  • Posts: 12
    • View Profile
Beginner Code Problems
« Reply #4 on: September 22, 2010, 09:52:31 pm »
Oh I see, I didn't know that. Thanks so much!

I managed to get it work by putting a for loop right after the while App.IsOpened got declared, and ended it after App.Display.

However, this seems really inconvenient and unwieldy, especially if the program is designed to have fade in text, then fade out, then fade in another line of text, etc. Is there maybe a better way to do this? Maybe I can build some kind of function where you give it the string of text and the location of where you want the text displayed and have some kind of fading effect built into that function?

Sorry for being a total noob.

Beliar

  • Newbie
  • *
  • Posts: 27
    • View Profile
Beginner Code Problems
« Reply #5 on: September 22, 2010, 10:41:22 pm »
No, this is not what i meant.

Her is an example
Code: [Select]

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
    sf::Image Image;
    Image.LoadFromFile("<PathToImage>")
    sf::Sprite Sprite(Image);

    unsigned int Alpha = 255; //Used to store the alpha value
    int Direction = -1;
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }
         Alpha += Direction
         if(Alpha = 0) Direction = 1 //If Alpha is 0 then start increasing it
         else if (Alpha = 255)  Direction = -1 //Else if Alpha is 255 start decreasing it
        // Clear the screen (fill it with black color)
        App.Clear();
        Sprite.SetColor(sf::Color(255, 255, 255, Alpha)); //Set the Alpha value of the color to the stored Value
        App.Draw(Sprite) ///Draw the Sprite
        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


[edit]
I just realized that the code had a logical error. Now it should work. (Didn't test it though)
[/edit]
Debuggers don't remove bugs. They only show them in slow motion.

Friend

  • Newbie
  • *
  • Posts: 12
    • View Profile
Beginner Code Problems
« Reply #6 on: September 22, 2010, 10:44:55 pm »
I see, I understand now.

App.Display() is when the frame is displayed. Everything that happens before then is just setting up the frame/scene for it to be displayed.

In the loop, App.Display() occurs once every iteration, meaning that every loop would be a "frame." So, if I want a particular frame to loop a certain way, all I need to do is do App.Display(), of course, with setup beforehand.

Thanks so much for all this, it was very helpful and I'll be on my way to continue the program.

Beliar

  • Newbie
  • *
  • Posts: 27
    • View Profile
Beginner Code Problems
« Reply #7 on: September 22, 2010, 10:49:30 pm »
Yes, that is basically correct.
Debuggers don't remove bugs. They only show them in slow motion.

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Beginner Code Problems
« Reply #8 on: September 23, 2010, 08:36:23 am »
Quote from: "Friend"
Is there maybe a better way to do this? Maybe I can build some kind of function where you give it the string of text and the location of where you want the text displayed and have some kind of fading effect built into that function?
Since you understood basics, now you can create an object containing a string, a begining alpha value, a ending alpha value, and a speed changing value in order to make some text appears or disappears easily...
Mindiell
----

Friend

  • Newbie
  • *
  • Posts: 12
    • View Profile
Beginner Code Problems
« Reply #9 on: September 24, 2010, 10:51:42 pm »
Hi everyone, I have another very beginner question.

I've been trying to write some functions involving sf::RenderWindow and sf::Event. For example, I try to write a void function to do repetitive display work (like playing some cutscenes, etc), but I'm trying to make it so if the player presses esc, then the cutscene would skip or bring up menus or something.

So, this means I need this section from the tutorials
Code: [Select]

        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }


But the problem is if I try to pass sf::RenderWindow into the function, it gives me odd errors which involve sfml's own files. I'm obviously doing something wrong here, is there some way around this/a better way to do it?


EDIT:
Another example, a typewriter function I just wrote. I plan to implement this graphical thing into the program, but the App.Display(); and App.Draw(Text); things require the sf::RenderWindow to be passed? into the function?

(Not yet configured into SFML form due to conflict with the renderwindow thing.
Code: [Select]
void typewriter(string s)
{
    for(int i = 0; i < s.size(); ++i)
    {
        for (int j = 0; j < i; ++j)
            cout << s[j];
        cout << s[i];
    }
}

It feels almost as if Ninjastrum has the same problem I do.


EDIT: that was very dumb of me. A friend pointed out that I could've just used global variables to define App, which I totally forgot I can do. Problem completely solved.

Friend

  • Newbie
  • *
  • Posts: 12
    • View Profile
Beginner Code Problems
« Reply #10 on: September 25, 2010, 03:37:34 am »
Hey again, this time I really came across something I can't figure out.

I wrote a text display function so that the text will have a type writer effect (1 letter displayed at a time until the whole string is displayed).

Code: [Select]
void writer (string s, int xpos, int ypos) //s is the whole string planned to be displayed, xpos and ypos are the position coordinates of the string.
{
    vector<char> vechars; //vector to store the letters from the string one by one.
    string text(s.size(), ' '); //a second string to be put into sf::String with the portion of the original string taken from the vector to be displayed.

    for (int i = 0; i <= s.size(); ++i)
    {
        vechars.push_back(s[i]); //put a letter from s to the vector
        for (int j = 0; j <= vechars.size(); ++j)
        {
            text[i] = vechars[i]; // map all the chars from the vector to the new string.
        }
        sf::String temptext(text, MyFont, 25);// put the new string into SFML to be displayed.
        temptext.Move(xpos, ypos);
        App.Draw(temptext);
        for (int w = 0; w < 10; ++w)
        {
            App.Display(); // display it for 10 frames per letter so the effect lasts longer.
        }
    }
}


Supposedly, this code should work, and it does, everything does run. however, when the letters are being displayed one by one, the entire string is constantly very jittery and flickers a lot. At first I assumed that this was before I did not do App.Clear(); before every frame, but then I realized I can't really do it because then I would have to pass EVERYTHING (background images, other images, etc) to the function to even get it to run.

Taking out the for loop for the 20 frame per letter display makes it not jittery anymore, but the font looks a lot fatter than it should, making it impossible to be read. I assume this is a problem with not properly clearing the screen every time app.display is called, but I don't really know how I can clear the screen if there's already background images which can't really be passed into the function.

 

anything