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);//設置透明度


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM