Game Development Reference
In-Depth Information
The preceding code demonstrates how to create a very small image (5 x 5). Note how the
array does not contain elements of the type sf::Color , but rather contains the RGBA
components of a color. However, the method of specifying colors in both cases is the
same—passing a byte ( Uint8 ) for each of the four components. This means that each
four bytes ( 4xUint8 ) represents a single pixel of the image.
Images can also be loaded from a file which is extremely straightforward and is shown as
follows:
The code assumes that there is an image in the working directory of the program, named
myImage.png . Loading from a file is an effective way to create images if you want to
use an image which is already available on the machine. SFML supports the following file
formats: bmp , png , tga , gif , psd , hdr , pic , and jpg (progressive JPEG is not sup-
ported.) If we try to load an image with a different file format, or the given file does not
exist, Image::loadFromFile() returns false and prints a message in the console:
When an image fails to load (for any reason), we need to take an action (inform the user,
terminate the program, and so on). The following code exits the main() function if the
image is not loaded correctly. This is a safe way to prevent any future bugs from occurring
in the code.
If Image::loadFromFile() fails, the image is left unchanged.
Search WWH ::




Custom Search