SDL使用内存像素数据绘制(SDL_CreateRGBSurfaceFrom)

不多说,主要是利用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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Barry__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值