Merge branch 'lua_versions' into main/rendor-staging
[ryzomcore.git] / snowballs2 / client / src / compass.cpp
blobbedcbc4bb1aeb0d2b3bebbc8dd7d22b58cad9daf
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013-2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 // Includes
24 #include <nel/misc/types_nl.h>
25 #include <nel/misc/vector.h>
26 #include <nel/misc/matrix.h>
27 #include <nel/misc/config_file.h>
29 #include <nel/3d/u_material.h>
30 #include <nel/3d/u_camera.h>
31 #include <nel/3d/u_driver.h>
32 #include <nel/3d/u_text_context.h>
33 #include <nel/3d/u_texture.h>
34 #include <nel/3d/stereo_hmd.h>
36 #include "mouse_listener.h"
37 #include "camera.h"
38 #include "snowballs_client.h"
39 #include "entities.h"
42 // Namespaces
45 using namespace NLMISC;
46 using namespace NL3D;
47 using namespace std;
49 namespace SBCLIENT {
52 // Variables
55 static NL3D::UMaterial CompassMaterial = NULL;
57 // These variables are automatically set with the config file
59 static float CompassPosX, CompassPosY, CompassRadius;
60 static CRGBA CompassColor;
63 // Functions
66 void cbUpdateCompass (CConfigFile::CVar &var)
68 if (var.Name == "CompassPosX") CompassPosX = var.asFloat ();
69 else if (var.Name == "CompassPosY") CompassPosY = var.asFloat ();
70 else if (var.Name == "CompassRadius") CompassRadius = var.asFloat ();
71 else if (var.Name == "CompassColor")
73 CompassColor.set(var.asInt(0), var.asInt(1), var.asInt(2), var.asInt(3));
74 CompassMaterial.setColor(CompassColor);
76 else nlwarning ("Unknown variable update %s", var.Name.c_str());
79 void initCompass ()
81 CompassMaterial = Driver->createMaterial ();
82 CompassMaterial.initUnlit ();
83 CompassMaterial.setBlendFunc (UMaterial::srcalpha, UMaterial::invsrcalpha);
84 CompassMaterial.setBlend(true);
86 ConfigFile->setCallback ("CompassPosX", cbUpdateCompass);
87 ConfigFile->setCallback ("CompassPosY", cbUpdateCompass);
88 ConfigFile->setCallback ("CompassRadius", cbUpdateCompass);
89 ConfigFile->setCallback ("CompassColor", cbUpdateCompass);
91 cbUpdateCompass (ConfigFile->getVar ("CompassPosX"));
92 cbUpdateCompass (ConfigFile->getVar ("CompassPosY"));
93 cbUpdateCompass (ConfigFile->getVar ("CompassRadius"));
94 cbUpdateCompass (ConfigFile->getVar ("CompassColor"));
97 void updateCompass ()
99 float x = CompassPosX;
100 float y = CompassPosY;
101 float radius = CompassRadius;
103 // tri
104 CTriangle tri;
105 tri.V0.set (-radius, 0, 0);
106 tri.V1.set (radius, 0, 0);
107 tri.V2.set (0, 3*radius, 0);
109 CQuad quad;
110 // quad
111 quad.V0.set (-radius, -radius, 0);
112 quad.V1.set ( radius, -radius, 0);
113 quad.V2.set ( radius, radius, 0);
114 quad.V3.set (-radius, radius, 0);
116 Driver->setMatrixMode2D43();
118 CMatrix mtx;
120 // up
121 mtx.identity();
122 mtx.translate(CVector(x,y,0));
123 mtx.rotateZ(MouseListener->getOrientation() - (float)Pi/2);
124 mtx.translate(CVector(0,radius,0));
125 Driver->setModelMatrix (mtx);
126 Driver->drawTriangle (tri, CompassMaterial);
128 // down
129 mtx.identity();
130 mtx.translate(CVector(x,y,0));
131 mtx.rotateZ(MouseListener->getOrientation() + (float)Pi/2);
132 mtx.translate(CVector(0,radius,0));
133 Driver->setModelMatrix (mtx);
134 Driver->drawTriangle (tri, CompassMaterial);
136 // left
137 mtx.identity();
138 mtx.translate(CVector(x,y,0));
139 mtx.rotateZ(MouseListener->getOrientation());
140 mtx.translate(CVector(0,radius,0));
141 Driver->setModelMatrix (mtx);
142 Driver->drawTriangle (tri, CompassMaterial);
144 // right
145 mtx.identity();
146 mtx.translate(CVector(x,y,0));
147 mtx.rotateZ(MouseListener->getOrientation() - (float)Pi);
148 mtx.translate(CVector(0,radius,0));
149 Driver->setModelMatrix (mtx);
150 Driver->drawTriangle (tri, CompassMaterial);
152 // center
153 mtx.identity();
154 mtx.translate(CVector(x,y,0));
155 mtx.rotateZ(MouseListener->getOrientation());
156 Driver->setModelMatrix (mtx);
157 Driver->drawQuad (quad, CompassMaterial);
159 x *= 3.0/4.0f;
161 // Print position
162 TextContext->setHotSpot(UTextContext::MiddleTop);
163 TextContext->setColor(CompassColor);
164 TextContext->setFontSize(14);
165 if (Self != NULL)
166 TextContext->printfAt(x, y-4.0f*radius, "%.2f %.2f %.2f", Self->Position.x, Self->Position.y, Self->Position.z);
167 else
168 TextContext->printfAt(x, y-4.0f*radius, "%.2f %.2f %.2f", MouseListener->getPosition().x, MouseListener->getPosition().y, MouseListener->getPosition().z);
171 void releaseCompass ()
173 ConfigFile->setCallback("CompassPosX", NULL);
174 ConfigFile->setCallback("CompassPosY", NULL);
175 ConfigFile->setCallback("CompassRadius", NULL);
176 ConfigFile->setCallback("CompassColor", NULL);
178 Driver->deleteMaterial (CompassMaterial);
179 CompassMaterial = NULL;
182 } /* namespace SBCLIENT */
184 /* end of file */