2013-02-24 3 views
3

알레그로를 사용하여 비트 맵을 특정 크기로로드하려고합니다.비트 맵 이미지를 특정 크기로 로딩

al_crate_bitmap (x, y) - 특정 크기의 비트 맵을 만듭니다. al_load_bitmap (filename) - 필요한 이미지를 원본 크기로로드합니다.

내가 설정 한 크기로 비트 맵을로드해야합니다. 어떤 아이디어?

감사합니다, 소니

답변

2

키는 al_draw_scaled_bitmap()입니다. 이를 통해 모든 소스 비트 맵을 새로운 크기로 대상 비트 맵에 블릿 할 수 있습니다. 따라서 실제로는 새로운 비트 맵을 생성 할 필요가 없습니다. 이 함수를 사용하여 비트 맵을 다른 크기로 그릴 수 있습니다. Allegro 5는 하드웨어 가속 기능을 제공하므로 이러한 유형의 작업은 종종 "무료"입니다. 나는 코드의 기본 단계를 주석 한

ALLEGRO_BITMAP *load_bitmap_at_size(const char *filename, int w, int h) 
{ 
    ALLEGRO_BITMAP *resized_bmp, *loaded_bmp, *prev_target; 

    // 1. create a temporary bitmap of size we want 
    resized_bmp = al_create_bitmap(w, h); 
    if (!resized_bmp) return NULL; 

    // 2. load the bitmap at the original size 
    loaded_bmp = al_load_bitmap(filename); 
    if (!loaded_bmp) 
    { 
    al_destroy_bitmap(resized_bmp); 
    return NULL; 
    } 

    // 3. set the target bitmap to the resized bmp 
    prev_target = al_get_target_bitmap(); 
    al_set_target_bitmap(resized_bmp); 

    // 4. copy the loaded bitmap to the resized bmp 
    al_draw_scaled_bitmap(loaded_bmp, 
    0, 0,        // source origin 
    al_get_bitmap_width(loaded_bmp),  // source width 
    al_get_bitmap_height(loaded_bmp), // source height 
    0, 0,        // target origin 
    w, h,        // target dimensions 
    0         // flags 
); 

    // 5. restore the previous target and clean up 
    al_set_target_bitmap(prev_target); 
    al_destroy_loaded_bmp(loaded_bmp); 

    return resized_bmp;  
} 

:

직접 질문에 대답합니다. Allegro 5에는 대개 디스플레이의 백 버퍼 인 암시 적 대상이 있습니다. 그러나 위의 코드에서 볼 수 있듯이 비트 맵에서 비트 맵으로 작업해야하는 경우 다른 비트 맵을 대상으로 지정할 수 있습니다.

다른 방법은 함수 외부 대상 비트 맵을 이동 :

void load_bitmap_onto_target(const char *filename) 
{ 
    ALLEGRO_BITMAP *loaded_bmp; 
    const int w = al_get_bitmap_width(al_get_target_bitmap()); 
    const int h = al_get_bitmap_height(al_get_target_bitmap()); 

    // 1. load the bitmap at the original size 
    loaded_bmp = al_load_bitmap(filename); 
    if (!loaded_bmp) return; 

    // 2. copy the loaded bitmap to the resized bmp 
    al_draw_scaled_bitmap(loaded_bmp, 
    0, 0,        // source origin 
    al_get_bitmap_width(loaded_bmp),  // source width 
    al_get_bitmap_height(loaded_bmp), // source height 
    0, 0,        // target origin 
    w, h,        // target dimensions 
    0         // flags 
); 

    // 3. cleanup 
    al_destroy_bitmap(loaded_bmp); 
} 

ALLEGRO_BITMAP *my_bmp = al_create_bitmap(1000,1000); 
al_set_target_bitmap(my_bmp); 
load_bitmap_onto_target("test.png"); 
// perhaps restore the target bitmap to the back buffer, or continue 
// to modify the my_bmp with more drawing operations. 
+0

마태는 내가 필요 정확히,이 주셔서 감사합니다. Im 조금 부분에 혼란 스러울지라도. prev_target = al_get_target_bitmap(); 나는 pre_target이 무엇을하는지 이해하지 못합니까? – codingNightmares

+0

Allegro에는 al_set_target_bitmap()을 통해 설정된 암시 적 대상 비트 맵이 있습니다. 이후의 모든 그리기 작업은 해당 비트 맵에 수행됩니다. 이 함수가 대상 비트 맵을 복원하지 못하면 호출 코드는 대상이 변경되었음을 인식하지 못하고 실수로 비트 맵을 그릴 수 있습니다. 일반적으로 A5에서는 함수를 호출하기 전에 이것을 처리 할 것이지만, 함수 안에 그대로 두어 그대로 사용할 수있게했습니다. – Matthew

+0

@codingNightmares, 두 번째 예제를 추가했습니다. 요점은 일반적으로 대상 비트 맵을 변경하는 것과 같은 부작용이 없어야하는 함수입니다. 따라서 두 번째 예제에서 함수는 현재 타겟에 그려집니다. 그 연습은 일반적으로 A5의 API로 더 잘 맞습니다. – Matthew

관련 문제