Create a 2D rendering context for a window.
SDL_Renderer * SDL_CreateRenderer(SDL_Window * window,int index, Uint32 flags);
window |
the window where rendering is displayed |
index |
the index of the rendering driver to initialize, or -1 to initialize the first one supporting the requested flags |
flags |
0, or one or more SDL_RendererFlags OR'd together |
Returns a valid rendering context or NULL if there was an error; call SDL_GetError() for more information.
This function is available since SDL 2.0.0.
#include "SDL.h"
int main(int argc, char *argv[])
{
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *bitmapTex = NULL;
SDL_Surface *bitmapSurface = NULL;int posX = 100, posY = 100, width = 320, height = 240;
SDL_bool loopShouldStop = SDL_FALSE;
SDL_Init(SDL_INIT_VIDEO);
"Hello World", posX, posY, width, height, 0);
win = SDL_CreateWindow(
1, SDL_RENDERER_ACCELERATED);
renderer = SDL_CreateRenderer(win, -
"img/hello.bmp");
bitmapSurface = SDL_LoadBMP(
bitmapTex = SDL_CreateTextureFromSurface(renderer, bitmapSurface);
SDL_FreeSurface(bitmapSurface);
while (!loopShouldStop)
{
SDL_Event event;while (SDL_PollEvent(&event))
{switch (event.type)
{case SDL_QUIT:
loopShouldStop = SDL_TRUE;break;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bitmapTex, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(bitmapTex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}