Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Getting parameter problems.

This thread is locked; no one can reply to it. rss feed Print
Getting parameter problems.
Doctor Cop
Member #16,833
April 2018
avatar

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?

#SelectExpand
1void list_box(ALLEGRO_EVENT event, int *y) 2{ 3 int i; 4 // Main Background rectangle 5 al_draw_filled_rectangle(75, 75, 775, 575, al_map_rgb(12, 83, 95));// rgb(51, 102, 153) 6 7 // Drawing separation lines 8 for(i=0; i < 475; i+=25) 9 { 10 al_draw_line(100, 100+i, 740, 100+i, al_map_rgb(255, 255, 255), 1); 11 } 12 //al_draw_filled_rectangle(750, 75, 775, 575, al_map_rgb(51, 102, 153)); 13 14 //scroll bar 15 al_draw_line(757, 100, 757, 550, al_map_rgb(255, 255, 255), 1); 16 17 if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && event.mouse.x >= 754 && event.mouse.x <= 760 && event.mouse.y >= 100+*y && event.mouse.y <= 200+*y) 18 { 19 if(event.mouse.button == 1) 20 { 21 *y += 5; 22 } 23 24 } 25 26 al_draw_filled_rounded_rectangle(754, 100+*y, 760, 200+*y, 2, 2, al_map_rgb(255, 255, 70)); 27 28}

Audric
Member #907
January 2001

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)

Doctor Cop
Member #16,833
April 2018
avatar

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);

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Doctor Cop
Member #16,833
April 2018
avatar

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?

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3//#include <allegro5/allegro_image.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_ttf.h> 6#include <allegro5/keyboard.h> 7#include <allegro5/mouse.h> 8 9void menu_icon(int, int, int, int, ALLEGRO_COLOR, int); 10void list_box(ALLEGRO_EVENT, int*); 11 12#define MAXWIDTH 800 13#define MAXHEIGHT 600 14 15 int width=50, height=50, place_x=25, place_y=25, animate = 0; 16 17int main() 18{ 19 al_init(); 20 al_init_primitives_addon(); 21 //al_init_image_addon(); 22 al_init_font_addon(); 23 al_init_ttf_addon(); 24 al_install_keyboard(); 25 al_install_mouse(); 26 27 //al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR); 28 29 ALLEGRO_DISPLAY *display = al_create_display(MAXWIDTH, MAXHEIGHT); 30 ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue(); 31 ALLEGRO_TIMER *timer = al_create_timer(2.0/60.0); 32 ALLEGRO_COLOR m_color = al_map_rgb(255, 214, 51); 33 ALLEGRO_FONT *font = al_load_ttf_font("res/L.ttf", 14, 1); 34 //ALLEGRO_BITMAP *background = al_load_bitmap("res/background.jpg"); 35 36 int x=0, y=0, main_loop = true, menu = 0; 37 int width=50, height=50, place_x=25, place_y=25, animate = 0; 38 //enum {}; 39 40 al_register_event_source(queue, al_get_display_event_source(display)); 41 al_register_event_source(queue, al_get_mouse_event_source()); 42 al_register_event_source(queue, al_get_keyboard_event_source()); 43 al_register_event_source(queue, al_get_timer_event_source(timer)); 44 45 al_start_timer(timer); 46 47 while(main_loop) 48 { 49 ALLEGRO_EVENT event; 50 //if(al_is_event_queue_empty(queue)) 51 al_wait_for_event(queue, &event); 52 53 if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 54 main_loop = false; 55 56 else if(event.type == ALLEGRO_EVENT_TIMER) 57 { 58 al_clear_to_color(al_map_rgb(0, 77, 77)); //rgb(153,50,204) 59 60 //al_draw_scaled_bitmap(background, 0, 0, 4896, 3264, 0, 0, 800, 600, 0); 61 al_draw_rounded_rectangle(400-100, 300-20, 400+100, 300+20, 20, 20, al_map_rgb(70, 60, 100), 2); 62 al_draw_filled_rectangle(0, 0, 50, 50, m_color); 63 al_draw_filled_rectangle(0, 50, 50, MAXHEIGHT, al_map_rgb(255, 214, 51)); //rgb(218, 112, 214) rgb(255, 214, 51) al_map_rgb(230, 46, 0) 64 al_draw_line(50, 0, 50, MAXHEIGHT, al_map_rgb(0, 0, 0), 1); 65 menu_icon(50, 50, 25, 25, al_map_rgb(0, 0, 0), animate); 66 list_box(event, &y); 67 68 if(menu) 69 { 70 al_draw_filled_rectangle(50, 0, MAXWIDTH, 50, al_map_rgb(22, 88, 110)); 71 } 72 73 al_flip_display(); 74 al_rest(0.02); 75 } 76 77 else if(event.type == ALLEGRO_EVENT_MOUSE_AXES) 78 { 79 if(event.mouse.x < 50 && event.mouse.y < 50) 80 m_color = al_map_rgb(140, 152, 63); 81 else 82 m_color = al_map_rgb(255, 214, 51); 83 } 84 85 else if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && event.mouse.x >= (place_x-(width/2)) && event.mouse.x <= (place_x+(width/2)) && event.mouse.y >= (place_y-(height/2)) && event.mouse.y <= (place_y+(height/2))) 86 { 87 if(event.mouse.button == 1) 88 { 89 (5 == animate) ? (animate = 0) : (animate = 5); 90 menu = !menu; 91 } 92 93 } 94 95 if(event.type == ALLEGRO_EVENT_KEY_DOWN) 96 { 97 switch(event.keyboard.keycode) 98 { 99 case ALLEGRO_KEY_ESCAPE : 100 exit(1); 101 break; 102 } 103 } 104 105 } 106 107 al_destroy_display(display); 108 al_destroy_event_queue(queue); 109 return 0; 110} 111 112void menu_icon(int width, int height, int place_x, int place_y, ALLEGRO_COLOR colorU, int animate) 113{ 114 al_draw_line(place_x-(width/2)+width/4 + animate, place_y-(height/2)+height/3, place_x-(width/2)+width-(width/4) + animate, place_y-(height/2)+height/3, colorU, 5); 115 al_draw_line(place_x-(width/2)+width/4 - animate, place_y-(height/2)+(height/2), place_x-(width/2)+width-(width/4) - animate, place_y-(height/2)+(height/2), colorU, 5); 116 al_draw_line(place_x-(width/2)+width/4 + animate, place_y-(height/2)+height-(height/3), place_x-(width/2)+width-(width/4) + animate, place_y-(height/2)+height-(height/3), colorU, 5); 117} 118 119void list_box(ALLEGRO_EVENT event, int *y) 120{ 121 int i; 122 // Main Background rectangle 123 al_draw_filled_rectangle(75, 75, 775, 575, al_map_rgb(12, 83, 95));// rgb(51, 102, 153) 124 125 // Drawing separation lines 126 for(i=0; i < 475; i+=25) 127 { 128 al_draw_line(100, 100+i, 740, 100+i, al_map_rgb(255, 255, 255), 1); 129 } 130 //al_draw_filled_rectangle(750, 75, 775, 575, al_map_rgb(51, 102, 153)); 131 132 //scroll bar 133 al_draw_line(757, 100, 757, 550, al_map_rgb(255, 255, 255), 1); 134 135 if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && event.mouse.x >= 754 && event.mouse.x <= 760 && event.mouse.y >= 100+*y && event.mouse.y <= 200+*y) 136 { 137 if(event.mouse.button == 1) 138 { 139 *y += 5; 140 } 141 142 } 143 144 al_draw_filled_rounded_rectangle(754, 100+*y, 760, 200+*y, 2, 2, al_map_rgb(255, 255, 70)); 145 146}

Audric
Member #907
January 2001

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.

Doctor Cop
Member #16,833
April 2018
avatar

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.

Go to: