neoGFX
C++ GPU-oriented GUI library and app/game creation framework.
texture.hpp
Go to the documentation of this file.
1 // texture.hpp
2 /*
3  neogfx C++ GUI Library
4  Copyright (c) 2015 Leigh Johnston. All Rights Reserved.
5 
6  This program is free software: you can redistribute it and / or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include <neogfx/neogfx.hpp>
23 #include <vector>
24 #include <boost/optional.hpp>
25 #include <neogfx/core/colour.hpp>
26 #include <neogfx/gfx/i_texture.hpp>
28 
29 namespace neogfx
30 {
31  class i_image;
32  class i_native_texture;
33 
34  class texture : public i_texture
35  {
36  // construction
37  public:
38  texture();
39  texture(const neogfx::size& aExtents, dimension aDpiScaleFactor = 1.0, texture_sampling aSampling = texture_sampling::NormalMipmap, const optional_colour& aColour = optional_colour());
40  texture(const i_texture& aTexture);
41  texture(const i_image& aImage);
42  texture(const i_sub_texture& aSubTexture);
43  ~texture();
44  // operations
45  public:
46  type_e type() const override;
47  const i_sub_texture& as_sub_texture() const override;
48  dimension dpi_scale_factor() const override;
49  texture_sampling sampling() const override;
50  bool is_empty() const override;
51  size extents() const override;
52  size storage_extents() const override;
53  void set_pixels(const rect& aRect, const void* aPixelData) override;
54  void set_pixels(const i_image& aImage) override;
55  public:
56  virtual std::shared_ptr<i_native_texture> native_texture() const override;
57  // attributes
58  private:
59  std::shared_ptr<i_native_texture> iNativeTexture;
60  optional_sub_texture iSubTexture;
61  };
62 
63  typedef boost::optional<texture> optional_texture;
64  typedef std::shared_ptr<const i_texture> texture_pointer;
65  typedef std::pair<texture_pointer, optional_rect> texture_source;
66  typedef std::vector<texture_source> texture_list;
67  typedef boost::optional<texture_list> optional_texture_list;
68  typedef texture_list::size_type texture_index;
69  typedef std::shared_ptr<texture_list> texture_list_pointer;
70 
71  inline texture_pointer to_texture_pointer(const i_texture& aTexture)
72  {
73  return (aTexture.type() == i_texture::Texture ? static_cast<texture_pointer>(std::make_shared<texture>(aTexture)) : static_cast<texture_pointer>(std::make_shared<sub_texture>(aTexture.as_sub_texture())));
74  }
75 
76  inline texture_list_pointer to_texture_list_pointer(const i_texture& aTexture, const optional_rect& aTextureRect = optional_rect{})
77  {
78  return std::make_shared<texture_list>(1, texture_source{ to_texture_pointer(aTexture), aTextureRect });
79  }
80 
81  inline texture_list_pointer to_texture_list_pointer(texture_pointer aTexture, const optional_rect& aTextureRect = optional_rect{})
82  {
83  return std::make_shared<texture_list>(1, texture_source{ aTexture, aTextureRect });
84  }
85 
86  inline texture_list_pointer to_texture_list_pointer(texture_list& aTextureList, const i_texture& aTexture, const optional_rect& aTextureRect)
87  {
88  aTextureList.assign(1, texture_source{ to_texture_pointer(aTexture), aTextureRect });
89  return texture_list_pointer{ texture_list_pointer{}, &aTextureList };
90  }
91 
92  inline texture_list_pointer to_texture_list_pointer(texture_list& aTextureList, texture_pointer aTexture, const optional_rect& aTextureRect)
93  {
94  aTextureList.assign(1, texture_source{ aTexture, aTextureRect });
95  return texture_list_pointer{ texture_list_pointer{}, &aTextureList };
96  }
97 }
size extents() const override
const i_sub_texture & as_sub_texture() const override
size storage_extents() const override
boost::optional< texture_list > optional_texture_list
Definition: texture.hpp:67
texture_sampling
Definition: i_texture.hpp:29
boost::optional< sub_texture > optional_sub_texture
Definition: sub_texture.hpp:62
coordinate_value_type dimension
Definition: geometrical.hpp:33
texture_list_pointer to_texture_list_pointer(const i_texture &aTexture, const optional_rect &aTextureRect=optional_rect{})
Definition: texture.hpp:76
boost::optional< rect > optional_rect
virtual const i_sub_texture & as_sub_texture() const =0
virtual type_e type() const =0
texture_list::size_type texture_index
Definition: texture.hpp:68
std::shared_ptr< const i_texture > texture_pointer
Definition: texture.hpp:64
type_e type() const override
std::vector< texture_source > texture_list
Definition: texture.hpp:66
std::shared_ptr< texture_list > texture_list_pointer
Definition: texture.hpp:69
virtual std::shared_ptr< i_native_texture > native_texture() const override
bool is_empty() const override
boost::optional< texture > optional_texture
Definition: texture.hpp:63
texture_sampling sampling() const override
boost::optional< colour > optional_colour
Definition: colour.hpp:925
dimension dpi_scale_factor() const override
std::pair< texture_pointer, optional_rect > texture_source
Definition: texture.hpp:65
void set_pixels(const rect &aRect, const void *aPixelData) override
texture_pointer to_texture_pointer(const i_texture &aTexture)
Definition: texture.hpp:71