SDL2.0显示一张BMP图片方法一


***【在线视频教程】***

好文章,来自【福优学苑@音视频+流媒体】

SDL2.0显示一张BMP图片方法一

SDL默认只能加载bmp图片

SDL_LoadBMP

SDL_BlitSurface

///////////显示bmp图片/////////////

1.初始化SDL库 SDL_Init

2.创建窗口

3.获取窗口的表面

4.读取本地图片,返回表面: Surface *pSurface_Bmp = SDL_LoadBMP(...图片路径..)

5.复制表面: SDL_BlitSurce(srcSurface, srcArea,  dstSurface, dstArea);

6.显示表面:刷新

7.退出(清理、释放)SDL_Quit

////////////////////////

SDL_Surface* winSurface = NULL;
SDL_Window* window = NULL;
SDL_Init
SDL_Quit
SDL_CreateWindow
SDL_DestroyWindow
SDL_GetWindowSurface
SDL_UpdateWindowSurface
SDL_FillRect
#include <iostream>
 
#include "SDL.h"
#undef main
 
using namespace std;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main()
{
    //The window we'll be rendering to
    SDL_Window* gWindow = NULL;// 主窗口
 
//The surface contained by the window
/// 表面:surface:内存中的一块地址*(画布)
/// 显示:需要窗口来进行显示
    SDL_Surface* gScreenSurface = NULL;// 主窗口的表面
 
    //The image we will load and show on the screen
    SDL_Surface* gHelloWorld = NULL; // 图片的表面
 
    //首先初始化   初始化SD视频子系统
    if(SDL_Init(SDL_INIT_VIDEO)<0)
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        return false;
    }
    //创建窗口
    gWindow= SDL_CreateWindow("SHOW BMP",//窗口标题
                            SDL_WINDOWPOS_UNDEFINED,//窗口位置设置
                            SDL_WINDOWPOS_UNDEFINED,
                            SCREEN_WIDTH,//窗口的宽度
                            SCREEN_HEIGHT,//窗口的高度
                            SDL_WINDOW_SHOWN//显示窗口
                            );
    if(gWindow==NULL)
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        return false;
    }
    //Use this function to get the SDL surface associated with the window.
    //获取窗口对应的surface
    gScreenSurface=SDL_GetWindowSurface(gWindow);
 
//加载图片
//加载图片,内部一定会申请内存:malloc
///注意:图片路径
gHelloWorld = SDL_LoadBMP("../Hello_World.bmp");
    if( gHelloWorld == NULL )
    {
        printf( "Unable to load image %s! SDL Error: %s\n", "Hello_World.bmp", SDL_GetError() );
        return false;
    }
    //Use this function to perform a fast surface copy to a destination surface.
    //surface的快速复制
    //下面函数的参数分别为: SDL_Surface* src ,const SDL_Rect* srcrect , SDL_Surface* dst ,  SDL_Rect* dstrect
    SDL_BlitSurface( gHelloWorld ,NULL,gScreenSurface,NULL);
    SDL_UpdateWindowSurface(gWindow);//更新显示copy the window surface to the screen
    SDL_Delay(2000);//延时2000毫秒,2s后自动关闭
 
    //释放内存:注意FreeSurface的原理
    SDL_FreeSurface( gHelloWorld );//释放空间;gHelloWorld代表bmp图片的surface
    gHelloWorld = NULL;
 
    SDL_DestroyWindow(gWindow);//销毁窗口
    gWindow = NULL ;
 
    SDL_Quit();//退出SDL
 
    return 0;
}


好文章,来自【福优学苑@音视频+流媒体】
***【在线视频教程】***