Advertisement
Piratux

Untitled

Feb 12th, 2022
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include "PiraTimer.h"
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define OLC_PGE_APPLICATION
  7. #include "olcPixelGameEngine.h"
  8.  
  9. #include "olcPGEX_Media.h"
  10.  
  11. #include "video_reader.h"
  12.  
  13. struct media_info {
  14.     std::string name;
  15.     bool open_video;
  16.     bool open_audio;
  17. };
  18.  
  19. std::vector<media_info> medias{
  20.     {"assets/bispoos_gameplay.mp4", true, true},
  21.     {"assets/robot_gameplay.mp4", true, true},
  22.     {"assets/nokia_combat.gif", true, true},
  23.     {"assets/winter.ogg", true, true},
  24.     {"assets/skeleton.mp4", true, true},
  25.     {"assets/shooter.mp4", true, true}
  26. };
  27.  
  28. // Override base class with your custom functionality
  29. class Example : public olc::PixelGameEngine
  30. {
  31. public:
  32.     olc::Media media;
  33.     olc::Decal* frame;
  34.  
  35.     int idx = 0;
  36.  
  37. public:
  38.     Example()
  39.     {
  40.         // Name your application
  41.         sAppName = "Example";
  42.     }
  43.  
  44. public:
  45.     bool OpenNextVideo() {
  46.         idx = (idx + 1) % medias.size();
  47.         printf("Playing sound[%i]: %s\n", idx, medias[idx].name.c_str());
  48.         if (media.Open(medias[idx].name.c_str(), medias[idx].open_video, medias[idx].open_audio) == olc::MediaResult::Error)
  49.             return false;
  50.     }
  51.  
  52.     bool OpenPrevVideo() {
  53.         idx = (idx + medias.size() - 1) % medias.size();
  54.         printf("Playing sound[%i]: %s\n", idx, medias[idx].name.c_str());
  55.         if (media.Open(medias[idx].name.c_str(), medias[idx].open_video, medias[idx].open_audio) == olc::MediaResult::Error)
  56.             return false;
  57.     }
  58.  
  59.     bool OpenCurrVideo() {
  60.         printf("Playing sound[%i]: %s\n", idx, medias[idx].name.c_str());
  61.         if (media.Open(medias[idx].name.c_str(), medias[idx].open_video, medias[idx].open_audio) == olc::MediaResult::Error)
  62.             return false;
  63.     }
  64.  
  65.     bool OnUserCreate() override
  66.     {
  67.         OpenCurrVideo();
  68.  
  69.         return true;
  70.     }
  71.  
  72.     bool OnUserUpdate(float delta_time) override
  73.     {
  74.         if (GetKey(olc::ESCAPE).bPressed) {
  75.             return false;
  76.         }
  77.  
  78.         if (GetKey(olc::ENTER).bPressed) {
  79.             frame = media.GetVideoFrame();
  80.         }
  81.  
  82.         if (GetKey(olc::S).bPressed) {
  83.             if (media.SkipVideoFrame() == olc::MediaResult::Success)
  84.                 printf("Sucesfully skipped\n");
  85.         }
  86.  
  87.         if (GetKey(olc::SPACE).bHeld) {
  88.             frame = media.GetVideoFrame();
  89.         }
  90.  
  91.         if (GetKey(olc::M).bPressed) {
  92.             OpenNextVideo();
  93.         }
  94.  
  95.         if (GetKey(olc::N).bPressed) {
  96.             OpenPrevVideo();
  97.         }
  98.  
  99.         if (GetKey(olc::B).bPressed) {
  100.             OpenCurrVideo();
  101.         }
  102.  
  103.         if (GetKey(olc::V).bPressed) {
  104.             media.Close();
  105.         }
  106.  
  107.        
  108.  
  109.         if (media.IsVideoOpened()) {
  110.             frame = media.GetVideoFrame(delta_time);
  111.             if (frame != nullptr)
  112.                 DrawDecal({ 0,0 }, frame);
  113.             //else
  114.                 //return false;
  115.         }
  116.  
  117.         using namespace std::string_literals;
  118.  
  119.         DrawStringPropDecal(
  120.             { 5,5 },
  121.             "M - open next\n"
  122.             "N - open prev\n"
  123.             "B - open curr\n"
  124.             "V - close curr\n"
  125.             "video: "s + (medias[idx].open_video ? "on\n" : "off\n") +
  126.             "audio: "s + (medias[idx].open_audio ? "on\n" : "off\n"),
  127.             olc::RED,
  128.             {3,3}
  129.         );
  130.  
  131.         PiraTimer::end("InternalUpdate");
  132.  
  133.         PiraTimer::end("FrameLength");
  134.         PiraTimer::start("FrameLength");
  135.  
  136.         PiraTimer::start("OnUserUpdate");
  137.  
  138.         PiraTimer::start("GetFrame");
  139.  
  140.         //if (media.FinishedReading()) {
  141.         //    return false;
  142.         //}
  143.  
  144.         //std::this_thread::sleep_for(std::chrono::milliseconds(200));
  145.  
  146.        
  147.  
  148.            
  149.         //if (olc::MediaResult::Success != media.SkipVideoFrame())
  150.           //  return false;
  151.  
  152.  
  153.         PiraTimer::end("GetFrame");
  154.        
  155.         PiraTimer::end("OnUserUpdate");
  156.         PiraTimer::start("InternalUpdate");
  157.  
  158.         return true;
  159.     }
  160. };
  161.  
  162. int main()
  163. {
  164.     {
  165.         Example demo;
  166.         if (demo.Construct(1280, 720, 1, 1))
  167.             demo.Start();
  168.     }
  169.    
  170.     PiraTimer::print_stats();
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement