This code is working fine if I copy paste its contents into main but why is it not working when it's called as a function?
What is not working ? The function appears to do three things :
- draw a fixed shape
- optionally, modify the caller's y
- draw a single element at a position that depends on (the new) y
What does it do different ?
The usual convention for typedef structs (UPPERCASE types) is to pass them by reference, so it's suspicious to see ALLEGRO_EVENT event rather than ALLEGRO_EVENT *event
I don't know the internals enough to say if there's an issue in passing a "flat copy" of the allegro event like that.
Be sure that you're not passing wrong type (ie. the second parameter should be the address of an int, not the address of a short, or unsigned int, or anything else)
Audric, everything I did was working fine, it's just that the event passed to the function is not working.
Yes, I tried to change it to pointer but it didn't work and my other functions are taking the event as a 'flat copy', they are working fine.
when I commented out the if statements containing event.mouse.* something, it worked.
If I don't comment it out then it does nothing.
Here's the function calling: list_box(event, &y);
Check the order of operations being performed in your event check. you may find that operator precedence is making things go haywire.
Here's my code and I don't find any operator precedence problem and if it is working fine when the event part is copied to main then why should be operator precedence problem.
Thinking of it, I remember that when I previously created menu_icon() function then at that time the event passed by parameters didn't work. What might be causing it?
In your last post, the function is only called from inside the block if(event.type == ALLEGRO_EVENT_TIMER) { }. (The indentation of lines 60-74 is misguiding)
So you're calling list_box() with an event that can never be a ALLEGRO_EVENT_MOUSE_BUTTON_DOWN. I bet that the version of your code 'that works' actually has the 3 pieces of code at different places.
You're using a system where you systematically redraw everything 30 times per second, it's fine, it means you don't need to bother keep track of "what has changed, what graphic part is obsolete and should be redrawn) So, keep the input code separate from the display one : When the user clicks on "next" arrow, you only modify the "current item" value.
Note that on line 95 you're missing an else. It's misguiding, because the entire series of if (event.type == type1) {} else if (event.type == type2) is meant to process ONE event, and handle different situations.
Thanks Audric and yes, when I tried to 'work' my code out I just copied that portion into another (previously saved version before Edgar suggested me) copy to not to mess with my code.
It now works, thanks.