Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Which is preferred: al_mouse_button_down or mouse_state.buttons?

This thread is locked; no one can reply to it. rss feed Print
Which is preferred: al_mouse_button_down or mouse_state.buttons?
Brooklyn
Member #12,433
December 2010
avatar

I was reading through the documentation and noticed there are two functions to do exactly the same thing:

ALLEGRO_MOUSE_STATE* state;
al_get_mouse_state(state);
if(state.buttons & 1) { //First button is down (usually LMB)
    //MOVE THE THING!
}

OR

ALLEGRO_MOUSE_STATE* state;
al_get_mouse_state(state);
if(al_mouse_button_down(state, 1)) { //First button is down (usually LMB)
    //AND...THE OTHER THING!
}

Do they do the same thing internally?

Is one preferred over the other in certain situations?

Chris Katko
Member #1,881
January 2002
avatar

They're not two functions. One is a function, the other is the variable in the structure that the function is reading.

There is a slim possibility that the API could change 10 years from now where "1" wouldn't mean the first button.

It's encapsulation. You use accessor functions (getters / setters) so that the underlying structure can be changed without affecting the rest of the code. For example, the "buttons" variable could be renamed "button_list" inside the mouse state structure and the second version will still work, whereas the direct-access one will now be broken. You could even change the variable type of "buttons" to be an int, a struct, or even a SQL query to a database, and that al_mouse_button_down() interface will remain the same. But you surely wouldn't be accessing a SQL database from a variable. *

If you don't mind the extra writing, use accessor functions always.

* (Fun sidenote: The D language supports "property functions" so that a getter/setter can appear to be, and be accessed like a variable. example)

[edit] And to make sure we're talking about the same thing here, you're still missing al_get_mouse_state(state); in your second example, so I'm assuming you meant to put it there.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Niunio
Member #1,975
March 2002
avatar

* (Fun sidenote: The D language supports "property functions" so that a getter/setter can appear to be, and be accessed like a variable. example [dlang.org])

I don't wan't to start a language war, but you were first. ;)

Object Pascal has properties too. The documentation description is quite complex, so there's a simple example:

  TYPE
    TMyClass = CLASS (TObject)
    PRIVATE
      fValue: INTEGER;

      FUNCTION GetValue: INTEGER;
      PROCEDURE SetValue (CONST aValue: INTEGER);
    PUBLIC
    (* Read-only property. *)
      PROPERTY OneProperty: INTEGER READ fValue;
    (* Getter and setter... *)
      PROPERTY OtherProperty: INTEGER READ GetValue WRITE SetValue;
    END;

C++ nor Java don't have properties, but you can always use methods.

-----------------
Current projects: Allegro.pas | MinGRo

Go to: