SFML community forums
Help => Graphics => Topic started by: Devil0150 on June 15, 2009, 02:06:45 pm
-
I am trying to make a some kind of tetris game and i'm having a problem with my window. This is my code. It might be a little long but its very easy to understand.
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
sf::RenderWindow Tetris(sf::VideoMode(800, 600, 32), "Dedris");
class Piece
{
public:
float BlockHeight;
float BlockWidth;
sf::Sprite mySprite;
};
class Cube : public Piece
{
public:
Cube(std::string CubeFile, sf::Image& ImageCube)
{
mySprite.SetImage(ImageCube);
sf::Vector2f SprSize = mySprite.GetSize();
BlockWidth = SprSize.x / 2;
BlockHeight = SprSize.y / 2;
mySprite.SetPosition(Tetris.GetWidth() / 2 - BlockWidth, 1);
}
};
class L : public Piece
{
public:
L(std::string LFile, sf::Image& ImageL)
{
mySprite.SetImage(ImageL);
sf::Vector2f SprSize = mySprite.GetSize();
BlockWidth = SprSize.x / 2;
BlockHeight = SprSize.y / 3;
mySprite.SetPosition(Tetris.GetWidth() / 2 - BlockWidth, 1);
}
};
class backL : public Piece
{
public:
backL(std::string backLFile, sf::Image& ImageBackL)
{
mySprite.SetImage(ImageBackL);
sf::Vector2f SprSize = mySprite.GetSize();
BlockWidth = SprSize.x / 2;
BlockHeight = SprSize.y / 3;
mySprite.SetPosition(Tetris.GetWidth() / 2 - BlockWidth, 1);
}
};
class I : public Piece
{
public:
I(std::string IFile, sf::Image& ImageI)
{
mySprite.SetImage(ImageI);
sf::Vector2f SprSize = mySprite.GetSize();
BlockWidth = SprSize.x;
BlockHeight = SprSize.y / 4;
mySprite.SetPosition(Tetris.GetWidth() / 2 - BlockWidth, 1);
}
};
class T : public Piece
{
public:
T(std::string TFile, sf::Image& ImageT)
{
mySprite.SetImage(ImageT);
sf::Vector2f SprSize = mySprite.GetSize();
BlockWidth = SprSize.x / 3;
BlockHeight = SprSize.y / 2;
mySprite.SetPosition(Tetris.GetWidth() / 2 - BlockWidth, 1);
}
};
int main()
{
sf::Event Termino;
while (Tetris.IsOpened())
{
while (Tetris.GetEvent(Termino))
{
if (Termino.Type == sf::Event::Closed)
Tetris.Close();
if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Escape))
Tetris.Close();
}
sf::Image PieceImg1;
if (!PieceImg1.LoadFromFile("D:\\C++\\I.jpg"))
goto LoadError;
Cube myCube("D:\\C++\\cube.jpg", PieceImg1);
sf::Image PieceImg2;
if (!PieceImg2.LoadFromFile("D:\\C++\\I.jpg"))
goto LoadError;
L myL("D:\\C++\\L.jpg", PieceImg2);
sf::Image PieceImg3;
if (!PieceImg3.LoadFromFile("D:\\C++\\I.jpg"))
goto LoadError;
backL myBackL("D:\\C++\\LBack.jpg", PieceImg3);
sf::Image PieceImg4;
if (!PieceImg4.LoadFromFile("D:\\C++\\I.jpg"))
goto LoadError;
I myI("D:\\C++\\I.jpg", PieceImg4);
sf::Image PieceImg5;
if (!PieceImg5.LoadFromFile("D:\\C++\\I.jpg"))
goto LoadError;
T myT("D:\\C++\\T.jpg", PieceImg5);
sf::Sprite* show;
int random = sf::Randomizer::Random(1, 5);
switch(random)
{
case 1:
{
show = &(myCube.mySprite);
break;
}
case 2:
{
show = &(myL.mySprite);
break;
}
case 3:
{
show = &(myBackL.mySprite);
break;
}
case 4:
{
show = &(myI.mySprite);
break;
}
case 5:
{
show = &(myT.mySprite);
break;
}
Tetris.Clear();
sf::Clock my4Clock;
(*show).Move(0, 10);
while (my4Clock.GetElapsedTime() < 1.f);
my4Clock.Reset();
Tetris.Draw(*show);
Tetris.Display();
}
}
return 0;
LoadError:
return EXIT_FAILURE;
}
Help would be appreciated.
-
And what's your problem?
-
My window doesn't display. All i can see is the blue title bar with the usual close, maximize, minimize buttons and nothing in the middle. Not even the usual black screen.
-
Some advice:
1. Do not use goto. In most cases, there is no reason to use it. The goto keyword interrupts your program flow, often leading to unexpected results (so-called spaghetti code).
2. Initialize your graphics once instead of every loop frame, which is extremely inefficient (especially loading images).
3. Structure your code, watch your indentation. Some of your code is never executed. This is also the cause of your error.
4. Use a debugger. By stepping through the program you can find out what's wrong in less than a minute.