Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ClipState.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
6#include "./RenderItem.hpp"
7#include "./DrawCommand.hpp"
8
9#include <unordered_map>
10
11namespace esengine {
12
13class ClipState {
14public:
21
22 void setScissor(u32 entity, i32 x, i32 y, i32 w, i32 h) {
23 auto& e = entries_[entity];
24 e.flags |= CMD_STATE_SCISSOR;
25 e.scissor = {x, y, w, h};
26 }
27
28 void setStencilMask(u32 entity, i32 refValue) {
29 auto& e = entries_[entity];
30 e.flags |= CMD_STATE_STENCIL_WRITE;
32 e.stencil_ref = refValue;
33 e.stencil_depth = 0;
34 }
35
36 void setUIStencilMask(u32 entity, i32 depth) {
37 setStencilMask(entity, depth);
38 entries_[entity].flags |= CMD_STATE_UI_STENCIL;
39 entries_[entity].stencil_depth = static_cast<u16>(depth);
40 }
41
42 void setUIStencilTest(u32 entity, i32 depth) {
43 setStencilTest(entity, depth);
44 entries_[entity].flags |= CMD_STATE_UI_STENCIL;
45 entries_[entity].stencil_depth = static_cast<u16>(depth);
46 }
47
49 bool overflow = false;
50 for (auto& [entity, entry] : entries_) {
51 if (!(entry.flags & CMD_STATE_UI_STENCIL)) continue;
52 entry.stencil_ref += count;
53 if (entry.stencil_ref > 255) {
54 overflow = true;
55 entry.flags = CMD_STATE_SCISSOR;
56 entry.scissor = {0, 0, 0, 0};
57 }
58 }
59 return overflow;
60 }
61
62 bool isStencilMask(Entity entity) const {
63 const auto it = entries_.find(entity.id());
64 return it != entries_.end() && (it->second.flags & CMD_STATE_STENCIL_WRITE) != 0;
65 }
66
67 void setStencilTest(u32 entity, i32 refValue) {
68 auto& e = entries_[entity];
69 e.flags |= CMD_STATE_STENCIL_TEST;
71 e.stencil_ref = refValue;
72 e.stencil_depth = 0;
73 }
74
76 void setStencilTestOutside(u32 entity, i32 refValue) {
77 auto& e = entries_[entity];
80 e.stencil_ref = refValue;
81 e.stencil_depth = 0;
82 }
83
84 void applyTo(Entity entity, DrawCommand& cmd) const {
85 auto it = entries_.find(entity.id());
86 if (it != entries_.end()) {
87 cmd.state_flags = it->second.flags;
88 cmd.scissor = it->second.scissor;
89 cmd.stencil_ref = it->second.stencil_ref;
90 cmd.stencil_depth = it->second.stencil_depth;
91 }
92 }
93
94 bool hasEntry(u32 entity) const {
95 return entries_.count(entity) > 0;
96 }
97
98 const Entry* getEntry(u32 entity) const {
99 auto it = entries_.find(entity);
100 return it != entries_.end() ? &it->second : nullptr;
101 }
102
103 bool isEmptyScissor(Entity entity) const {
104 if (entries_.empty()) return false;
105 const auto* entry = getEntry(entity.id());
106 return entry && (entry->flags & CMD_STATE_SCISSOR)
107 && (entry->scissor.w <= 0 || entry->scissor.h <= 0);
108 }
109
110 void clear() { entries_.clear(); }
111 bool empty() const { return entries_.empty(); }
112
113private:
114 std::unordered_map<u32, Entry> entries_;
115};
116
117} // namespace esengine
Core type definitions and utilities for ESEngine.
void setUIStencilMask(u32 entity, i32 depth)
Definition ClipState.hpp:36
bool isEmptyScissor(Entity entity) const
bool hasEntry(u32 entity) const
Definition ClipState.hpp:94
void setUIStencilTest(u32 entity, i32 depth)
Definition ClipState.hpp:42
bool reserveSpriteStencilReferences(i32 count)
Definition ClipState.hpp:48
const Entry * getEntry(u32 entity) const
Definition ClipState.hpp:98
void setStencilTestOutside(u32 entity, i32 refValue)
Drawn only where the mask is NOT — the hole, rather than the window.
Definition ClipState.hpp:76
void setStencilTest(u32 entity, i32 refValue)
Definition ClipState.hpp:67
void setScissor(u32 entity, i32 x, i32 y, i32 w, i32 h)
Definition ClipState.hpp:22
void applyTo(Entity entity, DrawCommand &cmd) const
Definition ClipState.hpp:84
void setStencilMask(u32 entity, i32 refValue)
Definition ClipState.hpp:28
bool isStencilMask(Entity entity) const
Definition ClipState.hpp:62
bool empty() const
static constexpr u16 CMD_STATE_UI_STENCIL
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
static constexpr u16 CMD_STATE_SCISSOR
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
static constexpr u16 CMD_STATE_STENCIL_OUT
static constexpr u16 CMD_STATE_STENCIL_WRITE
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
static constexpr u16 CMD_STATE_STENCIL_TEST
Entity identifier with packed index + generation.
Definition Types.hpp:135
constexpr u32 id() const
Returns the raw packed value for FFI serialization.
Definition Types.hpp:159