我希望有一个半透明的SDL背景(与子表面或图像无关),这样,而不是有一个黑色的背景,它实际上是透明的,但我画的其他东西不是。我目前的代码是code::Blocks的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: %sn", 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: %sn", SDL_GetError());
return 1;
}
// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
printf("Unable to load bitmap: %sn", 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 cleanlyn");
return 0;
}
我认为你要做的实际上是一个形状的窗口(窗口的部分是透明的取决于你提供的遮罩)。似乎没有办法在SDL 1.2中做到这一点,但是在SDL 1.3中有一个SDL_SetWindowShape
函数,您可以在这里找到预发布快照,但它甚至还没有进入测试版,所以我建议等到它正式发布:)
这个链接到一篇非常简洁的文章,内容是关于Mac OS 9的旧应用程序的开发,该应用程序也不支持形状窗口。这实际上是一篇关于软件开发的简洁文章。
但是这个想法看起来很聪明,我想知道你是否也能让它在这里工作。他们没有尝试做一个透明的背景,而是在他们的窗口将要去的地方取一个电脑的屏幕截图,然后用那个屏幕截图作为背景。当用户在屏幕上拖动窗口时,他们会继续用新的屏幕截图更新背景。我认为这可能比你想象的要复杂,但这确实是一个有趣的想法。