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

Author Topic: C++ SFML 2.2 graphics not rendering  (Read 3115 times)

0 Members and 1 Guest are viewing this topic.

KuziNs

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
C++ SFML 2.2 graphics not rendering
« on: June 08, 2015, 07:14:24 pm »
I'm just playing around with C++ SFML stuff and I kinda don't understand why my code isn't working. The thing I want to do is to draw like let's say 5, squares in Window randomly placed around the screen using vector, but I don't understand why it's not working. And it doesn't give any error as well, I can open game like normal, but it's just not rendering.

This is the main game class:

#include "main_game.h"
#include "main_menu.h"

void main_game::Initialize(sf::RenderWindow* window)
{
    this->Player = new player();
    this->Player->setOrigin(this->Player->getGlobalBounds().width / 2, this->Player->getGlobalBounds().height / 2);

    this->TestObject = new testObject();
    this->TestObject->Initialize();
    this->TestObject->setOrigin(this->TestObject->getGlobalBounds().width / 2, this->TestObject->getGlobalBounds().height / 2);
}

void main_game::Update(sf::RenderWindow* window)
{
    this->Player->setPosition(sf::Mouse::getPosition(*window).x, sf::Mouse::getPosition(*window).y);
    this->Player->Update();

    if (this->Player->CheckCollision(TestObject))
    {
        this->TestObject->setColor(sf::Color::Red);
    }
    else
    {
        this->TestObject->setColor(sf::Color::Cyan);
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
    {
        coreState.SetState(new main_menu());
    }
}

void main_game::Render(sf::RenderWindow* window, std::vector<sf::Sprite> sprites)
{
    this->TestObject->Render(*window, sprites);

    window->draw(*this->Player);
}

void main_game::Destroy(sf::RenderWindow* window)
{
    delete this->Player;
    delete this->TestObject;
}

This is the testObject.h class

#pragma once

#include "entity.h"

class testObject : public Entity
{
public:
    testObject();
    void Initialize();
    void Render(sf::RenderWindow &window, std::vector<sf::Sprite> sprites);
    void Update();
private:
    sf::RenderWindow window;
};

And this is testObject.cpp class

#include "testObject.h"

testObject::testObject()
{
    this->Load("testObject.png");
}

void testObject::Initialize()
{
    sf::Texture testObjectTexture;
    sf::Sprite testObjectSprite;

    testObjectTexture.loadFromFile("testObject.png");
    testObjectSprite.setTexture(testObjectTexture);

    std::vector<sf::Sprite> sprites(5, sf::Sprite(testObjectSprite));

    srand(time(0));

    for (unsigned int i = 0; i < sprites.size(); i++)
    {
        sprites[i].setPosition(1 + (rand() % 1024 - 32), rand() % 640 - 32);
    }
}

void testObject::Render(sf::RenderWindow &window, std::vector<sf::Sprite> sprites)
{
    for (unsigned int i = 0; i < sprites.size(); i++)
    {
        window.draw(sprites[i]);
    }
}

void testObject::Update()
{
    Entity::Update();
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: C++ SFML 2.2 graphics not rendering
« Reply #1 on: June 08, 2015, 07:21:09 pm »
Do you have a background in Java? Because your code looks a lot like Java and less like C++. Also class constructors and destructors exist for a reason. ;)

As for the rendering, you need to call clear() then draw() all your objects and finally call display(), otherwise nothing will show up.

Also since this is the second post in a short time, maybe you should spend a bit more time trying to figure out the issues on your own, I mean that is was a programmer does. ;)
« Last Edit: June 08, 2015, 07:23:25 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/

KuziNs

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: C++ SFML 2.2 graphics not rendering
« Reply #2 on: June 08, 2015, 07:24:46 pm »
No I have c++ in Visual Studio 2013.

As it goes for rendering I have that in main.cpp where there is window.clear(); and window.display();

#include <Windows.h>
#include <iostream>

#include "game_state.h"
#include "main_menu.h"


game_state coreState;
bool quitGame = false;

int main()
{
        sf::RenderWindow window(sf::VideoMode(1024, 640), "Game");

        window.setMouseCursorVisible(false);

        coreState.SetWindow(&window);
        coreState.SetState(new main_menu());

        sf::Clock timer;
        sf::Time elapsed;

        while (window.isOpen())
        {
                sf::Event event;

                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                elapsed = timer.getElapsedTime();
                if (elapsed.asMicroseconds() > 16666)
                {
                        window.clear(sf::Color::Black);

                        coreState.Update();
                        coreState.Render();

                        window.display();

                        if (quitGame)
                        {
                                window.close();
                        }
                        timer.restart();
                }
        }
        return 0;
}

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: C++ SFML 2.2 graphics not rendering
« Reply #3 on: June 08, 2015, 08:16:04 pm »
This does not pass code-review (at least not if I'm the reviewer). For (at least) the following reasons:

- lots of explicit this dereferences that are entirely pointless.
- 'old style' for loops where range-based for would be much clearer and potentially faster.
- numerous uses of (incorrect) explicit types causing potential truncation where auto would both get the correct type and (arguably) be more clear.
- use of an explicit Initialize() function where the constructor would seem to be the better fit.
- explicit memory management (new/delete) where use of RAII and smart pointers would be much clearer, safer and less error prone.
- return of 0 from main which should either be left out (since the standard guarantees the return value from main) or at least be EXIT_SUCCESS.
- use of srand/rand where you should be using the functions from <random> instead - don't use rand.
« Last Edit: June 08, 2015, 08:23:40 pm by Jesper Juhl »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: C++ SFML 2.2 graphics not rendering
« Reply #4 on: June 08, 2015, 08:41:17 pm »
To answer your question about why sprites aren't showing up, it probably has to do with the texture you are creating going out of scope. In testObject::Initialize() you are creating a texture and then setting a sprite to use that texture. However, sprites only store pointers to the original texture, they don't copy it. When your function returns, that texture will go out of scope and the sprite will now be pointing to an invalid location in memory. You need to find a way to manage your textures so that they don't go out of scope.

KuziNs

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: C++ SFML 2.2 graphics not rendering
« Reply #5 on: June 08, 2015, 08:45:16 pm »
Okay, I don't understand anything of that stuff. :D.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: C++ SFML 2.2 graphics not rendering
« Reply #6 on: June 08, 2015, 08:47:11 pm »
Time to go research/learn then...

KuziNs

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: C++ SFML 2.2 graphics not rendering
« Reply #7 on: June 08, 2015, 08:50:54 pm »
Yeah, well I was just expermenting with stuff and how it all works. I don't really need like super efficient and super correct code. Just wanted to know, WHY it doesn't work as I expected to.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: C++ SFML 2.2 graphics not rendering
« Reply #8 on: June 09, 2015, 12:45:48 am »
As has been said, your texture goes out of scope. If you don't understand that, you really have to read up on scopes in C++, it's a fundamental topic. ;)

There are also red boxes about this in the official tutorials.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/