运行linux桌面并检查sdl2,第一课: 使用Linux命令行(SDL2教程翻译)

Lesson 0: 使用Linux命令行

在linux上编译项目,我们将使用简单的makefile,makefile文件将为我们设置好需要的库文件和链接。这个makefile文件假设你的SDL库文件安装在/usr/local/lib,头文件位于/usr/local/include。如果你要通过cmake来编译项目,这里是文件的安装目录,更多的细节在编译时可以通过这里找到。如果你是安装开发库是通过包管理软件或者你把库文件和头文件放置在其他的位置,你需要修改makefile中相应的文件的位置。你可以使用sdl2-config加上参数 --cflag 和 --libs 去查询是否安装SDL2开发库或者确定文件的位置。

如果你不是很熟悉Makefile的语法,你可以点击这里这个链接查看简单的makefile用法

Makefile文件内容

CXX = g++

# Update these paths to match your installation

# You may also need to update the linker option rpath, which sets where to look for

# the SDL2 libraries at runtime to match your install

SDL_LIB = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib

SDL_INCLUDE = -I/usr/local/include

# You may need to change -std=c++11 to -std=c++0x if your compiler is a bit older

CXXFLAGS = -Wall -c -std=c++11 $(SDL_INCLUDE)

LDFLAGS = $(SDL_LIB)

EXE = SDL_Lesson0

all: $(EXE)

$(EXE): main.o

$(CXX) $< $(LDFLAGS) -o $@

main.o: main.cpp

$(CXX) $(CXXFLAGS) $< -o $@

clean:

rm *.o && rm $(EXE)

测试代码

下面的代码是一段简单的测试代码,用来检测SDL是否安装成功。程序先初始化了SDL video 子系统,接着检查是否有报错,然后退出程序。这个源文件名字应该为“main.cpp”,或者你可以修改makefile中对应的源代码文件名。

#include

#include

/*

* Lesson 0: Test to make sure SDL is setup properly

*/

int main(int, char**){

if (SDL_Init(SDL_INIT_VIDEO) != 0){

std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;

return 1;

}

SDL_Quit();

return 0;

}

如果你成功设置了环境变量,这个程序应该成功运行并且没有什么事件发生。如果有错误发生,请一步一步检查确定全部步骤正确。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用SDL2在Linux上播放视频,需要先安装SDL2库和FFmpeg库。接下来,需要用FFmpeg将视频解码成yuv像素数据,再使用SDL2将像素数据渲染到屏幕上。 以下是基本的步骤: 1. 引入SDL2和FFmpeg库,定义相关变量和函数。 ```c #include <SDL2/SDL.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> AVCodecContext *codec_ctx; AVFormatContext *format_ctx; AVCodec *codec; SDL_Renderer* renderer; SDL_Texture* texture; SDL_Window *window; SDL_Rect rect; unsigned char *output_buffer; int pitch; struct SwsContext *sws_ctx; ``` 2. 初始化SDL2和FFmpeg库。 ```c if(SDL_Init(SDL_INIT_VIDEO) != 0) { printf("SDL_Init Error: %s", SDL_GetError()); return -1; } av_register_all(); avformat_network_init(); format_ctx = avformat_alloc_context(); if(avformat_open_input(&format_ctx, file_path, NULL, NULL) != 0) { return -1; } if(avformat_find_stream_info(format_ctx, NULL) < 0) { return -1; } int video_stream_index = -1; for(int i=0; i < format_ctx->nb_streams; i++) { if(format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } } if(video_stream_index == -1) { return -1; } codec_ctx = format_ctx->streams[video_stream_index]->codec; codec = avcodec_find_decoder(codec_ctx->codec_id); if(codec == NULL) { return -1; } if(avcodec_open2(codec_ctx, codec, NULL) < 0) { return -1; } ``` 3. 配置SDL2窗口、渲染器和纹理。 ```c window = SDL_CreateWindow("Video Player", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, codec_ctx->width, codec_ctx->height, SDL_WINDOW_OPENGL); renderer = SDL_CreateRenderer(window, -1, 0); texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, codec_ctx->width, codec_ctx->height); rect.w = codec_ctx->width; rect.h = codec_ctx->height; ``` 4. 读取视频流、解码和渲染。 ```c AVPacket packet; int frame_finished; AVFrame* frame = av_frame_alloc(); while(av_read_frame(format_ctx, &packet) >= 0) { if(packet.stream_index == video_stream_index) { if(avcodec_send_packet(codec_ctx, &packet) == 0) { while(avcodec_receive_frame(codec_ctx, frame) == 0) { sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL); sws_scale(sws_ctx, (const uint8_t * const*)frame->data, frame->linesize, 0, codec_ctx->height, (uint8_t * const *)output_buffer, &pitch); SDL_UpdateYUVTexture(texture, NULL, output_buffer, codec_ctx->width, output_buffer+codec_ctx->width*codec_ctx->height, codec_ctx->width/2, output_buffer+codec_ctx->width*codec_ctx->height*5/4, codec_ctx->width/2); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderPresent(renderer); sws_freeContext(sws_ctx); } } } av_packet_unref(&packet); } ``` 5. 释放资源。 ```c avformat_close_input(&format_ctx); avcodec_free_context(&codec_ctx); SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); ``` 总体上来说,使用SDL2播放视频需要对FFmpeg库有一定的了解,掌握基本的视频解码、像素转换和纹理渲染等技巧。对于初学者来说,需要仔细研究相关文档和示例程序,并多做实践。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值