purpose

The main purpose of rendering is to test the program and try to make sure there are bugs in the SDL layer before adding FFmpeg. The subfile can be used simply as a template. It can also be simply encapsulated into C++ class files for easy use. I’m not going to expand it here

An FFmpeg player class is then created. Then call FFmpeg directly from here to make a simple universal player.

Color rendering

Running the code, you can see that you create a window, randomly generate a color background, and then start gradually changing the color. The source code is very simple. Less than 100 lines of direct code.

The code is different from what we saw before, but it’s basically adding some judgment, because C code has to free up memory, so I’m just going to fill in what I left out.

#include <iostream> using namespace std; extern "C" { #include "SDL.h" } int main(int argc, char* argv[]) { int ret = -1; //set defualt size of window int video_width = 640; int video_height = 480; // here is the code needed for SDL rendering section SDL_Window* window; SDL_Renderer* renderer; SDL_Texture* texture; SDL_Rect sdlRect{0,0, video_width, video_height}; if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ERROR SDL_Init - %s\n", SDL_GetError()); return ret; } window = SDL_CreateWindow("Easy Player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, video_width, video_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); if (window == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ERROR SDL_CreateWindow - %s\n", SDL_GetError()); return -1; } renderer = SDL_CreateRenderer(window, -1, 0); if (! renderer) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ERROR SDL_CreateRenderer - %s\n", SDL_GetError()); return -1; } int pixformat = SDL_PIXELFORMAT_IYUV; texture = SDL_CreateTexture(renderer, pixformat, SDL_TEXTUREACCESS_STREAMING, video_width, video_height); if (! texture) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ERROR SDL_CreateTexture - %s\n", SDL_GetError()); return -1; } int count = 1000; int color_r = 255; int color_g = 255; int color_b = 255; while (count--) { //SDL_UpdateYUVTexture(texture, NULL, // pict->data[0], pict->linesize[0], // pict->data[1], pict->linesize[1], // pict->data[2], pict->linesize[2]); //SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); // Default yellow color_r = (color_r += 1) % 255; color_g = (color_g += 2) % 255; color_b = (color_b += 3) % 255; cout << "color_r = " << color_r << " color_g = " << color_g << " color_b = " << color_b << endl; SDL_SetRenderDrawColor(renderer, color_r, color_g, color_b, 255); SDL_RenderClear(renderer); //SDL_RenderCopy(renderer, texture, NULL, &sdlRect); SDL_RenderPresent(renderer); SDL_Delay(40); } if (texture) { SDL_DestroyTexture(texture); } if (renderer) { SDL_DestroyRenderer(renderer); } if (window) { SDL_DestroyWindow(window); } // Clean up SDL_Quit(); return 0; }Copy the code