在MFC中使用SDL2.0(SDL窗口嵌入到MFC中)

SDL2.0的下载可以参考这篇文章:SDL2学习笔记1-环境搭建以及HelloSDL

 

    本例开发环境:win7 64位 +VS2012 + SDL2.0.3 (stable)

 

    第一步:新建MFC基于对话框的应用程序(此例工程命名为MFC_SDL),然后直接点击完成即可,如下图。

 

 

    第二步:删除“TODO:在此放置对话框控件”。添加Picture Control和Button到对话框中,修改Button的名字为显示图片。

 

 

    

  第三步:SDL相关头文件、lib库以及dll动态链接库的设置可以参考这篇文章:SDL2学习笔记1-环境搭建以及HelloSDL

 

  第四步:打开MFC_SDLDlg.cpp文件,在程序中添加头文件

 

[cpp]  view plain  copy
 
 print?
  1. #include <SDL.h>  

 

 

  第五步:双击Button控件,转到鼠标点击响应程序。添加程序。程序如下:

 

[cpp]  view plain  copy
 
 print?
  1. void CMFC_SDLDlg::OnBnClickedButton1()  
  2. {  
  3.     // TODO: 在此添加控件通知处理程序代码  
  4.     //The window we'll be rendering to  
  5.     SDL_Window* gWindow = NULL;  
  6.     //The surface contained by the window  
  7.     SDL_Surface* gScreenSurface = NULL;  
  8.   
  9.     //The image we will load and show on the screen  
  10.     SDL_Surface* gHelloWorld = NULL;  
  11.   
  12.     //首先初始化  
  13.     if(SDL_Init(SDL_INIT_VIDEO)<0)  
  14.     {  
  15.         printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );  
  16.         return ;  
  17.     }  
  18.     //创建窗口  
  19.     gWindow=SDL_CreateWindowFrom( (void *)( GetDlgItem(IDC_STATIC)->GetSafeHwnd() ) );  
  20.     if(gWindow==NULL)  
  21.     {  
  22.         printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );  
  23.         return ;  
  24.     }  
  25.     //Use this function to get the SDL surface associated with the window.  
  26.     //获取窗口对应的surface  
  27.     gScreenSurface=SDL_GetWindowSurface(gWindow);  
  28.   
  29.     //加载图片  
  30.     gHelloWorld = SDL_LoadBMP("Hello_World.bmp");//加载图片  
  31.     if( gHelloWorld == NULL )  
  32.     {  
  33.         printf( "Unable to load image %s! SDL Error: %s\n", "Hello_World.bmp", SDL_GetError() );  
  34.         return ;  
  35.     }  
  36.     //Use this function to perform a fast surface copy to a destination surface.    
  37.     //surface的快速复制  
  38.     //              SDL_Surface* src ,const SDL_Rect* srcrect , SDL_Surface* dst ,  SDL_Rect* dstrect  
  39.     SDL_BlitSurface( gHelloWorld ,     NULL ,                     gScreenSurface ,          NULL);  
  40.     SDL_UpdateWindowSurface(gWindow);//更新显示copy the window surface to the screen  
  41.   
  42. }  

 

 

  第六步:运行程序,点击“显示图片”按钮,现象如下:

 

  从程序中可以看出,要将SDL窗口嵌入到MFC中很简单,只要将SDL原来创建窗口的函数:

 

[cpp]  view plain  copy
 
 print?
  1. SDL_Window* gWindow = SDL_CreateWindow("SHOW BMP",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);  

 

 

  改成下面的函数即可:

 

[cpp]  view plain  copy
 
 print?
  1. SDL_Window* gWindow=SDL_CreateWindowFrom( (void *)( GetDlgItem(IDC_STATIC)->GetSafeHwnd() ) );  

 

 

其中,IDC_STATIC为PictureControl的ID

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值