C++ SDL2中SDL_Renderer使用

C++ SDL2中SDL_Renderer使用

配置请参照前面的笔记https://www.cnblogs.com/zzr-stdio/p/14514043.html

主要介绍SDL_Renderer。

示例程序:

#include <iostream>
#include<SDL.h>
#include<SDL_image.h>
using namespace std;
const int WIDTH = 800;
const int HEIGHT = 600;
int main(int argc, char* argv[])
{
    ::SDL_Init(SDL_INIT_VIDEO);//初始化SDL
    ::SDL_Window* window = ::SDL_CreateWindow("SDL test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        WIDTH, HEIGHT, SDL_WINDOW_SHOWN);//创建窗体
    ::SDL_Renderer* rend = ::SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);//创建renderer
    ::SDL_RenderClear(rend);//清空
    ::SDL_Surface* imagesurface = ::SDL_LoadBMP("r.bmp");
    //创建纹理,转为可以贴在rend上的纹理
    ::SDL_Texture* image = ::SDL_CreateTextureFromSurface(rend, imagesurface);
    ::SDL_Rect rect;
    rect.x = 0;//显示位置
    rect.y = 0;
    rect.w = imagesurface->w;
    rect.h = imagesurface->h;
    bool quit = false;
    ::SDL_Event event;
    while (quit == false)
    {
        while (::SDL_PollEvent(&event))
        {
            if (event.type == ::SDL_QUIT)//退出事件
            {
                quit = true;
            }
            else if (event.type == SDL_MOUSEMOTION)
            {
                rect.x = event.motion.x - rect.w / 2;
                rect.y = event.motion.y - rect.h / 2;
                ::SDL_RenderClear(rend);//清空
                ::SDL_RenderCopy(rend, image, nullptr, &rect);//让图片跟随鼠标移动
            }
        }
        ::SDL_RenderPresent(rend);//显示
        ::SDL_Delay(10);
    }
    
    ::SDL_DestroyWindow(window);//销毁窗体
    ::SDL_Quit();//退出SDL
    return 0;
}


透明设置:

  1. 设置支持透明模式:SDL_SetTextureBlendMode(image, SDL_BLENDMODE_BLEND);//设置支持透明度
  2. 设置透明量:SDL_SetTextureAlphaMod(image, alpha);//设置透明度
原文地址:https://www.cnblogs.com/zzr-stdio/p/14514309.html