简体   繁体   中英

PySDL2 Making multiple Surfaces

I am drawing some sprites on first surface on few locations and i want to make second surface and add to it some other sprites from png images with transparency. The problem is that i cannot create second surface and ad Blit surfaces properly.

Here is some code how it looks:

    'Library init'
SDL_Init(SDL_INIT_EVERYTHING)

'Creating window'
window = SDL_CreateWindow(b"Game",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,screen_width,screen_height,SDL_WINDOW_SHOWN)

'Adding resources'
sprite_names = os.listdir(os.path.join(os.path.dirname(os.path.abspath(__file__)),"res")) 

'Sprite loading'
for sprite in sprite_names:
    sprite_list.append(IMG_Load(os.path.join(os.path.dirname(os.path.abspath(__file__)),"res", sprite).encode("utf-8")))
    print(sprite)

'Creating window surface'
window_surface = SDL_GetWindowSurface(window)
'I am trying to create surface in that way'
second_surface = SDL_CreateSurface()
'PNG file with background'
SDL_BlitSurface(sprite_list[0],None,window_surface,SDL_Rect(0,0))
'Second surface with object with transparency, i dont know how define second surface in PySDL to make it work'
SDL_Blitsurface(sprite_list[1],None,second_surface,SDL_Rect(100,100))
SDL_Blitsurface(second_surface,None,window_surface,None) 

SDL_UpdateWindowSurface(window)

i think i need to do something like that to have layers on screen i mean it will put first my background, then objects on it right? When i am trying to draw two objects on same locations i get flickering or i got background on object. Any ideas how get it to work?

I found a way to define second surface:

second_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,screen_width,screen_height,32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000)

It's transparent surface, now next problem i found that was making transparency darker each refresh because it was drawing multiple times same surface i used that code and it worked:

SDL_FillRect(second_surface, None, 0x000000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM