OSDN Git Service

Change implementation of ViewFloor() and drawFloor() in GameSurfaceSDL class.
[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 std::string toUTF8(LPCSTR str)
15 {
16         const int cchWideChar = ::MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
17         std::vector<WCHAR> lpw(cchWideChar);
18
19         const int nUnicodeCount = ::MultiByteToWideChar(CP_ACP, 0, str, -1, &lpw[0], cchWideChar);
20         if(nUnicodeCount <= 0)
21         {
22                 return "";
23         }
24
25         const int cchMultiByte = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, NULL, 0, NULL, NULL);
26         std::vector<CHAR> lpa(cchMultiByte);
27
28         const int nMultiCount = ::WideCharToMultiByte(CP_UTF8, 0, &lpw[0], -1, &lpa[0], cchMultiByte, NULL, NULL);
29         if(nMultiCount <= 0)
30         {
31                 return "";
32         }
33         return std::string(&lpa[0]);
34 }
35
36 GameSurfaceSDL::GameSurfaceSDL(void)
37 {
38         if(SDL_Init(SDL_INIT_VIDEO) < 0) return;
39         window = SDL_CreateWindow(GAME_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE);
40         SDL_GetWindowSurface(window);
41         if(!window) return;
42
43         if(TTF_Init() == -1) return;
44         if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) return;
45
46         initInterfaces();
47
48         viewCreaturePtr = NULL;
49         viewFloorPtr = NULL;
50
51         return;
52 }
53
54 GameSurfaceSDL::~GameSurfaceSDL(void)
55 {
56         IMG_Quit();
57         TTF_Quit();
58         SDL_Quit();
59         return;
60 }
61
62 void GameSurfaceSDL::pushAnyKey(void)
63 {
64         SDL_Event event;
65         SDL_Keycode key;
66
67         while(true)
68         {
69                 while (SDL_PollEvent(&event))
70                 {
71                         key=event.key.keysym.sym;
72                         if(event.type == SDL_KEYDOWN) return;
73                 }
74         }
75 }
76
77 void GameSurfaceSDL::initInterfaces(void)
78 {
79         font = TTF_OpenFont("ttf\\ipam.ttf", 18);
80         src.x = 0;
81         src.y = 0;
82         src.w = 300;
83         src.h = 200;
84         title.x = 0;
85         title.y = 0;
86         title.w = 512;
87         title.h = 512;
88         color.r = 255;
89         color.g = 223;
90         color.b = 200;
91         color.a = 255;
92
93         if(!font) exit(1);
94
95         rwop = SDL_RWFromFile("img\\Title.png", "rb");
96         error = IMG_GetError();
97         titleSurface = IMG_LoadPNG_RW(rwop);
98
99         renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
100 }
101
102 void GameSurfaceSDL::Redraw()
103 {
104         Creature dammy;
105         SDL_Surface *windowSurface = SDL_GetWindowSurface(window);
106         SDL_Rect rect = {0, 0, 0, 0};
107
108         SDL_GetWindowSize(window, &rect.w, &rect.h);
109
110         SDL_SetRenderDrawColor(renderer, 100, 100, 0, 255);
111
112         SDL_FillRect(windowSurface, &rect, SDL_MapRGBA(windowSurface->format, 50, 20, 10, 255));
113
114         SDL_BlitSurface(titleSurface, &title, windowSurface, &title); 
115
116         if(viewFloorPtr) drawFloor(viewFloorPtr, 0, 0, 20, 20);
117
118         if(viewCreaturePtr) drawCreatureStatus(viewCreaturePtr);
119
120         SDL_UpdateWindowSurface(window);
121
122         return;
123 }
124
125 void GameSurfaceSDL::ViewCreatureStatus(Creature *creaturePtr)
126 {
127         viewCreaturePtr = creaturePtr;
128         Redraw();
129         pushAnyKey();
130         viewCreaturePtr = NULL;
131 }
132
133 void GameSurfaceSDL::SetFloor(Floor *floorPtr)
134 {
135         viewFloorPtr = floorPtr;
136 }
137
138 void GameSurfaceSDL::drawCreatureStatus(Creature *creaturePtr)
139 {
140         int id;
141         SDL_Surface *windowSurface = SDL_GetWindowSurface(window);
142
143         enum CREATURE_STATUS_VIEW_POSITION
144         {
145                 POS_NAME,
146                 POS_LEVEL,
147                 POS_HP,
148                 POS_MP,
149                 POS_AC,
150                 POS_EV,
151                 POS_VO,
152                 POS_STR,
153                 POS_INT,
154                 POS_WIS,
155                 POS_DEX,
156                 POS_CON,
157                 POS_CHA,
158                 POS_SOUL,
159                 POS_GOOD,
160                 POS_EVIL,
161                 POS_ORDER,
162                 POS_CHAOS,
163                 POS_BALANCE,
164                 POS_HEIGHT,
165                 POS_WEIGHT,
166                 POS_BODY_SIZE,
167                 POS_MAX
168         };
169
170         SDL_Rect CreatureStatusViewPosition[POS_MAX] =
171         {
172                 {10, 10, 0, 0},
173                 {10, 30, 0, 0},
174                 {10, 50, 0, 0},
175                 {10, 70, 0, 0},
176                 {10, 90, 0, 0},
177                 {10, 110, 0, 0},
178                 {10, 130, 0, 0},
179                 {200, 30, 0, 0},
180                 {200, 50, 0, 0},
181                 {200, 70, 0, 0},
182                 {200, 90, 0, 0},
183                 {200, 110, 0, 0},
184                 {200, 130, 0, 0},
185                 {200, 150, 0, 0},
186                 {200, 170, 0, 0},
187                 {200, 190, 0, 0},
188                 {200, 210, 0, 0},
189                 {200, 230, 0, 0},
190                 {200, 250, 0, 0},
191                 {10, 150, 0, 0},
192                 {10, 170, 0, 0},
193                 {10, 190, 0, 0},
194         };
195
196         SDL_Rect CreatureStatusViewRect[POS_MAX];
197
198         const int STATUS_BUFSIZE = 100;
199         char statusBuf[POS_MAX][STATUS_BUFSIZE];
200         SDL_Surface *statusSurface[POS_MAX];
201
202         SDL_Rect masterRect = {10, 10, 490, 450};
203
204         sprintf_s(statusBuf[POS_NAME], STATUS_BUFSIZE, "\96¼\91O:%s", creaturePtr->GetName().c_str()); 
205         sprintf_s(statusBuf[POS_LEVEL], STATUS_BUFSIZE, "LV:%3d", creaturePtr->GetLevel()); 
206         sprintf_s(statusBuf[POS_HP], STATUS_BUFSIZE, "HP:%5d/%5d", creaturePtr->GetCurHP(), creaturePtr->GetMaxHP()); 
207         sprintf_s(statusBuf[POS_MP], STATUS_BUFSIZE, "MP:%5d/%5d", creaturePtr->GetCurMP(), creaturePtr->GetMaxMP()); 
208         sprintf_s(statusBuf[POS_AC], STATUS_BUFSIZE, "AC:%4d", creaturePtr->GetArmorSaving()); 
209         sprintf_s(statusBuf[POS_EV], STATUS_BUFSIZE, "EV:%4d", creaturePtr->GetEvasionSaving()); 
210         sprintf_s(statusBuf[POS_VO], STATUS_BUFSIZE, "VO:%4d", creaturePtr->GetVolitionSaving()); 
211         sprintf_s(statusBuf[POS_GOOD], STATUS_BUFSIZE, " \91P :%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_GOOD)); 
212         sprintf_s(statusBuf[POS_EVIL], STATUS_BUFSIZE, " \88« :%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_EVIL)); 
213         sprintf_s(statusBuf[POS_ORDER], STATUS_BUFSIZE, "\92\81\8f\98:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_ORDER)); 
214         sprintf_s(statusBuf[POS_CHAOS], STATUS_BUFSIZE, "\8d¬\93×:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_CHAOS)); 
215         sprintf_s(statusBuf[POS_BALANCE], STATUS_BUFSIZE, "\93V\94\89:%4d", creaturePtr->GetDiscipilneRank(DISCIPLINE_TYPE_BALANCE)); 
216         sprintf_s(statusBuf[POS_STR], STATUS_BUFSIZE, "\98r\97Í:%4d", creaturePtr->GetCurrentStatus(CS_STR)); 
217         sprintf_s(statusBuf[POS_INT], STATUS_BUFSIZE, "\92m\97Í:%4d", creaturePtr->GetCurrentStatus(CS_INT)); 
218         sprintf_s(statusBuf[POS_WIS], STATUS_BUFSIZE, "\8c«\82³:%4d", creaturePtr->GetCurrentStatus(CS_WIS)); 
219         sprintf_s(statusBuf[POS_DEX], STATUS_BUFSIZE, "\8aí\97p:%4d", creaturePtr->GetCurrentStatus(CS_DEX)); 
220         sprintf_s(statusBuf[POS_CON], STATUS_BUFSIZE, "\91Ï\8bv:%4d", creaturePtr->GetCurrentStatus(CS_CON)); 
221         sprintf_s(statusBuf[POS_CHA], STATUS_BUFSIZE, "\96£\97Í:%4d", creaturePtr->GetCurrentStatus(CS_CHA)); 
222         sprintf_s(statusBuf[POS_SOUL], STATUS_BUFSIZE, "\83\\83E\83\8b:%4d", creaturePtr->GetCurrentSoul()); 
223         sprintf_s(statusBuf[POS_HEIGHT], STATUS_BUFSIZE, "\90g\92·:%6dcm", creaturePtr->GetHeight()); 
224         sprintf_s(statusBuf[POS_WEIGHT], STATUS_BUFSIZE, "\91Ì\8fd:%6dkg", creaturePtr->GetWeight()); 
225         sprintf_s(statusBuf[POS_BODY_SIZE], STATUS_BUFSIZE, "\91Ì\8ai:%3d", creaturePtr->GetSize()); 
226
227         for(id = 0; id < POS_MAX; id++)
228         {
229                 statusSurface[id] = TTF_RenderUTF8_Blended(font, toUTF8(statusBuf[id]).c_str(), color);
230                 CreatureStatusViewRect[id].x = 0;
231                 CreatureStatusViewRect[id].y = 0;
232                 CreatureStatusViewRect[id].w = statusSurface[id]->w;
233                 CreatureStatusViewRect[id].h = statusSurface[id]->h;
234                 CreatureStatusViewPosition[id].x += masterRect.x;
235                 CreatureStatusViewPosition[id].y += masterRect.y;
236         }
237
238         SDL_FillRect(windowSurface, &masterRect, SDL_MapRGBA(windowSurface->format, 0, 0, 0, 120));
239
240         for(id = 0; id < POS_MAX; id++)
241         {
242                 SDL_BlitSurface(statusSurface[id], &CreatureStatusViewRect[id], windowSurface, &CreatureStatusViewPosition[id]); 
243         }
244
245         for(id = 0; id < POS_MAX; id++)
246         {
247                 SDL_FreeSurface(statusSurface[id]);
248         }
249         return;
250 }
251
252 GAME_COMMAND GameSurfaceSDL::GetCommand(void)
253 {
254         SDL_Event event;
255         SDL_Keycode key;
256
257         while (SDL_PollEvent(&event))
258         {
259                 switch(event.type)
260                 {
261
262                 case SDL_KEYDOWN:
263                         {
264                                 key=event.key.keysym.sym;
265
266                                 switch(key)
267                                 {
268                                 case SDLK_ESCAPE:
269                                         return GAME_COMMAND_EXIT;
270                                 case SDLK_c:
271                                         return GAME_COMMAND_VIEW_PLAYER_STATUS;
272                                 }
273
274                         }
275                         break;
276
277                 case SDL_QUIT:
278                         return GAME_COMMAND_EXIT;
279                         break;
280
281                 }
282         }
283
284         return GAME_COMMAND_REDRAW;
285 }
286
287 void GameSurfaceSDL::drawFloor(Floor *floorPtr, int x, int y, int w, int h)
288 {       
289         SDL_Surface *windowSurface = SDL_GetWindowSurface(window);
290         SDL_Rect symbolRect = {0, 0, 30, 30};
291         SDL_Surface *symbolSurface;
292         char symBuf[5];
293
294         int px, py;
295         for(py = y; py < y + h; py++)
296         {
297                 for(px = x; px < x + w; px++)
298                 {
299                         if(px < floorPtr->GetWidth() && py < floorPtr->GetHeight())
300                         {
301                                 TAG tag = floorPtr->GetSquare(px, py)->getFeatureTag();
302                                 Color symColor = floorPtr->GetGameWorld()->GetFeature(tag)->GetSymColor();
303                                 Color backColor = floorPtr->GetGameWorld()->GetFeature(tag)->GetBackColor();
304                                 SDL_Color sdlSymCol = {symColor.r, symColor.g, symColor.b, symColor.a};
305                                 SDL_Rect blitRect = {240 + px * 24, py * 24 , 24, 24};
306                                 SDL_FillRect(windowSurface, &blitRect, SDL_MapRGBA(windowSurface->format, backColor.r, backColor.g, backColor.b, backColor.a));
307                                 sprintf_s(symBuf, 5, "%c", floorPtr->GetGameWorld()->GetFeature(tag)->GetSymbol()); 
308
309                                 symbolSurface = TTF_RenderUTF8_Blended(font, toUTF8(symBuf).c_str(), sdlSymCol);
310                                 blitRect.x += (24 - symbolSurface->w) / 2;
311                                 blitRect.y += (24 - symbolSurface->h) / 2;
312                                 SDL_BlitSurface(symbolSurface, &symbolRect, windowSurface, &blitRect); 
313                                 SDL_FreeSurface(symbolSurface);
314                         }
315                 }
316         }
317         return;
318 }
319
320 void GameSurfaceSDL::ViewFloor(int x, int y)
321 {       
322         return;
323 }