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

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

gpu: add full support for alpha test in GL and D3D.

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