OSDN Git Service

Move GameSurfaceSDL::windowSurface to private member.
[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 namespace Deeangband
15 {
16
17         std::string toUTF8(LPCSTR str)
18         {
19                 const int cchWideChar = ::MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
20                 std::vector<WCHAR> lpw(cchWideChar);
21
22                 const int nUnicodeCount = ::MultiByteToWideChar(CP_ACP, 0, str, -1, &lpw[0], cchWideChar);
23                 if(nUnicodeCount <= 0)
24                 {
25                         return "";
26                 }
27
28                 const int cchMultiByte = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, NULL, 0, NULL, NULL);
29                 std::vector<CHAR> lpa(cchMultiByte);
30
31                 const int nMultiCount = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, &lpa[0], cchMultiByte, NULL, NULL);
32                 if(nMultiCount <= 0)
33                 {
34                         return "";
35                 }
36                 return std::string(&lpa[0]);
37         }
38
39         GameSurfaceSDL::GameSurfaceSDL(GameWorld *gameWorld) : GameSurface(gameWorld)
40         {
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);
44                 if(!window) return;
45
46                 if(TTF_Init() == -1) return;
47                 if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) return;
48
49                 initInterfaces();
50
51                 viewCreaturePtr = NULL;
52                 viewFloorPtr = NULL;
53
54                 windowSurface = SDL_GetWindowSurface(window);
55
56                 return;
57         }
58
59         GameSurfaceSDL::~GameSurfaceSDL(void)
60         {
61                 IMG_Quit();
62                 TTF_Quit();
63                 SDL_Quit();
64                 return;
65         }
66
67         void GameSurfaceSDL::pushAnyKey(void)
68         {
69                 SDL_Event event;
70                 do 
71                 {
72                         SDL_PollEvent(&event);
73                 } while(event.type != SDL_KEYDOWN);
74         }
75
76         void GameSurfaceSDL::initInterfaces(void)
77         {
78                 font = TTF_OpenFont("ttf\\ipam.ttf", 18);
79                 src.x = 0;
80                 src.y = 0;
81                 src.w = 300;
82                 src.h = 200;
83                 title.x = 0;
84                 title.y = 0;
85                 title.w = 512;
86                 title.h = 512;
87                 color.r = 255;
88                 color.g = 223;
89                 color.b = 200;
90                 color.a = 255;
91
92                 if(!font) exit(1);
93
94                 rwop = SDL_RWFromFile("img\\Title.png", "rb");
95                 error = IMG_GetError();
96                 titleSurface = IMG_LoadPNG_RW(rwop);
97
98                 renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
99         }
100
101         void GameSurfaceSDL::Redraw()
102         {
103                 SDL_Rect rect = {0, 0, 0, 0};
104
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); 
109
110                 if(viewFloorPtr) drawFloor(gameWorld, viewFloorPtr, 0, 0, 20, 20);
111                 if(viewCreaturePtr) drawCreatureStatus(viewCreaturePtr);
112
113                 SDL_UpdateWindowSurface(window);
114
115                 return;
116         }
117
118         void GameSurfaceSDL::ViewCreatureStatus(Creature *creaturePtr)
119         {
120                 viewCreaturePtr = creaturePtr;
121                 Redraw();
122                 pushAnyKey();
123                 viewCreaturePtr = NULL;
124         }
125
126         void GameSurfaceSDL::SetFloor(Floor *floorPtr)
127         {
128                 viewFloorPtr = floorPtr;
129         }
130
131         void GameSurfaceSDL::drawCreatureStatus(Creature *creaturePtr)
132         {
133                 int id;
134
135                 enum CREATURE_STATUS_VIEW_POSITION
136                 {
137                         POS_NAME,
138                         POS_LEVEL,
139                         POS_HP,
140                         POS_MP,
141                         POS_AC,
142                         POS_EV,
143                         POS_VO,
144                         POS_STR,
145                         POS_INT,
146                         POS_WIS,
147                         POS_DEX,
148                         POS_CON,
149                         POS_CHA,
150                         POS_SOUL,
151                         POS_GOOD,
152                         POS_EVIL,
153                         POS_ORDER,
154                         POS_CHAOS,
155                         POS_BALANCE,
156                         POS_HEIGHT,
157                         POS_WEIGHT,
158                         POS_BODY_SIZE,
159                         POS_MAX
160                 };
161
162                 SDL_Rect CreatureStatusViewPosition[POS_MAX] =
163                 {
164                         {10, 10, 0, 0},
165                         {10, 30, 0, 0},
166                         {10, 50, 0, 0},
167                         {10, 70, 0, 0},
168                         {10, 90, 0, 0},
169                         {10, 110, 0, 0},
170                         {10, 130, 0, 0},
171                         {200, 30, 0, 0},
172                         {200, 50, 0, 0},
173                         {200, 70, 0, 0},
174                         {200, 90, 0, 0},
175                         {200, 110, 0, 0},
176                         {200, 130, 0, 0},
177                         {200, 150, 0, 0},
178                         {200, 170, 0, 0},
179                         {200, 190, 0, 0},
180                         {200, 210, 0, 0},
181                         {200, 230, 0, 0},
182                         {200, 250, 0, 0},
183                         {10, 150, 0, 0},
184                         {10, 170, 0, 0},
185                         {10, 190, 0, 0},
186                 };
187
188                 SDL_Rect CreatureStatusViewRect[POS_MAX];
189
190                 const int STATUS_BUFSIZE = 100;
191                 char statusBuf[POS_MAX][STATUS_BUFSIZE];
192                 SDL_Surface *statusSurface[POS_MAX];
193
194                 SDL_Rect masterRect = {10, 10, 490, 450};
195
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()); 
218
219                 for(id = 0; id < POS_MAX; id++)
220                 {
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;
228                 }
229
230                 SDL_FillRect(windowSurface, &masterRect, SDL_MapRGBA(windowSurface->format, 0, 0, 0, 120));
231
232                 for(id = 0; id < POS_MAX; id++)
233                 {
234                         SDL_BlitSurface(statusSurface[id], &CreatureStatusViewRect[id], windowSurface, &CreatureStatusViewPosition[id]); 
235                 }
236
237                 for(id = 0; id < POS_MAX; id++)
238                 {
239                         SDL_FreeSurface(statusSurface[id]);
240                 }
241                 return;
242         }
243
244         GAME_COMMAND GameSurfaceSDL::GetCommand(void)
245         {
246                 SDL_Event event;
247                 SDL_Keycode key;
248
249                 while (SDL_PollEvent(&event))
250                 {
251                         switch(event.type)
252                         {
253
254                         case SDL_KEYDOWN:
255                                 {
256                                         key=event.key.keysym.sym;
257
258                                         switch(key)
259                                         {
260                                         case SDLK_ESCAPE:
261                                                 return GAME_COMMAND_EXIT;
262                                         case SDLK_c:
263                                                 return GAME_COMMAND_VIEW_PLAYER_STATUS;
264                                         case SDLK_F11:
265                                                 return GAME_COMMAND_DEBUG_XML_SAVE;
266                                         case SDLK_F12:
267                                                 return GAME_COMMAND_DEBUG_XML_LOAD;
268                                         }
269
270                                 }
271                                 break;
272
273                         case SDL_QUIT:
274                                 return GAME_COMMAND_EXIT;
275                                 break;
276
277                         }
278                 }
279
280                 return GAME_COMMAND_REDRAW;
281         }
282
283         void GameSurfaceSDL::drawFloor(GameWorld *gameWorld, Floor *floorPtr, int x, int y, int w, int h)
284         {       
285                 SDL_Rect symbolRect = {0, 0, 30, 30};
286                 SDL_Surface *symbolSurface;
287                 char symBuf[5];
288
289                 int px, py;
290                 for(py = y; py < y + h; py++)
291                 {
292                         for(px = x; px < x + w; px++)
293                         {
294                                 if(px < floorPtr->GetWidth() && py < floorPtr->GetHeight())
295                                 {
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'); 
304
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);
310                                 }
311                         }
312                 }
313                 return;
314         }
315
316         void GameSurfaceSDL::FocusFloor(int x, int y)
317         {       
318                 this->focusPoint.Set(x, y);
319         }
320
321         void GameSurfaceSDL::FocusFloor(Coordinates coord)
322         {       
323                 this->focusPoint.Set(coord.GetX(), coord.GetY());
324         }
325
326         void GameSurfaceSDL::Message(std::string message)
327         {
328                 SDL_Surface *messageSurface;
329                 SDL_Rect messageRect = {0, 0, 800, 30};
330                 SDL_Rect messagePositon = {100, 100, 0, 0}; 
331
332                 messageSurface = TTF_RenderUTF8_Blended(font, toUTF8(message.c_str()).c_str(), color);
333
334                 SDL_FillRect(messageSurface, &messageRect, SDL_MapRGBA(messageSurface->format, 0, 0, 0, 120));
335
336                 SDL_BlitSurface(messageSurface, &messageRect, windowSurface, &messagePositon); 
337                 SDL_FreeSurface(messageSurface);
338         }
339
340 }