美文网首页
SDL 教程 09 : 视口

SDL 教程 09 : 视口

作者: wjundong | 来源:发表于2020-02-19 01:03 被阅读0次

    示例代码

    /**
     * Title: 视口
     * 有时,您只想为迷你地图之类的内容渲染屏幕的一部分。使用视口可以控制屏幕上的渲染位置。
     */
    
    #include <SDL2/SDL.h>
    #include <SDL2/SDL_image.h>
    #include <stdio.h>
    
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;
    
    #define sdlerror() { printf("error in line %d %s\n", __LINE__, SDL_GetError()); exit(-1); }
    
    int main(int argc, char *argv[])
    {
        // 初始化
        if(SDL_Init(SDL_INIT_VIDEO) < 0) sdlerror();
            
        // 创建窗口和渲染器
        SDL_Window *window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, 
            SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
        // 初始化 IMG 为 支持 PNG
        if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) 
            sdlerror();
        // 加载图片
        SDL_Surface *pngSurface = IMG_Load("viewport.png");
        if(pngSurface == NULL) 
            sdlerror();
            
        // 从图片图层中创建纹理
        SDL_Texture *texture = SDL_CreateTextureFromSurface(render , pngSurface );
        SDL_FreeSurface( pngSurface );
        pngSurface = NULL;
    
        //清屏
        SDL_SetRenderDrawColor( render, 0xFF, 0xFF, 0xFF, 0xFF );
        SDL_RenderClear( render );
    
        //Top left corner viewport
        SDL_Rect topLeftViewport;
        topLeftViewport.x = 0;
        topLeftViewport.y = 0;
        topLeftViewport.w = SCREEN_WIDTH / 2;
        topLeftViewport.h = SCREEN_HEIGHT / 2;
        SDL_RenderSetViewport( render, &topLeftViewport );
        
        // 将纹理拷贝到渲染器
        SDL_RenderCopy( render, texture, NULL, NULL );
    
        // 首先,我们先渲染左上角,这与创建宽度/高度为屏幕一半的矩形并将此区域传递给 SDL_RenderDrawRect 一样简单。
        // 在该调用之后完成的任何渲染都将在给定视口定义的区域内渲染。它还将使用创建它的窗口的坐标系,
        // 因此我们创建的视图的底部仍然是y=480,而它距离顶部有240个像素
        SDL_Rect topRightViewport;
        topRightViewport.x = SCREEN_WIDTH / 2;
        topRightViewport.y = 0;
        topRightViewport.w = SCREEN_WIDTH / 2;
        topRightViewport.h = SCREEN_HEIGHT / 2;
        SDL_RenderSetViewport( render, &topRightViewport );
        
        // 将纹理拷贝到渲染器
        SDL_RenderCopy( render, texture, NULL, NULL );
    
        // 底部视窗
        SDL_Rect bottomViewport;
        bottomViewport.x = 0;
        bottomViewport.y = SCREEN_HEIGHT / 2;
        bottomViewport.w = SCREEN_WIDTH;
        bottomViewport.h = SCREEN_HEIGHT / 2;
        SDL_RenderSetViewport( render, &bottomViewport );
    
        // 将纹理拷贝到渲染器
        SDL_RenderCopy( render, texture, NULL, NULL );
    
        // 更新屏幕
        SDL_RenderPresent( render );
        SDL_Event event;
        while( SDL_PollEvent( &event ) != 0 || event.type != SDL_QUIT);
    
        SDL_DestroyTexture( texture );
        SDL_DestroyRenderer( render );
        SDL_DestroyWindow(window);
        IMG_Quit();
        SDL_Quit();
        return 0;
    }
    

    运行结果

    运行结果.png

    示例中的图片资源

    viewport.png

    相关文章

      网友评论

          本文标题:SDL 教程 09 : 视口

          本文链接:https://www.haomeiwen.com/subject/cpdofhtx.html