把图片放在C:\Users\admin\Documents\visual studio 2010\Projects\SDL3\SDL3下
过程:1.将bmp文件转化为SDL_Surface格式
2.将转化后的SDL_Surface块移(blit)到SDL_Surface建立的suface上。
3.将surface显示出来
// SDL3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
void LOOP();
void PressESC();
int d;
int _tmain(int argc, _TCHAR* argv[])
{
try{
if(SDL_Init(SDL_INIT_VIDEO==-1))
throw SDL_GetError();
}catch(const char* s){
std::cerr<<s << std::endl;
return -1;
}
atexit(SDL_Quit);
SDL_Surface* pScreen=0;
pScreen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
try{
if(pScreen==0){
throw SDL_GetError();
}else{
std::cout<<"pScreen init successful"<<std::endl;
}
}catch(const char* s){
std::cerr<<"pScreen init failed"<<"s"<<std::endl;
}
SDL_Surface* pShownBMP=0;
pShownBMP=SDL_LoadBMP("hello.bmp");
try{
if(pShownBMP==0){
throw SDL_GetError();
}else{
std::cout<< "SDL_LoadBMP successful"<<std::endl;
}
}catch(const char *s){
std::cerr<<"SDL_LoadBMP failed"<<s<<std::endl;
SDL_Quit();
return -1;
}
_sleep(5*1000);
SDL_Rect* pSrcRect=0;
SDL_Rect* pDstRect=0;
try{
if(SDL_BlitSurface(pShownBMP,pSrcRect,pScreen,pDstRect)!=0)
{
throw SDL_GetError();
}else{
std::cout<<"SDL_BlitSurface successful"<<std::endl;
}
}catch(const char* s){
std::cerr<< s << std::endl;
}
try{
if(SDL_Flip(pScreen)!=0){
throw SDL_GetError();
}else{
std::cout<<"the SDL_Flip successful"<<std::endl;
}
}catch(const char* s){
std::cerr<<"the SDL_Flip failed"<<s<<std::endl;
}
std::cout<<"press ESC to exit"<<std::endl;
PressESC();
system("pause");
return 0;
}
void PressESC(){
bool gameOver=false;
std::cout<<"you press the ESC"<<std::endl;
while(gameOver==false){
SDL_Event game;
SDL_PollEvent(&game);
if(&game!=0){
if(game.type==SDL_QUIT){
gameOver=true;
}
if(game.type==SDL_KEYDOWN){
if(game.key.keysym.sym==SDLK_ESCAPE){
gameOver=true;
}
}
}
LOOP();
}
return;
}
void LOOP(){
d++;
std::cout<<"-"<<d<<std::endl;
return;
}