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

来源:互联网 发布:公需科目大数据网站 编辑:程序博客网 时间:2024/06/01 09:22

不多说,主要是利用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 debugwidth = 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;//初始化SDLif (SDL_Init(SDL_INIT_EVERYTHING) < 0)    return 0;//创建窗口pWindow = SDL_CreateWindow("example04: for mediaplayer", 100, 100, 700, 590, 0);if (NULL == pWindow)    return 0;//创建RendererpRenderer = 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 SurfaceSDL_Surface    *pTmpSurface = SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, rmask, gmask, bmask, amask);if (NULL == pTmpSurface)    return 0;//创建TexturepTexture = 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;//清除RendererSDL_RenderClear(pRenderer);//Texture复制到RendererSDL_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;}


0 0
原创粉丝点击