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

Author Topic: sprites are white rectangles  (Read 3145 times)

0 Members and 1 Guest are viewing this topic.

b0rn2c0de

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
sprites are white rectangles
« on: April 24, 2013, 08:50:54 pm »
okay .. iknow that this question has been asked many times and i have even checked out the topics but i just cant seem to get my head around this problem
#include <SFML\graphics.hpp>
#include "stdafx.h"

int main()
{
        sf::Texture img;
        if(!img.loadFromFile("bg.png"))
        {std::cout<<"load error";}
        sf::Sprite bg(img,sf::Rect<int>(10,12,30,23));
        sf::RenderWindow app(sf::VideoMode(640,480),"test");
        while(app.isOpen())
        { sf::Event x;
        while(app.pollEvent(x))
        {if(x.type==sf::Event::Closed){app.close();}}
        app.clear(sf::Color(0,0,0,0));
        app.draw(bg);
        app.display();
        }
return 0;
}

my texture could not have gone out of scope as there is only a main function and even the image is getting loaded as it does not show any error
plzz help

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: sprites are white rectangles
« Reply #1 on: April 24, 2013, 09:00:05 pm »
Allocate textures after the window is open :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10396
    • View Profile
    • development blog
    • Email
Re: sprites are white rectangles
« Reply #2 on: April 24, 2013, 09:05:27 pm »
Allocate textures after the window is open :)
Yeah there can be some issues with allocating the texture before the window creation, but I don't think I've ever had the problem with VS...

Anyways, you should really use more whitespaces. Your code is nearly unreadable to a normal programmer, e.g. something like this:

#include <SFML/graphics.hpp>

int main()
{
    sf::RenderWindow app(sf::VideoMode(640,480), "test");

    sf::Texture img;
    if(!img.loadFromFile("bg.png"))
    {
                std::cout << "load error";
        }
       
    sf::Sprite bg(img, true);
       
    while(app.isOpen())
    {
                sf::Event x;
                while(app.pollEvent(x))
                {
                        if(x.type==sf::Event::Closed)
                        {
                                app.close();
                        }
                }
                app.clear(sf::Color(0, 0, 0, 0));
                app.draw(bg);
                app.display();
    }
}

Note: I removed the sf::Rect<int> to make sure you're not specifying a wrong position there.
Also you should always start with an empty project, thus you wouldn't need stdafx. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: sprites are white rectangles
« Reply #3 on: April 24, 2013, 09:07:36 pm »
Allocate textures after the window is open :)
Yeah there can be some issues with allocating the texture before the window creation, but I don't think I've ever had the problem with VS...

You can't create textures before a valid OpenGL context is active. I tought it was created along with the window, in the SFML case :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10396
    • View Profile
    • development blog
    • Email
Re: sprites are white rectangles
« Reply #4 on: April 24, 2013, 09:20:21 pm »
You can't create textures before a valid OpenGL context is active. I tought it was created along with the window, in the SFML case :)
Nope, the OpenGL contexts are created globally and shared.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

b0rn2c0de

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: sprites are white rectangles
« Reply #5 on: April 25, 2013, 01:51:05 pm »
thanx guys it worked :) .. i have 1 more question .. i was making a air hockey type game in which the puck was controlled by the mouse somewhat like this
if(event.type==sf::event::mousemoved)
{puck.setpositon(mousex,mousey})
is there some way to get the velocity of the puck ??

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: sprites are white rectangles
« Reply #6 on: April 25, 2013, 02:05:11 pm »
is there some way to get the velocity of the puck ??

Since v=s/t the velocity is nothing more than pixels (in this case) per unit of time (s/ms/whatever) So you measure the distance the mouse is moved in a certain period, and voila, you've got your velocity.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10396
    • View Profile
    • development blog
    • Email
Re: sprites are white rectangles
« Reply #7 on: April 25, 2013, 02:06:52 pm »
controlled by the mouse somewhat like this
if(event.type==sf::event::mousemoved)
{puck.setpositon(mousex,mousey})
You also might want to look into sf::Mouse::getPosition(window);

is there some way to get the velocity of the puck ??
Yes with knowledge in physics and math. ;)
(average) velocity = passed distance/passed time
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

b0rn2c0de

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: sprites are white rectangles
« Reply #8 on: April 25, 2013, 02:41:35 pm »
You also might want to look into sf::Mouse::getPosition(window);
Thanks i will definitely try it   :)
To get the time i thought about starting a timer as soon as the collision occurs , get the mouse position
After 5 mili sec or so get the mouse position again , get the distance and divide by the elapsed time .. But wont it pause my game for the elapsed time ??
Or is there a better way of doing so ??