14 Ağustos 2008 Perşembe

Allegroyu biraz kolaylaştıralım !

C++ ile Allegro programlarken, programa giriş noktası yani main() fonksiyonunun hemen başında, ilk satırlarında basit ama uğraştıran, birtakım kodlar yazmak zorundayız. Gerçi gerek yok ama sadece kolaylık olması için bu işleri yapan bir sınıf oluşturabilirz. Hem c++ için pratik yapmış oluruz. 

Kodlara gelince:

//İşimizi görecek olan sınıf
settings.h dosyası :

/* Allegro yardımcı sınıf (beta) */
#include <allegro.h>

enum __DEPTH { x8, x15, x16, x24, x32 };
enum __GFX { WINDOWED = 0, FULLSCREEN = 1, ISNULL = 2 };

/* interface */
class Settings
{
    BITMAP *buffer;
    public:
        enum __GFX gfx_mode;  
        enum __DEPTH color_depth;
        bool _install_keyboard;
        bool _install_mouse;
        bool _show_mouse;
        bool buffer_install;
        int buffer_w, buffer_h;
        BITMAP *__mouse;
        const char* window_title;
              
        Settings();
        ~Settings();         
        void install();               
};

Settings::Settings()
{
    gfx_mode = WINDOWED;
    color_depth = x16;
    _install_keyboard = true;
    _install_mouse = true;
    buffer_install = true;
    __mouse = NULL;
    _show_mouse = true;
    buffer_w = 800;
    buffer_h = 600;
    window_title = "Allegro Game";
}

Settings::~Settings()
{
    /* dispose */                    
}

void Settings::install()
{
    allegro_init();  
    
    if(buffer_install == true)
    {
         buffer = create_bitmap(buffer_w, buffer_h);
    }
    
    switch(color_depth)
    {
        case x8: set_color_depth(8); break;
        case x15: set_color_depth(15); break;
        case x16: set_color_depth(16); break;
        case x24: set_color_depth(24); break;
        case x32: set_color_depth(32); break;
    }
    
    switch(gfx_mode)
    {
         case WINDOWED: set_gfx_mode(GFX_AUTODETECT_WINDOWED, buffer_w, buffer_h, 0, 0); break;               
         case FULLSCREEN: set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, buffer_w, buffer_h, 0, 0); break;
         case ISNULL: set_gfx_mode(GFX_AUTODETECT, buffer_w, buffer_h, 0, 0); break;
    }
    
        if(_install_keyboard == true)
        {
            install_keyboard();
        }  
            if(_install_mouse == true)
            {
                install_mouse();
            }
                if(_show_mouse == true)
                {
                    show_mouse(__mouse);
                }
        
        set_window_title(window_title);
}

//Ana program kodlarımız
ve main.cs:

#include <stdlib.h>
#include <allegro.h>
#include "settings.h"

Settings gSettings;

void InstallAllegro() {
    gSettings.color_depth = x32;
    gSettings.window_title = "Allegro Denemesi";
    gSettings._show_mouse = false;
    gSettings.buffer_w = 320;
    gSettings.buffer_h = 240;
    gSettings.install();
}

int main()
{
    //Ne kadar kısa demi :)
    InstallAllegro();
    
    while(!key[KEY_ESC]);
    
    allegro_exit();
    return 0;
} END_OF_MAIN();

Hiç yorum yok: