本文主要是介绍SDL学习入门,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、搭建开发平台
(1)sudo apt-get install libsdl1.2-dev
最基本的开发包
(2)sudo apt-get install libsdl-image1.2-dev
关于图像的开发包
(3)sudo apt-get install libsdl-mixer1.2-dev
关于音频的开发包
(4)sudo apt-get install libsdl-ttf2.0-dev
关于文字的开发包
安装好以上四个开发包,开发平台算是搭建好了
2、最有代表性的简单程序,包括图像,文字,音乐,注释如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <SDL/SDL.h>
- #include <SDL/SDL_image.h>
- #include <SDL/SDL_mixer.h>
- #include <SDL/SDL_ttf.h>
- static SDL_Surface* screen;//SDL窗口
- int main(int argc, char *argv[])
- {
- int quit = 0;
- SDL_Surface *text_sur;//文字容器
- SDL_Surface *background;//图像容器
- SDL_Event event;
- SDL_Color color;
- SDL_Rect srect, drect;
- Mix_Music *bgsound;
- TTF_Font *font;
- //初始化SDL
- if (SDL_Init(SDL_INIT_VIDEO||SDL_INIT_AUDIO) < 0)
- {
- fprintf(stderr, "SDL init error:%s\n", SDL_GetError());
- exit(0);
- }
- atexit(SDL_Quit);//注册退出时调用的操作
- //设置SDL屏幕大小
- screen = SDL_SetVideoMode(600, 400, 24, SDL_HWSURFACE);
- if (screen == NULL)
- {
- fprintf(stderr, "Set video mode failure:%s\n", SDL_GetError());
- exit(0);
- }
- //设置SDL窗口标题
- SDL_WM_SetCaption("test", NULL);
- /*显示图像*/
- background = IMG_Load("background.jpg");//导入图像文件,并将图像放入文字容器
- srect.x = 0;
- srect.y = 0;
- srect.w = background->w;
- srect.h = background->h;
- drect = srect;//设置截取范围
- SDL_BlitSurface(background, &srect, screen, &drect);//将图像容器放入SDL窗口
- /*显示文字*/
- //初始化TTF
- if (TTF_Init() < 0)
- {
- fprintf(stderr, "TTF init error:%s\n", SDL_GetError());
- return;
- }
- font = TTF_OpenFont("test.ttf", 40);//导入字体文件
- color.r = 255;
- color.g = 0;
- color.b = 0;//设置文字颜色
- text_sur=TTF_RenderText_Solid(font, "Hello, Welcome to GAME!!", color);//将文字放入文字容器
- srect.x = 0;
- srect.y = 0;
- srect.w = text_sur->w;
- srect.h = text_sur->h;
- drect.x = (600 - text_sur->w) / 2;
- drect.y = (400 - text_sur->h) / 2;
- drect.w = text_sur->w;
- drect.h = text_sur->h;//设置截取范围
- SDL_BlitSurface(text_sur, &srect, screen, &drect);//将文字容器放入SDL窗口
- SDL_UpdateRect(screen, 0, 0, 600, 400);//更新SDL窗口,让新添加的容器显示
- /*播放声音*/
- Mix_OpenAudio(44100, AUDIO_S16, 2, 4096);//打开音频
- bgsound = Mix_LoadMUS("bgsound.mp3");//导入声音文件
- Mix_PlayMusic(bgsound, -1);//播放音频
- while (quit == 0)
- {
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- Mix_CloseAudio();//关闭音频
- quit = 1;
- break;
- default:
- break;
- }
- }
- SDL_Delay(100);
- }
- return 0;
- }
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_ttf.h>static SDL_Surface* screen;//SDL窗口int main(int argc, char *argv[])
{int quit = 0;SDL_Surface *text_sur;//文字容器SDL_Surface *background;//图像容器SDL_Event event;SDL_Color color;SDL_Rect srect, drect;Mix_Music *bgsound;TTF_Font *font;//初始化SDLif (SDL_Init(SDL_INIT_VIDEO||SDL_INIT_AUDIO) < 0){fprintf(stderr, "SDL init error:%s\n", SDL_GetError());exit(0);}atexit(SDL_Quit);//注册退出时调用的操作//设置SDL屏幕大小screen = SDL_SetVideoMode(600, 400, 24, SDL_HWSURFACE);if (screen == NULL){fprintf(stderr, "Set video mode failure:%s\n", SDL_GetError());exit(0);}//设置SDL窗口标题SDL_WM_SetCaption("test", NULL);/*显示图像*/background = IMG_Load("background.jpg");//导入图像文件,并将图像放入文字容器srect.x = 0;srect.y = 0;srect.w = background->w;srect.h = background->h;drect = srect;//设置截取范围SDL_BlitSurface(background, &srect, screen, &drect);//将图像容器放入SDL窗口/*显示文字*///初始化TTFif (TTF_Init() < 0){fprintf(stderr, "TTF init error:%s\n", SDL_GetError());return;}font = TTF_OpenFont("test.ttf", 40);//导入字体文件color.r = 255;color.g = 0;color.b = 0;//设置文字颜色text_sur=TTF_RenderText_Solid(font, "Hello, Welcome to GAME!!", color);//将文字放入文字容器srect.x = 0;srect.y = 0;srect.w = text_sur->w;srect.h = text_sur->h;drect.x = (600 - text_sur->w) / 2;drect.y = (400 - text_sur->h) / 2;drect.w = text_sur->w;drect.h = text_sur->h;//设置截取范围SDL_BlitSurface(text_sur, &srect, screen, &drect);//将文字容器放入SDL窗口SDL_UpdateRect(screen, 0, 0, 600, 400);//更新SDL窗口,让新添加的容器显示/*播放声音*/Mix_OpenAudio(44100, AUDIO_S16, 2, 4096);//打开音频bgsound = Mix_LoadMUS("bgsound.mp3");//导入声音文件Mix_PlayMusic(bgsound, -1);//播放音频while (quit == 0){while (SDL_PollEvent(&event)){switch (event.type){case SDL_QUIT:Mix_CloseAudio();//关闭音频quit = 1;break;default:break;}}SDL_Delay(100);}return 0;
}
问题:gcc main.c -o main编译时可能会出现如下错误:
- main.c:(.text+0x19): undefined reference to `SDL_Init'
- main.c:(.text+0x22): undefined reference to `SDL_GetError'
- main.c:(.text+0x50): undefined reference to `SDL_Quit'
- main.c:(.text+0x79): undefined reference to `SDL_SetVideoMode'
- main.c:(.text+0x8c): undefined reference to `SDL_GetError'
- main.c:(.text+0xc7): undefined reference to `SDL_WM_SetCaption'
- main.c:(.text+0xd3): undefined reference to `IMG_Load'
- main.c:(.text+0x132): undefined reference to `SDL_UpperBlit'
- main.c:(.text+0x137): undefined reference to `TTF_Init'
- main.c:(.text+0x140): undefined reference to `SDL_GetError'
- main.c:(.text+0x174): undefined reference to `TTF_OpenFont'
- main.c:(.text+0x1a3): undefined reference to `TTF_RenderText_Solid'
- main.c:(.text+0x24c): undefined reference to `SDL_UpperBlit'
- main.c:(.text+0x279): undefined reference to `SDL_UpdateRect'
- main.c:(.text+0x29d): undefined reference to `Mix_OpenAudio'
- main.c:(.text+0x2a9): undefined reference to `Mix_LoadMUS'
- main.c:(.text+0x2c1): undefined reference to `Mix_PlayMusic'
- main.c:(.text+0x2d5): undefined reference to `Mix_CloseAudio'
- main.c:(.text+0x2ec): undefined reference to `SDL_PollEvent'
- main.c:(.text+0x2fc): undefined reference to `SDL_Delay'
- collect2: ld returned 1 exit status
main.c:(.text+0x19): undefined reference to `SDL_Init'
main.c:(.text+0x22): undefined reference to `SDL_GetError'
main.c:(.text+0x50): undefined reference to `SDL_Quit'
main.c:(.text+0x79): undefined reference to `SDL_SetVideoMode'
main.c:(.text+0x8c): undefined reference to `SDL_GetError'
main.c:(.text+0xc7): undefined reference to `SDL_WM_SetCaption'
main.c:(.text+0xd3): undefined reference to `IMG_Load'
main.c:(.text+0x132): undefined reference to `SDL_UpperBlit'
main.c:(.text+0x137): undefined reference to `TTF_Init'
main.c:(.text+0x140): undefined reference to `SDL_GetError'
main.c:(.text+0x174): undefined reference to `TTF_OpenFont'
main.c:(.text+0x1a3): undefined reference to `TTF_RenderText_Solid'
main.c:(.text+0x24c): undefined reference to `SDL_UpperBlit'
main.c:(.text+0x279): undefined reference to `SDL_UpdateRect'
main.c:(.text+0x29d): undefined reference to `Mix_OpenAudio'
main.c:(.text+0x2a9): undefined reference to `Mix_LoadMUS'
main.c:(.text+0x2c1): undefined reference to `Mix_PlayMusic'
main.c:(.text+0x2d5): undefined reference to `Mix_CloseAudio'
main.c:(.text+0x2ec): undefined reference to `SDL_PollEvent'
main.c:(.text+0x2fc): undefined reference to `SDL_Delay'
collect2: ld returned 1 exit status
原因:那是因为该程序用到四个静态库,分别为:
(1)SDL
(2)SDL_image
(3)SDL_ttf
(4)SDL_mixer
需用-l参数连起来才能编译得过,如:gcc main.c -lSDL -lSDL_image -lSDL_ttf -lSDL_mixer -o main
这篇关于SDL学习入门的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!