gs_ffmpeg

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README

main.c (3018B)


      1 #include <gs/gs.h>
      2 #include <gs/util/gs_idraw.h>
      3 #define GS_AVDECODE_IMPL
      4 #include "gs_avdecode.h"
      5 
      6 static gs_immediate_draw_t gsi;
      7 static gs_command_buffer_t cb;
      8 
      9 static const char* filename;
     10 
     11 static gs_asset_texture_t tex;
     12 static gs_asset_texture_t ptex;
     13 static gs_avdecode_ctx_t video;
     14 static gs_avdecode_pthread_t pvideo;
     15 
     16 void app_update()
     17 {
     18         const gs_vec2 fb = gs_platform_framebuffer_sizev(gs_platform_main_window());
     19 
     20         gsi_defaults(&gsi);
     21         gsi_camera2D(&gsi, fb.x, fb.y);
     22         const float t = gs_platform_elapsed_time() * 0.0001f;
     23 
     24         gsi_camera3D(&gsi, fb.x, fb.y);
     25         gsi_rotatev(&gsi, gs_deg2rad(90.f), GS_ZAXIS); gsi_rotatev(&gsi, t, GS_YAXIS);
     26         gsi_sphere(&gsi, 0.f, 0.f, 0.f, 1.f, 50, 150, 200, 50, GS_GRAPHICS_PRIMITIVE_LINES);
     27 
     28         gsi_defaults(&gsi);
     29         gsi_camera2D(&gsi, fb.x, fb.y);
     30 
     31         int res = gs_avdecode_next_frame(&video);
     32         memcpy(*tex.desc.data, *video.img, video.img_sz);
     33         gs_graphics_texture_request_update(&cb, tex.hndl, &tex.desc);
     34 
     35         gsi_texture(&gsi, tex.hndl);
     36         gsi_rectvd(&gsi, gs_v2s(0.0f), fb, gs_v2s(0.f), gs_v2s(1.f), GS_COLOR_WHITE, GS_GRAPHICS_PRIMITIVE_TRIANGLES);
     37 
     38         if (!pvideo.done) {
     39                 gs_avdecode_aquire_m(&pvideo,
     40                         memcpy(*ptex.desc.data, *pvideo.video.img, pvideo.video.img_sz);
     41                         gs_graphics_texture_request_update(&cb, ptex.hndl, &ptex.desc);
     42                 );
     43 
     44                 gsi_texture(&gsi, ptex.hndl);
     45                 gsi_rectvd(&gsi, gs_v2(fb.x/2, fb.y/2), gs_v2(fb.x/2, fb.y/2), gs_v2s(0.f), gs_v2s(1.f), GS_COLOR_WHITE, GS_GRAPHICS_PRIMITIVE_TRIANGLES);
     46         } else if (pvideo.done > 0) {
     47                 gs_avdecode_pthread_destroy(&pvideo, &ptex);
     48                 pvideo.done = -1;
     49         }
     50 
     51         gsi_renderpass_submit(&gsi, &cb, gs_v4(0, 0, fb.x, fb.y), gs_color(10, 10, 10, 255));
     52         gs_graphics_command_buffer_submit(&cb);
     53 }
     54 
     55 void app_init()
     56 {
     57         cb = gs_command_buffer_new();
     58         gsi = gs_immediate_draw_new();
     59 
     60         int res = gs_avdecode_init(filename, &video, NULL, &tex);
     61 
     62         int res2 = gs_avdecode_pthread_play_video(&pvideo, filename, NULL, &ptex);
     63 
     64         if (res) {
     65                 gs_println("Unable to initialize video '%s' (error code %d)", filename, res);
     66                 exit(1);
     67         }
     68 }
     69 
     70 void app_shutdown()
     71 {
     72         gs_avdecode_destroy(&video, &tex);
     73 
     74         gs_immediate_draw_free(&gsi);
     75         gs_command_buffer_free(&cb);
     76 }
     77 
     78 gs_app_desc_t
     79 gs_main(int32_t argc, char** argv)
     80 {
     81         if (argc != 2) {
     82                 gs_println("----\nInvalid amount of arguments!\nUsage: ./App your-video");
     83                 exit(1);
     84         }
     85 
     86         filename = strdup(argv[1]);;
     87 
     88         return (gs_app_desc_t) {
     89                 .window = {
     90                         .width = 800,
     91                         .height = 600,
     92                 },
     93                 .init = app_init,
     94                 .update = app_update,
     95                 .shutdown = app_shutdown,
     96         };
     97 }