SDL creates a transparent surface

I searched how to create a transparent surface in the SDL, and found the following: http://samatkins.co.uk/blog/2012/04/25/sdl-blitting-to-transparent-surfaces/

Mainly:

SDL_Surface* surface; #if SDL_BYTEORDER == SDL_BIG_ENDIAN surface = SDL_CreateRGBSurface(SDL_HWSURFACE,width,height,32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); #else surface = SDL_CreateRGBSurface(SDL_HWSURFACE,width,height,32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); #endif 

and it works, but it seems damn awful to me, so I was wondering if there is a better way to do this.

+4
source share
3 answers

What you have is a check to see if the computer uses a large endian or a small end. SDL is multi-platform, and computers use different endiannness.

The author of this article wrote it in an "agnostic" platform. If you use this on a PC, you'll probably be safe just by using:

 surface = SDL_CreateRGBSurface(SDL_HWSURFACE,width,height,32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); 

You do not need conventions.

However, the code will not be ported to other platforms that use a large endiandess

+1
source

I have some experience with SDL2 with my IT class. But I am developing a simplified version of the functions for using SDL and the way to upload my images is as follows:

 ImageId LoadBmp(string FileName, int red, int green, int blue){ SDL_Surface* image = SDL_LoadBMP(FileName.c_str()); // File is loaded in the SDL_Surface* type variable GetDisplayError(!image, string("LoadBmp:\n Couldn't load image file ") + FileName); // Check if the file is found Images.push_back(image); // Send the file to the Images vector SDL_SetColorKey(Images[Images.size() - 1], SDL_TRUE, // enable color key (transparency) SDL_MapRGB(Images[Images.size() - 1]->format, red, green, blue)); // This is the color that should be taken as being the 'transparent' part of the image // Create a texture from surface (image) SDL_Texture* Texture = SDL_CreateTextureFromSurface(renderer, Images[Images.size() - 1]); Textures.push_back(Texture); return Images.size() - 1; // ImageId becomes the position of the file in the vector} 

What you are probably looking for is

 SDL_SetColorKey(Images[Images.size() - 1], SDL_TRUE, // enable color key (transparency) SDL_MapRGB(Images[Images.size() - 1]->format, red, green, blue)); // This is the color that should be taken as being the 'transparent' part of the image 

By doing this, you set RGB, considering it transparent. Hope this helps! Here the finished SDL template I'm working on now should be able to use some of them! https://github.com/maxijonson/SDL2.0.4-Ready-Functions-Template

0
source

Actually we call it alpha blending, and you can look at it here: http://lazyfoo.net/tutorials/SDL/13_alpha_blending/index.php

-1
source

Source: https://habr.com/ru/post/1481393/


All Articles