OSDN Git Service

correspondence multi login
[internetcity/prototype.git] / vc2010_client / vc2010_client / CClientUI.cpp
1 #include "CClientUI.h"
2
3 #include <iostream>
4
5 #include <Windows.h>
6 #include <cstdlib>
7 #include <iostream>
8 #include <cmath>
9 #include <time.h>
10 //#include <GL/glut.h>
11 #include "glut.h"
12
13 #pragma comment(lib, "ws2_32.lib")
14
15 #ifndef M_PI
16 #define M_PI 3.14159265357989
17 #endif // M_PI
18
19
20 // key status
21 long g_KeyStatus = 0;
22 const int KEY_UP_BIT    = 1 << 0;
23 const int KEY_DOWN_BIT  = 1 << 1;
24 const int KEY_RIGHT_BIT = 1 << 2;
25 const int KEY_LEFT_BIT  = 1 << 3;
26
27 CClientUI* CClientUI::sm_pUI = NULL;
28
29 ////////////////////////////////////////////////////////////////////////////////
30 // constructor
31 ////////////////////////////////////////////////////////////////////////////////
32 CClientUI::CClientUI()
33 {
34         sm_pUI = this;
35         m_isEnd = false;
36
37         m_CriticalSectionOfEvent = createCriticalSectionID();
38 }
39
40 ////////////////////////////////////////////////////////////////////////////////
41 // destructor
42 ////////////////////////////////////////////////////////////////////////////////
43 CClientUI::~CClientUI()
44 {
45         deleteCriticalSectionID(m_CriticalSectionOfEvent);
46         uninitialize();
47 }
48
49 ////////////////////////////////////////////////////////////////////////////////
50 // \89æ\96Ê\82ð\95`\89æ\82·\82é
51 ////////////////////////////////////////////////////////////////////////////////
52 void CClientUI::display()
53 {
54         // clear buffer
55         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
56
57         // initialize modelview matrix
58         glLoadIdentity();
59
60         // \8dÀ\95W\8cn\82Ì\92è\8b`
61         // x-y\95½\96Ê\82ð\92n\96Ê
62         // z = 0\82ð\95W\8d\820
63
64         // set look at matrix
65         // \8c»\8fó\82Í\8fã\82©\82ç\8e\8b\93_\81i\8fã\8bó5m\81j\82Æ\82·\82é
66         gluLookAt(
67                 0, 0, 5, // \8e\8b\93_\82Ì\88Ê\92u
68                 0, 0, 0,  // \8e\8b\93_\82Ì\96Ú\95W\92n\93_
69                 0, 1, 0   // \93ª\82Ì\8cü\82«
70         );
71
72         GLfloat position[] = {-1.0, 1.0, 1.0, 0.0};
73         glLightfv(GL_LIGHT0, GL_POSITION, position);
74
75         // \83A\83o\83^\81[\95`\89æ
76         // \8aÂ\8b«\90Ý\92è
77         GLfloat a_AmbientDiffuse[] = {1.0, 0.0, 0.0, 1.0};
78         GLfloat a_Specular[] = {1.0, 1.0, 1.0, 1.0};
79         GLfloat a_Shininess[] = {20.0};
80         glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, a_AmbientDiffuse);
81         glMaterialfv(GL_FRONT, GL_SPECULAR, a_Specular);
82         glMaterialfv(GL_FRONT, GL_SHININESS, a_Shininess);
83         // set avatar color to white
84         glColor3f(1.0, 1.0, 1.0);
85
86
87         const AvatarInformationList* a_pAvatarInformations = sm_pUI->m_pModel->getAvatarInformationList();
88         for(AvatarInformationList::const_iterator p = a_pAvatarInformations->begin(); p != a_pAvatarInformations->end(); p++){
89                 glPushMatrix();
90                 {
91                         glTranslated(p->m_Position[0], p->m_Position[1], p->m_Position[2]);
92                         glutSolidSphere(p->m_Radias, 10, 10);
93                         glBegin(GL_LINES);
94                         {
95                                 glVertex3d(0.0, 0.0, 0.0);
96                                 glVertex3d(p->m_Direction[0], p->m_Direction[1], p->m_Direction[2]);
97                         }
98                         glEnd();
99                 }
100                 glPopMatrix();
101         }
102
103         glutSwapBuffers();
104 }
105
106 ////////////////////////////////////////////////////////////////////////////////
107 // \8f\89\8aú\89»\82·\82é
108 ////////////////////////////////////////////////////////////////////////////////
109 void CClientUI::init()
110 {
111         // background color is black
112         glClearColor(0.0, 0.0, 0.0, 1.0);
113
114         // \8aÂ\8b«\95Ï\90\94\90Ý\92è
115         GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
116         GLfloat diffuse[] = {0.6, 0.6, 0.6, 1.0};
117         GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
118         GLfloat position[] = {-1.0, 1.0, 1.0, 0.0};
119
120         //glEnable(GL_LIGHTING);
121         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
122         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
123         glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
124         glLightfv(GL_LIGHT0, GL_POSITION, position);
125         //glEnable(GL_LIGHT0);
126
127         GLfloat material_ambient_diffuse[] = {1.0, 0.0, 0.0, 1.0};
128         GLfloat material_specular[] = {1.0, 1.0, 1.0, 1.0};
129         GLfloat material_shininess[] = {20.0};
130
131         glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material_ambient_diffuse);
132         glMaterialfv(GL_FRONT, GL_SPECULAR, material_specular);
133         glMaterialfv(GL_FRONT, GL_SHININESS, material_shininess);
134
135         glEnable(GL_DEPTH_TEST);
136         glEnable(GL_CULL_FACE);
137
138         //glShadeModel(GL_FLAT);
139
140         srand(static_cast<unsigned int>(time(NULL)));
141 }
142
143 ////////////////////////////////////////////////////////////////////////////////
144 // \88ê\94Ê\83L\81[\93ü\97Í
145 ////////////////////////////////////////////////////////////////////////////////
146 void CClientUI::keyboard(unsigned char in_Key, int in_x, int in_y)
147 {
148         switch(in_Key){
149         case 'c':
150                 // connect
151                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
152                 sm_pUI->m_Events.push_back("CONNECT");
153                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
154                 break;
155
156         case 'd':
157                 // disconnect
158                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
159                 sm_pUI->m_Events.push_back("DISCONNECT");
160                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
161                 break;
162
163         case 'i':
164                 // login
165                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
166                 sm_pUI->m_Events.push_back("LOGIN");
167                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
168                 break;
169
170         case 'o':
171                 // logout
172                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
173                 sm_pUI->m_Events.push_back("LOGOUT");
174                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
175                 break;
176
177         case 'p':
178                 // \8dÀ\95W\8eæ\93¾
179                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
180                 sm_pUI->m_Events.push_back("POSITION");
181                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
182                 break;
183
184
185         case 'q':
186         case 'Q':
187         case '\033':  /* '\033' \82Í ESC \82Ì ASCII \83R\81[\83h */
188                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
189                 sm_pUI->m_Events.push_back("END");
190                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
191                 //exit(0);
192         default:
193                 break;
194         }
195 }
196
197 ////////////////////////////////////////////////////////////////////////////////
198 // down special key
199 ////////////////////////////////////////////////////////////////////////////////
200 void CClientUI::downSpecialKey(int in_Key, int in_x, int in_y)
201 {
202         switch(in_Key){
203         case GLUT_KEY_UP:
204                 g_KeyStatus |= KEY_UP_BIT;
205                 break;
206         case GLUT_KEY_DOWN:
207                 g_KeyStatus |= KEY_DOWN_BIT;
208                 break;
209         case GLUT_KEY_RIGHT:
210                 g_KeyStatus |= KEY_RIGHT_BIT;
211                 break;
212         case GLUT_KEY_LEFT:
213                 g_KeyStatus |= KEY_LEFT_BIT;
214                 break;
215         }
216 }
217
218 ////////////////////////////////////////////////////////////////////////////////
219 // up special key
220 ////////////////////////////////////////////////////////////////////////////////
221 void CClientUI::upSpecialKey(int in_Key, int in_x, int in_y)
222 {
223         switch(in_Key){
224         case GLUT_KEY_UP:
225                 g_KeyStatus &= ~KEY_UP_BIT;
226                 break;
227         case GLUT_KEY_DOWN:
228                 g_KeyStatus &= ~KEY_DOWN_BIT;
229                 break;
230         case GLUT_KEY_RIGHT:
231                 g_KeyStatus &= ~KEY_RIGHT_BIT;
232                 break;
233         case GLUT_KEY_LEFT:
234                 g_KeyStatus &= ~KEY_LEFT_BIT;
235                 break;
236         }
237 }
238
239 ////////////////////////////////////////////////////////////////////////////////
240 // timer
241 ////////////////////////////////////////////////////////////////////////////////
242 void CClientUI::timer(int value)
243 {
244         // 1\95b\8aÔ\82É60\89ñ\8cÄ\82Ñ\8fo\82³\82ê\82é
245         glutTimerFunc(1000 / 60, timer, 0);
246
247         // \83L\81[\93ü\97Í\82Ì\8fó\91Ô\82É\82æ\82è\83A\83o\83^\81[\81ii = 0\82Ì\82Ý\81j\82ð\93®\82©\82·
248         // \91O\90i
249         if((g_KeyStatus & KEY_UP_BIT) && !(g_KeyStatus & KEY_DOWN_BIT)){
250                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
251                 sm_pUI->m_Events.push_back("FRONT");
252                 sm_pUI->m_Events.push_back("POSITION");
253                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
254         }
255         // \8cã\91Þ
256         else if(!(g_KeyStatus & KEY_UP_BIT) && (g_KeyStatus & KEY_DOWN_BIT)){
257                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
258                 sm_pUI->m_Events.push_back("BACK");
259                 sm_pUI->m_Events.push_back("POSITION");
260                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
261         }
262         // \89E\90ù\89ñ
263         if((g_KeyStatus & KEY_RIGHT_BIT) && !(g_KeyStatus & KEY_LEFT_BIT)){
264                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
265                 sm_pUI->m_Events.push_back("TURN_RIGHT");
266                 sm_pUI->m_Events.push_back("POSITION");
267                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
268         }
269         // \8d\90ù\89ñ
270         else if(!(g_KeyStatus & KEY_RIGHT_BIT) && (g_KeyStatus & KEY_LEFT_BIT)){
271                 sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
272                 sm_pUI->m_Events.push_back("TURN_LEFT");
273                 sm_pUI->m_Events.push_back("POSITION");
274                 sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
275         }
276
277         sm_pUI->enterCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
278         sm_pUI->m_Events.push_back("POSITION");
279         sm_pUI->leaveCriticalSectionID(sm_pUI->m_CriticalSectionOfEvent);
280
281
282         glutPostRedisplay();
283 }
284
285 ////////////////////////////////////////////////////////////////////////////////
286 // \83T\83C\83Y\95Ï\8dX
287 ////////////////////////////////////////////////////////////////////////////////
288 void CClientUI::reshape(int w, int h)
289 {
290         glViewport(0, 0, (GLsizei) w, (GLsizei) h);
291         glMatrixMode(GL_PROJECTION);
292         glLoadIdentity();
293         if(w <= h){
294                 glFrustum(-1.0, 1.0, -1.0 * (GLfloat)h / (GLfloat)w, 1.0 * (GLfloat)h / (GLfloat)w, 1.0, 30.0);
295         }
296         else{
297                 glFrustum(-1.0 * (GLfloat)w / (GLfloat)h, 1.0 * (GLfloat)w / (GLfloat)h, -1.0, 1.0, 1.0, 30.0);
298         }
299         glMatrixMode(GL_MODELVIEW);
300 }
301
302 ////////////////////////////////////////////////////////////////////////////////
303 // start input
304 ////////////////////////////////////////////////////////////////////////////////
305 bool CClientUI::initialize()
306 {
307         return true;
308 }
309
310 ////////////////////////////////////////////////////////////////////////////////
311 // stop input
312 ////////////////////////////////////////////////////////////////////////////////
313 bool CClientUI::uninitialize()
314 {
315         m_isEnd = true;
316         return true;
317 }
318
319 ////////////////////////////////////////////////////////////////////////////////
320 // thread loop
321 ////////////////////////////////////////////////////////////////////////////////
322 void CClientUI::run()
323 {
324         glutInit(m_pArgc, m_ppArgv);
325         glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
326         glutInitWindowSize(512, 512);
327         glutCreateWindow("internetcity");
328         glutDisplayFunc(display);
329         glutKeyboardFunc(keyboard);
330         glutSpecialFunc(downSpecialKey);
331         glutSpecialUpFunc(upSpecialKey);
332         glutTimerFunc(1000 / 60, timer, 0);
333         glutReshapeFunc(reshape);
334         init();
335         glutMainLoop();
336 }
337
338