Allow setting any number of sprites over the video

Default to no sprites over the video
This commit is contained in:
Sam Lantinga
2023-10-07 13:01:52 -07:00
parent ebf5e08fa1
commit ed6381b68d
2 changed files with 70 additions and 34 deletions

View File

@@ -185,10 +185,10 @@ endif()
set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL SWSCALE) set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL SWSCALE)
include("${SDL3_SOURCE_DIR}/cmake/FindFFmpeg.cmake") include("${SDL3_SOURCE_DIR}/cmake/FindFFmpeg.cmake")
if(FFmpeg_FOUND) if(FFmpeg_FOUND)
add_sdl_test_executable(testspriteffmpeg NO_C90 SOURCES testspriteffmpeg.c ${icon_bmp_header}) add_sdl_test_executable(testffmpeg NO_C90 SOURCES testffmpeg.c ${icon_bmp_header})
target_link_libraries(testspriteffmpeg PRIVATE ${FFMPEG_LIBRARIES}) target_link_libraries(testffmpeg PRIVATE ${FFMPEG_LIBRARIES})
else() else()
message(STATUS "Cannot find ffmpeg, skipping testspriteffmpeg") message(STATUS "Can't find ffmpeg, skipping testffmpeg")
endif() endif()
add_sdl_test_executable(checkkeys SOURCES checkkeys.c) add_sdl_test_executable(checkkeys SOURCES checkkeys.c)

View File

@@ -31,9 +31,10 @@
#define WINDOW_HEIGHT 480 #define WINDOW_HEIGHT 480
static SDL_Texture *sprite; static SDL_Texture *sprite;
static SDL_FRect position; static SDL_FRect *positions;
static SDL_FRect velocity; static SDL_FRect *velocities;
static int sprite_w, sprite_h; static int sprite_w, sprite_h;
static int num_sprites = 0;
static SDL_Window *window; static SDL_Window *window;
static SDL_Renderer *renderer; static SDL_Renderer *renderer;
@@ -67,24 +68,34 @@ static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned
static void MoveSprite(void) static void MoveSprite(void)
{ {
int max_w, max_h; SDL_Rect viewport;
SDL_FRect *position, *velocity;
int i;
SDL_GetCurrentRenderOutputSize(renderer, &max_w, &max_h); SDL_GetRenderViewport(renderer, &viewport);
/* Move the sprite, bounce at the wall, and draw */ for (i = 0; i < num_sprites; ++i) {
position.x += velocity.x; position = &positions[i];
if ((position.x < 0) || (position.x >= (max_w - sprite_w))) { velocity = &velocities[i];
velocity.x = -velocity.x; position->x += velocity->x;
position.x += velocity.x; if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
velocity->x = -velocity->x;
position->x += velocity->x;
}
position->y += velocity->y;
if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
velocity->y = -velocity->y;
position->y += velocity->y;
} }
position.y += velocity.y;
if ((position.y < 0) || (position.y >= (max_h - sprite_h))) {
velocity.y = -velocity.y;
position.y += velocity.y;
} }
/* Blit the sprite onto the screen */ /* Blit the sprite onto the screen */
SDL_RenderTexture(renderer, sprite, NULL, &position); for (i = 0; i < num_sprites; ++i) {
position = &positions[i];
/* Blit the sprite onto the screen */
SDL_RenderTexture(renderer, sprite, NULL, position);
}
} }
static AVCodecContext *OpenStream(AVFormatContext *ic, int stream) static AVCodecContext *OpenStream(AVFormatContext *ic, int stream)
@@ -366,6 +377,7 @@ static void HandleVideoFrame(AVFrame *frame, double pts)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
const char *file = NULL;
AVFormatContext *ic = NULL; AVFormatContext *ic = NULL;
int audio_stream = -1; int audio_stream = -1;
int video_stream = -1; int video_stream = -1;
@@ -374,6 +386,7 @@ int main(int argc, char *argv[])
AVPacket *pkt = NULL; AVPacket *pkt = NULL;
AVFrame *frame = NULL; AVFrame *frame = NULL;
double first_pts = -1.0; double first_pts = -1.0;
int i;
int result; int result;
int return_code = -1; int return_code = -1;
SDL_bool flushing = SDL_FALSE; SDL_bool flushing = SDL_FALSE;
@@ -382,8 +395,18 @@ int main(int argc, char *argv[])
/* Enable standard application logging */ /* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
if (argc != 2) { for (i = 1; i < argc; ++i) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: %s video_file\n", argv[0]); if (SDL_strcmp(argv[i], "--sprites") == 0 && argv[i+1]) {
num_sprites = SDL_atoi(argv[i+1]);
++i;
} else {
/* We'll try to open this as a media file */
file = argv[i];
break;
}
}
if (!file) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: %s [--sprites N] video_file\n", argv[0]);
return_code = 1; return_code = 1;
goto quit; goto quit;
} }
@@ -398,12 +421,12 @@ int main(int argc, char *argv[])
goto quit; goto quit;
} }
if (SDL_SetWindowTitle(window, argv[1]) < 0) { if (SDL_SetWindowTitle(window, file) < 0) {
SDL_Log("SDL_SetWindowTitle: %s", SDL_GetError()); SDL_Log("SDL_SetWindowTitle: %s", SDL_GetError());
} }
/* Open the media file */ /* Open the media file */
result = avformat_open_input(&ic, argv[1], NULL, NULL); result = avformat_open_input(&ic, file, NULL, NULL);
if (result < 0) { if (result < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open %s: %d", argv[1], result); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open %s: %d", argv[1], result);
return_code = 4; return_code = 4;
@@ -447,19 +470,30 @@ int main(int argc, char *argv[])
goto quit; goto quit;
} }
/* Initialize the sprite position */ /* Allocate memory for the sprite info */
int max_w, max_h; positions = (SDL_FRect *)SDL_malloc(num_sprites * sizeof(*positions));
SDL_GetCurrentRenderOutputSize(renderer, &max_w, &max_h); velocities = (SDL_FRect *)SDL_malloc(num_sprites * sizeof(*velocities));
if (positions == NULL || velocities == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
return_code = 3;
goto quit;
}
/* Position sprites and set their velocities */
SDL_Rect viewport;
SDL_GetRenderViewport(renderer, &viewport);
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
position.x = (float)(rand() % (max_w - sprite_w)); for (i = 0; i < num_sprites; ++i) {
position.y = (float)(rand() % (max_h - sprite_h)); positions[i].x = (float)(rand() % (viewport.w - sprite_w));
position.w = (float)sprite_w; positions[i].y = (float)(rand() % (viewport.h - sprite_h));
position.h = (float)sprite_h; positions[i].w = (float)sprite_w;
velocity.x = 0.0f; positions[i].h = (float)sprite_h;
velocity.y = 0.0f; velocities[i].x = 0.0f;
while (!velocity.x || !velocity.y) { velocities[i].y = 0.0f;
velocity.x = (float)((rand() % (2 + 1)) - 1); while (!velocities[i].x || !velocities[i].y) {
velocity.y = (float)((rand() % (2 + 1)) - 1); velocities[i].x = (float)((rand() % (2 + 1)) - 1);
velocities[i].y = (float)((rand() % (2 + 1)) - 1);
}
} }
/* Main render loop */ /* Main render loop */
@@ -543,6 +577,8 @@ int main(int argc, char *argv[])
} }
return_code = 0; return_code = 0;
quit: quit:
SDL_free(positions);
SDL_free(velocities);
av_frame_free(&frame); av_frame_free(&frame);
av_packet_free(&pkt); av_packet_free(&pkt);
avcodec_free_context(&audio_context); avcodec_free_context(&audio_context);