src/RenderState.hpp in gosu-2.0.0.pre6 vs src/RenderState.hpp in gosu-2.0.0.pre7

- old
+ new

@@ -1,25 +1,26 @@ #pragma once #include <Gosu/Transform.hpp> +#include "ClipRectStack.hpp" #include "GraphicsImpl.hpp" #include "OpenGLContext.hpp" #include "Texture.hpp" +#include <optional> // Properties that potentially need to be changed between each draw operation. // This does not include the color or vertex data of the actual quads. struct Gosu::RenderState { std::shared_ptr<Texture> texture; const Transform* transform; - ClipRect clip_rect; + std::optional<Rect> clip_rect; BlendMode mode; RenderState() : transform(0), mode(BM_DEFAULT) { - clip_rect.width = NO_CLIPPING; } bool operator==(const RenderState& rhs) const { return texture == rhs.texture && @@ -52,16 +53,16 @@ } } void apply_clip_rect() const { - if (clip_rect.width == NO_CLIPPING) { - glDisable(GL_SCISSOR_TEST); + if (clip_rect.has_value()) { + glEnable(GL_SCISSOR_TEST); + glScissor(clip_rect->x, clip_rect->y, clip_rect->width, clip_rect->height); } else { - glEnable(GL_SCISSOR_TEST); - glScissor(clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height); + glDisable(GL_SCISSOR_TEST); } } // Only used by Macro so far #ifndef GOSU_IS_OPENGLES @@ -74,16 +75,12 @@ #endif }; // Manages the OpenGL rendering state. It caches the current state, only forwarding the // changes to OpenGL if the new state is really different. -class Gosu::RenderStateManager : private Gosu::RenderState +class Gosu::RenderStateManager : private Gosu::RenderState, private Gosu::Noncopyable { - // Not copyable - RenderStateManager(const RenderStateManager&); - RenderStateManager& operator=(const RenderStateManager&); - void apply_transform() const { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -108,13 +105,11 @@ glPushMatrix(); } ~RenderStateManager() { - ClipRect no_clipping; - no_clipping.width = NO_CLIPPING; - set_clip_rect(no_clipping); + set_clip_rect(std::nullopt); set_texture(std::shared_ptr<Texture>()); // Return to previous MV matrix glMatrixMode(GL_MODELVIEW); glPopMatrix(); } @@ -125,13 +120,15 @@ set_transform(rs.transform); set_clip_rect(rs.clip_rect); set_alpha_mode(rs.mode); } - void set_texture(std::shared_ptr<Texture> new_texture) + void set_texture(const std::shared_ptr<Texture>& new_texture) { - if (new_texture == texture) return; + if (new_texture == texture) { + return; + } if (new_texture) { if (!texture) { glEnable(GL_TEXTURE_2D); } @@ -144,42 +141,32 @@ texture = new_texture; } void set_transform(const Transform* new_transform) { - if (new_transform == transform) return; + if (new_transform == transform) { + return; + } transform = new_transform; apply_transform(); } - void set_clip_rect(const ClipRect& new_clip_rect) + void set_clip_rect(const std::optional<Rect>& new_clip_rect) { - if (new_clip_rect.width == NO_CLIPPING) { - // Disable clipping - if (clip_rect.width != NO_CLIPPING) { - glDisable(GL_SCISSOR_TEST); - clip_rect.width = NO_CLIPPING; - } + if (clip_rect == new_clip_rect) { + return; } - else { - // Enable clipping if off - if (clip_rect.width == NO_CLIPPING) { - glEnable(GL_SCISSOR_TEST); - clip_rect = new_clip_rect; - glScissor(clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height); - } - // Adjust clipping if necessary - else if (!(clip_rect == new_clip_rect)) { - clip_rect = new_clip_rect; - glScissor(clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height); - } - } + + clip_rect = new_clip_rect; + apply_clip_rect(); } void set_alpha_mode(BlendMode new_mode) { - if (new_mode == mode) return; + if (new_mode == mode) { + return; + } mode = new_mode; apply_alpha_mode(); }