游戏引擎Allegro



  • 游戏引擎Allegro

    allegro是一个c/c++的游戏库,免费,开源,最初作者是shawn hargreaves,他支持DOS,UNIX(Linux, FreeBSD, Irix, Solaris, Darwin), Windows, QNX, BeOS 还有 MacOS X.提供了大量的图形,声音,玩家输入(键盘,鼠标,游戏手柄),定时器等函数。

    linux搭建环境

    1.下载源码

    git 地址 https://github.com/liballeg/allegro5.git

    https://github.com/liballeg/allegro5/releases/download/5.2.5.0/allegro-5.2.5.0.tar.gz

    2.解压

    $ tar -xzf allegro-5.2.5.0.tar.gz

    3.进入目录,打开README_make.txt,按照步骤执行

    4.在编译前要准备环境

    • 安装gcc,g++

      $ sudo apt install gcc

      $ sudo apt install g++

    • 安装cmake

      $ sudo apt install cmake

    • 安装x11

      $ sudo apt install libx11-dev

    • 安装OpenGL

      $ sudo apt-get install build-essential
      $ sudo apt-get install libgl1-mesa-dev
      $ sudo apt-get install libglu1-mesa-dev
      $ sudo apt-get install libglut-dev
      $ sudo apt-get install freeglut3-dev

    • 安装Xcursor
      $ sudo apt-get install libxcursor-dev

    5.在build目录运行

    $ cmake ..

    6.编译

    $ make -j4

    7.安装

    $ sudo make install

    第一个程序

    1.在allegro-5.2.5.0/examples目录有个ex_display_event.c例子

    2.改编例子,加注释,编译例子

    $ gcc ex_display_events.c -lallegro -lallegro_font -lallegro_primitives -o ex_display_events

    3.运行例子

    捕获.PNG

    4.代码地址
    5.代码浏览

    #include <stdarg.h>
    #include "allegro5/allegro.h"
    #include "allegro5/allegro_font.h"
    #include "allegro5/allegro_primitives.h"
    
    #include "common.c"
    
    #define MAX_EVENTS 23
    
    static char events[MAX_EVENTS][1024];
    
    static void add_event(char const *f, ...)
    {
    	va_list args;
    	memmove(events[1], events[0], (MAX_EVENTS - 1) * 1024);
    	va_start(args, f);
    	vsnprintf(events[0], 1024, f, args);
    	va_end(args);
    }
    
    const int WIN_WIDTH = 400;
    const int WIN_HEIGHT = 300;
    
    int main(int argc, char **argv)
    {
    	/*声明*/
    	ALLEGRO_DISPLAY *display;
    	ALLEGRO_EVENT_QUEUE *queue;
    	ALLEGRO_EVENT event;
    	ALLEGRO_FONT *font;
    	ALLEGRO_COLOR color, black, red, blue;
    
    	int i;
    
    	(void)argc;
    	(void)argv;
    
    	/*allegro初始化*/
    	if(!al_init()){
    		abort_example("Could not init Allegro.\n");
    	}
    
    	al_init_primitives_addon();	/*初始化基元插件*/
    	al_install_mouse();		/*安装鼠标驱动*/
    	al_install_keyboard();	/*安装键盘驱动*/
    	al_init_font_addon();	/*初始化字体插件*/
    
    	al_set_new_display_flags(ALLEGRO_RESIZABLE);	/*设置窗口可缩放*/
    	display = al_create_display(WIN_WIDTH, WIN_HEIGHT);	/*创建显示窗口*/
    	if(!display){;
    		abort_example("Error creating display\n");
    	}
    
    	al_set_window_title(display, "窗口标题");		/*创建窗口标题*/
    
    	font = al_create_builtin_font();		/*创建字体*/
    	if(!font){
    		abort_example("Error creating buildin font\n");
    	}
    
    	/*初始化颜色*/
    	black = al_map_rgb_f(0, 0, 0);
    	red = al_map_rgb_f(1, 0, 0);
    	blue = al_map_rgb_f(0, 0, 1);
    
    	/*创建事件队列*/
    	queue = al_create_event_queue();
    	al_register_event_source(queue, al_get_mouse_event_source());	/*注册鼠标事件*/
    	al_register_event_source(queue, al_get_keyboard_event_source());	/*注册键盘事件*/
    	al_register_event_source(queue, al_get_display_event_source(display));	/*注册显示事件*/
    
    	while(1){
    		if(al_is_event_queue_empty(queue)){
    			float x = 8, y = 28;
    			al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0));	/*背景颜色*/
    
    			al_draw_textf(font, blue, 8, 8, 0, "Hello World.");	/*写字*/
    
    			color = red;
    			for(i = 0; i < MAX_EVENTS; i++){
    				if(!events[i]) continue;
    				al_draw_textf(font, color, x, y, 0, "%s", events[i]);	/*循环打印事件*/
    				color = black;
    				y += 20;
    			}
    			al_flip_display();	/*更新显示*/
    		}
    
    		al_wait_for_event(queue, &event);	/*当队列有事件,会把事件存入event*/
    		switch(event.type){
    			case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY:
    				add_event("ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY");
    				break;
    
    			case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
    				add_event("ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY");
    				break;
    
    			case ALLEGRO_EVENT_KEY_DOWN:
    				if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE){
    					goto done;
    				}
    				else{
    					add_event("ALLEGRO_EVENT_KEY_DOWN.keycode=%d", event.keyboard.keycode);
    				}
    				break;
    
    			case ALLEGRO_EVENT_DISPLAY_RESIZE:
    				add_event("ALLEGRO_EVENT_DISPLAY_RESIZE x=%d, y=%d, "
    					"width=%d, height=%d",
    					event.display.x, event.display.y, event.display.width,
    					event.display.height);
    				al_acknowledge_resize(event.display.source);
    				break;
    
    			case ALLEGRO_EVENT_DISPLAY_CLOSE:
    				add_event("ALLEGRO_EVENT_DISPLAY_CLOSE");
    				break;
    
    			case ALLEGRO_EVENT_DISPLAY_LOST:
    				add_event("ALLEGRO_EVENT_DISPLAY_LOST");
    				break;
    
    			case ALLEGRO_EVENT_DISPLAY_FOUND:
    				add_event("ALLEGRO_EVENT_DISPLAY_FOUND");
    				break;
    
    			case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
    				add_event("ALLEGRO_EVENT_DISPLAY_SWITCH_OUT");
    				break;
    
    			case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
    				add_event("ALLEGRO_EVENT_DISPLAY_SWITCH_IN");
    				break;
    		}
    	}
    
    done:
    	al_destroy_event_queue(queue);	/*销毁事件队列*/	
    	al_destroy_display(display);	/*销毁显示*/	
    
    	return 0;
    }
    

    API查询手册


Log in to reply
 

走马观花

最近的回复

  • 诶 没有Linux吗??

    read more
  • 萌梦 男孩,女孩,和蛋

    menghome.png

    read more
  • 设计模式-工厂模式

    使用qt/qml来演示设计模式效果,便于学习理解

    1)定义创建对象的接口,封装对象的创建
    2)使具体化类的工作延迟到工厂子类中

    bg.png
    image.png

    1. 工厂类

    createProduct使用了参数来选择要创建哪个产品

    #ifndef FACTORY_H #define FACTORY_H #include <QObject> class Product; class QString; class Factory: public QObject { Q_OBJECT public: virtual ~Factory() = 0; virtual Product* createProduct(QString type) = 0; protected: Factory(); }; class ConcreteFactory: public Factory { Q_OBJECT public: ~ConcreteFactory(); ConcreteFactory(); public slots: Product* createProduct(QString type); }; #endif // FACTORY_H #include "factory.h" #include "product.h" #include <QtQml/qqml.h> Factory::~Factory() { } Factory::Factory() { qmlRegisterType<Product>("Product", 1, 0, "Product"); } ConcreteFactory::~ConcreteFactory() { } ConcreteFactory::ConcreteFactory() { } Product *ConcreteFactory::createProduct(QString type) { if(type == "boy") return static_cast<Product *>(new ConcreteProduct1()); else if(type == "girl") return static_cast<Product *>(new ConcreteProduct2()); return static_cast<Product *>(new ConcreteProduct1()); } 2 产品类

    一个产品是萌梦男,一个产品是萌梦女

    #ifndef PRODUCT_H #define PRODUCT_H #include <QObject> class Product: public QObject { Q_OBJECT public: virtual ~Product() = 0; Q_PROPERTY(QString icon READ icon NOTIFY iconChanged) QString m_icon; QString icon() const { return m_icon; } signals: void iconChanged(QString icon); protected: Product(); signals: public slots: }; class ConcreteProduct1: public Product { Q_OBJECT public: ~ConcreteProduct1(); ConcreteProduct1(); }; class ConcreteProduct2: public Product { Q_OBJECT public: ~ConcreteProduct2(); ConcreteProduct2(); }; #endif // PRODUCT_H #include "product.h" Product::~Product() { } Product::Product() { } ConcreteProduct1::~ConcreteProduct1() { } ConcreteProduct1::ConcreteProduct1() { m_icon = "qrc:/images/boy.png"; } ConcreteProduct2::~ConcreteProduct2() { } ConcreteProduct2::ConcreteProduct2() { m_icon = "qrc:/images/girl.png"; } 3. main.qml

    使用timer,canvas,listview等实现一个自动化生产的动画效果

    源代码

    Fork me on Gitee

    read more
  • blender建模 章鱼
    捕获2.PNG 😵

    read more

关注我们

微博
QQ群