Wiki Page Content

SDL_CreateRenderer

Use this function to create a 2D rendering context for a window.

Syntax

SDL_Renderer* SDL_CreateRenderer(SDL_Window* window,
                                 int         index,
                                 Uint32      flags)

Function Parameters

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; see Remarks for details

Return Value

Returns a valid rendering context or NULL if there was an error; call SDL_GetError() for more information.

Code Examples

#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_Init(SDL_INIT_VIDEO);

    win = SDL_CreateWindow("Hello World", posX, posY, width, height, 0);

    renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);

    bitmapSurface = SDL_LoadBMP("img/hello.bmp");
    bitmapTex = SDL_CreateTextureFromSurface(renderer, bitmapSurface);
    SDL_FreeSurface(bitmapSurface);

    while (1) {
        SDL_Event e;
        if (SDL_PollEvent(&e)) {
            if (e.type == SDL_QUIT) {
                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;
}

Remarks

flags may be any of the following OR'd together:

SDL_RENDERER_SOFTWARE

the renderer is a software fallback

SDL_RENDERER_ACCELERATED

the renderer uses hardware acceleration

SDL_RENDERER_PRESENTVSYNC

present is synchronized with the refresh rate

SDL_RENDERER_TARGETTEXTURE

the renderer supports rendering to texture

Note that providing no flags gives priority to available SDL_RENDERER_ACCELERATED renderers.


CategoryAPI, CategoryRender

None: SDL_CreateRenderer (last edited 2014-11-11 02:06:24 by RyanGordon)

Feedback
Please include your contact information if you'd like to receive a reply.
Submit