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)
12 #include "GameSurfaceSDL.h"
14 #include "GameEngine.h"
22 std::string toUTF8(LPCSTR str)
24 const int cchWideChar = ::MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
25 std::vector<WCHAR> lpw(cchWideChar);
27 const int nUnicodeCount = ::MultiByteToWideChar(CP_ACP, 0, str, -1, &lpw[0], cchWideChar);
28 if(nUnicodeCount <= 0)
33 const int cchMultiByte = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, NULL, 0, NULL, NULL);
34 std::vector<CHAR> lpa(cchMultiByte);
36 const int nMultiCount = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, &lpa[0], cchMultiByte, NULL, NULL);
41 return std::string(&lpa[0]);
44 GameSurfaceSDL::GameSurfaceSDL(GameWorld *gameWorld) : GameSurface(gameWorld)
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);
52 if(TTF_Init() == -1) return;
53 if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) return;
55 initInterfaces(gameWorld);
57 viewCreaturePtr = NULL;
60 windowSurface = SDL_GetWindowSurface(window);
62 gameMessageTexture = NULL;
63 systemMessageTexture = NULL;
68 frameDeltaTimeLast = frameTimeLast = SDL_GetTicks();
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);
78 GameSurfaceSDL::~GameSurfaceSDL(void)
86 void GameSurfaceSDL::fpsProcess()
94 getTicks = SDL_GetTicks();
95 frameDeltaTime = getTicks - frameDeltaTimeLast;
96 frameDeltaTimeLast = getTicks;
97 if (frameCount >= FPS_UPDATE)
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);
109 SDL_RenderCopy(renderer, fpsTexture, &rectFpsSrc, &rectFpsDst);
113 void GameSurfaceSDL::pushAnyKey(const CREATURE_IT& subjectCreatureIt)
118 this->Draw(subjectCreatureIt);
119 SDL_PollEvent(&event);
120 } while(event.type != SDL_KEYDOWN);
123 void GameSurfaceSDL::pushAnyKey(void)
128 SDL_Rect rect = {0, 0, 0, 0};
129 this->drawSystemMessage();
130 SDL_UpdateWindowSurfaceRects(window, &rect, 1);
131 SDL_UpdateWindowSurface(window);
133 SDL_PollEvent(&event);
134 } while(event.type != SDL_KEYDOWN);
138 void GameSurfaceSDL::initInterfaces(GameWorld *gameWorld)
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;
144 //! @note TTF
\83t
\83H
\83\93\83g
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý
146 font = TTF_OpenFont("ttf\\ipam.ttf", 14);
147 fontSmall = TTF_OpenFont("ttf\\ipag.ttf", 9);
161 if(!font || !fontSmall)
163 OutputDebugString(L"Failed: loading .ttf files.\n");
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);
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);
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);
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++)
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};
198 surface = SDL_CreateRGBSurface(0, squareGraphicWidth, squareGraphicHeight, 32, 0, 0, 0, 0);
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);
208 floorTextures.insert(std::map<TAG, SDL_Texture*>::value_type(floorIt->first, SDL_CreateTextureFromSurface(renderer, surface)));
211 SDL_GetWindowSurface(this->window);
212 this->windowSurface = SDL_GetWindowSurface(this->window);
215 void GameSurfaceSDL::Draw(const CREATURE_IT& subjectCreatureIt)
217 SDL_Rect rectTemp = {0, 0, 0, 0};
218 Coordinates mapDrawingSize;
220 SDL_RenderClear(renderer);
222 SDL_GetWindowSize(window, &rectTemp.w, &rectTemp.h);
224 mapDrawingSize.Set((rectTemp.w - 240) / squareGraphicWidth + 1, (rectTemp.h - 60) / squareGraphicHeight + 1);
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);
230 if(this->viewFieldPtr)
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();
239 this->drawField(subjectCreatureIt, gameWorld, viewFieldPtr, rectMainMapDst, fx, fy, mapDrawingSize.GetX(), mapDrawingSize.GetY());
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();
248 SDL_Delay(WAIT_MSEC - frameDeltaTime > 0 ? WAIT_MSEC - frameDeltaTime : 0);
250 SDL_RenderPresent(renderer);
255 void GameSurfaceSDL::ViewCreatureStatus(Creature *creaturePtr)
257 this->viewCreaturePtr = creaturePtr;
259 this->viewCreaturePtr = NULL;
262 void GameSurfaceSDL::SetSideStatusCreature(Creature *creaturePtr)
264 this->sideStatusCreatutePtr = creaturePtr;
265 if(creaturePtr) this->UpdateSideCreatureStatus(creaturePtr);
268 void GameSurfaceSDL::SetField(Field *fieldPtr)
270 this->viewFieldPtr = fieldPtr;
273 void GameSurfaceSDL::drawCreatureStatus(Creature *creaturePtr)
277 enum CREATURE_STATUS_VIEW_POSITION
304 SDL_Rect CreatureStatusViewPosition[POS_MAX] =
330 SDL_Rect CreatureStatusViewRect[POS_MAX];
332 const int STATUS_BUFSIZE = 100;
333 char statusBuf[POS_MAX][STATUS_BUFSIZE];
334 SDL_Surface *statusSurface[POS_MAX];
336 SDL_Rect masterRect = {10, 10, 490, 450};
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());
361 for(id = 0; id < POS_MAX; id++)
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;
372 SDL_FillRect(windowSurface, &masterRect, SDL_MapRGBA(windowSurface->format, 0, 0, 0, 120));
374 for(id = 0; id < POS_MAX; id++)
376 SDL_BlitSurface(statusSurface[id], &CreatureStatusViewRect[id], windowSurface, &CreatureStatusViewPosition[id]);
379 for(id = 0; id < POS_MAX; id++)
381 SDL_FreeSurface(statusSurface[id]);
386 void GameSurfaceSDL::UpdateTimeStatus(GAME_TIME currentTime)
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);
406 void GameSurfaceSDL::UpdateSideCreatureStatus(Creature *creaturePtr)
409 SDL_Surface *surface;
411 enum CREATURE_SIDE_STATUS_VIEW_POSITION
430 SDL_Rect CreatureStatusViewPosition[POS_MAX] =
448 SDL_Rect CreatureStatusViewRect[POS_MAX];
450 const int STATUS_BUFSIZE = 100;
451 char statusBuf[POS_MAX][STATUS_BUFSIZE];
452 SDL_Surface *statusSurface[POS_MAX];
454 SDL_Rect masterRect = {0, 0, 180, 350};
456 surface = SDL_CreateRGBSurface(0, 180, 350, 32, 0, 0, 0, 0);
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());
473 for(id = 0; id < POS_MAX; id++)
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;
484 SDL_FillRect(surface, &masterRect, SDL_MapRGBA(surface->format, 0, 0, 0, 120));
486 for(id = 0; id < POS_MAX; id++)
488 SDL_BlitSurface(statusSurface[id], &CreatureStatusViewRect[id], surface, &CreatureStatusViewPosition[id]);
491 for(id = 0; id < POS_MAX; id++)
493 SDL_FreeSurface(statusSurface[id]);
496 creatureSideStatusTexture = SDL_CreateTextureFromSurface(renderer, surface);
497 SDL_FreeSurface(surface);
503 void GameSurfaceSDL::drawSideCreatureStatus(void)
505 SDL_Rect masterRect = {0, 0, 180, 350};
506 SDL_Rect posRect = {10, 120, 180, 350};
507 SDL_RenderCopy(renderer, creatureSideStatusTexture, &masterRect, &posRect);
510 GAME_COMMAND GameSurfaceSDL::GetCommand(const CREATURE_IT& subjectCreatureIt)
515 this->Draw(subjectCreatureIt);
516 while (SDL_PollEvent(&event))
524 key=event.key.keysym.sym;
529 return GAME_COMMAND_EXIT;
531 return GAME_COMMAND_VIEW_PLAYER_STATUS;
533 return GAME_COMMAND_DEBUG_XML_SAVE;
535 return GAME_COMMAND_DEBUG_XML_LOAD;
537 return GAME_COMMAND_NORTH;
539 return GAME_COMMAND_NORTH_EAST;
541 return GAME_COMMAND_EAST;
543 return GAME_COMMAND_SOUTH_EAST;
545 return GAME_COMMAND_SOUTH;
547 return GAME_COMMAND_SOUTH_WEST;
549 return GAME_COMMAND_WEST;
551 return GAME_COMMAND_NORTH_WEST;
553 return GAME_COMMAND_STAY;
559 case SDL_WINDOWEVENT:
560 switch (event.window.event) {
561 case SDL_WINDOWEVENT_SHOWN:
563 case SDL_WINDOWEVENT_HIDDEN:
565 case SDL_WINDOWEVENT_EXPOSED:
567 case SDL_WINDOWEVENT_MOVED:
569 case SDL_WINDOWEVENT_RESIZED:
571 case SDL_WINDOWEVENT_MINIMIZED:
573 case SDL_WINDOWEVENT_MAXIMIZED:
575 case SDL_WINDOWEVENT_RESTORED:
577 case SDL_WINDOWEVENT_ENTER:
579 case SDL_WINDOWEVENT_LEAVE:
581 case SDL_WINDOWEVENT_FOCUS_GAINED:
583 case SDL_WINDOWEVENT_FOCUS_LOST:
585 case SDL_WINDOWEVENT_CLOSE:
590 this->Draw(subjectCreatureIt);
594 return GAME_COMMAND_EXIT;
600 return GAME_COMMAND_REDRAW;
603 void GameSurfaceSDL::drawField(const CREATURE_IT &subjectCreatureIt, GameWorld *gameWorld, Field *fieldPtr, SDL_Rect_Dee &rectDraw, int x, int y, int w, int h)
605 SDL_Rect symbolRect = {0, 0, squareGraphicWidth, squareGraphicHeight};
606 CREATURE_IT creatureIt;
607 Creature *subJectCreaturePtr = subjectCreatureIt->second.get();
611 SDL_RenderSetViewport(this->renderer, &rectDraw);
613 for(py = y; py <= y + h; py++)
615 for(px = x; px <= x + w; px++)
617 if(px >= 0 && py >= 0 && px < fieldPtr->GetWidth() && py < fieldPtr->GetHeight())
619 SDL_Rect blitRect = {(px - x) * squareGraphicWidth, (py - y) * squareGraphicHeight , squareGraphicWidth, squareGraphicHeight};
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};
627 if(subJectCreaturePtr->InSight(px, py))
629 TAG tag = fieldPtr->GetSquare(px, py)->GetFloorTag();
630 SDL_RenderCopy(renderer, floorTextures[tag], &symbolRect, &blitRect);
631 if(!subJectCreaturePtr->InSight(px, py))
633 SDL_Rect fieldRect = {blitRect.x % 128, blitRect.y % 128, squareGraphicWidth, squareGraphicHeight};
634 SDL_RenderCopy(this->renderer, unseenFieldTexture, &fieldRect, &blitRect);
639 SDL_Rect fieldRect = {blitRect.x % 128, blitRect.y % 128, squareGraphicWidth, squareGraphicHeight};
640 SDL_RenderCopy(this->renderer, unknownFieldTexture, &fieldRect, &blitRect);
645 if(fieldPtr->GetSightPass(px * 2 + 1, py * 2 + 1))
646 SDL_FillRect(windowSurface, &checkRect1, SDL_MapRGBA(windowSurface->format, 255, 255, 255, 255));
648 SDL_FillRect(windowSurface, &checkRect1, SDL_MapRGBA(windowSurface->format, 255, 0, 0, 255));
650 if(fieldPtr->GetSightPass(px * 2, py * 2))
651 SDL_FillRect(windowSurface, &checkRect2, SDL_MapRGBA(windowSurface->format, 255, 255, 255, 255));
653 SDL_FillRect(windowSurface, &checkRect2, SDL_MapRGBA(windowSurface->format, 255, 0, 0, 255));
655 if(fieldPtr->GetSightPass(px * 2 + 1, py * 2))
656 SDL_FillRect(windowSurface, &checkRect3, SDL_MapRGBA(windowSurface->format, 255, 255, 255, 255));
658 SDL_FillRect(windowSurface, &checkRect3, SDL_MapRGBA(windowSurface->format, 255, 0, 0, 255));
660 if(fieldPtr->GetSightPass(px * 2, py * 2 + 1))
661 SDL_FillRect(windowSurface, &checkRect4, SDL_MapRGBA(windowSurface->format, 255, 255, 255, 255));
663 SDL_FillRect(windowSurface, &checkRect4, SDL_MapRGBA(windowSurface->format, 255, 0, 0, 255));
669 for(creatureIt = gameWorld->GetCreatureList()->begin(); creatureIt != gameWorld->GetCreatureList()->end(); creatureIt++)
671 if(!creatureTextures[creatureIt->first])
673 updateCreatureGraphics(creatureIt);
676 if(subJectCreaturePtr->InSight(creatureIt->second->GetPosition().GetX(), creatureIt->second->GetPosition().GetY()))
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);
683 if(subjectCreatureIt->second->GetLore()->GetFieldLore(0, creatureIt->second->GetPosition().GetX(), creatureIt->second->GetPosition().GetY()) > 0)
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);
691 SDL_RenderSetViewport(this->renderer, NULL);
695 void GameSurfaceSDL::FocusField(int x, int y)
697 this->focusPoint.Set(x, y);
700 void GameSurfaceSDL::FocusField(Coordinates coord)
702 this->focusPoint.Set(coord.GetX(), coord.GetY());
705 void GameSurfaceSDL::drawSystemMessage(void)
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);
714 void GameSurfaceSDL::drawGameMessage(void)
716 SDL_Rect messageRect = {0, 0, 512, 20};
717 SDL_Rect messagePositon = {242, 4, 512, 20};
718 SDL_RenderCopy(renderer, gameMessageTexture, &messageRect, &messagePositon);
721 void GameSurfaceSDL::drawTimeStatus(void)
723 SDL_Rect messageRect = {0, 0, 200, 50};
724 SDL_Rect messagePositon = {10, 480, 200, 50};
725 SDL_RenderCopy(renderer, gameTimeStatusTexture, &messageRect, &messagePositon);
728 void GameSurfaceSDL::SystemMessage(std::string message)
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);
735 this->currentSystemMessage.erase();
736 SDL_FreeSurface(surface);
739 void GameSurfaceSDL::GameMessage(std::string message)
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);
753 void GameSurfaceSDL::updateCreatureGraphics(CREATURE_IT creatureIt)
755 SDL_Rect symbolRect = {0, 0, squareGraphicWidth, squareGraphicHeight};
756 SDL_Surface *surface, *symbolSurface;
759 SDL_DestroyTexture(creatureTextures[creatureIt->first]);
760 creatureTextures.erase(creatureIt->first);
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};
768 symbolSurface = TTF_RenderUTF8_Blended(font, toUTF8(symBuf).c_str(), sdlSymCol);
770 blitRect.x += (squareGraphicWidth - symbolSurface->w) / 2;
771 blitRect.y += (squareGraphicHeight - symbolSurface->h) / 2;
773 SDL_BlitSurface(symbolSurface, &symbolRect, surface, &blitRect);
774 SDL_FreeSurface(symbolSurface);
776 creatureTextures.insert(std::map<ID, SDL_Texture*>::value_type(creatureIt->first, SDL_CreateTextureFromSurface(renderer, surface)));
777 SDL_FreeSurface(surface);