SDL2.0学习笔记4--用SDL画图(SDL_Surface)

学习笔记3里说的是怎么显示外部存储的图片,这次要探索的是怎么把内存中的图片数据显示到屏幕上(多媒体解码后所得数据即在内存中)。主要是利用SDL_CreateRGBSurfaceFrom()函数,以RGB8888的图像为例,假设pixels中存储的是一副RGBA8888格式的像素值。

示例源码:

#include "SDL.h"

#include 
#include 

void GetRGBA8888Data(void *&pixels, int &width, int &height)
{
	// for debug
	width = 600;
	height = 400;
	pixels = calloc(width*height*4,1);	// 一像素占4字节
}

void example04()
{
    SDL_Window		*pWindow = NULL;
    SDL_Renderer    *pRenderer = NULL;
    SDL_Texture		*pTexture;
    SDL_Rect        srcRect;
    SDL_Rect        dstRect;

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)    return;

    pWindow = SDL_CreateWindow("example04: for mediaplayer", 100, 100, 700, 590, 0);
    if (NULL == pWindow)    return;

    pRenderer = SDL_CreateRenderer(pWindow, -1, 0);
    if (NULL == pRenderer)    return;

    SDL_SetRenderDrawColor(pRenderer, 0, 255, 0, 255);

    void *pixels;
    int  width, height, depth, pitch;
	
	GetRGBA8888Data(pixels, width, height);
	pitch = height*4;	// 每行图像所占直接数
	depth = 4*8;		// 每像素所占位数		

    int rmask, gmask, bmask, amask;
    //rmask = 0xFF000000; gmask = 0x00FF0000; bmask = 0x0000FF00; amask = 0x000000FF;	// RGBA8888模式
	rmask = 0xFF000000; gmask = 0x00FF0000; bmask = 0x0000FF00; amask = 0x00000000;	// RGB8888模式
    SDL_Surface    *pTmpSurface = SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, rmask, gmask, bmask, amask);
    if (NULL == pTmpSurface)    return;

    pTexture = SDL_CreateTextureFromSurface(pRenderer,pTmpSurface);
    if (NULL == pTexture)    return;
    //SDL_FreeSurface(pTmpSurface);

    dstRect.x = srcRect.x = 0;
    dstRect.y = srcRect.y = 0;
    dstRect.w = srcRect.w = width;
    dstRect.h = srcRect.h = height;

    SDL_RenderClear(pRenderer);
    
    SDL_RenderCopy(pRenderer, pTexture, &srcRect, &dstRect);

    SDL_RenderPresent(pRenderer);

    SDL_Delay(3000);
}




你可能感兴趣的:(SDL)