不多说,主要是利用SDL_CreateRGBSurfaceFrom函数使用内存中的像素数据创建一个Suiface

SDL_Surface* SDL_CreateRGBSurfaceFrom(void*  pixels,//已存在的像素数据
                                      int    width,//Suiface宽
                                      int    height,//Suiface高
                                      int    depth,//Surface的位深度
                                      int    pitch,//一行像素所占字节数
                                      Uint32 Rmask,//r掩码
                                      Uint32 Gmask,//g掩码
                                      Uint32 Bmask,//b掩码
                                      Uint32 Amask)//a掩码
depthSurface位深度介绍

If depth is 4 or 8 bits, an empty palette is allocated for the surface. If depth is greater than 8 bits, the pixel format is set using the [RGBA]mask parameters.

The [RGBA]mask parameters are the bitmasks used to extract that color from a pixel. For instance, Rmask being FF000000 means the red data is stored in the most significant byte. Using zeros for the RGB masks sets a default value, based on the depth. (e.g. SDL_CreateRGBSurface(0,w,h,32,0,0,0,0);) However, using zero for the Amask results in an Amask of 0.

By default surfaces with an alpha mask are set up for blending as with

  • SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND)

You can change this by calling SDL_SetSurfaceBlendMode() and selecting a different blendMode.

No copy is made of the pixel data. Pixel data is not managed automatically; you must free the surface before you free the pixel data.


#include "stdafx.h"
#include "BrButton.h"
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#pragma comment(lib, "SDL.LIB")

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

int _tmain(int argc, _TCHAR* argv[])
{
	SDL_Window		*pWindow = NULL;
	SDL_Renderer    *pRenderer = NULL;
	SDL_Texture		*pTexture;
	
	SDL_Rect        srcRect;
	SDL_Rect        dstRect;
	//初始化SDL
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)    return 0;
	//创建窗口
	pWindow = SDL_CreateWindow("example04: for mediaplayer", 100, 100, 700, 590, 0);
	if (NULL == pWindow)    return 0;
	//创建Renderer
	pRenderer = SDL_CreateRenderer(pWindow, -1, 0);
	if (NULL == pRenderer)    return 0;
	//设置Renderer画笔颜色,透明度(此时透明度不会生效,因为没有设置BlendMode)
	SDL_SetRenderDrawColor(pRenderer, 0, 255, 0, 255);

	void *pixels;
	int  width, height, depth, pitch;

	GetRGBA8888Data(pixels, width, height);
	pitch = height*4;	// 每行图像所占字节数
	depth = 4*8;		// 每像素所占位数(R8位G8位B8位A8位)		

	int rmask, gmask, bmask, amask;
	rmask = 0xFF000000; gmask = 0x00FF0000; bmask = 0x0000FF00; amask = 0x000000FF;	// RGBA8888模式
	//rmask = 0xFF000000; gmask = 0x00FF0000; bmask = 0x0000FF00; amask = 0x00000000;	// RGB8888模式
	//创建一个RGB Surface
	SDL_Surface    *pTmpSurface = SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, rmask, gmask, bmask, amask);
	if (NULL == pTmpSurface)    return 0;
	//创建Texture
	pTexture = SDL_CreateTextureFromSurface(pRenderer,pTmpSurface);
	if (NULL == pTexture)    return 0;

	SDL_FreeSurface(pTmpSurface);
	free(pixels);
	dstRect.x = srcRect.x = 0;
	dstRect.y = srcRect.y = 0;
	dstRect.w = srcRect.w = width;
	dstRect.h = srcRect.h = height;
	//清除Renderer
	SDL_RenderClear(pRenderer);
	//Texture复制到Renderer
	SDL_RenderCopy(pRenderer, pTexture, &srcRect, &dstRect);
	//更新Renderer显示
	SDL_RenderPresent(pRenderer);

	SDL_Delay(9000);
	//清理
	SDL_DestroyWindow(pWindow);
	SDL_DestroyRenderer(pRenderer);
	SDL_DestroyTexture(pTexture);
	SDL_Quit();
	return 0;
}


GitHub 加速计划 / sd / SDL
8.9 K
1.68 K
下载
Simple Directmedia Layer
最近提交(Master分支:3 个月前 )
a57c5669 - 3 个月前
20a6193e - 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐