本文主要是介绍windows下用mingw32+sdl进行简单2d游戏开发(c语言),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、下载sdl库:http://www.libsdl.org/release/SDL-devel-1.2.15-mingw32.tar.gz
2、下载sdl_image库: http://www.libsdl.org/projects/SDL_image/release/SDL_image-devel-1.2.12-VC.zip
3、在mingw32/include下建立SDL文件夹。把sdl库include中的文件全部copy到这里
4、把sdl的lib下文件全部copy到mingw32的lib下
5、把sdl_image库中的inlcude/sdl_image.h放到刚才建立的文件夹下
6、把sdl_image库中的lib中的dll文件全部copy到mingw32d的lib中
7、把sdl_imagelib中的dll和sdl中的sdl.dll放到你编译的exe文件目录
8、编译指令
gcc -o demo4.exe(输出文件名) demo4.c(源文件名) -lmingw32 -lSDL_image -lSDLmain -lSDL
测试代码(请在该目录放一个tt.png图片,随便。 自己网上找了):
#include<stdio.h> #include "SDL/SDL.h" #include "SDL/SDL_image.h" SDL_Surface *screen; void video_init(){ //游戏图形模式初始化 if(SDL_Init(SDL_INIT_VIDEO)){ fprintf(stderr,"Can't init SDL:%s\n",SDL_GetError()); exit(1); } screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE); if(screen==NULL){ fprintf(stderr,"Can't set mode 640x480:%s\n",SDL_GetError()); exit(1); } atexit(SDL_Quit); SDL_ShowCursor(0);//关闭鼠标显示 } SDL_Surface *loadIMG(const char *fileName){ SDL_Surface *img; img = IMG_Load(fileName); if(img == NULL) { fprintf(stderr,"Could not load %s: %s",fileName,SDL_GetError()); exit(1); } return img; } SDL_Surface *loadBMP(char *fileName) //加载bmp图片 { SDL_Surface *bmp; bmp = SDL_LoadBMP(fileName); if(bmp == NULL) { fprintf(stderr,"Could not load %s: %s",fileName,SDL_GetError()); exit(1); } return bmp; } int main(int argc, char* argv[]){ if(SDL_Init(SDL_INIT_EVERYTHING)==-1){ printf("error..\n"); }else{ printf("ok\n"); video_init(); SDL_Surface *img; img=loadIMG("tt.png"); while (1){ // Poll for events, and handle the ones we care about. SDL_Rect dest; dest.x=100; dest.y=100; dest.w=200; dest.h=200; SDL_Color color; color.r=255; color.g=0; color.b=0; SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format,0xFF,0xFF,0xFF)); SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format,0,0,0xFF)); dest.x=200; dest.y=200; dest.w=200; dest.h=200; SDL_BlitSurface(img, &screen->clip_rect, screen,&dest); SDL_Flip(screen); SDL_Event event; while (SDL_PollEvent(&event)){ if(event.type==SDL_KEYDOWN){ int key = event.key.keysym.sym; printf("%d\n",key); if(key==SDLK_ESCAPE){ printf("%d\n",1000); SDL_Quit(); exit(1); }else if(key==SDLK_UP){ printf("up\n"); }else if(key==SDLK_DOWN){ printf("down\n"); }else if(key==SDLK_LEFT){ printf("left\n"); }else if(key==SDLK_RIGHT){ printf("right\n"); } }else if(event.type==SDL_QUIT){ printf("%d\n",1000); SDL_Quit(); exit(1); } } } } return 0; }
这篇关于windows下用mingw32+sdl进行简单2d游戏开发(c语言)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!