SDL显示一张图片

把图片放在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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值