Programmer_21 [10:41 PM]
Hello Apiza

Apiza [10:41 PM]
Hi Programmer_21

Programmer_21 [10:41 PM]
I've never used Allegro before

Apiza [10:42 PM]
Ask about an Allegro function or feature and I will try to provide you with helpful information!

Programmer_21 [10:44 PM]
how do I get the key input?
How do I get the keyboard input in allegro?

Apiza [10:47 PM]
Does this help?

```ALLEGRO_EVENT_KEY_CHAR

A character was typed on the keyboard, or a character was auto-repeated.

keyboard.keycode (int)
    The code corresponding to the physical key which was last pressed. See the Key codes section for the list of ALLEGRO_KEY_* constants. 
keyboard.unichar (int)

    A Unicode code point (character). This may be zero or negative if the event was generated for a non-visible "character", such as an arrow or Function key. In that case you can act upon the keycode field.

    Some special keys will set the unichar field to their standard ASCII values: Tab=9, Return=13, Escape=27. In addition if you press the Control key together with A to Z the unichar field will have the values 1 to 26. For example Ctrl-A will set unichar to 1 and Ctrl-H will set it to 8.

    As of Allegro 5.0.2 there are some inconsistencies in the treatment of Backspace (8 or 127) and Delete (127 or 0) keys on different platforms. These can be worked around by checking the keycode field.
keyboard.modifiers (unsigned)
    This is a bitfield of the modifier keys which were pressed when the event occurred. See "Keyboard modifier flags" for the constants. 
keyboard.repeat (bool)
    Indicates if this is a repeated character. 
keyboard.display (ALLEGRO_DISPLAY *)
    The display which had keyboard focus when the event occurred.``` 

Programmer_21 [10:47 PM]
How do I use that?

Apiza [10:49 PM]
Here is some information about getting events:
```Event queues and input

Input comes from multiple sources: keyboard, mouse, joystick, timers, etc. Event queues aggregate events from all these sources, then you can query the queue for events.

Create an event queue with al_create_event_queue, then tell input sources to place new events into that queue using al_register_event_source. The usual input event sources can be retrieved with al_get_keyboard_event_source, al_get_mouse_event_source and al_get_joystick_event_source.

Events can be retrieved with al_wait_for_event or al_get_next_event. Check the event type and other fields of ALLEGRO_EVENT to react to the input.

Displays are also event sources, which emit events when they are resized. You'll need to set the ALLEGRO_RESIZABLE flag with al_set_new_display_flags before creating the display, then register the display with an event queue. When you get a resize event, call al_acknowledge_resize.

Timers are event sources which "tick" periodically, causing an event to be inserted into the queues that the timer is registered with. Create some with al_create_timer.

al_get_time and al_rest are more direct ways to deal with time.```

Programmer_21 [10:50 PM]
Do you have any examples?

Apiza [10:51 PM]
Here's an example of using events:
```#include <stdio.h>
#include <allegro5/allegro.h>
 
int main(int argc, char **argv){
   
   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_EVENT_QUEUE *event_queue = NULL;

   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }

   display = al_create_display(640, 480);
   if(!display) {
      fprintf(stderr, "failed to create display!\n");
      return -1;
   }

   event_queue = al_create_event_queue();
   if(!event_queue) {
      fprintf(stderr, "failed to create event_queue!\n");
      al_destroy_display(display);
      return -1;
   }
 
   al_register_event_source(event_queue, al_get_display_event_source(display));

   al_clear_to_color(al_map_rgb(0,0,0));
 
   al_flip_display();
 
   while(1)
   {
      ALLEGRO_EVENT ev;
      ALLEGRO_TIMEOUT timeout;
      al_init_timeout(&timeout, 0.06);
      
      bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);

      if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
         break;
      }

      al_clear_to_color(al_map_rgb(0,0,0));
      al_flip_display();
   }
 
   al_destroy_display(display);
   al_destroy_event_queue(event_queue);

   return 0;
}```

Programmer_21 [10:53 PM]
list of ALLEGRO_KEY_* constants

Apiza [10:54 PM]
These are the list of key codes used by Allegro, which are returned in the event.keyboard.keycode field of the ALLEGRO_KEY_DOWN and ALLEGRO_KEY_UP events and which you can pass to al_key_down:

```ALLEGRO_KEY_A ... ALLEGRO_KEY_Z
ALLEGRO_KEY_0 ... ALLEGRO_KEY_9
ALLEGRO_KEY_PAD_0 ... ALLEGRO_KEY_PAD_9
ALLEGRO_KEY_F1 ... ALLEGRO_KEY_F12
ALLEGRO_KEY_ESCAPE
ALLEGRO_KEY_TILDE
ALLEGRO_KEY_MINUS
ALLEGRO_KEY_EQUALS
ALLEGRO_KEY_BACKSP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ND

/* Since: 5.1.1 */
/* Android only for now */
ALLEGRO_KEY_BACK

/* Since: 5.1.2 */
/* Android only for now */
ALLEGRO_KEY_VOLUME_UP
ALLEGRO_KEY_VOLUME_DOWN

/* Since: 5.1.6 */
/* Android only for now */
ALLEGRO_KEY_SEARCH
ALLEGRO_KEY_DPAD_CENTER
ALLEGRO_KEY_BUTTON_X
ALLEGRO_KEY_BUTTON_Y
ALLEGRO_KEY_DPAD_UP
ALLEGRO_KEY_DPAD_DOWN
ALLEGRO_KEY_DPAD_LEFT
ALLEGRO_KEY_DPAD_RIGHT
ALLEGRO_KEY_SELECT
ALLEGRO_KEY_START
ALLEGRO_KEY_L1
ALLEGRO_KEY_R1```

Programmer_21 [10:59 PM]
example ALLEGRO_KEY_DOWN
example event.keyboard.keycode

Apiza [11:00 PM]
Untitled 
Here is an example using `KEY_DOWN`:
```
#include <stdio.h>
#include <allegro5/allegro.h>

Programmer_21 [11:10 PM]
debug text

Apiza [11:11 PM]
Sorry, I'm not sure what you mean by that

Programmer_21 [11:12 PM]
how do I output debug strings?
print function

Apiza [11:14 PM]
Does this function help?

```al_ustr_newf

ALLEGRO_USTR *al_ustr_newf(const char *fmt, ...)

Source Code

Create a new string using a printf-style format string.

Notes:

The "%s" specifier takes C string arguments, not ALLEGRO_USTRs. Therefore to pass an ALLEGRO_USTR as a parameter you must use al_cstr, and it must be NUL terminated. If the string contains an embedded NUL byte everything from that byte onwards will be ignored.

The "%c" specifier outputs a single byte, not the UTF-8 encoding of a code point. Therefore it is only usable for ASCII characters (value <= 127) or if you really mean to output byte values from 128--255. To insert the UTF-8 encoding of a code point, encode it into a memory buffer using al_utf8_encode then use the "%s" specifier. Remember to NUL terminate the buffer.

See also: al_ustr_new, al_ustr_appendf```

Programmer_21 [11:14 PM]
No.
ALLEGRO_EVENT_KEY_DOWN isn't triggering

Apiza [11:16 PM]
Here is some information about events that might help:

```What are Events?

Events tell you that something has happened in your program such as a key being pressed, the mouse has moved, a timer has been ticked, and the display has synchronized. Allegro uses them to notify you of all types of events, but you may create your own customized events if required.
Why do I want Events?

Using Allegro's event queue system is the most efficient way to handle events; you simply wait for the arrival of new events and handle them when they occur. An alternative method of handling events is via polling -- constantly checking whether an event has occurred. The event system reduces processor usage compared to the polling method.
Event Sources

An event source is something that notifies the program when something has occurred. There are several standard Allegro event sources, including but not limited to Timers, Displays, and Inputs.
Event Queues

An event queue is where events are placed in a first-in-first-out container and are removed when the events are being handled by the main program. You may create multiple event queues and direct the event sources into the appropriate queues. However, using a single event queue is the simplest method.``` 
Does this help with your question about printing?

```al_fprintf

int al_fprintf(ALLEGRO_FILE *pfile, const char *format, ...)

Source Code

Writes to a file with stdio "printf"-like formatting. Returns the number of bytes written, or a negative number on error.```

Programmer_21 [11:21 PM]
event.type to string
print event.type

Apiza [11:25 PM]
Here is the documentation I have on ALLEGRO_EVENT_TYPE:

```ALLEGRO_EVENT_TYPE

typedef unsigned int ALLEGRO_EVENT_TYPE;

Source Code

An integer used to distinguish between different types of events.```

Programmer_21 [11:28 PM]
al_register_event_source keyboard

Apiza [11:30 PM]
Does this work?

   ```al_register_event_source(event_queue, al_get_keyboard_event_source());```
Here is some more context for that line:

 ```event_queue = al_create_event_queue();
   if(!event_queue) {
      fprintf(stderr, "failed to create event_queue!\n");
      al_destroy_bitmap(bouncer);
      al_destroy_display(display);
      al_destroy_timer(timer);
      return -1;
   }
 
   al_register_event_source(event_queue, al_get_display_event_source(display));
 
   al_register_event_source(event_queue, al_get_timer_event_source(timer));

   al_register_event_source(event_queue, al_get_keyboard_event_source());```   

Programmer_21 [11:31 PM]
Thank you.
Load audio

Apiza [11:32 PM]
Here is an example of using audio:

```#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>

int main(int argc, char **argv){

   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_SAMPLE *sample=NULL;
   
   if(!al_init()){
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }
        
   if(!al_install_audio()){
      fprintf(stderr, "failed to initialize audio!\n");
      return -1;
   }

   if(!al_init_acodec_addon()){
      fprintf(stderr, "failed to initialize audio codecs!\n");
      return -1;
   }
   
   if (!al_reserve_samples(1)){
      fprintf(stderr, "failed to reserve samples!\n");
      return -1;
   }
   
   sample = al_load_sample( "footstep.wav" );

   if (!sample){
      printf( "Audio clip sample not loaded!\n" ); 
      return -1;
   }
   
   display = al_create_display(640, 480);
   
   if(!display){
      fprintf(stderr, "failed to create display!\n");
      return -1;
   }

   /* Loop the sample until the display closes. */
   al_play_sample(sample, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);

   al_rest(10.0);
   
   al_destroy_display(display);
   al_destroy_sample(sample);
   return 0;
}```

Programmer_21 [11:35 PM]
ALLEGRO_PLAYMODE_* single

Apiza [11:36 PM]
Here is the documentation I have about ALLEGRO_PLAYMODE:

```ALLEGRO_PLAYMODE

enum ALLEGRO_PLAYMODE

Source Code

Sample and stream playback mode.

    ALLEGRO_PLAYMODE_ONCE
    ALLEGRO_PLAYMODE_LOOP
    ALLEGRO_PLAYMODE_BIDIR```

Programmer_21 [11:38 PM]
hud display align top left

Apiza [11:40 PM]
Here is an example of some display code that might help:

```#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_native_dialog.h"

int main(int argc, char **argv){

   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_BITMAP  *image   = NULL;

   if(!al_init()) {
      al_show_native_message_box(display, "Error", "Error", "Failed to initialize allegro!", 
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      return 0;
   }

   if(!al_init_image_addon()) {
      al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!", 
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      return 0;
   }

   display = al_create_display(800,600);

   if(!display) {
      al_show_native_message_box(display, "Error", "Error", "Failed to initialize display!", 
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      return 0;
   }

   image = al_load_bitmap("image.png");

   if(!image) {
      al_show_native_message_box(display, "Error", "Error", "Failed to load image!", 
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      al_destroy_display(display);
      return 0;
   }

   al_draw_bitmap(image,200,200,0);

   al_flip_display();
   al_rest(2);

   al_destroy_display(display);
   al_destroy_bitmap(image);

   return 0;
}```

Programmer_21 [11:42 PM]
display ui text

Apiza [11:44 PM]
Here's an example of displaying text:

```#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>

int main(int argc, char **argv){

   if(!al_init()){
      fprintf(stderr, "Failed to initialize Allegro.\n");
      return -1;
   }

   al_init_font_addon(); // initialize the font addon
   al_init_ttf_addon();// initialize the ttf (True Type Font) addon

   ALLEGRO_DISPLAY *display = al_create_display(640,480);
   
   if (!display){
      fprintf(stderr, "Failed to create display.\n");
      return -1;
   }

   ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf",72,0 );

   if (!font){
      fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
      return -1;
   }

   al_clear_to_color(al_map_rgb(50,10,70));
   al_draw_text(font, al_map_rgb(255,255,255), 640/2, (480/4),ALLEGRO_ALIGN_CENTRE, "Your Text Here!");

   al_flip_display();

   al_rest(10.0);

   al_destroy_display(display);

   return 0;
}```

Programmer_21 [11:48 PM]
ALLEGRO_ALIGN_*

Apiza [11:50 PM]
Here's some documentation that might help:


```al_draw_text

void al_draw_text(const ALLEGRO_FONT *font,
   ALLEGRO_COLOR color, float x, float y, int flags,
   char const *text) 

Source Code

Writes the NUL-terminated string text onto the target bitmap at position x, y, using the specified font.

The flags parameter can be 0 or one of the following flags:

    ALLEGRO_ALIGN_LEFT - Draw the text left-aligned (same as 0).
    ALLEGRO_ALIGN_CENTRE - Draw the text centered around the given position.
    ALLEGRO_ALIGN_RIGHT - Draw the text right-aligned to the given position.

It can also be combined with this flag:

    ALLEGRO_ALIGN_INTEGER - Always draw text aligned to an integer pixel position. This was formerly the default behaviour. Since: 5.0.8, 5.1.4

This function does not support newline characters (\n), but you can use al_draw_multiline_text for multi line text output.

See also: al_draw_ustr, al_draw_textf, al_draw_justified_text, al_draw_multiline_text.```

Programmer_21 [11:51 PM]
screen size
al_draw_textf

Apiza [11:55 PM]
Here's the documentation on that function:

```al_draw_textf

void al_draw_textf(const ALLEGRO_FONT *font, ALLEGRO_COLOR color,
   float x, float y, int flags,
   const char *format, ...)

Source Code

Formatted text output, using a printf() style format string. All parameters have the same meaning as with al_draw_text otherwise.

See also: al_draw_text, al_draw_ustr```

Programmer_21 [11:55 PM]
al_draw_text integer

Apiza [11:56 PM]
Here's the documentation on that function:

```al_draw_text

void al_draw_text(const ALLEGRO_FONT *font,
   ALLEGRO_COLOR color, float x, float y, int flags,
   char const *text) 

Source Code

Writes the NUL-terminated string text onto the target bitmap at position x, y, using the specified font.

The flags parameter can be 0 or one of the following flags:

    ALLEGRO_ALIGN_LEFT - Draw the text left-aligned (same as 0).
    ALLEGRO_ALIGN_CENTRE - Draw the text centered around the given position.
    ALLEGRO_ALIGN_RIGHT - Draw the text right-aligned to the given position.

It can also be combined with this flag:

    ALLEGRO_ALIGN_INTEGER - Always draw text aligned to an integer pixel position. This was formerly the default behaviour. Since: 5.0.8, 5.1.4

This function does not support newline characters (\n), but you can use al_draw_multiline_text for multi line text output.

See also: al_draw_ustr, al_draw_textf, al_draw_justified_text, al_draw_multiline_text.```

Programmer_21 [11:57 PM]
al_draw_text number example (edited) 

Apiza [11:58 PM]
Here's an example function call:

```al_draw_text(font, al_map_rgb(255,255,255), 640/2, (480/4),ALLEGRO_ALIGN_CENTRE, "Your Text Here!");```

Programmer_21 [11:58 PM]
number to string

Apiza [12:00 AM]
Does this C function help?

```sprintf(str, "%d", 42);```

Programmer_21 [12:02 AM]
Allegro doesn't have the ability to display HUD numbers?

Apiza [12:03 AM]
Sorry, I'm not sure what you mean by that.

Programmer_21 [12:03 AM]
Does Allegro have a way to display a number in the UI?

Apiza [12:05 AM]
The native dialogs addon might help. Here is an example:

```#include "allegro5/allegro.h"
#include <allegro5/allegro_native_dialog.h>

int main(int argc, char **argv){

   if(al_init()) {
      al_show_native_message_box(al_get_current_display(), 
                                 "Window Title", 
                                 "Content Title", 
                                 "The error message here",
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      return 0;
   }

   return 0;
}```