如果有一套跨平台的框架程序能够不用在OS的API之间折腾,或者类似GLUT略坑。。 SDL就是这个答案,最早在StuntRally里面就看到过使用这个库作为框架窗口,里面的窗口分割,用户输入等也是基于SDL来做的。 一边看Lazy Foo的tutorial,一边感叹写的真是非常贴心,比N这样的无私分享奉献太可贵了,想起了很老的NEHE教程。。
框架无非就是初始化——加载资源——渲染(事件监听)——释放资源——结束这几个过程 采用SDL_Image lib,加载一幅JPEG图片,填满窗口作为渲染内容 当前三国杀新出的界限突破武将——界限突破许褚
做一些笔记过程
/*This source code copyrighted by Lazy Foo' Productions (2004-2013) and may not be redistributed without written permission.*/ //Using SDL, SDL_image, standard IO, and strings #include <SDL.h> #include <SDL_image.h> #include <stdio.h> #include <string> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; //Starts up SDL and creates window bool init(); //Loads media bool loadMedia(); //Frees media and shuts down SDL void close(); //Loads individual image SDL_Surface* loadSurface( std::string path ); //The window we'll be rendering to SDL_Window* gWindow = NULL; //The surface contained by the window SDL_Surface* gScreenSurface = NULL; //Current displayed PNG image SDL_Surface* gPNGSurface = NULL; bool init() { //Initialization flag bool success = true; //Initialize SDL if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() ); success = false; } else { //Create window gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if( gWindow == NULL ) { printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() ); success = false; } else { //Initialize Image loading int imgFlags = IMG_INIT_JPG; if( !( IMG_Init( imgFlags ) & imgFlags ) ) { printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() ); success = false; } else { //Get window surface gScreenSurface = SDL_GetWindowSurface( gWindow ); } } } return success; } bool loadMedia() { //Loading success flag bool success = true; //Load PNG surface gPNGSurface = loadSurface( "xuchu.jpg" ); if( gPNGSurface == NULL ) { printf( "Failed to load PNG image!\n" ); success = false; } return success; } void close() { //Free loaded image SDL_FreeSurface( gPNGSurface ); gPNGSurface = NULL; //Destroy window SDL_DestroyWindow( gWindow ); gWindow = NULL; //Quit SDL subsystems IMG_Quit(); SDL_Quit(); } SDL_Surface* loadSurface( std::string path ) { //The final optimized image SDL_Surface* optimizedSurface = NULL; //Load image at specified path SDL_Surface* loadedSurface = IMG_Load( path.c_str() ); if( loadedSurface == NULL ) { printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() ); } else { //Convert surface to screen format optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL ); if( optimizedSurface == NULL ) { printf( "Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() ); } //Get rid of old loaded surface SDL_FreeSurface( loadedSurface ); } return optimizedSurface; } int main( int argc, char* args[] ) { //Start up SDL and create window if( !init() ) { printf( "Failed to initialize!\n" ); } else { //Load media if( !loadMedia() ) { printf( "Failed to load media!\n" ); } else { //Main loop flag bool quit = false; //Event handler SDL_Event e; //While application is running //Apply the image stretched SDL_Rect stretchRect; stretchRect.x = 0; stretchRect.y = 0; stretchRect.w = SCREEN_WIDTH; stretchRect.h = SCREEN_HEIGHT; while( !quit ) { //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) { //User requests quit if( e.type == SDL_QUIT ) { quit = true; } } //Apply the PNG image to the window size SDL_BlitScaled( gPNGSurface, NULL, gScreenSurface, &stretchRect ); //Update the surface SDL_UpdateWindowSurface( gWindow ); } } } //Free resources and close SDL close(); return 0; }
SDL_Init 初始化 SDL_CreateWindow 创建渲染窗口,指定窗口大小 IMG_Init 初始化图像文件读取函数,需要指定图像文件类型如PNG JPG等 SDL_GetWindowSurface 获取Window的SDL_Surface* 类型 SDL_FreeSurface 释放Surface资源 SDL_PollEvent 处理事件队列里的事件,没有事件的时候返回0 SDL_BlitScaled 和下面的函数都是将surface内容copy到另外一个surface,这个新版本可以指定copy以后的大小,比如用于填充窗口
SDL_BlitSurface SDL_UpdateWindowSurface 刷新当前显示,更新双缓冲前后页面
IMG_Quit(); 退出对应的函数库 |
|