ge211
ge211_render.h
1 #pragma once
2 
3 #include "ge211_color.h"
4 #include "ge211_forward.h"
5 #include "ge211_geometry.h"
6 #include "ge211_window.h"
7 #include "ge211_util.h"
8 #include <memory>
9 
10 namespace ge211 {
11 
12 namespace detail {
13 
14 class Renderer
15 {
16 public:
17  explicit Renderer(const Window&);
18 
19  bool is_vsync() const noexcept;
20 
21  void set_color(Color);
22 
23  void clear();
24  void copy(const Texture&, Position);
25  void copy(const Texture&, Position, const Transform&);
26 
27  // Prepares a texture for rendering with this given renderer, without
28  // actually copying it.
29  void prepare(const Texture&) const;
30 
31  void present() noexcept;
32 
33 private:
34  friend Texture;
35 
36  SDL_Renderer* get_raw_() const noexcept;
37 
38  static SDL_Renderer* create_renderer_(SDL_Window*);
39 
40  delete_ptr<SDL_Renderer> ptr_;
41 };
42 
43 // A texture is initially created as a (device-independent) `SDL_Surface`,
44 // and then turned into an `SDL_Texture` the first time it gets rendered.
45 // The SDL_Texture is cached and the original `SDL_Surface` is deleted.
46 class Texture
47 {
48 public:
49  // An empty texture; don't render this or even ask for its dimensions.
50  Texture() noexcept;
51 
52  // Takes ownership of the `SDL_Surface*` and will delete it.
53  //
54  // \preconditions
55  // - The surface is not zero-sized.
56  explicit Texture(SDL_Surface*);
57  explicit Texture(delete_ptr<SDL_Surface>);
58 
59  Dimensions dimensions() const noexcept;
60 
61  // Returns nullptr if this `Texture` has been rendered, and can no
62  // longer be updated as an `SDL_Surface`.
63  SDL_Surface* as_surface() noexcept;
64 
65  bool empty() const noexcept;
66 
67 private:
68  friend Renderer;
69 
70  struct Impl_
71  {
72  Impl_(SDL_Surface*) noexcept;
73  Impl_(SDL_Texture*) noexcept;
74 
75  Impl_(delete_ptr<SDL_Surface>) noexcept;
76  Impl_(delete_ptr<SDL_Texture>) noexcept;
77 
78  delete_ptr<SDL_Surface> surface_;
79  delete_ptr<SDL_Texture> texture_;
80  // Invariant:
81  // - Exactly one surface_ and texture_ is non-null.
82  // - Whichever is non-null is non-zero-sized.
83  // Note: impl_ below is null for the empty Texture.
84  };
85 
86  SDL_Texture* get_raw_(const Renderer&) const;
87 
88  std::shared_ptr<Impl_> impl_;
89 };
90 
91 } // end namespace detail
92 
93 }
The game engine namespace.
Definition: ge211.h:17