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"
17 std::string toUTF8(LPCSTR str)
19 const int cchWideChar = ::MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
20 std::vector<WCHAR> lpw(cchWideChar);
22 const int nUnicodeCount = ::MultiByteToWideChar(CP_ACP, 0, str, -1, &lpw[0], cchWideChar);
23 if(nUnicodeCount <= 0)
28 const int cchMultiByte = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, NULL, 0, NULL, NULL);
29 std::vector<CHAR> lpa(cchMultiByte);
31 const int nMultiCount = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, &lpa[0], cchMultiByte, NULL, NULL);
36 return std::string(&lpa[0]);
39 GameSurfaceSDL::GameSurfaceSDL(GameWorld *gameWorld) : GameSurface(gameWorld)
41 if(SDL_Init(SDL_INIT_VIDEO) < 0) return;
42 window = SDL_CreateWindow(GAME_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE);
43 SDL_GetWindowSurface(window);
46 if(TTF_Init() == -1) return;
47 if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) return;
51 viewCreaturePtr = NULL;
54 windowSurface = SDL_GetWindowSurface(window);
59 GameSurfaceSDL::~GameSurfaceSDL(void)
67 void GameSurfaceSDL::pushAnyKey(void)
72 SDL_PollEvent(&event);
73 } while(event.type != SDL_KEYDOWN);
76 void GameSurfaceSDL::initInterfaces(void)
78 font = TTF_OpenFont("ttf\\ipam.ttf", 18);
94 rwop = SDL_RWFromFile("img\\Title.png", "rb");
95 error = IMG_GetError();
96 titleSurface = IMG_LoadPNG_RW(rwop);
98 renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
101 void GameSurfaceSDL::Redraw()
103 SDL_Rect rect = {0, 0, 0, 0};
105 SDL_GetWindowSize(window, &rect.w, &rect.h);
106 SDL_SetRenderDrawColor(renderer, 100, 100, 0, 255);
107 SDL_FillRect(windowSurface, &rect, SDL_MapRGBA(windowSurface->format, 50, 20, 10, 255));
108 SDL_BlitSurface(titleSurface, &title, windowSurface, &title);
110 if(viewFloorPtr) drawFloor(gameWorld, viewFloorPtr, 0, 0, 20, 20);
111 if(viewCreaturePtr) drawCreatureStatus(viewCreaturePtr);
113 SDL_UpdateWindowSurface(window);
118 void GameSurfaceSDL::ViewCreatureStatus(Creature *creaturePtr)
120 viewCreaturePtr = creaturePtr;
123 viewCreaturePtr = NULL;
126 void GameSurfaceSDL::SetFloor(Floor *floorPtr)
128 viewFloorPtr = floorPtr;
131 void GameSurfaceSDL::drawCreatureStatus(Creature *creaturePtr)
135 enum CREATURE_STATUS_VIEW_POSITION
162 SDL_Rect CreatureStatusViewPosition[POS_MAX] =
188 SDL_Rect CreatureStatusViewRect[POS_MAX];
190 const int STATUS_BUFSIZE = 100;
191 char statusBuf[POS_MAX][STATUS_BUFSIZE];
192 SDL_Surface *statusSurface[POS_MAX];
194 SDL_Rect masterRect = {10, 10, 490, 450};
196 sprintf_s(statusBuf[POS_NAME], STATUS_BUFSIZE, "
\96¼
\91O:%s", creaturePtr->GetName().c_str());
197 sprintf_s(statusBuf[POS_LEVEL], STATUS_BUFSIZE, "LV:%3d", creaturePtr->GetLevel());
198 sprintf_s(statusBuf[POS_HP], STATUS_BUFSIZE, "HP:%5d/%5d", creaturePtr->GetCurHP(), creaturePtr->GetMaxHP());
199 sprintf_s(statusBuf[POS_MP], STATUS_BUFSIZE, "MP:%5d/%5d", creaturePtr->GetCurMP(), creaturePtr->GetMaxMP());
200 sprintf_s(statusBuf[POS_AC], STATUS_BUFSIZE, "AC:%4d", creaturePtr->GetArmorSaving());
201 sprintf_s(statusBuf[POS_EV], STATUS_BUFSIZE, "EV:%4d", creaturePtr->GetEvasionSaving());
202 sprintf_s(statusBuf[POS_VO], STATUS_BUFSIZE, "VO:%4d", creaturePtr->GetVolitionSaving());
203 sprintf_s(statusBuf[POS_GOOD], STATUS_BUFSIZE, "
\91P :%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_GOOD));
204 sprintf_s(statusBuf[POS_EVIL], STATUS_BUFSIZE, "
\88« :%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_EVIL));
205 sprintf_s(statusBuf[POS_ORDER], STATUS_BUFSIZE, "
\92\81\8f\98:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_ORDER));
206 sprintf_s(statusBuf[POS_CHAOS], STATUS_BUFSIZE, "
\8d¬
\93×:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_CHAOS));
207 sprintf_s(statusBuf[POS_BALANCE], STATUS_BUFSIZE, "
\93V
\94\89:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_BALANCE));
208 sprintf_s(statusBuf[POS_STR], STATUS_BUFSIZE, "
\98r
\97Í:%4d", creaturePtr->GetCurrentStatus(CS_STR));
209 sprintf_s(statusBuf[POS_INT], STATUS_BUFSIZE, "
\92m
\97Í:%4d", creaturePtr->GetCurrentStatus(CS_INT));
210 sprintf_s(statusBuf[POS_WIS], STATUS_BUFSIZE, "
\8c«
\82³:%4d", creaturePtr->GetCurrentStatus(CS_WIS));
211 sprintf_s(statusBuf[POS_DEX], STATUS_BUFSIZE, "
\8aí
\97p:%4d", creaturePtr->GetCurrentStatus(CS_DEX));
212 sprintf_s(statusBuf[POS_CON], STATUS_BUFSIZE, "
\91Ï
\8bv:%4d", creaturePtr->GetCurrentStatus(CS_CON));
213 sprintf_s(statusBuf[POS_CHA], STATUS_BUFSIZE, "
\96£
\97Í:%4d", creaturePtr->GetCurrentStatus(CS_CHA));
214 sprintf_s(statusBuf[POS_SOUL], STATUS_BUFSIZE, "
\83\
\83E
\83\8b:%4d", creaturePtr->GetCurrentSoul());
215 sprintf_s(statusBuf[POS_HEIGHT], STATUS_BUFSIZE, "
\90g
\92·:%6dcm", creaturePtr->GetHeight());
216 sprintf_s(statusBuf[POS_WEIGHT], STATUS_BUFSIZE, "
\91Ì
\8fd:%6dkg", creaturePtr->GetWeight());
217 sprintf_s(statusBuf[POS_BODY_SIZE], STATUS_BUFSIZE, "
\91Ì
\8ai:%3d", creaturePtr->GetSize());
219 for(id = 0; id < POS_MAX; id++)
221 statusSurface[id] = TTF_RenderUTF8_Blended(font, toUTF8(statusBuf[id]).c_str(), color);
222 CreatureStatusViewRect[id].x = 0;
223 CreatureStatusViewRect[id].y = 0;
224 CreatureStatusViewRect[id].w = statusSurface[id]->w;
225 CreatureStatusViewRect[id].h = statusSurface[id]->h;
226 CreatureStatusViewPosition[id].x += masterRect.x;
227 CreatureStatusViewPosition[id].y += masterRect.y;
230 SDL_FillRect(windowSurface, &masterRect, SDL_MapRGBA(windowSurface->format, 0, 0, 0, 120));
232 for(id = 0; id < POS_MAX; id++)
234 SDL_BlitSurface(statusSurface[id], &CreatureStatusViewRect[id], windowSurface, &CreatureStatusViewPosition[id]);
237 for(id = 0; id < POS_MAX; id++)
239 SDL_FreeSurface(statusSurface[id]);
244 GAME_COMMAND GameSurfaceSDL::GetCommand(void)
249 while (SDL_PollEvent(&event))
256 key=event.key.keysym.sym;
261 return GAME_COMMAND_EXIT;
263 return GAME_COMMAND_VIEW_PLAYER_STATUS;
265 return GAME_COMMAND_DEBUG_XML_SAVE;
267 return GAME_COMMAND_DEBUG_XML_LOAD;
274 return GAME_COMMAND_EXIT;
280 return GAME_COMMAND_REDRAW;
283 void GameSurfaceSDL::drawFloor(GameWorld *gameWorld, Floor *floorPtr, int x, int y, int w, int h)
285 SDL_Rect symbolRect = {0, 0, 30, 30};
286 SDL_Surface *symbolSurface;
290 for(py = y; py < y + h; py++)
292 for(px = x; px < x + w; px++)
294 if(px < floorPtr->GetWidth() && py < floorPtr->GetHeight())
296 TAG tag = floorPtr->GetSquare(px, py)->getFeatureTag();
297 Color symColor = gameWorld->GetFeature(tag)->GetSymColor();
298 Color backColor = gameWorld->GetFeature(tag)->GetBackColor();
299 SDL_Color sdlSymCol = {(Uint8)symColor.GetRed(), (Uint8)symColor.GetGreen(), (Uint8)symColor.GetBlue(), (Uint8)symColor.GetAlpha()};
300 SDL_Rect blitRect = {240 + px * 24, py * 24 , 24, 24};
301 SDL_FillRect(windowSurface, &blitRect, SDL_MapRGBA(windowSurface->format, (Uint8)backColor.GetRed(), (Uint8)backColor.GetGreen(), (Uint8)backColor.GetBlue(), (Uint8)backColor.GetAlpha()));
302 sprintf_s(symBuf, 5, "%c", gameWorld->GetFeature(tag)->GetSymbol());
303 sprintf_s(symBuf, 5, "%c", 'X');
305 symbolSurface = TTF_RenderUTF8_Blended(font, toUTF8(symBuf).c_str(), sdlSymCol);
306 blitRect.x += (24 - symbolSurface->w) / 2;
307 blitRect.y += (24 - symbolSurface->h) / 2;
308 SDL_BlitSurface(symbolSurface, &symbolRect, windowSurface, &blitRect);
309 SDL_FreeSurface(symbolSurface);
316 void GameSurfaceSDL::FocusFloor(int x, int y)
318 this->focusPoint.Set(x, y);
321 void GameSurfaceSDL::FocusFloor(Coordinates coord)
323 this->focusPoint.Set(coord.GetX(), coord.GetY());
326 void GameSurfaceSDL::Message(std::string message)
328 SDL_Surface *messageSurface;
329 SDL_Rect messageRect = {0, 0, 800, 30};
330 SDL_Rect messagePositon = {100, 100, 0, 0};
332 messageSurface = TTF_RenderUTF8_Blended(font, toUTF8(message.c_str()).c_str(), color);
334 SDL_FillRect(messageSurface, &messageRect, SDL_MapRGBA(messageSurface->format, 0, 0, 0, 120));
336 SDL_BlitSurface(messageSurface, &messageRect, windowSurface, &messagePositon);
337 SDL_FreeSurface(messageSurface);