Close

We Can Haz Cheezburger?

A project log for ChassC

As in 'Chassis for C' Simple Direct Media Layer (SDL2) Template for C++

morningstarMorning.Star 02/19/2018 at 12:410 Comments

Yes we can.

Loading graphics is also simple to do, that is the point of SDL. It's part of the bitmap blitting routines.

You'll need to load the library for this.

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

 Graphics are loaded as Surfaces, and can be pasted over each other and the render.

SDL_Surface * tmp=SDL_LoadBMP("cheezburger.bmp");
SDL_Texture * cheez = SDL_CreateTextureFromSurface( render, tmp );
if (cheez==NULL) { cout << "cant has cheezburger!\n"; }
SDL_FreeSurface(tmp);

SDL_Rect src,dst;
int sw,sh;

SDL_QueryTexture(cheez,NULL,NULL, &sw, &sh);

src.x=0; src.y=0;
src.w=sw; src.height=sh;

dst.x=300; dst.y=0;
dst.w=sw; dst.h=sh;

SDL_RenderCopy(render,cheez,&src,&dst);

Note the use of RenderCopy to paste the image on the render.

you specify the destination, source, and then source and destination rectangles to take all or a piece of the source bitmap and paste it into the destination rectangle. These can both be NULL, to copy the entire image to the entire surface without specifying a size.

Details about the image are returned by QueryTexture, which returns the following values

Format - this is the texture depth and type. Usually this will be RGBA8888.

Access - The render has SDL_TEXTUREACCESS_TARGET but there are other values.

Width and Height - In Pixels.

Discussions