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

Author Topic: SFML 2.0 Drawing random colored shape  (Read 10532 times)

0 Members and 3 Guests are viewing this topic.

Vladislav

  • Newbie
  • *
  • Posts: 10
    • View Profile
SFML 2.0 Drawing random colored shape
« on: October 10, 2013, 03:24:51 pm »
Hi all! I' m trying to realize drawing random colored shape with SFML 2.0. But i have a problem - shape is not dispalyed.
Code:
//array of colors for random selecting
const sf::Color colorArray[5]={sf::Color::Cyan, sf::Color::Blue, sf::Color::Green, sf::Color::Red, sf::Color::Yellow };
srand(time(NULL));
RenderWindow window(VideoMode(Rows*10, Cols*10), "Blocks");
RectangleShape block(Vector2f(10, 10));
block.setFillColor(colorArray[rand()%5]);
block.setPosition(0, 0);
window.draw(block);
I have a black screen only. What i am doing wrong?
Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 Drawing random colored shape
« Reply #1 on: October 10, 2013, 03:41:46 pm »
What i am doing wrong?

Not reading the official tutorials.
It's quite obvious that the given code is incomplete in many parts.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Vladislav

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML 2.0 Drawing random colored shape
« Reply #2 on: October 10, 2013, 03:51:44 pm »
Hmm... Probably i am wrong, but i don't see the incomplete parts. Can you show me it, please?

BurrickSlayer

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML 2.0 Drawing random colored shape
« Reply #3 on: October 10, 2013, 04:05:59 pm »
Make sure you have a window.display() in your loop.

Vladislav

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML 2.0 Drawing random colored shape
« Reply #4 on: October 10, 2013, 04:10:30 pm »
Make sure you have a window.display() in your loop.
Yes, i have.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: SFML 2.0 Drawing random colored shape
« Reply #5 on: October 10, 2013, 04:14:28 pm »
Please look at this tutorial: http://www.sfml-dev.org/tutorials/2.1/graphics-draw.php

In the first bit of code shows the correct way to open and use a Render Window. Take a look at it, and then you should easily be able to see ways that you can rewrite your code.
DSFML - SFML for the D Programming Language.

Vladislav

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML 2.0 Drawing random colored shape
« Reply #6 on: October 10, 2013, 04:17:48 pm »
My main():
int main()
{
        srand(time(NULL));
        RenderWindow window(VideoMode(Rows*16, Cols*16), "Blocks");
        RectangleShape block(Vector2f(16, 16));
        block.setFillColor(colorArray[rand()%5]);
        while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

       window.clear();
           block.setPosition(0, 0);
           window.draw(block);
       window.display();
    }
    system("pause");
    return 0;

}
In my first post of this topic i gave only key actions (without loop and events). This is full code of main();
« Last Edit: October 10, 2013, 04:20:46 pm by Vladislav »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: SFML 2.0 Drawing random colored shape
« Reply #7 on: October 10, 2013, 04:28:46 pm »
i don't see anything wrong so far...

colorArray is an array that you predefined with some sf::Colors, right?
try changing its line to
 block.setFillColor(sf::Color::White);
and let's see if it works.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Vladislav

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML 2.0 Drawing random colored shape
« Reply #8 on: October 10, 2013, 04:32:05 pm »
i don't see anything wrong so far...

colorArray is an array that you predefined with some sf::Colors, right?
try changing its line to
 block.setFillColor(sf::Color::White);
and let's see if it works.
I've tried it before, and i learned that if i use sf::Color (nor from colorArray), it works. :o
« Last Edit: October 10, 2013, 04:34:44 pm by Vladislav »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: SFML 2.0 Drawing random colored shape
« Reply #9 on: October 10, 2013, 04:44:28 pm »
are you sure it's not displaying? i tried it, and its working for me.
try this, with the std::cout output we'll know if rand() is working as it should.

using namespace sf;
int main()
{
    const sf::Color colorArray[5]={sf::Color::Cyan, sf::Color::Blue, sf::Color::Green, sf::Color::Red, sf::Color::Yellow };
    srand(time(NULL));
    RenderWindow window(VideoMode(200, 200), "Blocks");
    RectangleShape block(Vector2f(16, 16));
    std::cout << rand()%5;
    block.setFillColor(colorArray[rand()%5]);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

       window.clear();
       block.setPosition(0, 0);
       window.draw(block);
       window.display();
    }
    return 0;
}
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Vladislav

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML 2.0 Drawing random colored shape
« Reply #10 on: October 10, 2013, 05:06:13 pm »

rand() works.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: SFML 2.0 Drawing random colored shape
« Reply #11 on: October 10, 2013, 05:14:52 pm »
geez, i have no idea. let's try taking off "const sf::" from here:
Color colorArray[5]={sf::Color::Cyan, sf::Color::Blue, sf::Color::Green, sf::Color::Red, sf::Color::Yellow };

if does not work, try changing:
block.setFillColor(colorArray[2]);
and see if it works.

if everything fails, i'd suggest you to update to SFML 2.1
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Vladislav

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML 2.0 Drawing random colored shape
« Reply #12 on: October 10, 2013, 05:19:44 pm »
Shit, it doesn't work :-( I will try to update my SFML.

Vladislav

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML 2.0 Drawing random colored shape
« Reply #13 on: October 10, 2013, 05:25:48 pm »
I have SFML 2.1 now. But it still not working.
« Last Edit: October 10, 2013, 05:30:04 pm by Vladislav »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: SFML 2.0 Drawing random colored shape
« Reply #14 on: October 11, 2013, 12:21:13 am »
what?
then, i really have no idea. does this works?

#include <iostream>
#include <string>
int main(){
  std::string test[5]={"zero", "one", "two", "three", "four"};
  std::cout << test[2];
  return 0;
}

if don't, then it may be something on your compiler. if it does, then we'll need help from someone more experienced.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything