sdl2-2.04 读取位图并显示
// sdl2_win32.cpp : Defines the entry point for the console application.
//
// 假定SDL的库文件和头文件和VC的工程文件在一起.
#include "stdafx.h"
#include "SDL2-2.0.4/include/SDL.h"
#pragma comment(lib, "SDL2-2.0.4/lib/x86/sdl2.lib")
#include <iostream>
void pressESCtoQuit();
int _tmain(int argc, _TCHAR* argv[])
{
try {
if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
throw SDL_GetError();
}
catch ( const char* s ) {
std::cerr << "SDL_Init() failed!\n" << s << std::endl;
return -1;
}
const int SCREEN_WIDTH = 640; // 0 means use current width.
const int SCREEN_HEIGHT = 480; // 0 means use current height.
const int SCREEN_BPP = 32; // 0 means use current bpp.
const Uint32 SCREEN_FLAGS = SDL_SWSURFACE; // SDL_SWSURFACE == 0,surface in system memory.
SDL_Surface* pScreen = 0;
SDL_Window* win = SDL_CreateWindow("title", 0,0,SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_FLAGS); // SDL1.X叫SDL_SetVideoMode()
pScreen = SDL_GetWindowSurface(win); // 相比SDL1.X多一步
try {
if ( pScreen == 0 )
throw SDL_GetError();
}
catch ( const char* s ) {
std::cerr << "SDL_SetVideoMode() failed!\n" << s << std::endl;
SDL_Quit();
return -1;
}
SDL_Surface* pShownBMP = 0;
pShownBMP = SDL_LoadBMP("hello.bmp"); // Load a BMP file, and convert it as a surface.
try {
if ( pShownBMP == 0 )
throw SDL_GetError();
}
catch ( const char* s ) {
std::cerr << "SDL_LoadBMP() failed!\n" << s << std::endl;
SDL_Quit();
return -1;
}
SDL_Rect* pSrcRect = 0; // If pSrcRect is NULL, the entire source surface is copied.
SDL_Rect* pDstRect = 0; // If pDstRect is NULL, then the destination position (upper left corner) is (0, 0).
try {
if ( SDL_BlitSurface(pShownBMP, pSrcRect, pScreen, pDstRect) != 0 ) // Put the BMP's surface on the SDL window's surface.
throw SDL_GetError();
}
catch ( const char* s ) {
std::cerr << "SDL_BlitSurface() failed!\n" << s << std::endl;
SDL_Quit();
return -1;
}
try {
if ( SDL_UpdateWindowSurface(win) != 0 ) // Show the SDL window's surface. SDL1.X叫SDL_Flip
throw SDL_GetError();
}
catch ( const char* s ) {
std::cerr << "SDL_Flip() failed!\n" << s << std::endl;
SDL_Quit();
return -1;
}
pressESCtoQuit();
SDL_Quit();
return 0;
}
void pressESCtoQuit()
{
bool gameOver = false;
while( gameOver == false ){
SDL_Event gameEvent;
while ( SDL_PollEvent(&gameEvent) != 0 ){
if ( gameEvent.type == SDL_QUIT ){
gameOver = true;
}
if ( gameEvent.type == SDL_KEYUP ){
if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
gameOver = true;
}
}
}
}
return;
}