-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackship.c
241 lines (211 loc) · 5.65 KB
/
blackship.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
ゲーム名 : blackship
制作者 : 石原 実
制作日時 : 2021/07/01
ファイル名 : blackship
機能 : メインシステム、モジュール呼び出し元
*/
#include "blackship.h"
#include <stdio.h>
void exitGame(ExitCode exitStatus);
static int InputEvent(void* data);
void getInputFromEvent(SDL_Event event);
SDL_mutex* mxLock;
InputInfo inputInfo = { SDL_FALSE, SDL_FALSE, SDL_FALSE, SDL_FALSE, SDL_FALSE, SDL_FALSE, 0, 0 };
GameMode gameMode = MAIN_WINDOW;
PlayerInfo playerInfo;
SDL_bool isCreatedPlayer = SDL_FALSE;
/* main */
int main(int argc, char* argv[])
{
int drawingDrutation = 100;
//初期化
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
exitGame(FAILED_INIT_SDL);
}
SDL_Thread* eventThread = SDL_CreateThread(InputEvent, "InputEvent", &inputInfo);
if (eventThread == NULL) {
exitGame(FAIELD_INIT_THREAD);
}
SDL_DetachThread(eventThread);
mxLock = SDL_CreateMutex();
if (mxLock == NULL) {
exitGame(FAILED_INIT_MXLOCK);
}
if (InitWindow() < 0) {
exitGame(FAILED_INIT_WINDOW);
}
if (initMainWin() < 0) {
exitGame(FAILED_INIT_MAIN_WIN);
}
if (initPartsWin() < 0) {
exitGame(FAILED_INIT_PARTS_WIN);
}
initActionWin();
while (1) {
if (gameMode == EXIT_GAME)
break;
SDL_PumpEvents();
//各モード処理
if (0 == SDL_LockMutex(mxLock)) {
SDL_RenderClear(render);
switch (gameMode) {
case MAIN_WINDOW:
drawMainWin();
break;
case PARTS_WINDOW:
drawPartsWin();
break;
case STAGE_WINDOW:
break;
case ACTION_WINDOW:
if (isCreatedPlayer) {
drawActionWin(drawingDrutation);
} else {
gameMode = MAIN_WINDOW;
}
break;
case EXIT_GAME:
exitGame(NONE_ERROR);
}
SDL_RenderPresent(render);
SDL_UnlockMutex(mxLock);
}
SDL_Delay(40); //動作が不安定になるため20->40に変更
}
/** 終了処理 **/
return 0;
}
void exitGame(ExitCode exitCode)
{
switch (exitCode) {
case NONE_ERROR:
DestroyWindow();
SDL_DestroyMutex(mxLock);
SDL_Quit();
exit(0);
case FAILED_INIT_ACTION_WIN:
destroyActionWin();
case FAILED_INIT_PARTS_WIN:
destroyPartsWin();
case FAILED_INIT_MAIN_WIN:
destroyMainWin();
case FAILED_INIT_WINDOW:
DestroyWindow();
case FAILED_INIT_MXLOCK:
SDL_DestroyMutex(mxLock);
case FAIELD_INIT_THREAD:
case FAILED_INIT_SDL:
SDL_Quit();
}
exit(1);
}
int InputEvent(void* arg)
{
SDL_Event event;
while (gameMode != EXIT_GAME) {
if (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0) {
getInputFromEvent(event);
}
}
return 0;
}
void getInputFromEvent(SDL_Event event)
{
switch (event.type) {
case SDL_QUIT:
/** スレッド終了指示 **/
gameMode = EXIT_GAME;
break;
case SDL_KEYDOWN:
if (event.key.repeat)
break;
/** キーが押された方向を設定 **/
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
inputInfo.escape = SDL_TRUE;
gameMode = EXIT_GAME;
break;
case SDLK_RIGHT:
inputInfo.right = SDL_TRUE;
break;
case SDLK_LEFT:
inputInfo.left = SDL_TRUE;
break;
case SDLK_a:
inputInfo.left = SDL_TRUE;
break;
case SDLK_d:
inputInfo.right = SDL_TRUE;
break;
case SDLK_SPACE:
inputInfo.fire = SDL_TRUE;
break;
case SDLK_q:
inputInfo.skill = SDL_TRUE;
break;
default:
break;
}
break;
case SDL_KEYUP:
if (event.key.repeat)
break;
/* 離されたときは解除 */
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
break;
case SDLK_RIGHT:
inputInfo.right = SDL_FALSE;
break;
case SDLK_LEFT:
inputInfo.left = SDL_FALSE;
break;
case SDLK_a:
inputInfo.left = SDL_FALSE;
break;
case SDLK_d:
inputInfo.right = SDL_FALSE;
break;
case SDLK_SPACE:
inputInfo.fire = SDL_FALSE;
break;
case SDLK_q:
inputInfo.skill = SDL_FALSE;
break;
default:
break;
}
break;
case SDL_MOUSEMOTION: // マウスが移動した時
inputInfo.mouseX = event.motion.x;
inputInfo.mouseY = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
inputInfo.mouseX = event.button.x;
inputInfo.mouseY = event.button.y;
switch (event.button.button) {
case SDL_BUTTON_LEFT:
inputInfo.mouseL = SDL_TRUE;
break;
case SDL_BUTTON_MIDDLE:
break;
case SDL_BUTTON_RIGHT:
break;
}
break;
case SDL_MOUSEBUTTONUP:
inputInfo.mouseX = event.button.x;
inputInfo.mouseY = event.button.y;
switch (event.button.button) {
case SDL_BUTTON_LEFT:
inputInfo.mouseL = SDL_FALSE;
break;
case SDL_BUTTON_MIDDLE:
break;
case SDL_BUTTON_RIGHT:
break;
}
break;
}
}