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

Last change on this file since 2768 was 2768, checked in by sam, 8 years ago

gpu: framebuffers now use the g_render object to restore viewport data.

File size: 4.0 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    Scene *m_scene;
56
57    TrackedState<ibox2> m_viewport;
58    TrackedState<vec4> m_clear_color;
59    TrackedState<float> m_clear_depth;
60    TrackedState<AlphaFunc> m_alpha_func;
61    TrackedState<float> m_alpha_value;
62    TrackedState<BlendFunc> m_blend_src, m_blend_dst;
63    TrackedState<DepthFunc> m_depth_func;
64    TrackedState<CullMode> m_face_culling;
65};
66
67/*
68 * Public RenderContext class
69 */
70
71RenderContext::RenderContext()
72  : m_data(new RenderContextData())
73{
74    m_data->m_scene = Scene::GetDefault();
75}
76
77RenderContext::~RenderContext()
78{
79    if (m_data->m_viewport.HasChanged())
80        g_renderer->SetViewport(m_data->m_viewport.GetValue());
81
82    if (m_data->m_clear_color.HasChanged())
83        g_renderer->SetClearColor(m_data->m_clear_color.GetValue());
84
85    if (m_data->m_clear_depth.HasChanged())
86        g_renderer->SetClearDepth(m_data->m_clear_depth.GetValue());
87
88    if (m_data->m_alpha_func.HasChanged())
89        g_renderer->SetAlphaFunc(m_data->m_alpha_func.GetValue(),
90                                 m_data->m_alpha_value.GetValue());
91
92    if (m_data->m_blend_src.HasChanged())
93        g_renderer->SetBlendFunc(m_data->m_blend_src.GetValue(),
94                                 m_data->m_blend_dst.GetValue());
95
96    if (m_data->m_depth_func.HasChanged())
97        g_renderer->SetDepthFunc(m_data->m_depth_func.GetValue());
98
99    if (m_data->m_face_culling.HasChanged())
100        g_renderer->SetFaceCulling(m_data->m_face_culling.GetValue());
101
102    delete m_data;
103}
104
105void RenderContext::SetViewport(ibox2 viewport)
106{
107    if (!m_data->m_viewport.HasChanged())
108        m_data->m_viewport.TrackValue(g_renderer->GetViewport());
109
110    g_renderer->SetViewport(viewport);
111}
112
113void RenderContext::SetClearColor(vec4 color)
114{
115    if (!m_data->m_clear_color.HasChanged())
116        m_data->m_clear_color.TrackValue(g_renderer->GetClearColor());
117
118    g_renderer->SetClearColor(color);
119}
120
121void RenderContext::SetClearDepth(float depth)
122{
123    if (!m_data->m_clear_depth.HasChanged())
124        m_data->m_clear_depth.TrackValue(g_renderer->GetClearDepth());
125
126    g_renderer->SetClearDepth(depth);
127}
128
129void RenderContext::SetAlphaFunc(AlphaFunc func, float alpha)
130{
131    if (!m_data->m_alpha_func.HasChanged())
132        m_data->m_alpha_func.TrackValue(g_renderer->GetAlphaFunc());
133    if (!m_data->m_alpha_value.HasChanged())
134        m_data->m_alpha_value.TrackValue(g_renderer->GetAlphaValue());
135
136    g_renderer->SetAlphaFunc(func, alpha);
137}
138
139void RenderContext::SetBlendFunc(BlendFunc src, BlendFunc dst)
140{
141    if (!m_data->m_blend_src.HasChanged())
142        m_data->m_blend_src.TrackValue(g_renderer->GetBlendFuncSrc());
143    if (!m_data->m_blend_dst.HasChanged())
144        m_data->m_blend_dst.TrackValue(g_renderer->GetBlendFuncDst());
145
146    g_renderer->SetBlendFunc(src, dst);
147}
148
149void RenderContext::SetDepthFunc(DepthFunc func)
150{
151    if (!m_data->m_depth_func.HasChanged())
152        m_data->m_depth_func.TrackValue(g_renderer->GetDepthFunc());
153
154    g_renderer->SetDepthFunc(func);
155}
156
157void RenderContext::SetFaceCulling(CullMode mode)
158{
159    if (!m_data->m_face_culling.HasChanged())
160        m_data->m_face_culling.TrackValue(g_renderer->GetFaceCulling());
161
162    g_renderer->SetFaceCulling(mode);
163}
164
165} /* namespace lol */
166
Note: See TracBrowser for help on using the repository browser.