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

Author Topic: Drawing inside the Game loop / RectangleShape?  (Read 3784 times)

0 Members and 1 Guest are viewing this topic.

Chiggy_Playz

  • Newbie
  • *
  • Posts: 15
    • View Profile
Drawing inside the Game loop / RectangleShape?
« on: April 17, 2019, 10:49:16 am »
I am using a switch to check the position of the mouse click and then display the sprite at the clicked location.
I am using this. But  the sprite comes for one frame and then goes away. also i have placed the switch inside the main game loop. So is there a way I can fix the Sprite once it is spawned so that it doesnt despawn. Or is there a way so that i can place my sprite automatically Inside the rectangle and also check if cell_1 == cell_2 == cell_3 so the player wins
case Event::MouseButtonPressed:

                                if (event.mouseButton.button == Mouse::Left) {

                                        if (Cell_1.getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) {
                                       
                                                cout << "Cell 1" << endl;
                                                window.draw(sprite);
                                                sprite.setPosition(Vector2f(125.f, cell_height));
                                                window.display();
                                               

                                               
                                        }
« Last Edit: April 17, 2019, 11:19:38 am by Chiggy_Playz »

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: Drawing inside the Game loop / RectangleShape?
« Reply #1 on: April 17, 2019, 06:43:08 pm »
The code is incomplete, so it's quite hard to tell.

Are you sure you don't set another position to your sprite inside the game loop?

Also, you should not call window.draw / window.display only if a given event happened.

Keep the rendering outside of the event handling method, see examples in tutorial https://www.sfml-dev.org/tutorials/2.5/graphics-draw.php

Chiggy_Playz

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Drawing inside the Game loop / RectangleShape?
« Reply #2 on: April 17, 2019, 08:44:10 pm »
The code is incomplete, so it's quite hard to tell.

Are you sure you don't set another position to your sprite inside the game loop?

Also, you should not call window.draw / window.display only if a given event happened.

Keep the rendering outside of the event handling method, see examples in tutorial https://www.sfml-dev.org/tutorials/2.5/graphics-draw.php

actually since i am using  an if else condition in the main loop i will also have to draw the sprite there only and the problem with that is that the sprite is only visible for 1 frame so is there a way around this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing inside the Game loop / RectangleShape?
« Reply #3 on: April 17, 2019, 09:03:56 pm »
You store a reference or index in a vector or similar for all the sprites that should be rendered. Then you just iterate that vector and render those sprites.

You shouldn't have multiple clear(), draw(), display() loops, but just one.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chiggy_Playz

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Drawing inside the Game loop / RectangleShape?
« Reply #4 on: April 17, 2019, 09:23:05 pm »
You store a reference or index in a vector or similar for all the sprites that should be rendered. Then you just iterate that vector and render those sprites.

You shouldn't have multiple clear(), draw(), display() loops, but just one.

How can I do that?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing inside the Game loop / RectangleShape?
« Reply #5 on: April 17, 2019, 11:41:06 pm »
I know it's not as easy to get started with programming, we've all been there at one point, at the same time, programming is about solving problems, which pretty much always involves more than just writing code. It includes stuff like thinking about a problem in a more abstract way, being able to search for resources on your own, trying out different things you can think of, etc.
Just waiting until someone can provide you a detailed enough explanation so you understand it better, will make you progress rather slowly, because you depend yourself fully on other people, who then usually also have other things to do. ;)
Meaning, it's probably best to read some books/tutorials/etc on programming/C++/SFML.

Instead of having just one sprite, you create a container like std::vector<sf::Sprite>.
Then whenever a collision happens, you add a new sprite to the vector.
Then you just render all the sprites that are in the vector.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything