3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file gameengine/BlenderRoutines/KX_BlenderCanvas.cpp
34 #include "KX_BlenderCanvas.h"
35 #include "DNA_screen_types.h"
39 KX_BlenderCanvas::KX_BlenderCanvas(struct wmWindow *win, RAS_Rect &rect, struct ARegion *ar) :
43 // area boundaries needed for mouse coordinates in Letterbox framing mode
44 m_area_left = ar->winrct.xmin;
45 m_area_top = ar->winrct.ymax;
48 KX_BlenderCanvas::~KX_BlenderCanvas()
52 void KX_BlenderCanvas::Init()
54 glDepthFunc(GL_LEQUAL);
58 void KX_BlenderCanvas::SwapBuffers()
60 BL_SwapBuffers(m_win);
63 void KX_BlenderCanvas::BeginFrame()
65 glEnable(GL_DEPTH_TEST);
66 glDepthFunc(GL_LEQUAL);
71 void KX_BlenderCanvas::EndFrame()
73 // this is needed, else blender distorts a lot
75 glPushAttrib(GL_ALL_ATTRIB_BITS);
82 void KX_BlenderCanvas::ClearColor(float r,float g,float b,float a)
84 glClearColor(r,g,b,a);
89 void KX_BlenderCanvas::ClearBuffer(int type)
93 if (type & RAS_ICanvas::COLOR_BUFFER )
94 ogltype |= GL_COLOR_BUFFER_BIT;
96 if (type & RAS_ICanvas::DEPTH_BUFFER )
97 ogltype |= GL_DEPTH_BUFFER_BIT;
101 int KX_BlenderCanvas::GetWidth(
103 return m_frame_rect.GetWidth();
106 int KX_BlenderCanvas::GetHeight(
108 return m_frame_rect.GetHeight();
111 int KX_BlenderCanvas::GetMouseX(int x)
113 float left = GetWindowArea().GetLeft();
114 return float(x - (left - m_area_left));
117 int KX_BlenderCanvas::GetMouseY(int y)
119 float top = GetWindowArea().GetTop();
120 return float(y - (m_area_top - top));
123 float KX_BlenderCanvas::GetMouseNormalizedX(int x)
125 int can_x = GetMouseX(x);
126 return float(can_x)/this->GetWidth();
129 float KX_BlenderCanvas::GetMouseNormalizedY(int y)
131 int can_y = GetMouseY(y);
132 return float(can_y)/this->GetHeight();
148 /* x1 and y1 are the min pixel coordinate (e.g. 0)
149 x2 and y2 are the max pixel coordinate
150 the width,height is calculated including both pixels
151 therefore: max - min + 1
153 int vp_width = (x2 - x1) + 1;
154 int vp_height = (y2 - y1) + 1;
155 int minx = m_frame_rect.GetLeft();
156 int miny = m_frame_rect.GetBottom();
158 m_area_rect.SetLeft(minx + x1);
159 m_area_rect.SetBottom(miny + y1);
160 m_area_rect.SetRight(minx + x2);
161 m_area_rect.SetTop(miny + y2);
163 glViewport(minx + x1, miny + y1, vp_width, vp_height);
164 glScissor(minx + x1, miny + y1, vp_width, vp_height);
168 void KX_BlenderCanvas::SetMouseState(RAS_MouseState mousestate)
170 m_mousestate = mousestate;
174 case MOUSE_INVISIBLE:
186 BL_NormalMouse(m_win);
197 // (0,0) is top left, (width,height) is bottom right
198 void KX_BlenderCanvas::SetMousePosition(int x,int y)
200 int winX = m_frame_rect.GetLeft();
201 int winY = m_frame_rect.GetBottom();
202 int winH = m_frame_rect.GetHeight();
204 BL_warp_pointer(m_win, winX + x, winY + (winH-y));
209 void KX_BlenderCanvas::MakeScreenShot(const char* filename)
211 ScrArea area_dummy= {0};
212 area_dummy.totrct.xmin = m_frame_rect.GetLeft();
213 area_dummy.totrct.xmax = m_frame_rect.GetRight();
214 area_dummy.totrct.ymin = m_frame_rect.GetBottom();
215 area_dummy.totrct.ymax = m_frame_rect.GetTop();
217 BL_MakeScreenShot(&area_dummy, filename);