您的位置:首页 > 其它

VC2005简单配置SDL环境

2009-03-30 01:50 435 查看
  当老师第一节课说完这学期只教2D游戏相关知识我就重新定位了这门课程。混。同样给我定义为混的还有周一的资讯安全概论。但我混也是混得有原则的,作业还是自己做。



  想到自己摆弄了GL一些日子了,SDL应该算是手到擒来了。首先下载SDL的相关开发库(包含H文件许53个,LIB文件6个,DLL文件14个)。进入对应的vc2005的安装目录。在路径vc/inlucde下新建SDL文件夹,拷贝头文件进入,同样,在vc/lib下拷贝LIB文件,然后进入c:/windows/system32里拷贝所有DLL。至此,环境就弄好了,然后在以后使用时记得include <SDL/SDL.h>或其他类似的文文件就好了。



  下面是我第一个demo的代码。一看就懂的。我到现在也没看过SDL的语法,都是一些能顾名思义的函数名。比着自带的sample里的代码修改就OK了。



#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *object3 = NULL, *object2 = NULL;
SDL_Surface *load_image( std::string filename ) 
{
    SDL_Surface* loadedImage = NULL;
    
   
    SDL_Surface* optimizedImage = NULL;
    
    loadedImage = SDL_LoadBMP( filename.c_str() );
    
    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        SDL_FreeSurface( loadedImage );
    }
    
    return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    SDL_Rect offset;
    
    offset.x = x;
    offset.y = y;
    
    SDL_BlitSurface( source, NULL, destination, &offset );
}
int main ( int argc, char* argv[] )
{
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { 
        printf("Could not initialize SDL: %s./n", SDL_GetError());
        exit(EXIT_FAILURE);
	}
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_HWSURFACE | SDL_ANYFORMAT);
    if (screen == NULL) {
        fprintf(stderr, "Couldn't set 640x480 video mode: %s/n",
                        SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_WM_SetCaption( "My First SDLDemo----Sink", NULL );
	background = load_image( "001.bmp" );
	apply_surface( 0, 0, background, screen );
	object2 = load_image( "002.bmp" );
	SDL_SetColorKey(object2, SDL_SRCCOLORKEY, SDL_MapRGB(object2->format, 0, 0, 0));
    apply_surface(62, 112, object2, screen );
	object3 = load_image( "003.bmp" );
	SDL_SetColorKey(object3, 0, SDL_MapRGB(object3->format, 0, 0, 0));
    apply_surface(332, 112, object3, screen );
#if 0
	ball = load_image( "circle.bmp" );
	SDL_SetColorKey(ball, SDL_SRCCOLORKEY, SDL_MapRGB(ball->format, 255, 0, 0));
	SDL_SetAlpha(ball, SDL_SRCALPHA, 128);
    apply_surface( 192, 112, ball, screen );
#endif
	//Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;    
    }
    //Wait 5 seconds
	SDL_Delay( 10000 );
	/* Shutdown all subsystems */
    SDL_Quit();
    return EXIT_SUCCESS;
}


(用到的图片自己随意弄就是了,001.BMP为640*480的就OK了。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: