source: trunk/src/gpu/rendercontext.cpp @ 2828

Last change on this file since 2828 was 2828, checked in by benlitz, 7 years ago

gpu: getters in RenderContext class

  • Property svn:eol-style set to LF
File size: 5.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16
17#include "core.h"
18
19namespace lol
20{
21
22template<typename T> class TrackedState
23{
24public:
25    inline TrackedState()
26      : m_changed(false)
27    {}
28
29    inline void TrackValue(T const &value)
30    {
31        m_value = value;
32        m_changed = true;
33    }
34
35    inline bool HasChanged()
36    {
37        return m_changed;
38    }
39
40    inline T GetValue()
41    {
42        return m_value;
43    }
44
45private:
46    T m_value;
47    bool m_changed;
48};
49
50class RenderContextData
51{
52    friend class RenderContext;
53
54private:
55    TrackedState<ibox2> m_viewport;
56    TrackedState<vec4> m_clear_color;
57    TrackedState<float> m_clear_depth;
58    TrackedState<AlphaFunc> m_alpha_func;
59    TrackedState<float> m_alpha_value;
60    TrackedState<BlendFunc> m_blend_src, m_blend_dst;
61    TrackedState<DepthFunc> m_depth_func;
62    TrackedState<CullMode> m_cull_mode;
63    TrackedState<PolygonMode> m_polygon_mode;
64};
65
66/*
67 * Public RenderContext class
68 */
69
70RenderContext::RenderContext()
71  : m_data(new RenderContextData())
72{
73}
74
75RenderContext::~RenderContext()
76{
77    if (m_data->m_viewport.HasChanged())
78        g_renderer->SetViewport(m_data->m_viewport.GetValue());
79
80    if (m_data->m_clear_color.HasChanged())
81        g_renderer->SetClearColor(m_data->m_clear_color.GetValue());
82
83    if (m_data->m_clear_depth.HasChanged())
84        g_renderer->SetClearDepth(m_data->m_clear_depth.GetValue());
85
86    if (m_data->m_alpha_func.HasChanged())
87        g_renderer->SetAlphaFunc(m_data->m_alpha_func.GetValue(),
88                                 m_data->m_alpha_value.GetValue());
89
90    if (m_data->m_blend_src.HasChanged())
91        g_renderer->SetBlendFunc(m_data->m_blend_src.GetValue(),
92                                 m_data->m_blend_dst.GetValue());
93
94    if (m_data->m_depth_func.HasChanged())
95        g_renderer->SetDepthFunc(m_data->m_depth_func.GetValue());
96
97    if (m_data->m_cull_mode.HasChanged())
98        g_renderer->SetCullMode(m_data->m_cull_mode.GetValue());
99
100    if (m_data->m_polygon_mode.HasChanged())
101        g_renderer->SetPolygonMode(m_data->m_polygon_mode.GetValue());
102
103    delete m_data;
104}
105
106void RenderContext::SetViewport(ibox2 viewport)
107{
108    if (!m_data->m_viewport.HasChanged())
109        m_data->m_viewport.TrackValue(g_renderer->GetViewport());
110
111    g_renderer->SetViewport(viewport);
112}
113ibox2 RenderContext::GetViewport()
114{
115    return g_renderer->GetViewport();
116}
117
118void RenderContext::SetClearColor(vec4 color)
119{
120    if (!m_data->m_clear_color.HasChanged())
121        m_data->m_clear_color.TrackValue(g_renderer->GetClearColor());
122
123    g_renderer->SetClearColor(color);
124}
125
126vec4 RenderContext::GetClearColor()
127{
128    return g_renderer->GetClearColor();
129}
130
131void RenderContext::SetClearDepth(float depth)
132{
133    if (!m_data->m_clear_depth.HasChanged())
134        m_data->m_clear_depth.TrackValue(g_renderer->GetClearDepth());
135
136    g_renderer->SetClearDepth(depth);
137}
138
139float RenderContext::GetClearDepth()
140{
141    return g_renderer->GetClearDepth();
142}
143
144void RenderContext::SetAlphaFunc(AlphaFunc func, float alpha)
145{
146    if (!m_data->m_alpha_func.HasChanged())
147        m_data->m_alpha_func.TrackValue(g_renderer->GetAlphaFunc());
148    if (!m_data->m_alpha_value.HasChanged())
149        m_data->m_alpha_value.TrackValue(g_renderer->GetAlphaValue());
150
151    g_renderer->SetAlphaFunc(func, alpha);
152}
153
154AlphaFunc RenderContext::GetAlphaFunc()
155{
156    return g_renderer->GetAlphaFunc();
157}
158
159float RenderContext::GetAlphaValue()
160{
161    return g_renderer->GetAlphaValue();
162}
163
164void RenderContext::SetBlendFunc(BlendFunc src, BlendFunc dst)
165{
166    if (!m_data->m_blend_src.HasChanged())
167        m_data->m_blend_src.TrackValue(g_renderer->GetBlendFuncSrc());
168    if (!m_data->m_blend_dst.HasChanged())
169        m_data->m_blend_dst.TrackValue(g_renderer->GetBlendFuncDst());
170
171    g_renderer->SetBlendFunc(src, dst);
172}
173
174BlendFunc RenderContext::GetBlendFuncSrc()
175{
176    return g_renderer->GetBlendFuncSrc();
177}
178
179BlendFunc RenderContext::GetBlendFuncDst()
180{
181    return g_renderer->GetBlendFuncDst();
182}
183
184void RenderContext::SetDepthFunc(DepthFunc func)
185{
186    if (!m_data->m_depth_func.HasChanged())
187        m_data->m_depth_func.TrackValue(g_renderer->GetDepthFunc());
188
189    g_renderer->SetDepthFunc(func);
190}
191
192DepthFunc RenderContext::GetDepthFunc()
193{
194    return g_renderer->GetDepthFunc();
195}
196
197void RenderContext::SetCullMode(CullMode mode)
198{
199    if (!m_data->m_cull_mode.HasChanged())
200        m_data->m_cull_mode.TrackValue(g_renderer->GetCullMode());
201
202    g_renderer->SetCullMode(mode);
203}
204
205CullMode RenderContext::GetCullMode()
206{
207    return g_renderer->GetCullMode();
208}
209
210void RenderContext::SetPolygonMode(PolygonMode mode)
211{
212    if (!m_data->m_polygon_mode.HasChanged())
213        m_data->m_polygon_mode.TrackValue(g_renderer->GetPolygonMode());
214
215    g_renderer->SetPolygonMode(mode);
216}
217
218PolygonMode RenderContext::GetPolygonMode()
219{
220    return g_renderer->GetPolygonMode();
221}
222
223} /* namespace lol */
224
Note: See TracBrowser for help on using the repository browser.