Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Windows XP loses focus at start

This thread is locked; no one can reply to it. rss feed Print
[A5] Windows XP loses focus at start
Neil Roy
Member #2,229
April 2002
avatar

I am working on a game with Allegro 5, and so far I am loving it. When I compile and run it on my Windows 7 machine, I have no problems but when I run the game on my wife's Windows XP machine, the game doesn't have focus when you first run it. It doesn't lose focus for any reason, it actually doesn't have focus at all when you start it. I have to click on the game window in order for it to get focus so I can use my keys.

I am using the latest release build, 5.0.9 with MinGW 4.6.2 TDM (with Code::Blocks).

Has anyone else noticed this behaviour with Windows XP? As I said, it doesn't happen with Windows 7.

Edit: I'm not using my mouse at all with this game. It's keyboard only. I wonder if that may have something to do with it?

Edgar Reynaldo
Member #8,592
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

Nice idea. I think I seen that suggestion from someone in another thread using that exact same trick actually.

It does seem like it may be a bug of some sort with the library.

Edit: Okay, I added in the following code. This will do for now, but I REALLY do not like having to add in Windows specific code to my project and hope this gets fixed.

   HWND window_handle = al_get_win_window_handle(setting.screen);
   SetFocus(window_handle);

Thomas Fjellstrom
Member #476
June 2000
avatar

I dunno, normally a window should be created with focus by default by the OS.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Neil Roy
Member #2,229
April 2002
avatar

It does work fine under Windows 7, it only happens on Windows XP.

I'm not doing anything special, just a normal game, but there is no focus at all on it when it is run. Very strange. I have to wait until my wife frees up her computer to test that little hack.

Edit: One other problem I just noticed. When I call a al_show_native_message_box() in ALLEGRO_FULLSCREEN mode, it shrinks down the game window to ask the question and bring up the dialog. And if I click cancel, the game window stays shrunk. I only use this for my level editor so I'll just limit it to ALLEGRO_FULLSCREEN_WINDOW or a normal window for now, but...

int selection = al_show_native_message_box(setting.screen, "Warning",
                "CHANGES NOT SAVED YET", "Are you sure?", NULL,
                ALLEGRO_MESSAGEBOX_OK_CANCEL | ALLEGRO_MESSAGEBOX_WARN);

Update: Okay, I tested it on my wife's system and it still loses focus right away. When I first run it, if I have the folder open where the game is, the game window opens, but keeps the folder in the foreground and the game window itself never gets focus, even with the window code to give it focus. Strange... I'll have to try initializing the allegro mouse interface I guess, perhaps it has something to do with me not using a mouse with this project. But it only happens on Windows XP.

I may create a test app that just opens a window and run it on her system to see if it still happens.

Edit: The dialog problem happens on both systems by the way.

torhu
Member #2,727
September 2002
avatar

Does sound a little bit like there's some program running on that PC that does something funny. Does it happen only to Allegro programs?

Neil Roy
Member #2,229
April 2002
avatar

I just tested out my old game (Deluxe Pacman) made with Allegro 4 in full screen and windowed mode and it has no problems. But then it uses the mouse, which makes me think that if I don't use the mouse, or at least initialize it with Allegro 5, it will lose focus. She doesn't run a lot on her computer, mainly her browser, and it is fine as well. Haven't seen this on anything else (other games) I have run on there.

I'll try recompiling my game with mouse support enabled and see what happens on hers.

EDIT: Okay, I narrowed it down. It is losing focus while displaying an intro sequence I created...

for (int x=WIDTH; x>=0; x-=16) {
   al_draw_scaled_bitmap(introback, 0, 0, tbw, tbh, x, 0, WIDTH-x, HEIGHT, 0);
   al_flip_display();
   al_rest(.015);
}

introback is an 800x600 PNG I load in and lock. About half way through displaying it (it is expanded from the right side of the screen until it is full screen) it suddenly loses focus.

WIDTH and HEIGHT are 800 and 600 respectively.

This doesn't make any sense as there's nothing going on here that should cause that.

Oh, this only happened with the release version of the game. Interesting that it doesn't happen with my debug version. And it only happens the first time the game runs. It runs this function other times (when you die etc).

Edit: I moved my 'SetFocus()' code to just after that intro and it didn't bring the window back into focus. I have tried everything, I changed the compiling options in every which way. The only one that doesn't lose focus is if I compile it as a console app, and I don't want that. It is compiled as a Windows GUI app as normal. No change in the compile options effects it at all. I shut off her browser so that nothing was running and still happens and like I said, it doesn't happen with my old game made with Allegro 4 (compiled as a GUI as well). I guess I'm screwed for now, if you use XP, you'll have to put up with it. It's frustrating though.

Edgar Reynaldo
Member #8,592
May 2007
avatar

NiteHackr said:

When I call a al_show_native_message_box() in ALLEGRO_FULLSCREEN mode, it shrinks down the game window to ask the question and bring up the dialog. And if I click cancel, the game window stays shrunk.

This makes sense - you're running in fullscreen mode - it has to give the message window focus and show the desktop again. Use SetFocus to restore it.

Thomas Fjellstrom
Member #476
June 2000
avatar

NiteHackr said:

Interesting that it doesn't happen with my debug version.

things happening or not happening depending on debug mode tends to be caused by uninitialized variables, or in rare-er cases, memory corruption.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Neil Roy
Member #2,229
April 2002
avatar

This makes sense - you're running in fullscreen mode - it has to give the message window focus and show the desktop again. Use SetFocus to restore it.

I see. I guess I thought it would work somehow. I'll just write my own function for that. It would look nicer if I done it myself anyhow. I only use it for the level editor actually so, I guess I could even just limit the editor to Windowed or Fullscreen Windowed mode. Not that big of a deal really. If I ever need it for the main game itself I'll write my own function for it.

things happening or not happening depending on debug mode tends to be caused by uninitialized variables, or in rare-er cases, memory corruption.

I was very careful to initialize all variables and destroy them. As I developed this, when I made a variable that needed to be destroyed later (like an 'ALLEGRO_BITMAP' for example) I always initialized it with a NULL when declaring it, and then the first thing I did was add it to a shutdown() function I have that destroys it (and sets it to NULL afterwards to be safe).

I think it has more to do with debug version is a console app, and the release is a GUI. It works fine on Windows 7 and I can't understand how it would lose focus and not be able to get it back without clicking on the window manually. It runs fine otherwise. If I can't figure this out, I'll not bother and just leave it as a pain in the butt item for XP users as my patience on this is wearing thin.

If it wasn't for certain features of Allegro 5 I like so much, I would be tempted to go back to Allegro 4. :-/

Edit: After more tests I put in a al_rest(15) early in my code, basically in my main() function I jump to an initialization function which then sets up PHYSFS, then the display screen and loads in various data (fonts, graphics etc). So the rest I threw in right after the display was set and during the 15 second pause it lost focus. I am beginning to wonder if it doesn't have something to do with PHYSFS maybe? It's about the only thing that gets initialized aside from the screen.

Edit: Okay, I just tested the program with PHYSFS commented out. All the game does is the usual allegro initialization functions...

And I set up some variables I intend to use. I checked them all and they are all set to default values to start (= NULL etc) but none of them are actually used at this point and it still loses focus. There's nothing left for me to test. I'll have to write an Allegro plain APP that opens a window and see what that does I guess.

YAY!!! I found the line that is causing the problem!!!

I ran my level editor which basically consists of a main function and not too much more and noticed it was doing the same thing. This made it easier for me to narrow down the problem. SO, I started commenting out lines until it stopped happening.

When I commented out this line, the problem went away...

al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);

Somehow, THIS line causes my programs to lose focus about 3 seconds after they have been run. I'm totally baffled. It only happens on my wife's WindowsXP machine. I don't know if it's her video card or what it could be. She has an older GeForce FX5200 in her system. I DO plan to give it a minor upgrade soon, but I would be curious as to why this would cause a program to lose focus.

Go to: