-
Notifications
You must be signed in to change notification settings - Fork 8
/
DxLib.cpp
291 lines (251 loc) · 6.53 KB
/
DxLib.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "DxLib.h"
SDL_Joystick* joystick;
bool keysHeld[SDLK_LAST];
bool sound = true;
void deinit();
int DxLib_Init()
{
atexit(deinit);
setlocale(LC_CTYPE, "ja_JP.UTF-8");
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
return -1;
}
if (!
(screen =
SDL_SetVideoMode(480 /*(int)fmax/100 */ ,
420 /*(int)fymax/100 */ , 32,
SDL_SWSURFACE | SDL_DOUBLEBUF))) {
SDL_Quit();
return -1;
}
SDL_WM_SetCaption("Syobon Action (しょぼんのアクション)",
NULL);
SDL_ShowCursor(SDL_DISABLE);
if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
{
fprintf(stderr, "Unable to init SDL_img: %s\n", IMG_GetError());
return -1;
}
//Initialize font
if (TTF_Init() == -1) {
fprintf(stderr, "Unable to init SDL_ttf: %s\n", TTF_GetError());
return -1;
}
//Audio Rate, Audio Format, Audio Channels, Audio Buffers
#define AUDIO_CHANNELS 4
if (sound && Mix_OpenAudio(22050, AUDIO_S16SYS, AUDIO_CHANNELS, 1024)) {
fprintf(stderr, "Unable to init SDL_mixer: %s\n", Mix_GetError());
sound = false;
}
//Try to get a joystick
joystick = SDL_JoystickOpen(0);
for (int i = 0; i < SDLK_LAST; i++)
keysHeld[i] = false;
for (int i = 0; i < FONT_MAX; i++)
font[i] = NULL;
srand(time(NULL));
return 0;
}
//Main screen
SDL_Surface *screen;
//Fonts
byte fontsize = 0;
TTF_Font *font[FONT_MAX];
//Strings
void SetFontSize(byte size)
{
fontsize = size;
if (font[size] == NULL) {
font[size] = TTF_OpenFont("res/sazanami-gothic.ttf", size);
if (font[size] == NULL) {
printf("Unable to load font: %s\n", TTF_GetError());
exit(1);
}
}
}
byte fontType = DX_FONTTYPE_NORMAL;
void ChangeFontType(byte type)
{
fontType = type;
}
void DrawString(int a, int b, const char *x, Uint32 c)
{
SDL_Color color = { c >> 16, c >> 8, c };
SDL_Surface *rendered = TTF_RenderUTF8_Solid(font[fontsize], x, color);
if (fontType == DX_FONTTYPE_EDGE) {
SDL_Color blk = { 0, 0, 0 };
SDL_Surface *shadow = TTF_RenderUTF8_Solid(font[fontsize], x, blk);
DrawGraphZ(a - 1, b - 1, shadow);
DrawGraphZ(a, b - 1, shadow);
DrawGraphZ(a + 1, b - 1, shadow);
DrawGraphZ(a - 1, b, shadow);
DrawGraphZ(a + 1, b, shadow);
DrawGraphZ(a - 1, b + 1, shadow);
DrawGraphZ(a, b + 1, shadow);
DrawGraphZ(a + 1, b + 1, shadow);
SDL_FreeSurface(shadow);
}
DrawGraphZ(a, b, rendered);
SDL_FreeSurface(rendered);
}
void DrawFormatString(int a, int b, Uint32 color, const char *str, ...)
{
va_list args;
char *newstr = new char[strlen(str) + 16];
va_start(args, str);
vsprintf(newstr, str, args);
va_end(args);
DrawString(a, b, newstr, color);
delete newstr;
}
//void DrawFormatString(int a, int b, int c
//Key Aliases
#define KEY_INPUT_ESCAPE SDLK_ESCAPE
bool ex = false;
void UpdateKeys()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
keysHeld[event.key.keysym.sym] = true;
break;
case SDL_KEYUP:
keysHeld[event.key.keysym.sym] = false;
break;
case SDL_JOYAXISMOTION:
if(event.jaxis.which == 0)
{
if(event.jaxis.axis == JOYSTICK_XAXIS)
{
if(event.jaxis.value < 0) keysHeld[SDLK_LEFT] = true;
else if(event.jaxis.value > 0) keysHeld[SDLK_RIGHT] = true;
else {
keysHeld[SDLK_LEFT] = false;
keysHeld[SDLK_RIGHT] = false;
}
}
else if(event.jaxis.axis == JOYSTICK_YAXIS)
{
if(event.jaxis.value < 0) keysHeld[SDLK_UP] = true;
else if(event.jaxis.value > 0) keysHeld[SDLK_DOWN] = true;
else {
keysHeld[SDLK_UP] = false;
keysHeld[SDLK_DOWN] = false;
}
}
}
break;
case SDL_QUIT:
ex = true;
break;
}
}
}
byte ProcessMessage()
{
return ex;
}
byte CheckHitKey(int key)
{
if(key == SDLK_z && keysHeld[SDLK_SEMICOLON]) return true;
return keysHeld[key];
}
byte WaitKey()
{
SDL_Event event;
while (true) {
while (SDL_PollEvent(&event))
if (event.type == SDL_KEYDOWN)
return event.key.keysym.sym;
}
}
/*Uint32 GetColor(byte r, byte g, byte b)
{
return r << 8 * 3 | g << 8 * 2 | b << 8 | 0xFF;
}*/
void DrawGraphZ(int a, int b, SDL_Surface * mx)
{
if(mx)
{
SDL_Rect offset;
offset.x = a;
offset.y = b;
SDL_BlitSurface(mx, NULL, screen, &offset);
}
}
void DrawTurnGraphZ(int a, int b, SDL_Surface * mx)
{
if(mx)
{
SDL_Rect offset;
offset.x = a;
offset.y = b;
SDL_Surface *flipped = zoomSurface(mx, -1, 1, 0);
SDL_SetColorKey(flipped, SDL_SRCCOLORKEY,
SDL_MapRGB(flipped->format, 9 * 16 + 9, 255, 255));
SDL_BlitSurface(flipped, NULL, screen, &offset);
SDL_FreeSurface(flipped);
}
}
void DrawVertTurnGraph(int a, int b, SDL_Surface * mx)
{
if(mx)
{
SDL_Rect offset;
offset.x = a - mx->w / 2;
offset.y = b - mx->h / 2;
SDL_Surface *flipped = rotozoomSurface(mx, 180, 1, 0);
SDL_SetColorKey(flipped, SDL_SRCCOLORKEY,
SDL_MapRGB(flipped->format, 9 * 16 + 9, 255, 255));
SDL_BlitSurface(flipped, NULL, screen, &offset);
SDL_FreeSurface(flipped);
}
}
SDL_Surface *DerivationGraph(int srcx, int srcy, int width, int height,
SDL_Surface * src)
{
SDL_Surface *img =
SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
screen->format->BitsPerPixel,
src->format->Rmask, src->format->Bmask,
src->format->Gmask, src->format->Amask);
SDL_Rect offset;
offset.x = srcx;
offset.y = srcy;
offset.w = width;
offset.h = height;
SDL_BlitSurface(src, &offset, img, NULL);
SDL_SetColorKey(img, SDL_SRCCOLORKEY,
SDL_MapRGB(img->format, 9 * 16 + 9, 255, 255));
return img;
}
//Noticably different than the original
SDL_Surface *LoadGraph(const char *filename)
{
SDL_Surface *image = IMG_Load(filename);
if (image) return image;
fprintf(stderr, "Error: Unable to load %s: %s\n", filename, IMG_GetError());
exit(1);
}
void PlaySoundMem(Mix_Chunk* s, int l)
{
if(sound) Mix_PlayChannel(-1, s, l);
}
Mix_Chunk* LoadSoundMem(const char* f)
{
if(!sound) return NULL;
Mix_Chunk* s = Mix_LoadWAV(f);
if(s) return s;
fprintf(stderr, "Error: Unable to load sound %s: %s\n", f, Mix_GetError());
return NULL;
}
Mix_Music* LoadMusicMem(const char* f)
{
if(!sound) return NULL;
Mix_Music* m = Mix_LoadMUS(f);
if(m) return m;
fprintf(stderr, "Error: Unable to load music %s: %s\n", f, Mix_GetError());
return NULL;
}