When using al_set_new_display_flags(ALLEGRO_RESIZABLE), is there a way to do the following:
1) Have a program detect the size of the window as it's being manually changed by the player using the mouse. I understand the actual created display size remains the same, but would like to change certain objects size and position for collision detection to work correctly between the mouse pointer and those objects after the window has been resized by the player, otherwise the collision detection between the mouse pointer and those objects remains in the same position relative to the initially created display, whilst the graphics change size and position as the display window size is manually adjusted. In other words the collision detection happens in the wrong visual place on screen.
2) After initializing al_set_new_display_flags(ALLEGRO_RESIZABLE) and creating the display, is there a way to program the window display size as opposed to the player adjusting it using the mouse. It's just that if a player adjusts the window size, then switches into fullscreen, and then back to resizable, I'd like to know what the previous resizable size was so I can set it back to that size, rather than it be initiated at the actual created display size.
Part of My Next Entry Quoted Below, Which I can't Edit Now, Is BALONEY
which I guess is essential, as if a player manually changed the window size in this way during play and the created display al_create_display(WIDTH, HEIGHT) was to change also, then the game/programming would crash.
1. I'm not sure I quite understand, but when Allegro detects a resize, you are sent the event which has the new display size:
I do something like:
So with that, you get the new display size and you can update your gui however you want.
2. To resize it yourself you do http://alleg.sourceforge.net/a5docs/refman/display.html#al_resize_display
So far I've found resizing the window with the mouse (whilst playing the game) when using ALLEGRO_RESIZABLE only shrinks or expands the graphics, which I guess is essential, as if a player manually changed the window size in this way during play and the created display al_create_display(WIDTH, HEIGHT) was to change also, then the game/programming would crash.
So what I'm finding (during play) is that even though the graphics shrink and expand as they should when using the mouse to adjust the window size, the mouse pointer collision detection isn't aligned with the graphics because the mouse pointer still references the created display al_create_display(WIDTH, HEIGHT) which understandably doesn't change as the window is resized. To describe this, if the window gets smaller the collision detection happens to the right of the objects as graphically they are displayed more to the left (the window shrinks along with the displayed graphics within it, but al_create_display(WIDTH, HEIGHT) stays the same). Similarly if the window gets larger the collision detection happens to the left of the objects.
I'm guessing now the features which I'm after don't exist within ALLEGRO. Please note this does not stop my main game from playing at all, but just messes up the various stages where I need to have the mouse effectively click on certain button images.
If access to the ALLEGRO_RESIZABLE window were available as parameters Width and Height, then I guess there would be various ways to compensate. For example if the window was made smaller, the int x = al_get_mouse_state_axis(&state, 0) could be increased proportionally to the window getting smaller effectively making int x collision detect correctly relative to al_create_display(WIDTH, HEIGHT).
With regards to my initial post where I suggested changing the size and positions of various objects, I'm now thinking that would only work if there was a second copy of the objects, one unchanged for the graphics (ALLEGRO_RESIZABLE), and one which does change proportionally to the resized window.
I think the easiest way (because of less work to do) might be the first way, adding or subtracting to the x = al_get_mouse_state_axis(&state, 0) values.
So it seems that for my question 1) I need access to the ALLEGRO_RESIABLE manually adjusted window width and height in the form of parameters, to work with within my programming, and for my question 2) I guess this also is not possible for me to do.
I'm just a bit confused by some of this. I understand your problem, but I don't quite understand why it should happen.
My game dynamically resizes and repositions elements and my gui mouse stuff works fine.
What I find strange too is that you're saying changing the window size affects the mouse position returned, which should not be since mouse starts at top left (0,0).
Is there a way you could either send your game src or make an example that demonstrates the problem. There might just be something simple you need to do.
Also, are you just upscaling your content? Because in that case, the mouse and gui positions won't line up, you'd have to transform your mouse position. I have code for that in my gui api if you need it.
Are you saying if you have an object exactly in the middle of the display which can be collision detected with the mouse pointer, that when in ALLEGRO_RESIZABLE if you manually resize the window (using the mouse) to approximately half the size whilst playing, that your object which is now half the size and still in the middle of the display (like it is for me), still has the collision detection with the mouse pointer working correctly, both being aligned.
For me changing the window size does not affect the mouse position returned. If it did so proportionally to the window size being changed as I've described, that might be the perfect solution.
I've rewritten some of my last post to try and make it a bit clearer, as some of it was not good.
Thank you jmasterx for helping with this
It's not about changing the mouse position but changing the button position. When you scale your graphics your button rectangle changes. My GUI mouse stuff works fine with al_resize_display and with ALLEGRO_EVENT_DISPLAY_RESIZE. Try and show us example code that doesn't work. You can use <code></code> tags.
Right, so you are proportionally resizing your elements.
Whereas in my case, in my gui, I literally have an algorithm running that says:
onResize:
position = screen / 2; //center
size = screen * 0.25f;
And thus, my mouse coordinates make sense.
I suspect that regardless of the size of your screen, your elements just RENDER proportionally, so you do not actually change their location and size.
This is the class I made that can transform your mouse.
https://github.com/jmasterx/Agui/blob/master/src/Agui/Transform.cpp
My gui uses it like this:
So the idea is, whatever transformation is applied to upscale the rendering must be applied to your mouse position.
The alternative, as I had mentioned is to change the size and position of your gui elements so that the mouse coordinates are screen-relative rather than world-oriented.
Thank you both
I'm not resizing the 'object graphics' or the 'created display' in the way that is perhaps being assumed. I'm not using any other resize() function/class other than the one below.
I'm using: al_set_new_display_flags(ALLEGRO_RESIZABLE), which only allows the display window to change size via use of the mouse whilst the game is running.
Please confirm that it's (ALLEGRO_RESIZABLE) to which you are referring
One important thing, do you call al_acknowledge_resize ?
If you do not you will get the issues you seem to be describing because the internal buffer might not resize itself if you do not acknowledge the resize event.
http://alleg.sourceforge.net/a5docs/refman/display.html#al_acknowledge_resize
Ah flipping heck.
No I don't. Not yet anyway.
I have to ask in excitement before I try It , should this work with al_set_new_display_flags(ALLEGRO_RESIZABLE) as I've described.
I'll give it a try
It only works with that display flag
in your event loop:
case ALLEGRO_EVENT_DISPLAY_RESIZE:
//call here
break;
http://alleg.sourceforge.net/a5docs/refman/events.html#allegro_event_display_resize
I'm only doing the following so far. I must be missing something by the looks of it. It's doing something but I need to figure it out. I'm not yet using "ALLEGRO_EVENT_DISPLAY_RESIZE". Do I need to add that in. I'll keep at it.
What does your main loop look like?
This is old and dated but might help https://www.allegro.cc/forums/thread/603224
Here's the entire routine. Most of this is relevant.
What if you tried:
if(ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { al_acknowledge_resize(display); }
Also, you should really use al_wait_for_event()
That will make your game more robust, consume less cpu, and you'll know the event is real.
I've tried it here and also immediately after al_flip_display(); and I'm still getting exactly the same behaviour. I'll keep investigating.
Thanks, think I just got a handle wich I asked for in my thread, something a NOOB to allegro 5 can understand.
Now I'm thinking the internal buffer was luckily resizing itself anyway ("internal buffer might not resize itself") without calling the following, and that the buffer resizing hasn't actually been the problem.
And that my initial ideas/observations are unfortunately correct
Damn. I really hope I'm wrong and talking total rubbish
Please help
I just want to point out once again for anybody new to this thread, that all graphics and all collision detection (apart from mouse), and so therefore the main part of my game all resize and work perfectly. It's only collision detection between the Mouse Pointer and the Various Objects as previously described that are the problem
The way you've done your main loop is a bit chaotic and I can't spot the problem from just looking. I would have to run it through a debugger but I don't have time right now.
Could you try to use al_wait_for_event() and sort of try to make your loop a bit more like my template one. I know the template behaves correctly.
But right now you are making an infinite while loop and not waiting for events which might be causing problems.
I've changed it to use al_wait_for_event() but it's still the same. A massive thank you for all your support. It's beer O'Clock for me now
EDIT: Just to clarify can somebody please comment/confirm on the following that I posted earlier in this post:
"Are you saying if you have an object exactly in the middle of the display which can be collision detected with the mouse pointer, that when in ALLEGRO_RESIZABLE if you manually resize the window (using the mouse) to approximately half the size whilst playing, that your object which is now half the size and still in the middle of the display (like it is for me), still has the collision detection with the mouse pointer working correctly, both being aligned." END OF EDIT
When you resize to half the size, it should look like when closing one eye; objects should be same size/position, just you do not see as much of them as you decrease the size.
I don't know why but that tickled me quite a lot another beer or two and I'll be cross eyed.
Seriously apart from the mouse collision detection problem, ALLEGRO_RESIZABLE works really great and I think it's a really cool feature. I can play my game perfectly, literally any size down to a large postage stamp size.
It's only on screens where the mouse is used to select objects that the issue arises. I could perhaps disable ALLEGRO_RESIZABLE for those screens, but I was hoping for a more complete solution.
EDIT: I'll upload a very short video clip when I get back from work in about 8 to 9 hours from now, showing how the game works perfectly in resizable mode apart from the mouse collision detection on certain screens.
Again thank you very much for all your support
EDIT: 2
Here's My Video Link: http://www.youtube.com/watch?v=Xhh4MTe-Q9A&list=UU2XSIT3tAf_-7ilWHAaiocA
Video Link Description: Mouse pointer collision detection misalignment which is not all that surprising, but is there a way to obtain the changing windows width and height to feed into my program so I can compensate.
Video Link Description: Mouse pointer collision detection misalignment which is not all that surprising, but is there a way to obtain the changing windows width and height to feed into my program so I can compensate.
When you acknowledge the resize event, the al_get_display_* functions should return the actual size of the backbuffer if I am not mistaken, and jmasterx seems to confirm this (and he is a much more trustable source )
I guess you should use that to transfom mouse input accordingly.
Excellent I understand now.
I'll give it a test later when I get back from work in about 7 to 8 hours.
Thanks again pkrcel
My apologies everyone for misunderstanding before.
If the following is true then that's exactly what I've been looking for:
When you acknowledge the resize event, the al_get_display_* functions should return the actual size of the backbuffer if I am not mistaken
IIRC, Edgar Reynaldo had a system where the button coordinates were a percentage of the screen dimensions.
And you'll get to do it all over again if you switch to OpenGL.
If the following is true then that's exactly what I've been looking for:
That IS definitely true, I've played around ex_resize2.exe in the allegro examples and if you fail to acknowledge the resize event (commenting the proper code) you see the picture that gets resized (automagically transformed) proportionally.
Althou this means that I see in your video means you're NOT acknowledging the resize.
Which could be what you were looking for, if you don not want to clip the graphics but would like to have the screen automatically re-proportioned.
In that case, you WILL have to transform the mouse input, but to get the resized coordinates you have to look in the EVENT struct fields, namely:
event.display.x event.display.y event.display.width event.display.height
when handling the resize event.
Which should yield the information you need to transform your mouse input accordingly.
I know, I'm a bit confused in my exposition....hope this anyway helps.
Yeah, your video shows that the display is not being acknowledged. I'm not sure why since you are indeed acknowledging it.
One thing that is very important. Do you register your display to your event queue?
Notice how I do it:
int main(int argc, char *argv[]) { // Start the event queue to handle keyboard input and our timer ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue(); al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)al_get_keyboard_event_source()); al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)al_get_mouse_event_source()); al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)timer); al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)display); ALLEGRO_EVENT event; while(!done) {
If you do not register your display, certain things might not work correctly, and resizing might be one of them.
You probably will not receive the resize event.
If you can, print the bool returned by acknowledge resize call to see if it returns true.
It's probably a better idea to use al_get_display_event_source and al_get_timer_event_source rather than just casting the pointers, "just in case" of a future change.
Oh wow, I never even knew about those functions hahah
At the time that I wrote the template, I was using 4.9.something and I'm not sure if those API's had been added yet
At the time that I wrote the template, I was using 4.9.something and I'm not sure if those API's had been added yet
You could be right
IIRC, Edgar Reynaldo had a system where the button coordinates were a percentage of the screen dimensions.
This is basically the gist of the whole thing :
outer_area is your screen, layout_area is the percentage rectangle, and LayoutArea returns the new rectangle based on the combination of the two.
fx,fy,fw,and fh are all floats representing the percentage from 0 to 1.0 (or beyond, but then offscreen) of that dimension's x or y or w or h.
Really, you should check to make sure you're transforming your buttons to the new position properly. All I saw was += fx + ... which is just a translation, and not a scaling.
It's all working as it should now I think
See video link:
http://www.youtube.com/watch?v=TjRlW3E-LDw&list=UU2XSIT3tAf_-7ilWHAaiocA
Before, I was making the mistake of assuming the instant resizing of the graphics within the window when adjusting the window size via the mouse, to be what al_acknowledge_resize() actually does
Now I just need to transform the fonts and bitmaps (perhaps all put onto one bitmap first, not quite sure yet) to match the new size as it's acknowledged. Also I now have the values to compensate for the mouse collision detection.
Interesting though. I'm left wondering if some people simply use the instant resizing of the graphics as usable for one of their display modes, as after all it does seem to work okay, as seen from my first video, but there's no values to use for compensating for the mouse issue I've was having, as I know all too well. Also I'm suspecting the graphics are only intended as a preview or even abandoned data which just happens to stay in tack enough to form an okay image, although it does seem to break up slightly irregular at points as the widow changes size, but this mostly seems to only distort the text.
Edit 4: My last sentence immediately above starting "Also I'm" is perhaps rubbish, I simply don't know.
Anyway I'm pretty sure at last that I'm on the right track
Also here's my modified routine with several mistakes corrected:
EDIT: Sorry all, I've only just spotted it's now on page two, I'll get reading your posts
Edit 2: I'm still reading, but yes you're absolutely correct jmasterx I was forgetting to re-register the display on the event queue.
Edit 3: pkrcel are you saying my suggestion of using the instant resizing of the display might be a valid option. And if so are you saying by using:
if(ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE)
I could compensate for the mouse pointer position as the window size changes and indeed not bother using:
al_acknowledge_resize(display)
Whether you are or not, I'm still extremely happy because now I understand
More About This: If I store the mouse position values instantly before ALLEGRO_EVENT_DISPLAY_RESIZE starts, and then compare them the instant ALLEGRO_EVENT_DISPLAY_RESIZE stops, then I should have the correct values to compensate for the mouse. Along with the above is this along the lines of what you mean.
Another Edit: Thanks Edgar and Thomas, I've found this which I think could help as well: https://wiki.allegro.cc/index.php?title=Achieving_Resolution_Independence
The main disadvantage of the resolution independence methods you linked is that your game can only run on 1 aspect ratio. If you want your game to run on 4:3,16:9,16:10 aspect ratios etc, then you will have to have black borders or everything will look stretched.
Up to now I've been thinking that either black borders or stretching are the only two possibilities for any method. Are you saying there are more options for certain other methods.
Yes, the other option involves using layouts / relative size algorithms for your gui, and for the game, you can use transformations such that the character is always the same size, but more land is in view in widescreen and so forth. My game supports just about any common aspect ration because of how I designed it.
The basic idea of layouts is that you specify constraints for your widgets/components. You put Widgets into containers with these constraints and the algorithm will size and position your Widgets such that they look optimal for the selected width and height.
Java's Swing API allows this sort of flexibility. My GUI API and a few other GUI APIs on a.cc support this sort of Layout concept.
Otherwise, the alternative quick n dirty way is for you to specify when you get a resize event:
button1.size = width * 0.2f, 60;
button1.location = width / 2, height / 2;
So basically, when the screen is resized, set the button to the center of the screen, and set its width 20% of the screen width, and its height to a fixed 60 pixels.
This way at any resolution the button will look correct.
It's a lot more work to do it this way but I personally prefer it over a static solution.
Totally Awesome
Sounds complicated, and thank you for such a clear explanation. Hopefully one day I'll be able to do it myself
Before, I was making the mistake of assuming the instant resizing of the graphics within the window when adjusting the window size via the mouse, to be what al_acknowledge_resize() actually does
If you run both allegro examples ex_resize and ex_resize2 you might end up thinking that, since both programs resize what's in the screen automatically; this led me to the initial confusion because clashed with what jamsterx said (and I actually remebered like that as well).
Upon looking at the code of the examples thou it was clear that's not the case.
Dunno, maybe the two examples could be tinkered with.
moving on...
pkrcel are you saying my suggestion of using the instant resizing of the display might be a valid option
Not exactly, because I don't know what are you aiming for specifically, but in principle if you're not bothered by the shrinking/stretching you indeed COULD use the automatic gfx adaptation.
It's not elegant nor eyecandy thou, you should at least set display constraints that set for a minimum and maximum width/height (see al_set_window_constraints), and could even handle the resize event (still without acknowledging) calling al_resize_display to FORCE a certain aspect ratio.
At least that's how it come to mind seeing your video, which admittedly it's not the optimal way to do it (I dunno if the gfx driver performance would be bothered for example, even thou I guess not).
Thanks again pkrcel.
There's a lot for me to play around with now, and I'm feeling positive
Felt obligated to mention this - the way the display looks during a resize is just a temporary way to keep something on the screen that looks remotely appropriate. The mouse and its positions will NOT be affected until you call al_acknowledge_resize or al_resize_display. Then they should work as expected. Allegro does not transform the mouse. It just stretches the display to the intermediate size.
Yeah, that's the main point in me saying it's not "elegant"....it's basically hijacking a side effect.
But you can transform yourself mouse coordinates if you query the relevant fields in the allegro resize event.