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

Author Topic: Black rectangle on top of window  (Read 18010 times)

0 Members and 2 Guests are viewing this topic.

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Black rectangle on top of window
« on: June 27, 2016, 12:07:05 pm »
Hi!

I have rectangle (1920 x ~25) on my window and well, i don't want it. Here's screenshot:


It covers everything i put there but my system's cursor.
What can cause that problem?

//Game.hpp
#ifndef GAME_H
#define GAME_H

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/Window/Event.hpp>

class Game
    : public sf::RenderWindow
{
    sf::Texture standardCursor;
    sf::Texture sfmlLogoTex;
    bool exitGame;

    void loadingScreen();
    void mainMenu();
public:
    Game();
    int start();
};

#endif // GAME_H
 

//Game.cpp
#include "headers/game.hpp"

/*
 * Private
 */


void Game::loadingScreen()
{
    sf::Clock passedTime;
    sf::Sprite sfmlLogo(sfmlLogoTex);
    sfmlLogo.setPosition({10.f, getSize().y - sfmlLogoTex.getSize().y - 10.f});

    while(passedTime.getElapsedTime().asSeconds() < 2.f)
    {
        clear({0, 0, 0});

        sfmlLogo.setColor({255, 255, 255, passedTime.getElapsedTime().asSeconds() / 2.f * 255});
        draw(sfmlLogo);

        display();
    }

    /* Here load assets etc. */
    standardCursor.loadFromFile("assets/graphics/UI/standardCursor.png");

    while(passedTime.getElapsedTime().asSeconds() < 3.f)
        display();
}

void Game::mainMenu()
{
    sf::Sprite mainMenuCursor(standardCursor);

    while(!exitGame)
    {
        for(sf::Event ev; pollEvent(ev);)
        {
            if(ev.type == sf::Event::KeyPressed && ev.key.code == sf::Keyboard::Escape)
                exitGame = true;
        }

        clear({255, 0, 0});

        mainMenuCursor.setPosition(mapPixelToCoords(sf::Mouse::getPosition(*this)) + sf::Vector2f(0, -25));
        draw(mainMenuCursor);

        display();
    }
}

/*
 * Public
 */


Game::Game()
{
    sfmlLogoTex.loadFromFile("assets/graphics/loading/sflogo.png");
    setFramerateLimit(120);
    exitGame = false;
}

int Game::start()
{
    this->setMouseCursorVisible(true);

    loadingScreen();
    mainMenu();

    return 0;
}
 

//main.cpp
#include "game.hpp"
#include <iostream>


int main(int argc, char *argv[])
{
    Game game;
    game.create(sf::VideoMode::getDesktopMode(), "game", sf::Style::Fullscreen);

    return game.start();
}
 

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Black rectangle on top of window
« Reply #1 on: June 27, 2016, 12:20:27 pm »
That happens when i put cursor in [0, 0] position in code:


Btw sometimes (about 10%) it works ok.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Black rectangle on top of window
« Reply #2 on: June 27, 2016, 04:54:39 pm »
I have rectangle (1920 x ~25) on my window and well, i don't want it. Here's screenshot:
(click to show/hide)
It covers everything i put there but my system's cursor.
I'm not sure where this rectangle is (I think you are suggesting that it's on the top of the window) but, from the screenshot, it looks like the cursor image you are displaying is being cropped on three sides, not just the top.

That happens when i put cursor in [0, 0] position in code:
(click to show/hide)
Okay. Is this what you expect to happen or not?

I'm curious about a few things:

• Why are you inheriting Game from RenderWindow? I don't think you can consider Game to 'be' a window not least because a window have no need for start method.

• I've not seen this before but it's quite an interesting take.
for(sf::Event ev; pollEvent(ev);)

• This is constantly flipping the buffers. You need to clear and draw before each display.
while(passedTime.getElapsedTime().asSeconds() < 3.f)
        display();

• It looks like you're clearing the window with a red colour. Where is it?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Black rectangle on top of window
« Reply #3 on: June 27, 2016, 08:49:31 pm »
I'm not sure where this rectangle is (I think you are suggesting that it's on the top of the window) but, from the screenshot, it looks like the cursor image you are displaying is being cropped on three sides, not just the top.
Sorry for bad screenshot, it's only one side - top.

Okay. Is this what you expect to happen or not?
Cursor should be in top-left corner, it isn't.

• Why are you inheriting Game from RenderWindow? I don't think you can consider Game to 'be' a window not least because a window have no need for start method.
In my case it's very handy. ;)

• I've not seen this before but it's quite an interesting take.
for(sf::Event ev; pollEvent(ev);)
I always use it to make "ev" visible in event loop only.

• This is constantly flipping the buffers. You need to clear and draw before each display.
while(passedTime.getElapsedTime().asSeconds() < 3.f)
        display();
It used it many times and never had any problems. :|

• It looks like you're clearing the window with a red colour. Where is it?
Changed that for tests, only difference is that all screen (including rectangle) is red.

I found out what was the problem -
sf::VideoMode::getDesktopMode()
gives wrong resolution, changing it to
sf::VideoMode(1920, 1080)
solved it. Btw it's propably this issue – https://github.com/SFML/SFML/issues/921.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Black rectangle on top of window
« Reply #4 on: June 28, 2016, 04:44:13 am »
Sorry for bad screenshot, it's only one side - top.
Why is it cropped on other sides too?

Okay. Is this what you expect to happen or not?
Cursor should be in top-left corner, it isn't.
Is the arrow in the top-left corner of the texture? I noticed you offset the mouse position by a negative y value.

• Why are you inheriting Game from RenderWindow? I don't think you can consider Game to 'be' a window not least because a window have no need for start method.
In my case it's very handy. ;)
But how does it make sense?

• This is constantly flipping the buffers. You need to clear and draw before each display.
while(passedTime.getElapsedTime().asSeconds() < 3.f)
        display();
It used it many times and never had any problems. :|
Not noticing any visible problems does not mean that 1) there aren't any, and 2) there never will be any.
The question, I suppose, is what do you expect it to display when you first call it since you haven't done anything with the current buffer. It's like creating a variable and not initialising it!  :'(

• It looks like you're clearing the window with a red colour. Where is it?
Changed that for tests, only difference is that all screen (including rectangle) is red.
Ah, I thought that the code created the screenshots.
So, it's not a black rectangle; it's more of an empty rectangle?

I found out what was the problem -
sf::VideoMode::getDesktopMode()
gives wrong resolution, changing it to
sf::VideoMode(1920, 1080)
solved it. Btw it's propably this issue – https://github.com/SFML/SFML/issues/921.
Is getDesktopMode() not returning 1920x1080?
Since you're creating a window to display full-screen, have you considered using sf::VideoMode::getFullscreenModes()[0] (or another from the list)?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Black rectangle on top of window
« Reply #5 on: June 29, 2016, 03:05:41 am »
Why is it cropped on other sides too?
It isn't, here's texture:


Is the arrow in the top-left corner of the texture? I noticed you offset the mouse position by a negative y value.
I just tried to fix that using negative offset, it didn't work.

But how does it make sense?
Game class handles everything, so assumption game = window may be ok.

Not noticing any visible problems does not mean that 1) there aren't any, and 2) there never will be any.
The question, I suppose, is what do you expect it to display when you first call it since you haven't done anything with the current buffer. It's like creating a variable and not initialising it!
Sorry, but without actual code with problem caused by this I won't put so much effort to rewrite it. Btw thanks, I'll try to be aware of this in future.

Ah, I thought that the code created the screenshots.
So, it's not a black rectangle; it's more of an empty rectangle?
Yup, sorry for missinforming you.

Is getDesktopMode() not returning 1920x1080?
Since you're creating a window to display full-screen, have you considered using sf::VideoMode::getFullscreenModes()[0] (or another from the list)?
Yes, I can chack it and let you know what i get. Now it isn't a problem, I can wait until this bug (https://github.com/SFML/SFML/issues/921) will be fixed.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Black rectangle on top of window
« Reply #6 on: June 29, 2016, 09:46:50 pm »
Why is it cropped on other sides too?
It isn't, here's texture:

I see. I didn't realise that your arrow was so..."croppy"  ;D

Sorry, but without actual code with problem caused by this I won't put so much effort to rewrite it. Btw thanks, I'll try to be aware of this in future.
while(passedTime.getElapsedTime().asSeconds() < 3.f)
{
        clear(); // black is the default
        draw(sfmlLogo);
        display();
}
You're welcome  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Black rectangle on top of window
« Reply #7 on: June 30, 2016, 03:48:58 pm »
while(passedTime.getElapsedTime().asSeconds() < 3.f)
{
        clear(); // black is the default
        draw(sfmlLogo);
        display();
}
You're welcome  :P

I may want to display something else in future. ;)
But k, I'll just take a screenshot and then display it.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Black rectangle on top of window
« Reply #8 on: June 30, 2016, 05:12:26 pm »
Since you're not processing any events anyway and the display isn't changing, you could simply just sleep the application for the length of time you require.
If you still want to use the while method, just remove the display() as it's doing more harm than good:
while(passedTime.getElapsedTime().asSeconds() < 3.f)
        ;
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Black rectangle on top of window
« Reply #9 on: June 30, 2016, 10:24:34 pm »
while(passedTime.getElapsedTime().asSeconds() < 3.f)
        ;

I feel like an idiot. ;-; Thanks! :)