SDL学习入门

2024-06-23 06:08
文章标签 学习 入门 sdl

本文主要是介绍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、最有代表性的简单程序,包括图像,文字,音乐,注释如下:
[cpp] view plain copy print ?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <SDL/SDL.h>  
  4. #include <SDL/SDL_image.h>  
  5. #include <SDL/SDL_mixer.h>  
  6. #include <SDL/SDL_ttf.h>  
  7.   
  8. static SDL_Surface* screen;//SDL窗口  
  9.   
  10. int main(int argc, char *argv[])  
  11. {  
  12.   
  13.     int quit = 0;  
  14.     SDL_Surface *text_sur;//文字容器  
  15.     SDL_Surface *background;//图像容器  
  16.     SDL_Event event;  
  17.     SDL_Color color;  
  18.     SDL_Rect srect, drect;  
  19.     Mix_Music *bgsound;  
  20.     TTF_Font *font;  
  21.       
  22.     //初始化SDL  
  23.     if (SDL_Init(SDL_INIT_VIDEO||SDL_INIT_AUDIO) < 0)  
  24.     {  
  25.         fprintf(stderr, "SDL init error:%s\n", SDL_GetError());  
  26.         exit(0);  
  27.     }  
  28.     atexit(SDL_Quit);//注册退出时调用的操作  
  29.       
  30.       
  31.     //设置SDL屏幕大小  
  32.     screen = SDL_SetVideoMode(600, 400, 24, SDL_HWSURFACE);  
  33.     if (screen == NULL)  
  34.     {  
  35.         fprintf(stderr, "Set video mode failure:%s\n", SDL_GetError());  
  36.         exit(0);  
  37.     }  
  38.       
  39.     //设置SDL窗口标题  
  40.     SDL_WM_SetCaption("test", NULL);  
  41.    
  42.     /*显示图像*/  
  43.     background = IMG_Load("background.jpg");//导入图像文件,并将图像放入文字容器  
  44.     srect.x = 0;  
  45.     srect.y = 0;  
  46.     srect.w = background->w;  
  47.     srect.h = background->h;  
  48.     drect = srect;//设置截取范围  
  49.     SDL_BlitSurface(background, &srect, screen, &drect);//将图像容器放入SDL窗口  
  50.   
  51.     /*显示文字*/  
  52.     //初始化TTF  
  53.     if (TTF_Init() < 0)  
  54.     {  
  55.         fprintf(stderr, "TTF init error:%s\n", SDL_GetError());  
  56.         return;  
  57.     }  
  58.       
  59.     font = TTF_OpenFont("test.ttf", 40);//导入字体文件  
  60.     color.r = 255;  
  61.     color.g = 0;  
  62.     color.b = 0;//设置文字颜色  
  63.     text_sur=TTF_RenderText_Solid(font, "Hello, Welcome to GAME!!", color);//将文字放入文字容器  
  64.       
  65.     srect.x = 0;  
  66.     srect.y = 0;  
  67.     srect.w = text_sur->w;  
  68.     srect.h = text_sur->h;  
  69.       
  70.     drect.x = (600 - text_sur->w) / 2;  
  71.     drect.y = (400 - text_sur->h) / 2;  
  72.     drect.w = text_sur->w;  
  73.     drect.h = text_sur->h;//设置截取范围  
  74.     SDL_BlitSurface(text_sur, &srect, screen, &drect);//将文字容器放入SDL窗口  
  75.       
  76.     SDL_UpdateRect(screen, 0, 0, 600, 400);//更新SDL窗口,让新添加的容器显示  
  77.   
  78.     /*播放声音*/  
  79.     Mix_OpenAudio(44100, AUDIO_S16, 2, 4096);//打开音频  
  80.     bgsound = Mix_LoadMUS("bgsound.mp3");//导入声音文件  
  81.     Mix_PlayMusic(bgsound, -1);//播放音频  
  82.     while (quit == 0)  
  83.     {  
  84.         while (SDL_PollEvent(&event))  
  85.         {  
  86.             switch (event.type)  
  87.             {  
  88.              case SDL_QUIT:  
  89.                 Mix_CloseAudio();//关闭音频  
  90.                 quit = 1;  
  91.                 break;  
  92.              default:  
  93.                 break;  
  94.             }  
  95.         }  
  96.         SDL_Delay(100);  
  97.     }  
  98.       
  99.     return 0;  
  100. }  
#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编译时可能会出现如下错误:
[cpp] view plain copy print ?
  1. main.c:(.text+0x19): undefined reference to `SDL_Init'  
  2. main.c:(.text+0x22): undefined reference to `SDL_GetError'  
  3. main.c:(.text+0x50): undefined reference to `SDL_Quit'  
  4. main.c:(.text+0x79): undefined reference to `SDL_SetVideoMode'  
  5. main.c:(.text+0x8c): undefined reference to `SDL_GetError'  
  6. main.c:(.text+0xc7): undefined reference to `SDL_WM_SetCaption'  
  7. main.c:(.text+0xd3): undefined reference to `IMG_Load'  
  8. main.c:(.text+0x132): undefined reference to `SDL_UpperBlit'  
  9. main.c:(.text+0x137): undefined reference to `TTF_Init'  
  10. main.c:(.text+0x140): undefined reference to `SDL_GetError'  
  11. main.c:(.text+0x174): undefined reference to `TTF_OpenFont'  
  12. main.c:(.text+0x1a3): undefined reference to `TTF_RenderText_Solid'  
  13. main.c:(.text+0x24c): undefined reference to `SDL_UpperBlit'  
  14. main.c:(.text+0x279): undefined reference to `SDL_UpdateRect'  
  15. main.c:(.text+0x29d): undefined reference to `Mix_OpenAudio'  
  16. main.c:(.text+0x2a9): undefined reference to `Mix_LoadMUS'  
  17. main.c:(.text+0x2c1): undefined reference to `Mix_PlayMusic'  
  18. main.c:(.text+0x2d5): undefined reference to `Mix_CloseAudio'  
  19. main.c:(.text+0x2ec): undefined reference to `SDL_PollEvent'  
  20. main.c:(.text+0x2fc): undefined reference to `SDL_Delay'  
  21. 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学习入门的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086337

相关文章

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

史上最全MybatisPlus从入门到精通

《史上最全MybatisPlus从入门到精通》MyBatis-Plus是MyBatis增强工具,简化开发并提升效率,支持自动映射表名/字段与实体类,提供条件构造器、多种查询方式(等值/范围/模糊/分页... 目录1.简介2.基础篇2.1.通用mapper接口操作2.2.通用service接口操作3.进阶篇3

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三