DxLibEx
animation_graph.hpp
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (C) 2015-2017 DxLibEx project
3  https://github.com/Nagarei/DxLibEx/
4 
5  Distributed under the Boost Software License, Version 1.0.
6  (See http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #ifndef DXLE_INC_TEXTURE_ANIMATION_GRAPH_HPP_
9 #define DXLE_INC_TEXTURE_ANIMATION_GRAPH_HPP_
10 
12 #include <iterator>
13 #include <vector>
14 #include <mutex>
15 #include <functional>
16 #include "DxLib.h"
17 #include "texture2d.hpp"
18 #include "dxlibex/time.hpp"
19 #include "dxlibex/thread.hpp"
20 
21 namespace dxle
22 {
24  namespace graph2d
25  {
26  namespace gr_impl
27  {
29  {
30  protected:
32 #ifdef DX_THREAD_SAFE
33  mutable std::mutex counter_mtx;
34 #endif
35 
37  : counter(std::move(counter_))
38  {
39  counter.start();
40  }
42  virtual std::unique_ptr<texture2d_handle_manager> clone() = 0;
43  };
44  template<typename Cont>
46  {
47  public:
48  int get_handle()const override { DXLE_GET_LOCK(counter_mtx); return texture2d_handle_manager::GetTextureRawHandle(graphs[counter.get() % graphs.size()]); }
49 
50  private:
52  typename std::remove_reference<Cont>::type graphs;
53 
56  , graphs(std::forward<Cont>(graphs))
57  {}
58  std::unique_ptr<texture2d_handle_manager> clone() override
59  {
60  std::vector<texture2d> temp_graphs;
61  temp_graphs.resize(graphs.size());
62  for(size_t i = 0;i < graphs.size(); ++i) {
63  temp_graphs[i] = (std::move(*graphs[i].clone()));
64  }
65  return animation_handle_manager<std::vector<texture2d>>::get_unique(time::counter(this->counter), std::move(temp_graphs));
66  }
67  template<typename Cont2>
69  friend animation_graph;
70  template<typename... Args> static inline std::unique_ptr<texture2d_handle_manager> get_unique(Args&&... args) {
71  return std::unique_ptr<texture2d_handle_manager>(new this_T(std::forward<Args>(args)...));
72  }
73  };
74 #ifdef DXLE_TEMP_IMPL_MAKE_ANI_HM
75  static_assert(false, "");
76 #endif
77 #define DXLE_TEMP_IMPL_MAKE_ANI_HM(template_param, specialization, size)\
78  template<template_param>\
79  class animation_handle_manager<specialization> final : public animation_handle_manager_bace\
80  {\
81  public:\
82  int get_handle()const override\
83  {\
84  DXLE_GET_LOCK(counter_mtx);\
85  return texture2d_handle_manager::GetTextureRawHandle(graphs.get()[counter.get() % size]);\
86  }\
87  private:\
88  using Cont = specialization;\
89  using this_T = animation_handle_manager<Cont>;\
90  typename std::remove_reference<Cont>::type graphs;\
91  animation_handle_manager(time::counter&& counter, Cont&& graphs)\
92  : animation_handle_manager_bace(std::move(counter))\
93  , graphs(std::move(graphs))\
94  {}\
95  std::unique_ptr<texture2d_handle_manager> clone() override\
96  {\
97  return get_unique(*this);\
98  }\
99  friend animation_graph;\
100  template<typename... Args> static inline std::unique_ptr<texture2d_handle_manager> get_unique(Args&&... args) {\
101  return std::unique_ptr<texture2d_handle_manager>(new this_T(std::forward<Args>(args)...));\
102  }\
103  }
104  DXLE_TEMP_IMPL_MAKE_ANI_HM(typename Cont_value_T, std::reference_wrapper<Cont_value_T>, graphs.get().size());
105  DXLE_TEMP_IMPL_MAKE_ANI_HM(size_t N, std::reference_wrapper<dxle::texture2d[N]>, N);
106  DXLE_TEMP_IMPL_MAKE_ANI_HM(size_t N, std::reference_wrapper<const dxle::texture2d[N]>, N);
107  DXLE_TEMP_IMPL_MAKE_ANI_HM(size_t N, std::reference_wrapper<volatile dxle::texture2d[N]>, N);
108  DXLE_TEMP_IMPL_MAKE_ANI_HM(size_t N, std::reference_wrapper<const volatile dxle::texture2d[N]>, N);
109 #undef DXLE_TEMP_IMPL_MAKE_ANI_HM
110 
111  template<typename Cont>
113  {
114  public:
115  const texture2d& operator[](size_t i)const{ return graphs[i]; }
116  size_t size()const DXLE_NOEXCEPT_OR_NOTHROW{ return size_; }
117  cont_wrapper_with_size(Cont&& graphs, size_t size)
118  : graphs(std::forward<Cont>(graphs))
119  , size_(size)
120  {}
121  private:
122  typename std::remove_reference<Cont>::type graphs;
123  size_t size_;
124  };
125  template<typename Cont>
126  class cont_wrapper_with_size<std::reference_wrapper<Cont>>final
127  {
128  public:
129  const texture2d& operator[](size_t i)const{ return (graphs.get())[i]; }
130  size_t size()const DXLE_NOEXCEPT_OR_NOTHROW{ return size_; }
131  cont_wrapper_with_size(std::reference_wrapper<Cont>&& graphs, size_t size)
132  : graphs(std::move(graphs))
133  , size_(size)
134  {}
135  private:
136  std::reference_wrapper<Cont> graphs;
137  size_t size_;
138  };
139  }
140  class animation_graph final : public texture2d
141  {
142  public:
143  animation_graph()DXLE_NOEXCEPT = default;
144 
145  template<typename Cont>
149  : texture2d(gr_impl::animation_handle_manager<Cont>::get_unique(std::move(counter), std::move(graphs)))
150  {}
151  template<size_t N>
154  : animation_graph(counter, graphs, N)
155  {}
156  template<typename Cont>
159  animation_graph(time::counter counter, Cont graphs, size_t size)
160  : texture2d(gr_impl::animation_handle_manager<gr_impl::cont_wrapper_with_size<Cont>>::get_unique(std::move(counter), gr_impl::cont_wrapper_with_size<Cont>(std::move(graphs), size)))
161  {}
162 
164  animation_graph(const screen& other) = delete;
168  animation_graph& operator=(const animation_graph& other) = delete;
171 
173  std::unique_ptr<texture2d> clone()const override{ return cloneAni(); }
175  std::unique_ptr<animation_graph> cloneAni()const;
176 
177  void start(void);
178  void stop(void);
179  bool is_stop(void);
180  void reset_pass_time(std::chrono::milliseconds = std::chrono::milliseconds{ 0 });
181  std::chrono::milliseconds get_pass_time();
182 
183  size_t get_count();
184  void reset_count(size_t = 0);
185 
186  private:
187  static std::vector<texture2d> cast_to_vector(derivative_texture2d&&);
188  animation_graph(std::unique_ptr<gr_impl::texture2d_handle_manager>&& handle_manager)
189  : texture2d(std::move(handle_manager))
190  {}
191  };
192 
193  inline std::unique_ptr<animation_graph> animation_graph::cloneAni()const
194  {
195  auto hm_ptr = static_cast<gr_impl::animation_handle_manager_bace*>(handle_manager.get());
196  //return std::make_unique<animation_graph>(hm_ptr->clone());
197  return std::unique_ptr<animation_graph>(new animation_graph(hm_ptr->clone()));
198  }
199  inline std::vector<texture2d> animation_graph::cast_to_vector(derivative_texture2d&& div_texture)
200  {
201  std::vector<texture2d> temp_graphs(div_texture.size());
202  for (size_t i = 0; i < div_texture.size(); ++i){
203  temp_graphs[i] = std::move(div_texture[i]);
204  }
205  return temp_graphs;
206  }
207  inline void animation_graph::start(void)
208  {
209  static_cast<gr_impl::animation_handle_manager_bace*>(handle_manager.get())->counter.start();
210  }
211  inline void animation_graph::stop(void)
212  {
213  static_cast<gr_impl::animation_handle_manager_bace*>(handle_manager.get())->counter.stop();
214  }
215  inline bool animation_graph::is_stop(void)
216  {
217  return static_cast<gr_impl::animation_handle_manager_bace*>(handle_manager.get())->counter.is_stop();
218  }
219  inline void animation_graph::reset_pass_time(std::chrono::milliseconds new_pass_time)
220  {
221  static_cast<gr_impl::animation_handle_manager_bace*>(handle_manager.get())->counter.reset_pass_time(new_pass_time);
222  }
223  inline std::chrono::milliseconds animation_graph::get_pass_time()
224  {
225  return static_cast<gr_impl::animation_handle_manager_bace*>(handle_manager.get())->counter.get_pass_time();
226  }
228  {
229  return static_cast<gr_impl::animation_handle_manager_bace*>(handle_manager.get())->counter.get();
230  }
231  inline void animation_graph::reset_count(size_t new_count)
232  {
233  static_cast<gr_impl::animation_handle_manager_bace*>(handle_manager.get())->counter.reset_count(new_count);
234  }
235  }
236  using namespace graph2d;
237 }
238 
239 #endif
DXLE_TEMP_IMPL_MAKE_ANI_HM(typename Cont_value_T, std::reference_wrapper< Cont_value_T >, graphs.get().size())
Template class for 2D sizes specified by its coordinates width and height.
Definition: point2d.hpp:36
cont_wrapper_with_size(Cont &&graphs, size_t size)
std::chrono::milliseconds get_pass_time()
animation_graph(animation_graph &&other) DXLE_NOEXCEPT_OR_NOTHROW
所有権の譲渡
#define DXLE_NOEXCEPT
Definition: suffix.hpp:93
const texture2d & operator[](size_t i) const
animation_graph & operator=(animation_graph &&other) DXLE_NOEXCEPT_OR_NOTHROW
所有権の譲渡
animation_graph(time::counter counter, const texture2d(&graphs)[N])
std::unique_ptr< texture2d > clone() const override
画像を複製する
texture2d_handle_manager & operator=(const texture2d_handle_manager &) DXLE_NOEXCEPT_OR_NOTHROW
bool is_stop(void)
Definition: time.hpp:251
Definition: point2d.hpp:672
Definition: cast_if.hpp:12
animation_graph(time::counter counter, Cont graphs)
void reset_pass_time(std::chrono::milliseconds=std::chrono::milliseconds{ 0 })
Definition: time.hpp:255
texture2d & operator=(const texture2d &other)=delete
size_t get()
Definition: time.hpp:268
std::unique_ptr< animation_graph > cloneAni() const
画像を複製する
animation_graph(time::counter counter, Cont graphs, size_t size)
描画可能画像クラス
Definition: texture2d.hpp:334
#define DXLE_GET_LOCK(mtx)
Definition: thread.hpp:26
static int GetTextureRawHandle(const texture2d &)
Definition: texture2d.hpp:53
void reset_pass_time(std::chrono::milliseconds=std::chrono::milliseconds{ 0 })
virtual std::unique_ptr< texture2d_handle_manager > clone()=0
#define DXLE_NOEXCEPT_OR_NOTHROW
Definition: suffix.hpp:94
cont_wrapper_with_size(std::reference_wrapper< Cont > &&graphs, size_t size)
void start(void)
Definition: time.hpp:243
void stop(void)
Definition: time.hpp:247
void reset_count(size_t=0)
Definition: time.hpp:273
size_t size() const DXLE_NOEXCEPT_OR_NOTHROW
std::chrono::milliseconds get_pass_time()
Definition: time.hpp:263