SDL2 —— 创建Texture并在上面乱涂乱画

2017-1-12 Plan C SDL2

www.kurukurumi.com原创,转载请注明出处。

E-mail:hubenchang0515@outlook.com


        Texture可以作为Renderer绘画的目标,用来保存绘画的内容,使用SDL_CreateTexture来创建Texture。其参数依次为Renderer像素格式操作方式宽度高度。详见:http://wiki.libsdl.org/SDL_CreateTexture

#include <SDL_render.h>
SDL_Texture* SDL_CreateTexture(SDL_Renderer* renderer,Uint32 format,
                                int access,int w,int h);
//成功返回指针,失败返回NULL


        使用SDL_SetRenderTarget来设置Renderer的渲染目标,其参数依次为Renderer和Texture。只有创建时SDL_CreateTexture的access参数为SDL_TEXTUREACCESS_TARGET的Texture才能作Texture参数。将Texture参数设为NULL,可以将目标恢复为默认目标(显示器)。详见:http://wiki.libsdl.org/SDL_SetRenderTarget

#include <SDL_render.h>
int SDL_SetRenderTarget(SDL_Renderer* renderer,SDL_Texture* texture);
//成功返回0,失败返回非0


        使用SDL_RenderCopy可以将一个Texture复制到当前渲染目标上,其参数依次为RendererTextureTexture的长方形范围目标的长方形范围。后两个参数为NULL时复制整个范围。

        详见:http://wiki.libsdl.org/SDL_RenderCopy

#include <SDL_render.h>
int SDL_RenderCopy(SDL_Renderer* renderer,SDL_Texture* texture,
                    const SDL_Rect* srcrect,const SDL_Rect* dstrect);
//成功返回0,失败返回非0


        使用SDL_DestroyTexture来销毁不需要了的Texture。

#include <SDL_render.h>
void SDL_DestroyTexture(SDL_Texture* texture);


        下面是一段代码在两个Texture上画了不同的图像,并分别显示在窗口的左右两边。

/*
Author : PlanC
E-mail : hubenchang0515@outlook.com
Blog   : www.kurukurumi.com
Date   : 2016-12-12
*/

#include <SDL2/SDL.h>
#include <math.h>

int main(int argc,char* argv[])
{
    /* 初始化并创建窗口 */
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window* win = NULL;
    win = SDL_CreateWindow("SDL2",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,
                            640,400,SDL_WINDOW_RESIZABLE);
    /* 创建Renderer */
    SDL_Renderer* render = NULL;
    render = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED);
    
    
    /* 创建两个Texture,高度和窗口相同,宽度均为窗口的一半 */
    SDL_Texture* t1 = NULL;
    t1 = SDL_CreateTexture(render,SDL_PIXELFORMAT_RGBA8888,
                             SDL_TEXTUREACCESS_TARGET,320,400);
    SDL_Texture* t2 = NULL;
    t2 = SDL_CreateTexture(render,SDL_PIXELFORMAT_RGBA8888,
                             SDL_TEXTUREACCESS_TARGET,320,400);
    /* 定义一个长方形用来绘画 */
    SDL_Rect r = {100,100,100,100};
    /* 在t1上画一个红色的正方形 */
    SDL_SetRenderTarget(render,t1);
    SDL_RenderClear(render);
    SDL_SetRenderDrawColor(render,0xff,0,0,0);
    SDL_RenderFillRect(render,&r);
    /* 在t2上画一个绿色的正方形 */
    SDL_SetRenderDrawColor(render,0xff,0xff,0xff,0);
    SDL_SetRenderTarget(render,t2);//clear为白色
    SDL_RenderClear(render);
    SDL_SetRenderDrawColor(render,0,0xff,0,0);
    SDL_RenderFillRect(render,&r);
    /* 恢复为默认目标,显示 */
    SDL_SetRenderTarget(render,NULL);
    SDL_Rect left = {0,0,320,400};
    SDL_Rect right = {320,0,320,400};
    /* 复制t1到窗口左半边 */
    SDL_RenderCopy(render,t1,NULL,&left);
    /* 复制t2到窗口右半边 */
    SDL_RenderCopy(render,t2,NULL,&right);
    /* 显示 */
    SDL_RenderPresent(render);
   

    /* 等待退出 */
    SDL_Event e;
    while(1)
    {
        SDL_PollEvent(&e); 
        if(e.type == SDL_QUIT)
        {
            break;
        }
    }
    /* 销毁Texture */
    SDL_DestroyTexture(t1);
    SDL_DestroyTexture(t2);
    /* 销毁renderer */
    SDL_DestroyRenderer(render);
    /* 销毁窗口 */
    SDL_DestroyWindow(win);
    /* 关闭SDL子系统 */
    SDL_Quit();
	
    return 0;
}

发表评论:

Powered by emlog
鄂ICP备16003833号