出处:http://blog.csdn.net/happyweb/article/details/6201655

 

笔者这段时间在研究SDL,发现很多人在问如何让SDL支持视频双窗口或者创建多个视频窗口,而baidu和google上居然没有详细介绍的相关文档,故在此作一个简单介绍。 

   根据笔者的理解,在SDL1.2中是不支持多窗口的,而还没有正式发布的、非稳定状态下的SDL1.3是完全支持多窗口的。  

   下面是笔者写的一个简单的程序,其中创建2个视频窗口并在每一个窗口中显示不同位图:

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <tchar.h>
#include "SDL.h"
#include "SDL_shape.h"

void renderBlue(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
{
    //Clear render-target to blue.
    SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
    SDL_RenderClear(renderer);

    //Render the texture.
    SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);

    SDL_RenderPresent(renderer);
}

void renderRed(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
{
    //Clear render-target to red.
    SDL_SetRenderDrawColor(renderer,0xff,0x00,0x00,0xff);
    SDL_RenderClear(renderer);

    //Render the texture.
    SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);

    SDL_RenderPresent(renderer);
}

int _tmain(int argc,TCHAR** argv)
{
    SDL_Color black = {0,0,0,0xff};
    Uint32 pixelFormat = 0;
    int access = 0;
    SDL_Rect texture_dimensions;;
    char *szAnsiFile="e://hello.bmp";
    char *szAnsiFile2="e://hello2.bmp";

    if(SDL_VideoInit(NULL) == -1) {
        printf("Could not initialize SDL video./n");
        exit(-2);
    }
   
    SDL_PixelFormat* pixformat = NULL;
    SDL_Window * win1 = SDL_CreateWindow("TEST1", 10, 10, 640,480, SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
    SDL_Renderer *render1 = SDL_CreateRenderer(win1,-1,0);
    SDL_Surface *imgSurface1 = SDL_LoadBMP(szAnsiFile);
    SDL_Texture *texture1 = SDL_CreateTextureFromSurface(render1,imgSurface1);
    SDL_QueryTexture(texture1,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
    SDL_SetWindowSize(win1,texture_dimensions.w,texture_dimensions.h);
    SDL_WindowShapeMode shapeMode1;
    pixformat = imgSurface1->format;
    if(pixformat->Amask != 0) {
        shapeMode1.mode = ShapeModeBinarizeAlpha;
        shapeMode1.parameters.binarizationCutoff = 255;
    }
    else {
        shapeMode1.mode = ShapeModeColorKey;
        shapeMode1.parameters.colorKey = black;
    }
    SDL_SetWindowShape(win1,imgSurface1,&shapeMode1);

    texture_dimensions.x = 0;
    texture_dimensions.y = 0;
    texture_dimensions.w = 640;
    texture_dimensions.h = 480;
    renderBlue(render1,texture1,texture_dimensions);   

    SDL_Window * win2 = SDL_CreateWindow("TEST2", 650, 10, 640,480, SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
    SDL_Renderer *render2 = SDL_CreateRenderer(win2,-1,0);
    SDL_Surface *imgSurface2 = SDL_LoadBMP(szAnsiFile2);
    SDL_Texture *texture2 = SDL_CreateTextureFromSurface(render2,imgSurface2);
    SDL_QueryTexture(texture2,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
    SDL_SetWindowSize(win2,texture_dimensions.w,texture_dimensions.h);
    SDL_WindowShapeMode shapeMode2;
   
    pixformat = imgSurface2->format;
    if(pixformat->Amask != 0) {
        shapeMode2.mode = ShapeModeBinarizeAlpha;
        shapeMode2.parameters.binarizationCutoff = 255;
    }
    else {
        shapeMode2.mode = ShapeModeColorKey;
        shapeMode2.parameters.colorKey = black;
    }
    SDL_SetWindowShape(win2,imgSurface2,&shapeMode2);

    texture_dimensions.x = 0;
    texture_dimensions.y = 0;
    texture_dimensions.w = 640;
    texture_dimensions.h = 480;
    renderRed(render2,texture2,texture_dimensions);      
   
    SDL_Delay(5000);

    SDL_DestroyTexture(texture1);
    SDL_DestroyRenderer(render1);
    SDL_DestroyWindow(win1);
    SDL_FreeSurface(imgSurface1);

    SDL_DestroyTexture(texture2);
    SDL_DestroyRenderer(render2);
    SDL_DestroyWindow(win2);
    SDL_FreeSurface(imgSurface2);

    //Call SDL_VideoQuit() before quitting.
    SDL_VideoQuit();

    return 0;
}


要编译上面这段代码,需要下载最新的SDL1.3源码,然后编译生成SDL.lib和SDL.dll,然后将SDL.lib导入库加入到VS2005的工程中即可。

GitHub 加速计划 / sd / SDL
8.9 K
1.68 K
下载
Simple Directmedia Layer
最近提交(Master分支:3 个月前 )
a57c5669 - 3 个月前
20a6193e - 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐