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

Author Topic: Can't even do the simplest thing right  (Read 3496 times)

0 Members and 1 Guest are viewing this topic.

tsanummy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Can't even do the simplest thing right
« on: January 18, 2013, 05:51:11 pm »
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <iostream>
#include <time.h>

using namespace std;


void inicializar_semilla() {
srand(time(NULL));
}
int aleatorio_mod255() {
int n = 0;
n = (rand() % 254 + 1);
return n;
}

double aleatorio_mod10() {
double n = 0.0;
n = (drand48() * (10.0-1.0));
return n;
}

double aleatorio_mod1000() {
double n = 0.0;
n = (drand48() * (1000.0-500.0));
return n;
}

double aleatorio_mod100() {
double n = 0.0;
n = (drand48() * (100.0-50.0));
return n;
}

int main () {
int colorR = 0, colorG = 0, colorB = 0;
double x1 = 0.0;
double x2 = 0.0;
double y1 = 0.0;
double y2 = 0.0;
double m1 = 0.0;
double m2 = 0.0;
int i;
sf::RenderWindow Screen (sf::VideoMode (800, 600, 32), "Title");
sf::Shape Rect [1500];
float x;
float y;

while(Screen.IsOpened ()){

    sf::Event Evento;;
    while(Screen.GetEvent (Evento)){
        if (Evento.Type == sf::Event::Closed || Evento.Key.Code == sf::Key::Escape)
            Screen.Close();

    }//End of Evento loop

    Screen.Clear();
    for (i = 0; i < 1500; i++){
        inicializar_semilla();
        x1 = aleatorio_mod1000();
        x2 = aleatorio_mod1000();
        y1 = aleatorio_mod1000();
        y2 = aleatorio_mod1000();
        colorR = aleatorio_mod255();
        colorG = aleatorio_mod255();
        colorB = aleatorio_mod255();
        printf("%d, %f", colorR,x1);
        Rect[i] = sf::Shape::Rectangle (x1,x2,y1,y2, sf::Color(colorR,colorG,colorB));
        m1 = aleatorio_mod100();
        m2 = aleatorio_mod100();
        Screen.Draw (Rect[i]);
        Rect[i].Move (m1, m2);
        Screen.Display();
        sf::Sleep(2.00);
    }





} //End of Game Loop

return 0;
}
 


I was messing with shapes and I got a sqare moving, thats nice, so I said to myself: What about I create multiple rectangles at once and make them move on the screen.

I used two random functions to set the size, color and speed of said rectangles random, I wanted tons of colors on my screen.

Okay, I advanced a bit, now it displays random rectangular shapes of various colors BUT they don't move at all, once the next shape is loading the rest stops moving I think thats the problem. Is there any way for me to make them move permanently?

Thank you

PS. BONUS:  just for testing and learning, is there any simple way to make them bounce off on the border of the screen?
« Last Edit: January 18, 2013, 06:10:36 pm by tsanummy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can't even do the simplest thing right
« Reply #1 on: January 18, 2013, 06:11:41 pm »
You shouldn't call Display() every shape iteration otherwise you'll only see one shape at a time. Also you then don't want the sleep call with in the loop, otherwise it will sleep every time one shape is drawn instead of between the frames.

Quote
PS. BONUS:  just for testing and learning, is there any simple way to make them bounce off on the border of the screen?
Depends on your definition of 'simple'. ;)
SFML doesn't provide sprite.bounce() or whatever, so you'll have to do the collision checking and response on your own. Depending on what you want to achieve, this can get quite complex or be very simple.
« Last Edit: January 18, 2013, 06:13:26 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Can't even do the simplest thing right
« Reply #2 on: January 18, 2013, 06:15:46 pm »
I don't have much time to test and analyze this through yet I will give you some tips.

If you are going to use iostream don't use cstdio(stdio.h) and the same applies backwards, that's mixing C and C++ which isn't exactly a good practice. Also use the c-libraryname, instead of libraryname.h in order to mantain the consistency in your programming.

Check that the random values are being assigned correctly, sometimes some freaky errors can be avoided by checking the data of the values you are assigning.

Make a minimal example in order to realize what is it that makes them not move, the amount of code for a trivial problem such isn't good. Make it as small as possible while reproducing the problem.

SFML 2.0 is far better than 1.6 as it fixes many bugs and has a better graphics API with many new features for both high and low level rendering. 1.6 may be "stable", but it is actually deprecated in many aspects and hasn't been touched in two years.

Read this: http://en.sfml-dev.org/forums/index.php?topic=5559.0 and this: en.sfml-dev.org/forums/index.php?topic=10306

The recommendations of eXpl0it3r apply very well and may solve the problem though it may (unlikely) be somewhere else.

Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

tsanummy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Can't even do the simplest thing right
« Reply #3 on: January 18, 2013, 06:22:27 pm »
You shouldn't call Display() every shape iteration otherwise you'll only see one shape at a time. Also you then don't want the sleep call with in the loop, otherwise it will sleep every time one shape is drawn instead of between the frames.

Quote
PS. BONUS:  just for testing and learning, is there any simple way to make them bounce off on the border of the screen?
Depends on your definition of 'simple'. ;)
SFML doesn't provide sprite.bounce() or whatever, so you'll have to do the collision checking and response on your own. Depending on what you want to achieve, this can get quite complex or be very simple.

hmm as it is now it just stacks the figures, so it doesn really clear the display in every iteration, plus the for loop does 1500 shapes and then goes back to the main loop so that would't be an issue.

I'm going to look into the sleep thing, thanks!

I'm leaving the bounce thing for another moment

I don't have much time to test and analyze this through yet I will give you some tips.

If you are going to use iostream don't use cstdio(stdio.h) and the same applies backwards, that's mixing C and C++ which isn't exactly a good practice. Also use the c-libraryname, instead of libraryname.h in order to mantain the consistency in your programming.

Check that the random values are being assigned correctly, sometimes some freaky errors can be avoided by checking the data of the values you are assigning.

Make a minimal example in order to realize what is it that makes them not move, the amount of code for a trivial problem such isn't good. Make it as small as possible while reproducing the problem.

SFML 2.0 is far better than 1.6 as it fixes many bugs and has a better graphics API with many new features for both high and low level rendering. 1.6 may be "stable", but it is actually deprecated in many aspects and hasn't been touched in two years.

Read this: http://en.sfml-dev.org/forums/index.php?topic=5559.0 and this: en.sfml-dev.org/forums/index.php?topic=10306

The recommendations of eXpl0it3r apply very well and may solve the problem though it may (unlikely) be somewhere else.

I'm following a series of tutorials that use 1.6 so I'm not quite ready to jump to 2.0 yet..
« Last Edit: January 18, 2013, 06:25:20 pm by tsanummy »

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Can't even do the simplest thing right
« Reply #4 on: January 19, 2013, 01:32:30 am »
Quote
hmm as it is now it just stacks the figures, so it doesn really clear the display in every iteration, plus the for loop does 1500 shapes and then goes back to the main loop so that would't be an issue.

Display should only be called once per iteration, not 1500 times. Also note that if you set a frame limit the display function will call sf::Sleep on it's own, so you don't have to call it explicitly and you can have an average speed in your program.

Quote
I'm following a series of tutorials that use 1.6 so I'm not quite ready to jump to 2.0 yet.

Much of the concepts used in 1.6 still apply for 2.0 and unless you start working with the low level API or doing crazy things right away you will only notice a few differences, which end up being far better than anything else 1.6 has to offer.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

7krs

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: Can't even do the simplest thing right
« Reply #5 on: January 20, 2013, 02:05:59 am »
PS. BONUS:  just for testing and learning, is there any simple way to make them bounce off on the border of the screen?

How fortunate I had some code I implement for testing my own screen refreshes:

{
    std::vector<int> x, y;
    for (int i = 0; i < 50; i++)
    {
        rs.emplace_back(new sf::RectangleShape(sf::Vector2f(10, 10)));
        rs[i]->setPosition((std::rand() % 79) * 10, (std::rand() % 59) * 10);
        rs[i]->setFillColor(sf::Color(std::rand() % 255, std::rand() % 255, std::rand() % 255));
        x.emplace_back(1);
        y.emplace_back(1);
    }

    ready_rsMover.wait();
    rs_ready.notify();
    while (keep_drawing)
    {
        for (int i = 0; i < 50; i++)
        {
            if (rs[i]->getPosition().x >= wnd->getSize().x - 10)
                x[i] = -(std::rand() % 2 + 1); // Make the counter go negative.
            else if (rs[i]->getPosition().x <= 0)
                x[i] = (std::rand() % 2 + 1); // Make the x counter plus.
            if (rs[i]->getPosition().y >= wnd->getSize().y - 10)
                y[i] = -(std::rand() % 2 + 1);
            else if (rs[i]->getPosition().y <= 0)
                y[i] = (std::rand() % 2 + 1);
            rs[i]->move(x[i], y[i]);
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(20));
        drawer_flare.notify();
    }
}
 

Focus on the body of the while (keep_drawing) loop, ignore that drawer_flare, replace that with your drawing mechanism (simply iterate over all rectangle shapes, then display).