SDL2 —— 创建Renderer并用来乱涂鸦

2017-1-11 Plan C SDL2

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

E-mail:hubenchang0515@outlook.com


        Renderer渲染器是绘画的工具,使用SDL_CreateRenderer函数来创建Renderer,它的参数依次是所属的窗口、在该窗口中的索引和renderer的属性。可以将index设为-1自动分配索引,详见:http://wiki.libsdl.org/SDL_CreateRenderer

#include <SDL_render.h>
SDL_Renderer* SDL_CreateRenderer(SDL_Window* window,int index,Uint32 flag);
//成功返回Renderer的指针,否则返回NULL


        创建了Renderer之后就可以在上面画图了,不过在这之前应该先用SDL_RenderClear函数清空Renderer。

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


        可以使用SDL_SetRenderDrawColor函数设置颜色,它的参数依次是Renderer、RGBA颜色的红色分量(Red)绿色分量(Green)蓝色分量(Blue)Alpha分量。详见:http://wiki.libsdl.org/SDL_SetRenderDrawColor

        SDL_SetRenderDrawBlendMode函数设置颜色的混合方式,不混合时(SDL_BLENDMODE_NONE)颜色的Alpha分量是没有用的。详见:http://wiki.libsdl.org/SDL_SetRenderDrawBlendMode

#include <SDL_render.h>
int SDL_SetRenderDrawColor(SDL_Renderer* renderer,
                            Uint8 r,Uint8 g Uint8 b,Uint8 a);
//成功返回0,失败返回非0
int  SDL_SetRenderDrawBlendMode(SDL_Renderer* renderer,SDL_BlendMode blendMode);
//成功返回0,失败返回非0


        使用SDL_RenderDrawLine可以划线,使用SDL_RenderDrawPoint可以画点,使用SDL_RenderDrawRect和SDL_RenderFillRect可以换长方形好充填长方形。

        http://wiki.libsdl.org/SDL_RenderDrawLine

        http://wiki.libsdl.org/SDL_RenderDrawPoint

        http://wiki.libsdl.org/SDL_RenderDrawRect

        http://wiki.libsdl.org/SDL_RenderFillRect


        使用SDL_RenderPresent函数来显示Render。 http://wiki.libsdl.org/SDL_RenderPresent

#include <SDL_render.h>
void SDL_RenderPresent(SDL_Renderer* renderer);

         使用SDL_DestroyRenderer函数来销毁Rendererer,释放资源。http://wiki.libsdl.org/SDL_DestroyRenderer

#include <SDL_render.h>
void SDL_DestroyRender(SDL_Renderer* renderer);


        下面是一段乱涂乱画的代码:

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

#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);
    /* 清空Render */
    SDL_RenderClear(render);
    /* 画值线 */
    SDL_RenderDrawLine(render,0,0,640,400);
    /* 画弧线 */
    for(int x=0;x<=100;x++)
    {
        SDL_RenderDrawPoint(render,x,(int)pow(10000-x*x,0.5));
        SDL_RenderDrawPoint(render,(int)pow(10000-x*x,0.5),x);
    }
    /* 设置混合方式 */
    SDL_SetRenderDrawBlendMode(render,SDL_BLENDMODE_ADD);
    /* 设置颜色,画一个充填长方形 */
    SDL_SetRenderDrawColor(render,0,0xff,0,0xff);
    SDL_Rect rect; //长方形结构体
    rect.x = 100;
    rect.y = 100;
    rect.w = 100;
    rect.h = 100;
    SDL_RenderFillRect(render,&rect);
    /* 画一个另一种颜色的充填长方形 */
    SDL_SetRenderDrawColor(render,0,0,0xff,0xff);
    rect.x = 150;
    rect.y = 150;
    SDL_RenderFillRect(render,&rect);
    /* 显示Render */
    SDL_RenderPresent(render);

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

发表评论:

Powered by emlog
鄂ICP备16003833号