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

Author Topic: Game Freezing  (Read 2017 times)

0 Members and 1 Guest are viewing this topic.

Kody

  • Newbie
  • *
  • Posts: 2
    • View Profile
Game Freezing
« on: July 07, 2011, 08:48:45 pm »
I'm having trouble keeping my game running. My code below makes my game freeze up and not respond. Can anyone help me understand why and how to fix it?

Code: [Select]
int main()
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "World Domination");

srand( time(NULL) );

sf::Clock Clock;

int PlayerHealth = 100;
int EnemyHealth = 100;

int lvl = 0;

bool map1Highlighted = false;
bool map2Highlighted = false;
bool map1Selected = false;
bool map2Selected = false;

bool isStarted = false;
bool gameStarted = false;

sf::Image MapLoad;
if(!MapLoad.LoadFromFile("Images/Edited.png"))
return EXIT_FAILURE;

sf::Sprite Map(MapLoad);

Map.SetPosition(0,0);
Map.SetScale(1.f,1.f);

sf::Image MapLoad2;
if(!MapLoad2.LoadFromFile("Images/Select_1.png"))
return EXIT_FAILURE;

sf:: Image MapLoad3;
if(!MapLoad3.LoadFromFile("Images/Select_2.png"))
return EXIT_FAILURE;

sf::String map1Text;
map1Text.SetFont(sf::Font::GetDefaultFont());
map1Text.SetText("North America");
map1Text.SetSize(15);
map1Text.SetPosition(55,160);
map1Text.SetColor(sf::Color(255,0,0,255));

sf::String map2Text;
map2Text.SetFont(sf::Font::GetDefaultFont());
map2Text.SetText("South America");
map2Text.SetSize(15);
map2Text.SetPosition(135,350);
map2Text.SetColor(sf::Color(255,0,0,255));

sf::String startText;
startText.SetFont(sf::Font::GetDefaultFont());
startText.SetText("Player 1's Turn");
startText.SetSize(75);
startText.SetPosition(250,250);
startText.SetColor(sf::Color(255,0,0,255));

while(App.IsOpened())
{
sf::Event Event;
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
}

App.Clear();

float timer = Clock.GetElapsedTime();

App.Draw(Map);

float ElaspedTime = App.GetFrameTime();
int randomMap = 1 + rand() % 2;

if(map1Selected == false && map2Selected == false)
{
if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
if(App.GetInput().GetMouseX() > 50 && App.GetInput().GetMouseX() < 150 && App.GetInput().GetMouseY() > 0 && App.GetInput().GetMouseY() < 300)
{
Map.SetImage(MapLoad2);
map2Highlighted = false;
map1Highlighted = true;
}
}

if(map1Selected == false && map2Selected == false)
{
if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
if(App.GetInput().GetMouseX() > 100 && App.GetInput().GetMouseX() < 300 && App.GetInput().GetMouseY() > 300 && App.GetInput().GetMouseY() < 600)
{
Map.SetImage(MapLoad3);
map1Highlighted = false;
map2Highlighted = true;
}
}

if(map1Selected == false && map2Selected == false)
if(App.GetInput().IsKeyDown(sf::Key::R))
if(randomMap == 1)
{
Map.SetImage(MapLoad2);
map2Highlighted = false;
map1Highlighted = true;
Sleep(250);
}
else
{
Map.SetImage(MapLoad3);
map1Highlighted = false;
map2Highlighted = true;
Sleep(250);
}

if(map1Highlighted == true)
{
App.Draw(map1Text);
if(App.GetInput().IsKeyDown(sf::Key::Return))
map1Selected = true;
}

if(map2Highlighted == true)
{
App.Draw(map2Text);
if(App.GetInput().IsKeyDown(sf::Key::Return))
map2Selected = true;
}

if(map1Selected == true || map2Selected == true)
{
Map.SetImage(MapLoad);
if(map1Highlighted == true)
map1Text.SetColor(sf::Color(0,255,0,255));
if(map2Highlighted == true)
map2Text.SetColor(sf::Color(0,255,0,255));
isStarted = true;
}

if(isStarted == true)
{
App.Draw(startText);
Sleep(2000);
startText.SetText("");
lvl = 1;
}

if(lvl == 1)
{
bool Pturn = false;
bool Eturn = false;;
if(PlayerHealth <= 0)
{
App.Close();
}
else
{
if(EnemyHealth <= 0)
{
App.Close();
}
else
{
Pturn = true;
}
}

if(Pturn == true)
{
if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
{
EnemyHealth = EnemyHealth - 10;
Eturn = true;
Pturn = false;
}
}
if(Eturn == true)
{
PlayerHealth = PlayerHealth - 10;
Pturn = true;
Eturn = false;
}
}

App.Display();

}

return EXIT_SUCCESS;
}

Haikarainen

  • Guest
Game Freezing
« Reply #1 on: July 07, 2011, 09:44:52 pm »
This code is a mess, protip is to get some kindof order by creating classes and stuff. Make it all structural. It REALLY helps debugging.

Where does it freeze? Try finding the spot by doing this:
Code: [Select]
Codesegment here
std::cout << "Doing blablabla nr 1\n";
Another codesegment here
std::cout << "Doing blablabla nr 2\n";


And see how many it prints before freezing up. By doing that you narrow the problematic code down and it helps you see your problem.

aquaglow

  • Newbie
  • *
  • Posts: 15
    • View Profile
Game Freezing
« Reply #2 on: July 07, 2011, 10:56:35 pm »
The simplest thing is setting some breakpoints and stepping through your code with a debugger (gdb if you're using g++, visual studio/xcode have a debugger integrated in the IDE). If you're not familiar with using a debugger it's worth investing the time to learn.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Game Freezing
« Reply #3 on: July 08, 2011, 08:52:33 am »
aquaglow is right, this is the best way to do it : you don't need to modify your code to test it. But don't forget to compile your project in debug mode (add -g3 to g++'s compile option if you're using some Makefile or directly compiling in the terminal).

If you're using some kind of linux I guess you can install and use DDD. It's a graphical interface to gdb really easy to use.
SFML / OS X developer

Kody

  • Newbie
  • *
  • Posts: 2
    • View Profile
Game Freezing
« Reply #4 on: July 08, 2011, 06:44:58 pm »
Quote from: "Haikarainen"
This code is a mess, protip is to get some kindof order by creating classes and stuff. Make it all structural.


As an example, like this?

Code: [Select]

while(App.IsOpened())
{
   sf::Event Event;
   while(App.GetEvent(Event))
   {
      if(Event.Type == sf::Event::Closed)
         App.Close();
   }

      App.Clear();

      App.Draw(Map);

      MapSelect();

      GameStart();

      App.Display();
}
 


Quote from: "aquaglow"
The simplest thing is setting some breakpoints and stepping through your code with a debugger (gdb if you're using g++, visual studio/xcode have a debugger integrated in the IDE). If you're not familiar with using a debugger it's worth investing the time to learn.


Can you give me some links for how to learn debugging?

I'm using Visual Studio 2008.

 

anything