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

Last change on this file since 2816 was 2816, checked in by lolbot, 7 years ago

fixed 542 files out of 2754:

  • removed 0 CR characters
  • removed 0 trailing whitespaces
  • replaced 0 tabs with spaces
  • fixed 542 svn:eol-style properties
  • Property svn:eol-style set to LF
File size: 4.3 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}
113
114void RenderContext::SetClearColor(vec4 color)
115{
116    if (!m_data->m_clear_color.HasChanged())
117        m_data->m_clear_color.TrackValue(g_renderer->GetClearColor());
118
119    g_renderer->SetClearColor(color);
120}
121
122void RenderContext::SetClearDepth(float depth)
123{
124    if (!m_data->m_clear_depth.HasChanged())
125        m_data->m_clear_depth.TrackValue(g_renderer->GetClearDepth());
126
127    g_renderer->SetClearDepth(depth);
128}
129
130void RenderContext::SetAlphaFunc(AlphaFunc func, float alpha)
131{
132    if (!m_data->m_alpha_func.HasChanged())
133        m_data->m_alpha_func.TrackValue(g_renderer->GetAlphaFunc());
134    if (!m_data->m_alpha_value.HasChanged())
135        m_data->m_alpha_value.TrackValue(g_renderer->GetAlphaValue());
136
137    g_renderer->SetAlphaFunc(func, alpha);
138}
139
140void RenderContext::SetBlendFunc(BlendFunc src, BlendFunc dst)
141{
142    if (!m_data->m_blend_src.HasChanged())
143        m_data->m_blend_src.TrackValue(g_renderer->GetBlendFuncSrc());
144    if (!m_data->m_blend_dst.HasChanged())
145        m_data->m_blend_dst.TrackValue(g_renderer->GetBlendFuncDst());
146
147    g_renderer->SetBlendFunc(src, dst);
148}
149
150void RenderContext::SetDepthFunc(DepthFunc func)
151{
152    if (!m_data->m_depth_func.HasChanged())
153        m_data->m_depth_func.TrackValue(g_renderer->GetDepthFunc());
154
155    g_renderer->SetDepthFunc(func);
156}
157
158void RenderContext::SetCullMode(CullMode mode)
159{
160    if (!m_data->m_cull_mode.HasChanged())
161        m_data->m_cull_mode.TrackValue(g_renderer->GetCullMode());
162
163    g_renderer->SetCullMode(mode);
164}
165
166void RenderContext::SetPolygonMode(PolygonMode mode)
167{
168    if (!m_data->m_polygon_mode.HasChanged())
169        m_data->m_polygon_mode.TrackValue(g_renderer->GetPolygonMode());
170
171    g_renderer->SetPolygonMode(mode);
172}
173
174} /* namespace lol */
175
Note: See TracBrowser for help on using the repository browser.