金子邦彦研究室プログラミングC/C++ でキーボード,マウス,テキスト描画(C/C++, Allegro 5 を使用)ゲームエンジン Allegro バージョン 5.0 のインストールとサンプルプログラム

ゲームエンジン Allegro バージョン 5.0 のインストールとサンプルプログラム

Allegro バージョン 5.0 の概要


 

ダウンロードとインストール

  1. 前準備として前提ソフトウェアをインストール

    Ubuntu での操作手順例

    sudo apt -y update
    sudo apt -y install flac libflac++-dev libflac-dev 
    sudo apt -y install libdump 
    sudo apt -y install libvorbis-dev
    sudo apt -y install libogg-dev
    sudo apt -y install libxxf86dga-dev oss4-dev x11proto-xf86dga-dev
    
  2. Allegro の Web ページを開く

    https://liballeg.org/license.html

  3. 「Latest version」をクリック

    [image]
  4. ダウンロード

    [image]
  5. 解凍

    cd /tmp
    tar -xvzof allegro-5.0.8.tar.gz
    cd allegro-5.0.8
    
  6. ccmake でオプションを確認

    次の手順でオプションを確認

    1. 「ccmake . 」を実行

      [image]
    2. 次の画面で c キーを押す.警告は無視

      [image]
    3. 次に e キーを押すと、オプションに関する画面が現れる(下記)

      [image]
    4. 終了したい時は q キー
  7. ビルド

    cmake .
    make
    

    ※ cmake のあとには半角スペースと、半角のピリオド

  8. make の結果の確認

    [image]
  9. インストール

    sudo make install
    
  10. インストールの結果の確認

    [image]

サンプルプログラム

ftp://ftp.pucmg.br/Computacao/Mais_Utilizados/Programacao_Software/C_-_C++/CodeBlocks/allegro/allegro/examples/ex_ttf.c

ごく簡単なサンプル (textout)

#define ALLEGRO_STATICLINK
#include<stdio.h>
#include<allegro.h>
#include<allegro_ttf.h>
#include<allegro_primitives.h>

static ALLEGRO_FONT *font24;

int doit()
{
  al_draw_textf(font24, al_map_rgb(128, 100, 20), 50, 50, ALLEGRO_ALIGN_CENTRE, "hoge");  

  return 0; 
}


int main()
{
  /* you should always do this at the start of Allegro programs */
  if(!al_init()) {
    return -1;
  }

  /* set up the keyboard handler */
  al_install_keyboard();
  /* set up the mouse handler */
  al_install_mouse();
  /* set up fonts */
  al_init_font_addon();
  al_init_ttf_addon();

  /* set a graphics mode sized 800x640 */
  ALLEGRO_DISPLAY *display = NULL;
  display = al_create_display(800, 640);
  if(!display) {
    return -1;
  }

  /* load font */ 
  font24 = al_load_font("/usr/share/fonts/truetype/freefont/FreeSans.ttf",24,0);
  al_clear_to_color(al_map_rgb(0,0,0)); // clears the screen to the selected color
  
  doit();

  ALLEGRO_EVENT_QUEUE *EventQueue;
  ALLEGRO_EVENT Event;
  bool Exit = false;
  EventQueue = al_create_event_queue();
  al_register_event_source(EventQueue, al_get_display_event_source(display));
  al_register_event_source(EventQueue, al_get_keyboard_event_source());
  al_register_event_source(EventQueue, al_get_mouse_event_source());

  while(Exit == false) {
      al_flip_display();
      al_wait_for_event(EventQueue, &Event);

      if(Event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
      Exit = true;
      }
  }

  al_destroy_display(display);
  return 0;
}

次の手順でコンパイル

g++ hoge.cc -I/usr/local/include/allegro5 -L/usr/local/lib -lallegro -lallegro_font -lallegro_ttf -lm

次の手順で実行

./a.out

[image]