Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Making Buttons

This thread is locked; no one can reply to it. rss feed Print
Making Buttons
Zephelin
Member #12,952
June 2011

How can I find a tutorial for making buttons and setting events happening when clicked

EDIT: Also another problem with my compiler, thanks if you can tell why;

When I compile using Release option in VS, I get;

1>------ Build started: Project: Allegro, Configuration: Release Win32 ------
1>main.obj : error LNK2001: unresolved external symbol __imp__al_draw_bitmap_region
1>main.obj : error LNK2001: unresolved external symbol __imp__al_flip_display
1>main.obj : error LNK2001: unresolved external symbol __imp__al_map_rgb
1>main.obj : error LNK2001: unresolved external symbol __imp__al_destroy_event_queue
1>main.obj : error LNK2001: unresolved external symbol __imp__al_clear_to_color
1>main.obj : error LNK2001: unresolved external symbol __imp__al_register_event_source
1>main.obj : error LNK2001: unresolved external symbol __imp__al_wait_for_event
1>main.obj : error LNK2001: unresolved external symbol __imp__al_destroy_timer
1>main.obj : error LNK2001: unresolved external symbol __imp__al_start_timer
1>main.obj : error LNK2001: unresolved external symbol __imp__al_get_keyboard_event_source
1>main.obj : error LNK2001: unresolved external symbol __imp__al_draw_bitmap
1>main.obj : error LNK2001: unresolved external symbol __imp__al_create_event_queue
1>main.obj : error LNK2001: unresolved external symbol __imp__al_create_display
1>main.obj : error LNK2001: unresolved external symbol __imp__al_create_timer
1>main.obj : error LNK2001: unresolved external symbol __imp__al_load_bitmap
1>main.obj : error LNK2001: unresolved external symbol __imp__al_install_system
1>main.obj : error LNK2001: unresolved external symbol __imp__al_get_display_event_source
1>main.obj : error LNK2001: unresolved external symbol __imp__al_get_timer_event_source
1>main.obj : error LNK2001: unresolved external symbol _al_init_image_addon
1>main.obj : error LNK2001: unresolved external symbol __imp__al_destroy_display
1>main.obj : error LNK2001: unresolved external symbol __imp__al_set_new_bitmap_flags
1>main.obj : error LNK2001: unresolved external symbol __imp__al_install_keyboard
1>main.obj : error LNK2001: unresolved external symbol __imp__al_path_cstr
1>main.obj : error LNK2001: unresolved external symbol __imp__al_is_event_queue_empty
1>main.obj : error LNK2001: unresolved external symbol __imp__al_destroy_bitmap
1>main.obj : error LNK2001: unresolved external symbol __imp__al_destroy_path
1>main.obj : error LNK2001: unresolved external symbol __imp__al_get_standard_path
1>C:\Documents and Settings\Quarynn\my documents\visual studio 2010\Projects\Allegro\Release\Allegro.exe : fatal error LNK1120: 27 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thought I don't get any with Debug option, why?

RPGillespie
Member #12,946
June 2011

I used this logic to code all of my buttons (the actual sprites were created in GIMP/Powerpoint):

#SelectExpand
1 //x represents the x coordinate of the bitmap 2 //y represents the y coordinate of the bitmap 3 //state is of type ALLEGRO_MOUSE_STATE 4 //mouse_hovering and left_down are bools 5 //setDrawFrame() is a function I wrote to switch which bitmap is being drawn 6 //This code goes inside the update function of your button object 7 8 //updates state, which is a struct that holds mouse information such as its coordinates on the screen 9 al_get_mouse_state(&state); 10 11 12 //Checks if mouse is hovering over the sprite. 13 //if so, it will start drawing a different sprite (kind of like when you hover your mouse over any of your browser buttons 14 if (state.x < (x+30) && state.x > x && state.y < (y+30) && state.y > y) 15 { 16 setDrawFrame(1); 17 mouse_hovering = true; 18 } 19 else 20 { 21 setDrawFrame(0); 22 mouse_hovering = false; 23 } 24 25 //If the left mouse button is being pressed and the mouse is hovering over the button, set a flag; 26 if ((state.buttons & 1) && mouse_hovering) 27 { 28 //you could also set the sprite to yet another frame here, one of the button being depressed. 29 left_down = true; 30 } 31 32 //If the flag is on and the mouse button is no longer being clicked, you know the user has released the mouse button 33 if (left_down && !al_mouse_button_down(&state, 1)) 34 { 35 //don't forget to switch the sprite back to the non-depressed version here. 36 left_down = false; //don't forget to reset the flag 37 38 39 //here goes whatever code you wanted to execute when the button was pressed// 40 }

As for your other problem, I think it's good practice to code a little, then test, then code a little, then test. Using this approach, I never have to deal with more than 1 or 2 errors at a time, and it's really easy to tell where the error is coming from when it worked 2 seconds ago, you coded a little, and now it's broken.

My Games:
Nexus (a 2D Portal Clone) - http://www.yoyogames.com/games/155296
Mastermind (Codebreaker Puzzle Game) - http://www.allegro.cc/files/attachment/604323

Zephelin
Member #12,952
June 2011

What's the identifier of state?

RPGillespie
Member #12,946
June 2011

//state is of type ALLEGRO_MOUSE_STATE

I put it in the comments at the top

My Games:
Nexus (a 2D Portal Clone) - http://www.yoyogames.com/games/155296
Mastermind (Codebreaker Puzzle Game) - http://www.allegro.cc/files/attachment/604323

Karadoc ~~
Member #2,749
September 2002
avatar

The errors in 'release' mode are saying that certain allegro functions are not being found in your final problem. unresolved external symbol means a function was declared and used, but not defined.

To fix the problem, you need to link to the appropriate libraries which contain the function definitions. The reason it is happening to you in release mode and not debug mode is that you are using different versions of the allegro library. Probably the static version for release and the dynamically linked version for debug.

Although you are using MSVC, you'll probably find what you need to know here.

-----------

Zephelin
Member #12,952
June 2011

1>------ Build started: Project: Allegro, Configuration: Debug Win32 ------
1> main.cpp
1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(6): warning C4273: 'al_init_image_addon' : inconsistent dll linkage
1> c:\program files\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(32) : see previous definition of 'al_init_image_addon'
1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(153): error C2664: 'al_get_mouse_state' : cannot convert parameter 1 from 'ALLEGRO_MOUSE_STATE **' to 'ALLEGRO_MOUSE_STATE *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(157): error C2228: left of '.x' must have class/struct/union
1> type is 'ALLEGRO_MOUSE_STATE *'
1> did you intend to use '->' instead?
1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(157): error C2228: left of '.x' must have class/struct/union
1> type is 'ALLEGRO_MOUSE_STATE *'
1> did you intend to use '->' instead?
1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(157): error C2228: left of '.y' must have class/struct/union
1> type is 'ALLEGRO_MOUSE_STATE *'
1> did you intend to use '->' instead?
1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(157): error C2228: left of '.y' must have class/struct/union
1> type is 'ALLEGRO_MOUSE_STATE *'
1> did you intend to use '->' instead?
1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(167): error C2228: left of '.buttons' must have class/struct/union
1> type is 'ALLEGRO_MOUSE_STATE *'
1> did you intend to use '->' instead?
1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(173): error C2664: 'al_mouse_button_down' : cannot convert parameter 1 from 'ALLEGRO_MOUSE_STATE **' to 'const ALLEGRO_MOUSE_STATE *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

AMCerasoli
Member #11,955
May 2010
avatar

Although you are using MSVC, you'll probably find what you need to know here [www.cerebrospain.com].

Well, actually I put the tutorial there when I didn't know that there was a wiki about Allegro, well, to be honest I did know that there was a wiki, but I didn't know how to use it, and was a complete disaster, now still being a disaster but not complete ;D... Anyway the same tutorial is here and has being improved by the own Edgar Reynaldo, so it's approved.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Zephelin said:

1>c:\documents and settings\quarynn\my documents\visual studio 2010\projects\allegro\allegro\main.cpp(6): warning C4273: 'al_init_image_addon' : inconsistent dll linkage

1>c:\program files\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(32) : see previous definition of 'al_init_image_addon'

It sounds like you're including two different definitions of al_init_image_addon, but A5 should be using include guards so I don't know how that is possible.

Zephelin said:

'al_get_mouse_state' : cannot convert parameter 1 from 'ALLEGRO_MOUSE_STATE **' to 'ALLEGRO_MOUSE_STATE *'

It says exactly what it means - you are passing the address of an ALLEGRO_MOUSE_STATE* when it expects an ALLEGRO_MOUSE_STATE*.

Zephelin said:

left of '.x' must have class/struct/union
1>type is 'ALLEGRO_MOUSE_STATE *'
1>did you intend to use '->' instead?

You're trying to use '.' to access a pointer. You have to use '->' to access member variables through a pointer.

After you fix these errors, if you still have errors, post your full code and the errors you get with it.

Go to: