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

Author Topic: [SOLVED] App chash when minimize  (Read 2203 times)

0 Members and 1 Guest are viewing this topic.

eglomer

  • Newbie
  • *
  • Posts: 13
    • View Profile
[SOLVED] App chash when minimize
« on: October 24, 2011, 12:38:21 pm »
Hi!

I have write a code to keep aspect ratio in my app, but when I minimize the window, my app crash.

The code is:
Code: [Select]
// sys.panoramica is 1 if resolution screen is panoramic
// sys.proporcional set if we will keep the aspect ratio

sf::Event Event;
while (App.GetEvent(Event)){

        // keep aspect
if (Event.Type == sf::Event::Resized){
if (sys.panoramica ){
if (sys.proporcional && ( (App.GetHeight()*100 / App.GetWidth() != 56) && (App.GetHeight()*100 / App.GetWidth() != 60) ) ){
App.SetSize(App.GetWidth(), ( App.GetWidth() * 60 / 100) );
}
}else{
if (sys.proporcional && (App.GetHeight()*100 / App.GetWidth() != 75)){
App.SetSize(App.GetWidth(), ( App.GetWidth() * 75 / 100) );
}
}
}
}


Some idea?  :?

I'm using Win7-32, CodeLitte and SFML 1.6

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED] App chash when minimize
« Reply #1 on: October 24, 2011, 12:54:47 pm »
I think you get an infinite loop, because calling SetSize will create new Resized events.
Laurent Gomila - SFML developer

eglomer

  • Newbie
  • *
  • Posts: 13
    • View Profile
[SOLVED] App chash when minimize
« Reply #2 on: October 24, 2011, 04:02:34 pm »
But it only crash when I minimize the window. When I resize it there's no problem.

Could I know when the minimize button is pushed? Some event or something like this.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED] App chash when minimize
« Reply #3 on: October 24, 2011, 04:10:41 pm »
Ok... so it's maybe not an infinite loop :)

Could you post a complete and minimal example that reproduces this problem?
Laurent Gomila - SFML developer

eglomer

  • Newbie
  • *
  • Posts: 13
    • View Profile
[SOLVED] App chash when minimize
« Reply #4 on: October 24, 2011, 04:22:20 pm »
Here it is ^^.

Just copy all to main.cpp and compile.

Code: [Select]
// INCLUDES
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Audio.hpp>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <ios>
#include <iostream>
#include <fstream>
#include <ctime>


typedef struct{
int panoramica; // 1 for panoramic, 0 for 4:3
int proporcional; // 1 to keep aspect ratio
}SYSTEM;


// GLOBALS
SYSTEM sys;
sf::RenderWindow App;


int main(){

App.Create(sf::VideoMode(1024, 768, 32), "Minimize error");
App.SetFramerateLimit(60);
App.SetPosition(0,0);
App.Clear(sf::Color::Black );

sys.panoramica = false;
sys.proporcional = true;



const sf::Input& Input = App.GetInput();

// Main Loop
int i = 0;
    while (App.IsOpened()){

        sf::Event Event;
        while (App.GetEvent(Event)){

            // Close app(ESC)
            if (Event.Type == sf::Event::Closed || (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape) ){
sf::Sleep (0.5);
App.Close();
}

// Keep aspect ratio
if (Event.Type == sf::Event::Resized){
if (sys.panoramica ){
if (sys.proporcional && ( (App.GetHeight()*100 / App.GetWidth() != 56) && (App.GetHeight()*100 / App.GetWidth() != 60) ) ){
App.SetSize(App.GetWidth(), ( App.GetWidth() * 60 / 100) );
}
}else{
if (sys.proporcional && (App.GetHeight()*100 / App.GetWidth() != 75)){
App.SetSize(App.GetWidth(), ( App.GetWidth() * 75 / 100) );
}
}
}

}
       


        // Clean the screen
App.Clear(sf::Color::Black );
App.Display();
sf::Sleep (0.1);
    }

sf::Sleep(1);
fflush (stdin);
    return EXIT_SUCCESS;
}



EDIT: The problem was in App.GetWidth(), which at minimize window return 0, so there's a zero division. I solved it with:
Code: [Select]
// Keep aspect ratio
if (Event.Type == sf::Event::Resized){
if (App.GetHeight() > 10 && App.GetWidth() > 10){
if (sys.panoramica ){
    ...


Thank you, Laurent!

 

anything