SDL学习入门
来源:互联网 发布:赛诺数据官网 编辑:程序博客网 时间:2024/06/04 23:20
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;
- }
问题: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
(1)SDL
(2)SDL_image
(3)SDL_ttf
(4)SDL_mixer
需用-l参数连起来才能编译得过,如:gcc main.c -lSDL -lSDL_image -lSDL_ttf -lSDL_mixer -o main
0 0
- SDL 简单入门学习
- SDL入门学习
- SDL 简单入门学习
- SDL学习入门
- SDL入门学习教程
- SDL入门学习续-在SDL中使用OpenGL
- SDL入门学习一.装载和退出
- SDL入门学习二.事件查询机制
- SDL学习
- SDL学习
- SDL入门学习之三.加载BMP图片,实现动画
- SDL入门及范例
- SDL图形入门
- SDL入门(一)
- 【SDL】SDL学习笔记二 定时器
- SDL之学习1
- SDL学习之2
- SDL学习笔记之一
- C++获取系统信息(IP地址、硬件信息等)
- ACM-尼姆博弈之取(m堆)石子游戏——hdu2176
- 图的遍历
- 配置vimrc文件
- Convert Sorted List to Binary Search Tree
- SDL学习入门
- 桥接模式
- 黑马程序员—多线程
- [原创]一点一滴学习网络游戏开发,第二章节:框架设计初步
- HDU 2895 Edit distance(数学+模拟)
- 有关请求地址的学问
- ZOJ 3626 Treasure Hunt I
- 使用hadoop进行大规模数据的全局排序
- HDU_1804Deli Deli