OSDN Git Service

GameSurfaceSDL::Draw() 関連のリファクタリング / Code refactoring for GameSurfaceSDL::Draw().
[deeangband/Deeangband-new.git] / Deeangband / GameSurfaceSDL.cpp
1 /*!
2 * @file GameSurfaceSDL.cpp
3 * @brief \83Q\81[\83\80\82Ì\83\81\83C\83\93\83C\83\93\83^\81[\83t\83F\83C\83X(SDL2.0\8eÀ\91\95)
4 * @date 2014/02/19
5 * @author Deskull
6 * 2014 Sikabane Works.
7 */
8
9 #include <vector>
10 #include <string>
11 #include "stdafx.h"
12 #include "GameSurfaceSDL.h"
13
14 #include "GameEngine.h"
15
16 #include "Creature.h"
17 #include "Field.h"
18
19 namespace Deeangband
20 {
21
22         std::string toUTF8(LPCSTR str)
23         {
24                 const int cchWideChar = ::MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
25                 std::vector<WCHAR> lpw(cchWideChar);
26
27                 const int nUnicodeCount = ::MultiByteToWideChar(CP_ACP, 0, str, -1, &lpw[0], cchWideChar);
28                 if(nUnicodeCount <= 0)
29                 {
30                         return "";
31                 }
32
33                 const int cchMultiByte = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, NULL, 0, NULL, NULL);
34                 std::vector<CHAR> lpa(cchMultiByte);
35
36                 const int nMultiCount = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, &lpa[0], cchMultiByte, NULL, NULL);
37                 if(nMultiCount <= 0)
38                 {
39                         return "";
40                 }
41                 return std::string(&lpa[0]);
42         }
43
44         GameSurfaceSDL::GameSurfaceSDL(GameWorld *gameWorld) : GameSurface(gameWorld)
45         {
46                 if(SDL_Init(SDL_INIT_VIDEO) < 0) return;
47                 window = SDL_CreateWindow(GAME_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE);
48                 SDL_GetWindowSurface(window);
49                 renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
50                 if(!window) return;
51
52                 if(TTF_Init() == -1) return;
53                 if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) return;
54
55                 initInterfaces(gameWorld);
56
57                 viewCreaturePtr = NULL;
58                 viewFieldPtr = NULL;
59
60                 windowSurface = SDL_GetWindowSurface(window);
61
62                 gameMessageTexture = NULL;
63                 systemMessageTexture = NULL;
64
65                 frameTime = 0;
66                 frameCount = 0;
67                 framesPerSecond = 0;
68                 frameDeltaTimeLast = frameTimeLast = SDL_GetTicks();
69
70                 this->rectTitleDst.Set(10, 10, 266, 266);
71                 this->rectTitleSrc.Set(0, 0, 256, 256);
72                 this->rectFpsDst.Set(0, 0, 50, 20);
73                 this->rectFpsSrc.Set(0, 0, 50, 20);
74
75                 return;
76         }
77
78         GameSurfaceSDL::~GameSurfaceSDL(void)
79         {
80                 IMG_Quit();
81                 TTF_Quit();
82                 SDL_Quit();
83                 return;
84         }
85
86         void GameSurfaceSDL::fpsProcess()
87         {
88                 SDL_Surface *surface;
89                 char fpsBuf[12];
90         int getTicks;
91         int i;
92
93         frameCount++;
94                 getTicks = SDL_GetTicks();
95                 frameDeltaTime = getTicks - frameDeltaTimeLast;
96                 frameDeltaTimeLast = getTicks;
97         if (frameCount >= FPS_UPDATE)
98                 {
99                         frameCount = 0;
100                         frameTime = getTicks - frameTimeLast;
101                         frameTimeLast = getTicks;
102                         framesPerSecond = 1000.f * FPS_UPDATE / frameTime;
103                         sprintf_s(fpsBuf, 8, "fps:%d", framesPerSecond); 
104                         surface = TTF_RenderUTF8_Blended(fontSmall, fpsBuf, color);
105                         fpsTexture = SDL_CreateTextureFromSurface(renderer, surface);
106                         SDL_FreeSurface(surface);
107                 }
108
109                 SDL_RenderCopy(renderer, fpsTexture, &rectFpsSrc, &rectFpsDst); 
110
111         }
112
113         void GameSurfaceSDL::pushAnyKey(const CREATURE_IT& subjectCreatureIt)
114         {
115                 SDL_Event event;
116                 do 
117                 {
118                         this->Draw(subjectCreatureIt);
119                         SDL_PollEvent(&event);
120                 } while(event.type != SDL_KEYDOWN);
121         }
122
123         void GameSurfaceSDL::pushAnyKey(void)
124         {
125                 SDL_Event event;
126                 do 
127                 {
128                         SDL_Rect rect = {0, 0, 0, 0};
129                         this->drawSystemMessage();
130                         SDL_UpdateWindowSurfaceRects(window, &rect, 1);
131                         SDL_UpdateWindowSurface(window);
132
133                         SDL_PollEvent(&event);
134                 } while(event.type != SDL_KEYDOWN);
135         }
136
137
138         void GameSurfaceSDL::initInterfaces(GameWorld *gameWorld)
139         {
140                 std::map<TAG, boost::shared_ptr<Floor>>::iterator floorIt;
141                 std::map<TAG, boost::shared_ptr<Species>>::iterator speciesIt;
142                 SDL_Surface *surface, *tempSurface, *symbolSurface;
143
144                 //! @note TTF\83t\83H\83\93\83g\82Ì\93Ç\82Ý\8d\9e\82Ý
145
146                 font = TTF_OpenFont("ttf\\ipam.ttf", 14);
147                 fontSmall = TTF_OpenFont("ttf\\ipag.ttf", 9);
148                 src.x = 0;
149                 src.y = 0;
150                 src.w = 300;
151                 src.h = 200;
152                 title.x = 0;
153                 title.y = 0;
154                 title.w = 256;
155                 title.h = 256;
156                 color.r = 255;
157                 color.g = 223;
158                 color.b = 200;
159                 color.a = 255;
160
161                 if(!font || !fontSmall)
162                 {       
163                         OutputDebugString(L"Failed: loading .ttf files.\n");
164                         exit(1);
165                 }
166
167                 //! @note \83V\83X\83e\83\80\83O\83\89\83t\83B\83b\83N\82Ì\93Ç\82Ý\8d\9e\82Ý
168                 rwop = SDL_RWFromFile("img\\Title.png", "rb");
169                 error = IMG_GetError();
170                 tempSurface = IMG_LoadPNG_RW(rwop);
171                 titleTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
172                 SDL_FreeSurface(tempSurface);
173
174                 //! @note \96¢\92m\92n\8c`\83O\83\89\83t\83B\83b\83N\82Ì\93Ç\82Ý\8d\9e\82Ý
175                 rwop = SDL_RWFromFile("img\\UnknownField.png", "rb");
176                 error = IMG_GetError();
177                 tempSurface = IMG_LoadPNG_RW(rwop);
178                 unknownFieldTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
179                 SDL_FreeSurface(tempSurface);
180
181                 //! @note \8e\8b\8aE\8aO\92n\8c`\83O\83\89\83t\83B\83b\83N\82Ì\93Ç\82Ý\8d\9e\82Ý
182                 rwop = SDL_RWFromFile("img\\UnseenField.png", "rb");
183                 error = IMG_GetError();
184                 tempSurface = IMG_LoadPNG_RW(rwop);
185                 unseenFieldTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
186                 SDL_FreeSurface(tempSurface);
187
188                 //! @note \8f°\92n\8c`\83O\83\89\83t\83B\83b\83N\82Ì\90Ý\92è
189                 for(floorIt = gameWorld->GetFloorList()->begin(); floorIt != gameWorld->GetFloorList()->end(); floorIt++)
190                 {
191                         Color symColor = floorIt->second->GetSymbolColor();
192                         Color backColor = floorIt->second->GetBackColor();
193                         SDL_Color sdlSymCol = {(Uint8)symColor.GetRed(), (Uint8)symColor.GetGreen(), (Uint8)symColor.GetBlue(), (Uint8)symColor.GetAlpha()};
194                         SDL_Rect blitRect = {0, 0, squareGraphicWidth, squareGraphicHeight};
195                         SDL_Rect symbolRect = {0, 0, 30, 30};
196                         char symBuf[80];
197
198                         surface = SDL_CreateRGBSurface(0, squareGraphicWidth, squareGraphicHeight, 32, 0, 0, 0, 0);
199
200                         SDL_FillRect(surface, &blitRect, SDL_MapRGBA(surface->format, (Uint8)backColor.GetRed(), (Uint8)backColor.GetGreen(), (Uint8)backColor.GetBlue(), (Uint8)backColor.GetAlpha()));
201                         sprintf_s(symBuf, 5, "%s", floorIt->second->GetSymbol().c_str()); 
202                         symbolSurface = TTF_RenderUTF8_Blended(font, toUTF8(symBuf).c_str(), sdlSymCol);
203                         blitRect.x += (24 - symbolSurface->w) / 2;
204                         blitRect.y += (24 - symbolSurface->h) / 2;
205                         SDL_BlitSurface(symbolSurface, &symbolRect, surface, &blitRect);
206                         SDL_FreeSurface(symbolSurface);
207
208                         floorTextures.insert(std::map<TAG, SDL_Texture*>::value_type(floorIt->first, SDL_CreateTextureFromSurface(renderer, surface)));
209                                         }
210
211                 SDL_GetWindowSurface(this->window);
212                 this->windowSurface = SDL_GetWindowSurface(this->window);
213         }
214
215         void GameSurfaceSDL::Draw(const CREATURE_IT& subjectCreatureIt)
216         {
217                 SDL_Rect rectTemp = {0, 0, 0, 0};
218                 Coordinates mapDrawingSize;
219
220                 SDL_RenderClear(renderer);
221
222                 SDL_GetWindowSize(window, &rectTemp.w, &rectTemp.h);
223
224                 mapDrawingSize.Set((rectTemp.w - 240) / squareGraphicWidth + 1, (rectTemp.h - 60) / squareGraphicHeight + 1);
225
226                 this->rectMainMapDst.Set(240, 30, rectTemp.w - 260, rectTemp.h - 60);
227                 SDL_SetRenderDrawColor(renderer, 50, 20, 0, 255);
228                 SDL_RenderCopy(renderer, titleTexture, &rectTitleSrc, &rectTitleDst); 
229
230                 if(this->viewFieldPtr)
231                 {
232                         int fx = focusPoint.GetX() - (mapDrawingSize.GetX() / 2);
233                         int fy = focusPoint.GetY() - (mapDrawingSize.GetY() / 2);
234                         if(fx > viewFieldPtr->GetWidth() - mapDrawingSize.GetX()) fx = viewFieldPtr->GetWidth() - mapDrawingSize.GetX();
235                         if(fy > viewFieldPtr->GetHeight() - mapDrawingSize.GetY()) fy = viewFieldPtr->GetHeight() - mapDrawingSize.GetY();
236                         if(fx < 0) fx = 0;
237                         if(fy < 0) fy = 0;
238
239                         this->drawField(subjectCreatureIt, gameWorld, viewFieldPtr, rectMainMapDst, fx, fy, mapDrawingSize.GetX(), mapDrawingSize.GetY());
240                 }
241                 if(this->sideStatusCreatutePtr) this->drawSideCreatureStatus();
242                 if(this->viewCreaturePtr) this->drawCreatureStatus(viewCreaturePtr);
243                 if(this->currentSystemMessage.size() > 0) this->drawSystemMessage();
244                 if(this->gameMessageTexture) this->drawGameMessage();
245                 this->drawTimeStatus();
246
247                 this->fpsProcess();
248                 SDL_Delay(WAIT_MSEC - frameDeltaTime > 0 ? WAIT_MSEC - frameDeltaTime : 0);
249
250                 SDL_RenderPresent(renderer);
251
252                 return;
253         }
254
255         void GameSurfaceSDL::ViewCreatureStatus(Creature *creaturePtr)
256         {
257                 this->viewCreaturePtr = creaturePtr;
258                 //pushAnyKey(NULL);
259                 this->viewCreaturePtr = NULL;
260         }
261
262         void GameSurfaceSDL::SetSideStatusCreature(Creature *creaturePtr)
263         {
264                 this->sideStatusCreatutePtr = creaturePtr;
265                 if(creaturePtr) this->UpdateSideCreatureStatus(creaturePtr);
266         }
267
268         void GameSurfaceSDL::SetField(Field *fieldPtr)
269         {
270                 this->viewFieldPtr = fieldPtr;
271         }
272
273         void GameSurfaceSDL::drawCreatureStatus(Creature *creaturePtr)
274         {
275                 int id;
276
277                 enum CREATURE_STATUS_VIEW_POSITION
278                 {
279                         POS_NAME,
280                         POS_LEVEL,
281                         POS_HP,
282                         POS_MP,
283                         POS_AC,
284                         POS_EV,
285                         POS_VO,
286                         POS_STR,
287                         POS_INT,
288                         POS_WIS,
289                         POS_DEX,
290                         POS_CON,
291                         POS_CHA,
292                         POS_SOUL,
293                         POS_GOOD,
294                         POS_EVIL,
295                         POS_ORDER,
296                         POS_CHAOS,
297                         POS_BALANCE,
298                         POS_HEIGHT,
299                         POS_WEIGHT,
300                         POS_BODY_SIZE,
301                         POS_MAX
302                 };
303
304                 SDL_Rect CreatureStatusViewPosition[POS_MAX] =
305                 {
306                         {10, 10, 0, 0},
307                         {10, 30, 0, 0},
308                         {10, 50, 0, 0},
309                         {10, 70, 0, 0},
310                         {10, 90, 0, 0},
311                         {10, 110, 0, 0},
312                         {10, 130, 0, 0},
313                         {200, 30, 0, 0},
314                         {200, 50, 0, 0},
315                         {200, 70, 0, 0},
316                         {200, 90, 0, 0},
317                         {200, 110, 0, 0},
318                         {200, 130, 0, 0},
319                         {200, 150, 0, 0},
320                         {200, 170, 0, 0},
321                         {200, 190, 0, 0},
322                         {200, 210, 0, 0},
323                         {200, 230, 0, 0},
324                         {200, 250, 0, 0},
325                         {10, 150, 0, 0},
326                         {10, 170, 0, 0},
327                         {10, 190, 0, 0},
328                 };
329
330                 SDL_Rect CreatureStatusViewRect[POS_MAX];
331
332                 const int STATUS_BUFSIZE = 100;
333                 char statusBuf[POS_MAX][STATUS_BUFSIZE];
334                 SDL_Surface *statusSurface[POS_MAX];
335
336                 SDL_Rect masterRect = {10, 10, 490, 450};
337
338                 sprintf_s(statusBuf[POS_NAME], STATUS_BUFSIZE, "\96¼\91O:%s", creaturePtr->GetName().c_str()); 
339                 sprintf_s(statusBuf[POS_LEVEL], STATUS_BUFSIZE, "LV:%3d", creaturePtr->GetLevel()); 
340                 sprintf_s(statusBuf[POS_HP], STATUS_BUFSIZE, "HP:%5d/%5d", creaturePtr->GetCurHP(), creaturePtr->GetMaxHP()); 
341                 sprintf_s(statusBuf[POS_MP], STATUS_BUFSIZE, "MP:%5d/%5d", creaturePtr->GetCurMP(), creaturePtr->GetMaxMP()); 
342                 sprintf_s(statusBuf[POS_AC], STATUS_BUFSIZE, "AC:%4d", creaturePtr->GetArmorSaving()); 
343                 sprintf_s(statusBuf[POS_EV], STATUS_BUFSIZE, "EV:%4d", creaturePtr->GetEvasionSaving()); 
344                 sprintf_s(statusBuf[POS_VO], STATUS_BUFSIZE, "VO:%4d", creaturePtr->GetVolitionSaving()); 
345                 sprintf_s(statusBuf[POS_GOOD], STATUS_BUFSIZE, " \91P :%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_GOOD)); 
346                 sprintf_s(statusBuf[POS_EVIL], STATUS_BUFSIZE, " \88« :%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_EVIL)); 
347                 sprintf_s(statusBuf[POS_ORDER], STATUS_BUFSIZE, "\92\81\8f\98:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_ORDER)); 
348                 sprintf_s(statusBuf[POS_CHAOS], STATUS_BUFSIZE, "\8d¬\93×:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_CHAOS)); 
349                 sprintf_s(statusBuf[POS_BALANCE], STATUS_BUFSIZE, "\93V\94\89:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_BALANCE)); 
350                 sprintf_s(statusBuf[POS_STR], STATUS_BUFSIZE, "\98r\97Í:%4d", creaturePtr->GetCurrentStatus(CS_STR)); 
351                 sprintf_s(statusBuf[POS_INT], STATUS_BUFSIZE, "\92m\97Í:%4d", creaturePtr->GetCurrentStatus(CS_INT)); 
352                 sprintf_s(statusBuf[POS_WIS], STATUS_BUFSIZE, "\8c«\82³:%4d", creaturePtr->GetCurrentStatus(CS_WIS)); 
353                 sprintf_s(statusBuf[POS_DEX], STATUS_BUFSIZE, "\8aí\97p:%4d", creaturePtr->GetCurrentStatus(CS_DEX)); 
354                 sprintf_s(statusBuf[POS_CON], STATUS_BUFSIZE, "\91Ï\8bv:%4d", creaturePtr->GetCurrentStatus(CS_CON)); 
355                 sprintf_s(statusBuf[POS_CHA], STATUS_BUFSIZE, "\96£\97Í:%4d", creaturePtr->GetCurrentStatus(CS_CHA)); 
356                 sprintf_s(statusBuf[POS_SOUL], STATUS_BUFSIZE, "\83\\83E\83\8b:%4d", creaturePtr->GetCurrentSoul()); 
357                 sprintf_s(statusBuf[POS_HEIGHT], STATUS_BUFSIZE, "\90g\92·:%13s", GameElement::HeightFormat(creaturePtr->GetHeight()).c_str()); 
358                 sprintf_s(statusBuf[POS_WEIGHT], STATUS_BUFSIZE, "\91Ì\8fd:%13s", GameElement::WeightFormat(creaturePtr->GetWeight()).c_str()); 
359                 sprintf_s(statusBuf[POS_BODY_SIZE], STATUS_BUFSIZE, "\91Ì\8ai:%3d", creaturePtr->GetSize()); 
360
361                 for(id = 0; id < POS_MAX; id++)
362                 {
363                         statusSurface[id] = TTF_RenderUTF8_Blended(font, toUTF8(statusBuf[id]).c_str(), color);
364                         CreatureStatusViewRect[id].x = 0;
365                         CreatureStatusViewRect[id].y = 0;
366                         CreatureStatusViewRect[id].w = statusSurface[id]->w;
367                         CreatureStatusViewRect[id].h = statusSurface[id]->h;
368                         CreatureStatusViewPosition[id].x += masterRect.x;
369                         CreatureStatusViewPosition[id].y += masterRect.y;
370                 }
371
372                 SDL_FillRect(windowSurface, &masterRect, SDL_MapRGBA(windowSurface->format, 0, 0, 0, 120));
373
374                 for(id = 0; id < POS_MAX; id++)
375                 {
376                         SDL_BlitSurface(statusSurface[id], &CreatureStatusViewRect[id], windowSurface, &CreatureStatusViewPosition[id]); 
377                 }
378
379                 for(id = 0; id < POS_MAX; id++)
380                 {
381                         SDL_FreeSurface(statusSurface[id]);
382                 }
383                 return;
384         }
385
386         void GameSurfaceSDL::UpdateTimeStatus(GAME_TIME currentTime)
387         {
388
389                 SDL_Rect rect = {0, 0, 200, 50};
390                 SDL_Rect StatusViewRect = {0, 0, 200, 50};
391                 SDL_Rect StatusViewPosition = {10, 10, 0, 0};
392                 SDL_Surface *surface, *statusSurface;
393                 const int STATUS_BUFSIZE = 100;
394                 char statusBuf[STATUS_BUFSIZE];
395                 if(gameTimeStatusTexture) SDL_DestroyTexture(gameTimeStatusTexture);
396                 surface = SDL_CreateRGBSurface(0, 200, 50, 32, 0, 0, 0, 0);
397                 SDL_FillRect(surface, &rect, SDL_MapRGBA(windowSurface->format, 0, 0, 0, 120));
398                 sprintf_s(statusBuf, STATUS_BUFSIZE, "\83^\81[\83\93:%d", currentTime); 
399                 statusSurface = TTF_RenderUTF8_Blended(font, toUTF8(statusBuf).c_str(), color);
400                 SDL_BlitSurface(statusSurface, &StatusViewRect, surface, &StatusViewPosition); 
401                 gameTimeStatusTexture = SDL_CreateTextureFromSurface(renderer, surface);
402                 SDL_FreeSurface(statusSurface);
403                 SDL_FreeSurface(surface);
404         }
405
406         void GameSurfaceSDL::UpdateSideCreatureStatus(Creature *creaturePtr)
407         {
408                 int id;
409                 SDL_Surface *surface;
410
411                 enum CREATURE_SIDE_STATUS_VIEW_POSITION
412                 {
413                         POS_NAME,
414                         POS_LEVEL,
415                         POS_HP,
416                         POS_MP,
417                         POS_AC,
418                         POS_EV,
419                         POS_VO,
420                         POS_STR,
421                         POS_INT,
422                         POS_WIS,
423                         POS_DEX,
424                         POS_CON,
425                         POS_CHA,
426                         POS_SOUL,
427                         POS_MAX
428                 };
429
430                 SDL_Rect CreatureStatusViewPosition[POS_MAX] =
431                 {
432                         {10, 10, 0, 0},
433                         {10, 30, 0, 0},
434                         {10, 50, 0, 0},
435                         {10, 70, 0, 0},
436                         {10, 90, 0, 0},
437                         {10, 110, 0, 0},
438                         {10, 130, 0, 0},
439                         {200, 30, 0, 0},
440                         {200, 50, 0, 0},
441                         {200, 70, 0, 0},
442                         {200, 90, 0, 0},
443                         {200, 110, 0, 0},
444                         {200, 130, 0, 0},
445                         {200, 150, 0, 0},
446                 };
447
448                 SDL_Rect CreatureStatusViewRect[POS_MAX];
449
450                 const int STATUS_BUFSIZE = 100;
451                 char statusBuf[POS_MAX][STATUS_BUFSIZE];
452                 SDL_Surface *statusSurface[POS_MAX];
453                 
454                 SDL_Rect masterRect = {0, 0, 180, 350};
455
456                 surface = SDL_CreateRGBSurface(0, 180, 350, 32, 0, 0, 0, 0);
457
458                 sprintf_s(statusBuf[POS_NAME], STATUS_BUFSIZE, "\96¼\91O:%s", creaturePtr->GetName().c_str()); 
459                 sprintf_s(statusBuf[POS_LEVEL], STATUS_BUFSIZE, "LV:%3d", creaturePtr->GetLevel()); 
460                 sprintf_s(statusBuf[POS_HP], STATUS_BUFSIZE, "HP:%5d/%5d", creaturePtr->GetCurHP(), creaturePtr->GetMaxHP()); 
461                 sprintf_s(statusBuf[POS_MP], STATUS_BUFSIZE, "MP:%5d/%5d", creaturePtr->GetCurMP(), creaturePtr->GetMaxMP()); 
462                 sprintf_s(statusBuf[POS_AC], STATUS_BUFSIZE, "AC:%4d", creaturePtr->GetArmorSaving()); 
463                 sprintf_s(statusBuf[POS_EV], STATUS_BUFSIZE, "EV:%4d", creaturePtr->GetEvasionSaving()); 
464                 sprintf_s(statusBuf[POS_VO], STATUS_BUFSIZE, "VO:%4d", creaturePtr->GetVolitionSaving()); 
465                 sprintf_s(statusBuf[POS_STR], STATUS_BUFSIZE, "\98r\97Í:%4d", creaturePtr->GetCurrentStatus(CS_STR)); 
466                 sprintf_s(statusBuf[POS_INT], STATUS_BUFSIZE, "\92m\97Í:%4d", creaturePtr->GetCurrentStatus(CS_INT)); 
467                 sprintf_s(statusBuf[POS_WIS], STATUS_BUFSIZE, "\8c«\82³:%4d", creaturePtr->GetCurrentStatus(CS_WIS)); 
468                 sprintf_s(statusBuf[POS_DEX], STATUS_BUFSIZE, "\8aí\97p:%4d", creaturePtr->GetCurrentStatus(CS_DEX)); 
469                 sprintf_s(statusBuf[POS_CON], STATUS_BUFSIZE, "\91Ï\8bv:%4d", creaturePtr->GetCurrentStatus(CS_CON)); 
470                 sprintf_s(statusBuf[POS_CHA], STATUS_BUFSIZE, "\96£\97Í:%4d", creaturePtr->GetCurrentStatus(CS_CHA)); 
471                 sprintf_s(statusBuf[POS_SOUL], STATUS_BUFSIZE, "\83\\83E\83\8b:%4d", creaturePtr->GetCurrentSoul()); 
472
473                 for(id = 0; id < POS_MAX; id++)
474                 {
475                         statusSurface[id] = TTF_RenderUTF8_Blended(font, toUTF8(statusBuf[id]).c_str(), color);
476                         CreatureStatusViewRect[id].x = 0;
477                         CreatureStatusViewRect[id].y = 0;
478                         CreatureStatusViewRect[id].w = statusSurface[id]->w;
479                         CreatureStatusViewRect[id].h = statusSurface[id]->h;
480                         CreatureStatusViewPosition[id].x += masterRect.x;
481                         CreatureStatusViewPosition[id].y += masterRect.y;
482                 }
483
484                 SDL_FillRect(surface, &masterRect, SDL_MapRGBA(surface->format, 0, 0, 0, 120));
485
486                 for(id = 0; id < POS_MAX; id++)
487                 {
488                         SDL_BlitSurface(statusSurface[id], &CreatureStatusViewRect[id], surface, &CreatureStatusViewPosition[id]); 
489                 }
490
491                 for(id = 0; id < POS_MAX; id++)
492                 {
493                         SDL_FreeSurface(statusSurface[id]);
494                 }
495
496                 creatureSideStatusTexture = SDL_CreateTextureFromSurface(renderer, surface);
497                 SDL_FreeSurface(surface);
498
499                 return;
500
501         }
502
503         void GameSurfaceSDL::drawSideCreatureStatus(void)
504         {
505                 SDL_Rect masterRect = {0, 0, 180, 350};
506                 SDL_Rect posRect = {10, 120, 180, 350};
507                 SDL_RenderCopy(renderer, creatureSideStatusTexture, &masterRect, &posRect);
508         }
509
510         GAME_COMMAND GameSurfaceSDL::GetCommand(const CREATURE_IT& subjectCreatureIt)
511         {
512                 SDL_Event event;
513                 SDL_Keycode key;
514
515                 this->Draw(subjectCreatureIt);
516                 while (SDL_PollEvent(&event))
517                 {
518
519                         switch(event.type)
520                         {
521
522                         case SDL_KEYDOWN:
523                                 {
524                                         key=event.key.keysym.sym;
525
526                                         switch(key)
527                                         {
528                                         case SDLK_ESCAPE:
529                                                 return GAME_COMMAND_EXIT;
530                                         case SDLK_c:
531                                                 return GAME_COMMAND_VIEW_PLAYER_STATUS;
532                                         case SDLK_F11:
533                                                 return GAME_COMMAND_DEBUG_XML_SAVE;
534                                         case SDLK_F12:
535                                                 return GAME_COMMAND_DEBUG_XML_LOAD;
536                                         case SDLK_k:
537                                                 return GAME_COMMAND_NORTH;
538                                         case SDLK_u:
539                                                 return GAME_COMMAND_NORTH_EAST;
540                                         case SDLK_l:
541                                                 return GAME_COMMAND_EAST;
542                                         case SDLK_n:
543                                                 return GAME_COMMAND_SOUTH_EAST;
544                                         case SDLK_j:
545                                                 return GAME_COMMAND_SOUTH;
546                                         case SDLK_b:
547                                                 return GAME_COMMAND_SOUTH_WEST;
548                                         case SDLK_h:
549                                                 return GAME_COMMAND_WEST;
550                                         case SDLK_y:
551                                                 return GAME_COMMAND_NORTH_WEST;
552                                         case SDLK_s:
553                                                 return GAME_COMMAND_STAY;
554                                         }
555
556                                 }
557                                 break;
558
559                         case SDL_WINDOWEVENT:
560                                 switch (event.window.event) {
561                                 case SDL_WINDOWEVENT_SHOWN:
562                                         break;
563                                 case SDL_WINDOWEVENT_HIDDEN:
564                                         break;
565                                 case SDL_WINDOWEVENT_EXPOSED:
566                                         break;
567                                 case SDL_WINDOWEVENT_MOVED:
568                                         break;
569                                 case SDL_WINDOWEVENT_RESIZED:
570                                         break;
571                                 case SDL_WINDOWEVENT_MINIMIZED:
572                                         break;
573                                 case SDL_WINDOWEVENT_MAXIMIZED:
574                                         break;
575                                 case SDL_WINDOWEVENT_RESTORED:
576                                         break;
577                                 case SDL_WINDOWEVENT_ENTER:
578                                         break;
579                                 case SDL_WINDOWEVENT_LEAVE:
580                                         break;
581                                 case SDL_WINDOWEVENT_FOCUS_GAINED:
582                                         break;
583                                 case SDL_WINDOWEVENT_FOCUS_LOST:
584                                         break;
585                                 case SDL_WINDOWEVENT_CLOSE:
586                                         break;
587                                 default:
588                                         break;
589                                 }
590                                 this->Draw(subjectCreatureIt);
591                                 break;
592
593                         case SDL_QUIT:
594                                 return GAME_COMMAND_EXIT;
595                                 break;
596
597                 }
598         }
599
600         return GAME_COMMAND_REDRAW;
601 }
602
603 void GameSurfaceSDL::drawField(const CREATURE_IT &subjectCreatureIt, GameWorld *gameWorld, Field *fieldPtr, SDL_Rect_Dee &rectDraw, int x, int y, int w, int h)
604 {       
605         SDL_Rect symbolRect = {0, 0, squareGraphicWidth, squareGraphicHeight};
606         CREATURE_IT creatureIt;
607         Creature *subJectCreaturePtr = subjectCreatureIt->second.get();
608
609         MAP_LENGTH px, py;
610
611         SDL_RenderSetViewport(this->renderer, &rectDraw);
612
613         for(py = y; py <= y + h; py++)
614         {
615                 for(px = x; px <= x + w; px++)
616                 {
617                         if(px >= 0 && py >= 0 && px < fieldPtr->GetWidth() && py < fieldPtr->GetHeight())
618                         {
619                                 SDL_Rect blitRect = {(px - x) * squareGraphicWidth, (py - y) * squareGraphicHeight , squareGraphicWidth, squareGraphicHeight};
620                                 /*
621                                 SDL_Rect checkRect1 = {GameSurfaceSDL::sideBarWidth + (px - x) * squareGraphicWidth - 2 + 12, 30 + (py - y) * squareGraphicHeight - 2 + 12, 4, 4};
622                                 SDL_Rect checkRect2 = {GameSurfaceSDL::sideBarWidth + (px - x) * squareGraphicWidth - 2, 30 + (py - y) * squareGraphicHeight - 2, 4, 4};
623                                 SDL_Rect checkRect3 = {GameSurfaceSDL::sideBarWidth + (px - x) * squareGraphicWidth - 6 + 12, 30 + (py - y) * squareGraphicHeight - 1, 12, 2};
624                                 SDL_Rect checkRect4 = {GameSurfaceSDL::sideBarWidth + (px - x) * squareGraphicWidth - 1, 30 + (py - y) * squareGraphicHeight - 6 + 12, 2, 12};
625                                 */
626
627                                 if(subJectCreaturePtr->InSight(px, py))
628                                 {
629                                         TAG tag = fieldPtr->GetSquare(px, py)->GetFloorTag();
630                                         SDL_RenderCopy(renderer, floorTextures[tag], &symbolRect, &blitRect);
631                                         if(!subJectCreaturePtr->InSight(px, py))
632                                         {
633                                                 SDL_Rect fieldRect = {blitRect.x % 128, blitRect.y % 128, squareGraphicWidth, squareGraphicHeight};
634                                                 SDL_RenderCopy(this->renderer, unseenFieldTexture, &fieldRect, &blitRect);
635                                         }
636                                 }
637                                 else
638                                 {
639                                         SDL_Rect fieldRect = {blitRect.x % 128, blitRect.y % 128, squareGraphicWidth, squareGraphicHeight};
640                                         SDL_RenderCopy(this->renderer, unknownFieldTexture, &fieldRect, &blitRect);
641                                 }
642
643 /*                      
644
645                                 if(fieldPtr->GetSightPass(px * 2 + 1, py * 2 + 1))
646                                         SDL_FillRect(windowSurface, &checkRect1, SDL_MapRGBA(windowSurface->format, 255, 255, 255, 255));
647                                 else
648                                         SDL_FillRect(windowSurface, &checkRect1, SDL_MapRGBA(windowSurface->format, 255, 0, 0, 255));
649
650                                 if(fieldPtr->GetSightPass(px * 2, py * 2))
651                                         SDL_FillRect(windowSurface, &checkRect2, SDL_MapRGBA(windowSurface->format, 255, 255, 255, 255));
652                                 else
653                                         SDL_FillRect(windowSurface, &checkRect2, SDL_MapRGBA(windowSurface->format, 255, 0, 0, 255));
654
655                                 if(fieldPtr->GetSightPass(px * 2 + 1, py * 2))
656                                         SDL_FillRect(windowSurface, &checkRect3, SDL_MapRGBA(windowSurface->format, 255, 255, 255, 255));
657                                 else
658                                         SDL_FillRect(windowSurface, &checkRect3, SDL_MapRGBA(windowSurface->format, 255, 0, 0, 255));
659
660                                 if(fieldPtr->GetSightPass(px * 2, py * 2 + 1))
661                                         SDL_FillRect(windowSurface, &checkRect4, SDL_MapRGBA(windowSurface->format, 255, 255, 255, 255));
662                                 else
663                                         SDL_FillRect(windowSurface, &checkRect4, SDL_MapRGBA(windowSurface->format, 255, 0, 0, 255));
664 */
665                         }
666                 }
667         }
668
669         for(creatureIt = gameWorld->GetCreatureList()->begin(); creatureIt != gameWorld->GetCreatureList()->end(); creatureIt++)
670         {
671                 if(!creatureTextures[creatureIt->first])
672                 {
673                         updateCreatureGraphics(creatureIt);
674                 }
675
676                 if(subJectCreaturePtr->InSight(creatureIt->second->GetPosition().GetX(), creatureIt->second->GetPosition().GetY()))
677                 {
678                         SDL_Rect blitRect = {(creatureIt->second->GetPosition().GetX() - x) * squareGraphicWidth, (creatureIt->second->GetPosition().GetY()- y) * squareGraphicHeight, squareGraphicWidth, squareGraphicHeight};
679                         SDL_RenderCopy(renderer, creatureTextures[creatureIt->first], &symbolRect, &blitRect); 
680                 }
681                 
682                 /*
683                 if(subjectCreatureIt->second->GetLore()->GetFieldLore(0, creatureIt->second->GetPosition().GetX(), creatureIt->second->GetPosition().GetY()) > 0)
684                 {
685                         SDL_Rect blitRect = {GameSurfaceSDL::sideBarWidth + (creatureIt->second->GetPosition().GetX() - x) * squareGraphicWidth, 30 + (creatureIt->second->GetPosition().GetY()- y) * squareGraphicHeight, squareGraphicWidth, squareGraphicHeight};
686                         SDL_BlitSurface(creatureSurfaces[creatureIt->first], &symbolRect, windowSurface, &blitRect); 
687                 }
688                 */
689         }
690
691         SDL_RenderSetViewport(this->renderer, NULL);
692
693 }
694
695         void GameSurfaceSDL::FocusField(int x, int y)
696         {       
697                 this->focusPoint.Set(x, y);
698         }
699
700         void GameSurfaceSDL::FocusField(Coordinates coord)
701         {       
702                 this->focusPoint.Set(coord.GetX(), coord.GetY());
703         }
704
705         void GameSurfaceSDL::drawSystemMessage(void)
706         {
707                 SDL_Rect messageRect = {0, 0, 200, 200};
708                 SDL_Rect messagePositon = {220, 180, 420, 380}; 
709                 //SDL_Rect windowRect = {210, 170, 420, 220};
710                 //SDL_FillRect(windowSurface, &windowRect, SDL_MapRGBA(systemMessageSurface->format, 0, 0, 0, 120));
711                 SDL_RenderCopy(renderer, systemMessageTexture, &messageRect, &messagePositon);
712         }
713
714         void GameSurfaceSDL::drawGameMessage(void)
715         {
716                 SDL_Rect messageRect = {0, 0, 512, 20};
717                 SDL_Rect messagePositon = {242, 4, 512, 20}; 
718                 SDL_RenderCopy(renderer, gameMessageTexture, &messageRect, &messagePositon);
719         }
720
721         void GameSurfaceSDL::drawTimeStatus(void)
722         {
723                 SDL_Rect messageRect = {0, 0, 200, 50};
724                 SDL_Rect messagePositon = {10, 480, 200, 50}; 
725                 SDL_RenderCopy(renderer, gameTimeStatusTexture, &messageRect, &messagePositon);
726         }
727
728         void GameSurfaceSDL::SystemMessage(std::string message)
729         {
730                 SDL_Surface *surface;
731                 this->currentSystemMessage = message;
732                 surface = TTF_RenderUTF8_Blended(font, toUTF8(this->currentSystemMessage.c_str()).c_str(), color);
733                 systemMessageTexture = SDL_CreateTextureFromSurface(renderer, surface);
734                 pushAnyKey();
735                 this->currentSystemMessage.erase();
736                 SDL_FreeSurface(surface);
737         }
738
739         void GameSurfaceSDL::GameMessage(std::string message)
740         {
741                 SDL_Surface *boxSurface, *textSurface;
742                 SDL_Rect textPosition = {2, 2, 0, 0}; 
743                 this->currentGameMessage = message;
744                 if(gameMessageTexture) SDL_DestroyTexture(gameMessageTexture);
745                 boxSurface = SDL_CreateRGBSurface(0, 512, 20, 32, 0, 0, 0, 0);
746                 textSurface = TTF_RenderUTF8_Blended(font, toUTF8(this->currentGameMessage.c_str()).c_str(), color);
747                 SDL_BlitSurface(textSurface, NULL, boxSurface, &textPosition); 
748                 gameMessageTexture = SDL_CreateTextureFromSurface(renderer, boxSurface);
749                 SDL_FreeSurface(textSurface);
750                 SDL_FreeSurface(boxSurface);
751         }
752
753         void GameSurfaceSDL::updateCreatureGraphics(CREATURE_IT creatureIt)
754         {
755                 SDL_Rect symbolRect = {0, 0, squareGraphicWidth, squareGraphicHeight};
756                 SDL_Surface *surface, *symbolSurface;
757                 char symBuf[5];
758
759                 SDL_DestroyTexture(creatureTextures[creatureIt->first]);
760                 creatureTextures.erase(creatureIt->first);
761
762                 surface = SDL_CreateRGBSurface(0, squareGraphicWidth, squareGraphicHeight, 32, 0, 0, 0, 0);
763                 Color symColor = creatureIt->second->GetSymbolColor();
764                 SDL_Color sdlSymCol = {(Uint8)symColor.GetRed(), (Uint8)symColor.GetGreen(), (Uint8)symColor.GetBlue(), (Uint8)symColor.GetAlpha()};
765                 sprintf_s(symBuf, 5, "%s", creatureIt->second->GetSymbol().c_str()); 
766                 SDL_Rect blitRect = {0, 0, squareGraphicWidth, squareGraphicHeight};
767
768                 symbolSurface = TTF_RenderUTF8_Blended(font, toUTF8(symBuf).c_str(), sdlSymCol);
769
770                 blitRect.x += (squareGraphicWidth - symbolSurface->w) / 2;
771                 blitRect.y += (squareGraphicHeight - symbolSurface->h) / 2;
772
773                 SDL_BlitSurface(symbolSurface, &symbolRect, surface, &blitRect); 
774                 SDL_FreeSurface(symbolSurface);
775
776                 creatureTextures.insert(std::map<ID, SDL_Texture*>::value_type(creatureIt->first, SDL_CreateTextureFromSurface(renderer, surface)));
777                 SDL_FreeSurface(surface);
778         }
779
780
781 }