aboutsummaryrefslogtreecommitdiffstats
path: root/src/menu/clipping_helper.cpp
blob: a4cd6e9eb537d4a396e5b477152108ec9443ba40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "clipping_helper.hpp"

namespace usdx
{
	log4cpp::Category& ClippingHelper::log =
		log4cpp::Category::getInstance("usdx.menu.clipping_helper");

	Rectangle<int> ClippingHelper::makeRect(GLint box[4])
	{
		return Rectangle<int>(Point<int>(box[0], box[1]),
							  Point<int>(box[0] + box[2], box[1] + box[3]));
	}

	ClippingHelper::ClippingHelper(const Container *parent,
								   const Rectangle<int> &rect)
	{
		was_enabled = glIsEnabled(GL_SCISSOR_TEST);

		Rectangle<int> new_scissor_box(rect);

		// calculate window coordinates of rectangle
		if (parent) {
			int offset = parent->get_size().get_height() - rect.get_bottom();
			new_scissor_box.set_top(parent->get_window_coords().get_y() +
									offset);

			new_scissor_box.set_left(parent->get_window_coords().get_x() +
								 rect.get_left());
		}

		// calculate intersection of old clipping and requested clipping
		if (was_enabled) {
			glGetIntegerv(GL_SCISSOR_BOX, scissor_box);
			new_scissor_box = new_scissor_box.intersect(makeRect(scissor_box));
		}

		// setup clipping box
		glScissor(new_scissor_box.get_left(), new_scissor_box.get_top(),
				  new_scissor_box.get_width(), new_scissor_box.get_height());

		// enable clipping
		glEnable(GL_SCISSOR_TEST);
	}

	ClippingHelper::~ClippingHelper()
	{
		// restore old clipping settings
		glScissor(scissor_box[0], scissor_box[1], scissor_box[2],
				  scissor_box[3]);

		if (! was_enabled) {
			glDisable(GL_SCISSOR_TEST);
		}
	}
}