2011-07-24 158 views
2

我希望有一个半透明的SDL背景(无关子表面或图像),这样的即代替具有黑色背景它实际上是透明的,但其他的事情我画不是。我当前的代码是代码:: Blocks的SDL项目,类似的稍微修改副本的应用程序如何将各种具有圆形边框或矩形之外奇怪的形状。C++半透明窗口SDL

#ifdef __cplusplus 
    #include <cstdlib> 
#else 
    #include <stdlib.h> 
#endif 
#ifdef __APPLE__ 
#include <SDL/SDL.h> 
#else 
#include <SDL.h> 
#endif 

int main (int argc, char** argv) 
{ 
    putenv("SDL_VIDEO_WINDOW_POS"); 
    putenv("SDL_VIDEO_CENTERED=1"); 

    // initialize SDL video 
    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
    { 
     printf("Unable to init SDL: %s\n", SDL_GetError()); 
     return 1; 
    } 

    // make sure SDL cleans up before exit 
    atexit(SDL_Quit); 

    // create a new window 
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16, 
              SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_NOFRAME); 
    if (!screen) 
    { 
     printf("Unable to set 640x480 video: %s\n", SDL_GetError()); 
     return 1; 
    } 

    // load an image 
    SDL_Surface* bmp = SDL_LoadBMP("cb.bmp"); 
    if (!bmp) 
    { 
     printf("Unable to load bitmap: %s\n", SDL_GetError()); 
     return 1; 
    } 

    // centre the bitmap on screen 
    SDL_Rect dstrect; 
    dstrect.x = (screen->w - bmp->w)/2; 
    dstrect.y = (screen->h - bmp->h)/2; 

    // program main loop 
    bool done = false; 
    while (!done) 
    { 
     // message processing loop 
     SDL_Event event; 
     while (SDL_PollEvent(&event)) 
     { 
      // check for messages 
      switch (event.type) 
      { 
       // exit if the window is closed 
      case SDL_QUIT: 
       done = true; 
       break; 

       // check for keypresses 
      case SDL_KEYDOWN: 
       { 
        // exit if ESCAPE is pressed 
        if (event.key.keysym.sym == SDLK_ESCAPE) 
         done = true; 
        break; 
       } 
      } // end switch 
     } // end of message processing 

     // DRAWING STARTS HERE 

     // clear screen 
     SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); 

     // draw bitmap 
     SDL_BlitSurface(bmp, 0, screen, &dstrect); 

     // DRAWING ENDS HERE 

     // finally, update the screen :) 
     SDL_Flip(screen); 
    } // end main loop 

    // free loaded bitmap 
    SDL_FreeSurface(bmp); 

    // all is well ;) 
    printf("Exited cleanly\n"); 
    return 0; 
} 

回答

2

我认为你想要做的事实上是一个形状的窗口(窗口的部分是透明的,取决于你提供的掩码)。似乎有没有办法做到这一点与SDL 1.2,但存在SDL 1.3 SDL_SetWindowShape功能只为这可以为它找到一个预发布快照here但它不是即使在测试版尚未所以我建议等到它officialy发布:)

1

this是大约为Mac OS 9,它不具有不规则窗口的支持,无论是旧的应用程序的开发一个漂亮整洁的文章的链接。这实际上是一篇关于软件开发的整洁文章。

但这个想法似乎很聪明,我不知道你也许能得到它的工作也在这里。而不是试图做一个透明的背景下,他们居然拿自己所在的窗口是要去电脑右侧的屏幕截图,然后使用该屏幕截图为他们的背景。当用户在屏幕上拖动窗口时,他们继续用新的屏幕截图更新背景。我认为这可能比你希望的更复杂,但它确实是一个有趣的想法。