Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/RailMap.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Sun Aug 15 01:53:13 2010 UTC (13 years, 9 months ago) by okadu
File MIME type: text/x-c++src
File size: 6231 byte(s)


1 #include "stdafx.h"
2 #include "RailMap.h"
3 #include "CVertexDump.h"
4 #include "CConfigMode.h"
5 #include "CSceneryMode.h"
6 #include "CSurfacePlugin.h"
7 #include "CScene.h"
8
9 // 内部グローバル
10 bool g_MapDrawNeeded; // 地図描画が必要かどうか
11 float g_RailMapScale = 0.5f; // 2D マップスケール
12 VEC3 g_RailMapCenter = V3ZERO; // 2D マップ 3D 基準位置
13 VEC2 g_RailMapOffset; // 2D マップ描画中心位置
14 VEC2 g_RailMapMove; // 2D マップ描画移動距離
15 bool g_RotateMap = false; // 2D マップ回転
16 VEC2 g_MapRotateRight; // 2D マップ回転 right
17 VEC2 g_MapRotateDir; // 2D マップ回転 dir
18 CLineDumpTL *g_LineDumpTL = NULL; // 2D マップダンパ
19 CLineDumpTL *g_LineDumpTL2 = NULL; // 2D マップダンパ (影用)
20
21 struct RailMapTextData{
22 string m_Text;
23 int m_PosX, m_PosY;
24 int m_Width;
25 D3DCOLOR m_Color;
26 RailMapTextData(char *text, VEC2 pos, D3DCOLOR color){
27 m_Text = text;
28 m_PosX = Round(pos.x);
29 m_PosY = Round(pos.y);
30 m_Width = g_StrTex->DrawString(text, 0xff000000)->GetWidth();
31 m_Color = color;
32 }
33 void Draw(){
34 g_StrTex->RenderLeft(m_PosX, m_PosY, m_Color, 0xff000000, m_Text.c_str());
35 }
36 int Right(){ return m_PosX+m_Width; }
37 int Bottom(){ return m_PosY+FONT_HEIGHT; }
38 bool HitTest(RailMapTextData *rhs){
39 return m_PosX<rhs->Right() && rhs->m_PosX<Right()
40 && m_PosY<rhs->Bottom() && rhs->m_PosY<Bottom();
41 }
42 };
43 typedef list<RailMapTextData> RailMapTextDataList;
44 RailMapTextDataList g_RailMapTextData;
45
46 /*
47 * 2D マップ初期化
48 */
49 void InitRailMap(){
50 if(g_ConfigMode->GetShowMap() && CSceneryMode::GetPhotoMode()!=2){
51 g_MapDrawNeeded = true;
52 DELETE_V(g_LineDumpTL);
53 DELETE_V(g_LineDumpTL2);
54 g_LineDumpTL = new CLineDumpTL(1024);
55 g_LineDumpTL2 = new CLineDumpTL(1024);
56 g_RailMapTextData.clear();
57 if(g_RotateMap){
58 g_RailMapCenter = CCamera::GetCurrentCamera()->GetFocus();
59 VEC3 vdir = GetVDir();
60 V2Norm(&g_MapRotateDir, &VEC2(vdir.x, -vdir.z));
61 g_MapRotateRight = VEC2(-g_MapRotateDir.y, g_MapRotateDir.x);
62 }else{
63 g_RailMapCenter = V3ZERO;
64 }
65 g_RailMapOffset = VEC2(g_DispWidth, g_DispHeight)*0.5f+g_RailMapMove;
66 CSurfacePlugin *surface = g_Scene->GetSurface();
67 float size_x = surface->GetSizeX(), size_z = surface->GetSizeZ();
68 VEC3 border1(0.5f*size_x, 0.0f, 0.5f*size_z), border2(0.5f*size_x, 0.0f, -0.5f*size_z);
69 RailMapLine(border1, 0x80ffffff, border2, 0x80ffffff);
70 RailMapLine(border2, 0x80ffffff, -border1, 0x80ffffff);
71 RailMapLine(-border1, 0x80ffffff, -border2, 0x80ffffff);
72 RailMapLine(-border2, 0x80ffffff, border1, 0x80ffffff);
73 }else{
74 g_MapDrawNeeded = false;
75 }
76 }
77
78 /*
79 * 2D マップ直線ダンプ
80 */
81 void DumpMapLine(VEC2 p1, D3DCOLOR c1, VEC2 p2, D3DCOLOR c2, bool shadow){
82 if(!g_MapDrawNeeded) return;
83 g_LineDumpTL->Add(p1, c1, p2, c2);
84 if(shadow){
85 // g_LineDumpTL2->Add(p1+VEC2(-1, -1), c1&0xff000000, p2+VEC2(-1, -1), c2&0xff000000);
86 // g_LineDumpTL2->Add(p1+VEC2(0, -1), c1&0xff000000, p2+VEC2(0, -1), c2&0xff000000);
87 // g_LineDumpTL2->Add(p1+VEC2(1, -1), c1&0xff000000, p2+VEC2(1, -1), c2&0xff000000);
88 // g_LineDumpTL2->Add(p1+VEC2(-1, 0), c1&0xff000000, p2+VEC2(-1, 0), c2&0xff000000);
89 // g_LineDumpTL2->Add(p1+VEC2(1, 0), c1&0xff000000, p2+VEC2(1, 0), c2&0xff000000);
90 // g_LineDumpTL2->Add(p1+VEC2(-1, 1), c1&0xff000000, p2+VEC2(-1, 1), c2&0xff000000);
91 // g_LineDumpTL2->Add(p1+VEC2(0, 1), c1&0xff000000, p2+VEC2(0, 1), c2&0xff000000);
92 g_LineDumpTL2->Add(p1+VEC2(1, 1), c1&0xff000000, p2+VEC2(1, 1), c2&0xff000000);
93 }
94 }
95
96 /*
97 * 2D マップ座標変換
98 */
99 VEC2 TransformMapPos(VEC3 p){
100 VEC3 t = (p-g_RailMapCenter)*g_RailMapScale;
101 VEC2 t2(t.x, -t.z);
102 if(g_RotateMap) t2 = VEC2(V2Dot(&t2, &g_MapRotateRight), -V2Dot(&t2, &g_MapRotateDir));
103 return t2+g_RailMapOffset;
104 }
105
106 /*
107 * 3D マップ直線ダンプ
108 */
109 void RailMapLine(VEC3 p1, D3DCOLOR c1, VEC3 p2, D3DCOLOR c2, bool shadow, bool bold){
110 if(!g_MapDrawNeeded) return;
111 VEC2 sc1 = TransformMapPos(p1);
112 VEC2 sc2 = TransformMapPos(p2);
113 if(bold){
114 VEC2 dir = sc2-sc1;
115 V2Norm(&dir, &dir);
116 VEC2 p = VEC2(-dir.y, dir.x)*0.5f;
117 DumpMapLine(sc1+p, c1, sc2+p, c2, shadow);
118 DumpMapLine(sc1, c1, sc2, c2, shadow);
119 DumpMapLine(sc1-p, c1, sc2-p, c2, shadow);
120 }else{
121 DumpMapLine(sc1, c1, sc2, c2, shadow);
122 }
123 }
124
125 void RailMapText(VEC3 pos, char *text, D3DCOLOR color){
126 if(!g_MapDrawNeeded) return;
127 RailMapTextData data(text, TransformMapPos(pos), color);
128 int iy = data.m_PosY;
129 while(true){
130 bool hit = false;
131 RailMapTextDataList::iterator mtl = g_RailMapTextData.begin();
132 for(; mtl!=g_RailMapTextData.end(); ++mtl){
133 if(mtl->HitTest(&data)){
134 hit = true;
135 if(iy<data.m_PosY || iy==data.m_PosY && iy>mtl->m_PosY)
136 data.m_PosY = mtl->Bottom();
137 else data.m_PosY = mtl->m_PosY-FONT_HEIGHT;
138 break;
139 }
140 }
141 if(!hit) break;
142 }
143 g_RailMapTextData.push_back(data);
144 if(iy<data.m_PosY) DumpMapLine(VEC2(data.m_PosX, iy), 0xffff0000,
145 VEC2(data.m_PosX, data.m_PosY), 0xffff0000, false);
146 else if(data.Bottom()<iy) DumpMapLine(VEC2(data.m_PosX, iy), 0xffff0000,
147 VEC2(data.m_PosX, data.Bottom()), 0xffff0000, false);
148 }
149
150 /*
151 * 2D マップレンダリング
152 */
153 void RenderRailMap(){
154 if(!g_MapDrawNeeded) return;
155 devSetLighting(FALSE);
156 CCamera *camera = CCamera::GetCurrentCamera();
157 float fov = camera->GetFieldOfView();
158 VEC3 vpos = GetVPos();
159 VEC3 vdir = GetVDir(), vright = GetVRight();
160 V3Norm(&vdir, &vdir);
161 V3Norm(&vright, &vright);
162 float sinfov = sinf(fov*0.5f), cosfov = cos(fov*0.5f);
163 const float VLLEN = 100.0f, VPCRS = 10.0f;
164 VEC2 fp = TransformMapPos(camera->GetFocus());
165 DumpMapLine(fp, 0xff80c0ff, fp-VEC2(VPCRS, 0), 0x0080c0ff);
166 DumpMapLine(fp, 0xff80c0ff, fp-VEC2(0, VPCRS), 0x0080c0ff);
167 DumpMapLine(fp, 0xff80c0ff, fp+VEC2(VPCRS, 0), 0x0080c0ff);
168 DumpMapLine(fp, 0xff80c0ff, fp+VEC2(0, VPCRS), 0x0080c0ff);
169 RailMapLine(vpos, 0xff80c0ff, vpos+VLLEN*(cosfov*vdir+sinfov*vright), 0x0080c0ff);
170 RailMapLine(vpos, 0xff80c0ff, vpos+VLLEN*(cosfov*vdir-sinfov*vright), 0x0080c0ff);
171 g_LineDumpTL2->Render(true);
172 g_LineDumpTL->Render(true);
173 RailMapTextDataList::iterator mtl = g_RailMapTextData.begin();
174 for(; mtl!=g_RailMapTextData.end(); ++mtl) mtl->Draw();
175 devSetTexture(0, NULL);
176 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26