2013-11-24 5 views
0

반투명 한 빨간색 사각형을 화면 표면에 그리는 방법을 알 수 없습니다.SDL : 반투명 사각형을 그립니다.

#!/usr/bin/perl 

use SDL; 
use SDL::Video; 
use SDL::Surface; 
use SDL::Rect; 

# the size of the window box or the screen resolution if fullscreen 
my $screen_width = 800; 
my $screen_height = 600; 

SDL::init(SDL_INIT_VIDEO); 

# setting video mode 
my $screen_surface = SDL::Video::set_video_mode($screen_width, $screen_height, 32, SDL_ANYFORMAT|SDL_SRCALPHA); 

# drawing something somewhere 
my $mapped_color = SDL::Video::map_RGBA($screen_surface->format(), 255, 0, 0, 128); #should be half-transparent, I suppose? 

SDL::Video::fill_rect($screen_surface, 
SDL::Rect->new($screen_width/4, $screen_height/4, 
       $screen_width/2, $screen_height/2), $mapped_color); 

# update an area on the screen so its visible 
SDL::Video::update_rect($screen_surface, 0, 0, $screen_width, $screen_height); 

sleep(5); # just to have time to see it 

그것은 내가 달성하기 위해 노력하고 무엇을하지 않은 검은 색 바탕에 빨간색 불투명 한 사각형 결과 : 는 여기에 지금까지 가지고있는 코드입니다.

답변

3

투명도를 위해 SDL_FillRect를 사용할 수 없습니다. 이 함수는 서페이스를 color 값으로 덮어 씁니다. 그것을 "memset"이라고 생각하십시오.

인용구 : 색상 값에 알파 값이 포함 된 경우 대상은 해당 알파 정보로 간단하게 채워지고 블렌딩은 발생하지 않습니다.

투명도를 얻으려면 SDL_BlitSurface를 사용하십시오. 먼저 텍스처를 색으로 채운 다음 blit합니다.

내가 연습 약간의 테스트 케이스를 만들어 :

#include <SDL.h> 

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 

SDL_Surface* CreateSurface(int width , int height) 
{ 
    uint32_t rmask , gmask , bmask , amask ; 

    /* SDL interprets each pixel as a 32-bit number, so our masks must depend 
     on the endianness (byte order) of the machine */ 
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN 
     rmask = 0xff000000; 
     gmask = 0x00ff0000; 
     bmask = 0x0000ff00; 
     amask = 0x000000ff; 
    #else 
     rmask = 0x000000ff; 
     gmask = 0x0000ff00; 
     bmask = 0x00ff0000; 
     amask = 0xff000000; 
    #endif 

    SDL_Surface* surface = SDL_CreateRGBSurface(0 , width , height , 32 , rmask , gmask , bmask , amask) ; 
    if(surface == NULL) 
    { 
     (void)fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError()); 
     exit(1); 
    } 

return surface ; 
} 

void Quit(void) 
{ 
    SDL_Quit() ; 
    exit(0) ; 
} 


int main(int argc, char *argv[]) 
{ 
    (void)argc ; 
    (void)argv ; 

    int init = !(SDL_Init(SDL_INIT_EVERYTHING)); 
    if(!init) 
     Quit() ; 


    SDL_Surface* screen = SDL_SetVideoMode(800 , 600 , 32 , 0) ; 
    if(!screen) 
     Quit() ; 

    int run = true ; 

    while(run) 
    { 

     SDL_Event event ; 
     while(SDL_PollEvent(&event)) 
     { 
      switch(event.type) 
      { 
       case SDL_QUIT: 
        run = false ; 
        break; 
      } 
     } 

     SDL_Surface* s = CreateSurface(300 ,300) ; 
     (void)SDL_FillRect(s , NULL , 0xAA0000FF) ; 


     SDL_Rect rect = { 100 , 100 } ; 
     (void)SDL_BlitSurface(s , NULL , screen , &rect) ; 

     rect.x = 200 ; 
     rect.y = 200 ; 
     (void)SDL_FillRect(s , NULL , 0x440000FF) ; 
     (void)SDL_BlitSurface(s , NULL , screen , &rect) ; 

     SDL_FreeSurface(s) ; 

     (void)SDL_Flip(screen) ; 
     SDL_Delay(15) ; 

     (void)SDL_FillRect(screen , NULL , 0x00FFFF) ; 
    } 

    Quit() ; 

    return 0; 
} 
+0

감사합니다, 그것은 도움이! – varnie

+0

나는 왜 물어볼 필요가있다. 왜 "(void)"를 넣을 까? 컴파일러는 반환 값을 무시하고 저장하지 않습니까? –

관련 문제