HoverRace  2.0
SdlDynamicTexture.h
Go to the documentation of this file.
1 
2 // SdlDynamicTexture.h
3 //
4 // Copyright (c) 2015-2016 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "../../Exception.h"
25 
26 #include "SdlTexture.h"
27 
28 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
29 # ifdef MR_ENGINE
30 # define MR_DllDeclare __declspec( dllexport )
31 # else
32 # define MR_DllDeclare __declspec( dllimport )
33 # endif
34 #else
35 # define MR_DllDeclare
36 #endif
37 
38 namespace HoverRace {
39  namespace Display {
40  namespace SDL {
41  class SdlDisplay;
42  }
43  }
44 }
45 
46 namespace HoverRace {
47 namespace Display {
48 namespace SDL {
49 
55 {
56  using SUPER = SdlTexture;
57 
58 public:
59  SdlDynamicTexture() = delete;
60  SdlDynamicTexture(SdlDisplay &display, SDL_Texture *texture);
61  SdlDynamicTexture(SdlDisplay &display, int width, int height);
62  SdlDynamicTexture(const SdlDynamicTexture&) = delete;
63 
65  {
66  if (surface) SDL_FreeSurface(surface);
67  }
68 
69  SdlDynamicTexture &operator=(const SdlDynamicTexture&) = delete;
70 
71 public:
79  bool IsDirty() const { return dirty; }
80 
81 public:
87  bool Update()
88  {
89  if (dirty) {
90  SDL_UpdateTexture(texture, nullptr,
91  surface->pixels, surface->pitch);
92  dirty = false;
93  return true;
94  }
95  else {
96  return false;
97  }
98  }
99 
106  template<class Fn>
107  void Update(Fn fn)
108  {
109  dirty = true;
110 
111  bool retv;
112  if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
113  try {
114  retv = static_cast<bool>(fn(surface));
115  }
116  catch (...) {
117  if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
118  throw;
119  }
120  if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
121  if (retv) Update();
122  }
123 
124 protected:
125  SDL_Surface *surface;
126  bool dirty;
127 };
128 
134 {
135  using SUPER = Exception;
136 
137 public:
139  DynamicTextureExn(const std::string &msg) : SUPER(msg) { }
140  DynamicTextureExn(const char *msg) : SUPER(msg) { }
141  DynamicTextureExn(const std::string &msg, const std::string &sdlError) :
142  SUPER(msg + sdlError) { }
143  virtual ~DynamicTextureExn() noexcept { }
144 };
145 
146 } // namespace SDL
147 } // namespace Display
148 } // namespace HoverRace
149 
150 #undef MR_DllDeclare
DynamicTextureExn(const std::string &msg, const std::string &sdlError)
Definition: SdlDynamicTexture.h:141
virtual ~DynamicTextureExn() noexcept
Definition: SdlDynamicTexture.h:143
bool Update()
Updates the texture from the backing surface.
Definition: SdlDynamicTexture.h:87
#define MR_DllDeclare
Definition: SdlDynamicTexture.h:35
Thrown when the dynamic texture could not be created.
Definition: SdlDynamicTexture.h:133
void Update(Fn fn)
Updates the texture with new pixel data.
Definition: SdlDynamicTexture.h:107
SDL_Surface * surface
Definition: SdlDynamicTexture.h:125
Wrapper for an SDL texture that can be updated.
Definition: SdlDynamicTexture.h:54
virtual ~SdlDynamicTexture()
Definition: SdlDynamicTexture.h:64
bool dirty
Definition: SdlDynamicTexture.h:126
DynamicTextureExn(const char *msg)
Definition: SdlDynamicTexture.h:140
std::exception SUPER
Definition: Exception.h:44
Wrapper for SDL_Texture.
Definition: SdlTexture.h:54
Definition: Announcement.h:24
Base exception, providing constructors for setting the message.
Definition: Exception.h:42
DynamicTextureExn()
Definition: SdlDynamicTexture.h:138
DynamicTextureExn(const std::string &msg)
Definition: SdlDynamicTexture.h:139
bool IsDirty() const
Check if this texture has changes that haven&#39;t been updated yet.
Definition: SdlDynamicTexture.h:79
SDL-based software rendering.
Definition: SdlDisplay.h:60