File: nt_msg.c

package info (click to toggle)
xsystem35 2.17.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 8,096 kB
  • sloc: ansic: 38,159; java: 4,085; xml: 249; sh: 134; python: 15; makefile: 12
file content (470 lines) | stat: -rw-r--r-- 10,213 bytes parent folder | download
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <SDL.h>

#include "portab.h"
#include "system.h"
#include "utfsjis.h"
#include "ags.h"
#include "nact.h"
#include "input.h"
#include "drawtext.h"
#include "night.h"
#include "nt_msg.h"
#include "sprite.h"
#include "sactcg.h"
#include "sactstring.h"


extern int ntsel_dosel(void);

#define SNAME_RYO "亮    "
#define SNAME_RYO_DEF "亮"

// Normal message window
static const SDL_Rect msgframe_0 = {
	.x = 6,
	.y = 347,
	.w = 628,
	.h = 125
};

// Full screen message window
static const SDL_Rect msgframe_1 = {
	.x = 0,
	.y = 0,
	.w = 640,
	.h = 480
};

// Normal message (with face) window
static const SDL_Rect msgframe_2 = {
	.x = 6 + 160,
	.y = 347,
	.w = 628 - 160,
	.h = 125
};

// キー入力アニメーション位置
#define HAKANIM_LOC_X 620
#define HAKANIM_LOC_Y 450
// キー入力アニメーション間隔
#define HAKANIM_INTERVAL 250

// デフォルトメッセージフォントサイズ
#define MSG_DEFFONTSIZE 24


static void ntmsg_out(int wNum, int wSize, int wColorR, int wColorG, int wColorB, int wFont, int wSpeed, int wLineSpace);
static void set_align(char *msg, sprite_t *sp, int wSize);
static uint8_t *get_char(uint8_t *msg, char *mbuf, int bufmax);
static void cb_keyrelease(agsevent_t *e);
static void cb_mousemove(agsevent_t *e);
static void setup_hakanim();
static void hakanim(int i);
static int ntmsg_keywait();
static void ntmsg_clear(int wNum);


// 初期化
void ntmsg_init() {
	sprite_t *sp;

	// メッセージスプライト
	sp = night.sp[SPNO_MSGFRAME_FG] = nt_sp_msg_new(SPNO_MSGFRAME_FG, 0, 0, main_surface->w, main_surface->h);
	nt_sp_add_updatelist(sp);
	
	// メッセージ背景用CG
	sp = night.sp[SPNO_MSGFRAME_BG] = nt_sp_new(SPNO_MSGFRAME_BG, CGNO_MSGFRAME_LCG, 0, 0, SPRITE_NORMAL);
	nt_sp_add_updatelist(sp);
	
	// 文字背景CG作成
	nt_scg_create(CGNO_MSGFR_BG,
		   main_surface->w, main_surface->h,
		   0, 0, 0, 255);
	// night.cg[CGNO_MSGFR_BG]->refcnt++;
	
	// 文字背景CGのためのsprite
	sp = night.sp[SPNO_MSGBG] = nt_sp_new(SPNO_MSGBG, CGNO_MSGFR_BG, 0, 0, SPRITE_NORMAL);
	nt_sp_add_updatelist(sp);
	
	// キー入力アニメーションCG作成
	nt_scg_cut(CGNO_MSGHAK_1, CGNO_MSGHAK_LCG, 0, 0, 9, 9);
	//night.cg[CGNO_MSGHAK_1]->refcnt++;
	nt_scg_cut(CGNO_MSGHAK_2, CGNO_MSGHAK_LCG, 0, 9, 9, 9);
	//night.cg[CGNO_MSGHAK_2]->refcnt++;
	sp = night.sp[SPNO_MSG_KEYANIM] = nt_sp_new(SPNO_MSG_KEYANIM, CGNO_MSGHAK_1, CGNO_MSGHAK_2, 0, SPRITE_ANIME);
	sp->u.anime.interval = HAKANIM_INTERVAL;
	nt_sp_add_updatelist(sp);
	nt_sp_set_loc(sp, HAKANIM_LOC_X, HAKANIM_LOC_Y);
	nt_sp_set_show(sp, false);
	sp->update = nt_sp_draw_scg;

	

	


	// 主人公名前
	nt_sstr_regist_replace(fromUTF8(SNAME_RYO), fromUTF8(SNAME_RYO_DEF));
	
	night.msg.cbmove = cb_mousemove;
	night.msg.cbrelease = cb_keyrelease;
	
	ntmsg_set_frame(0);
	ntmsg_set_place(0);
	
	night.selmode = -1;
	
}

// メッセージ枠の種類
//   type=0 消去
//   type=1 枠あり
//   type=2 中央
void ntmsg_set_frame(int type) {
	night.msgframe = type;

	SDL_Surface *sf = night.sp[SPNO_MSGBG]->curcg->sf;
	SDL_FillRect(sf, NULL, 0);
	
	switch(type) {
	case 0:
		nt_sp_set_show(night.sp[SPNO_MSGBG], false);
		nt_sp_set_show(night.sp[SPNO_MSGFRAME_BG], false);
		nt_sp_set_show(night.sp[SPNO_MSGFRAME_FG], false);
		break;
	case 1:
		nt_sp_set_show(night.sp[SPNO_MSGBG], true);
		nt_sp_set_show(night.sp[SPNO_MSGFRAME_BG], true);
		nt_sp_set_show(night.sp[SPNO_MSGFRAME_FG], true);
		
		SDL_FillRect(sf, &msgframe_0, SDL_MapRGBA(sf->format, 16, 32, 64, 192));
		ntmsg_clear(SPNO_MSGFRAME_FG);
		nt_sp_update_all(true);
		break;
	case 2:
		nt_sp_set_show(night.sp[SPNO_MSGBG], true);
		nt_sp_set_show(night.sp[SPNO_MSGFRAME_BG], false);
		nt_sp_set_show(night.sp[SPNO_MSGFRAME_FG], true);
		
		SDL_FillRect(sf, &msgframe_1, SDL_MapRGBA(sf->format, 32, 32, 32, 128));
		ntmsg_clear(SPNO_MSGFRAME_FG);
		nt_sp_update_all(true);
	
		break;
	}
}

/*
  メッセージ領域の設定
  type=0: 628x125+6+347
  type=1: 640x480+0+0
  type=2: (628-120)x125+(6+120)+347
  
*/
void ntmsg_set_place(int type) {
	night.msgplace = type;
}

void ntmsg_newline() {
	uint8_t buf[2] = {'\n', '\0'};
	ntmsg_add(buf);
}

void ntmsg_add(const char *msg) {
	int len;
	
	SACT_DEBUG("len = %d", strlen(msg));
	
	if (msg[0] == '\0') return;
	
	len = MSGBUFMAX - (int)strlen(night.msgbuf);
	if (len < 0) {
		WARNING("buf shortage (%d)", len);
		return;
	}
	strncat(night.msgbuf, msg, len);
	night.msgbuf[MSGBUFMAX -1] = '\0';
}

int ntmsg_ana(void) {
	int ret;
	
	if (night.selmode == 0) {
		// sel start
		ret = ntsel_dosel();
	} else {
		// msg newpage
		ntmsg_out(SPNO_MSGFRAME_FG, night.fontsize, 255, 255, 255, night.fonttype, 0, 2);
		nt_sp_update_clipped();
		ret = ntmsg_keywait();
		
		// clear msgsprite
		ntmsg_clear(SPNO_MSGFRAME_FG);
		//nt_sp_update_clipped();
	}
	
	night.selmode = -1;
	return ret;
}

static void ntmsg_out(int wNum, int wSize, int wColorR, int wColorG, int wColorB, int wFont, int wSpeed, int wLineSpace) {
	char *msg;
	sprite_t *sp;
	bool needupdate = false;
	SDL_Rect uparea = {0,0,0,0};
	int savex;
	
	if (night.msgbuf[0] == '\0') return;
	
	// shortcut
	sp = night.sp[wNum];
	
	msg = nt_sstr_replacestr(night.msgbuf);
	
	// 文字アラインメントの調整
	set_align(msg, sp, wSize);
	savex = sp->u.msg.dspcur.x;
	
	
	while (*msg) {
		char mbuf[20];
		int cw, delta, wcnt;
		
		wcnt = sys_get_ticks();
		
		mbuf[0] = '\0';
		msg = get_char(msg, mbuf, sizeof(mbuf) -1); 
		
		if (mbuf[0] == '\n') {
			sp->u.msg.dspcur.x = savex;
			sp->u.msg.dspcur.y += (wSize + wLineSpace);
			continue;
		}
		
		dt_setfont(wFont, wSize);

		cw = dt_drawtext_col(sp->u.msg.canvas,
				     sp->u.msg.dspcur.x,
				     sp->u.msg.dspcur.y,
				     mbuf,
				     wColorR, wColorG, wColorB);
		
		needupdate = true;
		
		if (wSpeed > 0) {
			nt_sp_updateme_part(sp,
					 sp->u.msg.dspcur.x,
					 sp->u.msg.dspcur.y,
					 cw,
					 wSize);
			nt_sp_update_clipped();
			needupdate = false;
			
			// keywait
			delta = sys_get_ticks() - wcnt;
			if (delta < wSpeed) {
				if (sys_keywait(wSpeed - delta, KEYWAIT_NONCANCELABLE)) {
					
					wSpeed = 0;
				}
			}
		}
		
		sp->u.msg.dspcur.x += cw;
	}
	
	// バッファリング中の文字のクリア
	night.msgbuf[0] = '\0';
	
	// Waitなしの出力は最後にupdate
	if (needupdate) {
		uparea.w = sp->width;
		uparea.h = min(sp->height, uparea.y - sp->u.msg.dspcur.y + wLineSpace + wLineSpace);
		nt_sp_updateme_part(sp, uparea.x, uparea.y, uparea.w, uparea.h);
	}
	
}

/*
  メッセージキー入力待ち
  @param: none
  @return: 入力されたキー 
 */
static int ntmsg_keywait() {
	int i = 0;
	
	if (night.waitskiplv > 0) {
		sys_getInputInfo();
		return 0;
	}
	
	// アニメパターンの初期化
	setup_hakanim();
	
	night.waittype = KEYWAIT_MESSAGE;
	night.waitkey = -1;
	
	while (night.waitkey == -1 && !nact->is_quit) {
		int st = sys_get_ticks();
		int interval = 25;
		
		if (!night.zhiding) {
			interval = night.sp[SPNO_MSG_KEYANIM]->u.anime.interval;
			hakanim(i++);
		} 
		sys_keywait(interval - (sys_get_ticks() - st), KEYWAIT_NONCANCELABLE);
	}
	
	night.waittype = KEYWAIT_NONE;
	
	return night.waitkey;
}


static void set_align(char *msg, sprite_t *sp, int wSize) {
	int line, maxchar, c;
	
	switch(night.msgplace) {
	case 0:
		sp->u.msg.dspcur.x = msgframe_0.x;
		sp->u.msg.dspcur.y = msgframe_0.y + 8;
		break;
		
	case 1:
                // もっとも長い行が中心にくるようにし、
		// 他の行はその行の先頭に先頭をあわせる
		line = 0; maxchar = 0;
		
		c = 0;
		while (*msg) {
			if (*msg == '\n') {
				line++; 
				maxchar = max(maxchar, c);
				c = 0;
				msg++;
				continue;
			}
			msg++; c++;
		}
		
		maxchar = max(maxchar, c);
		line++;
		sp->u.msg.dspcur.x = (sp->width - (maxchar * wSize/2)) /2;
		sp->u.msg.dspcur.y = (sp->height - (line * (wSize+2))) /2;
		break;
	case 2:
		sp->u.msg.dspcur.x = msgframe_2.x;
		sp->u.msg.dspcur.y = msgframe_2.y + 8;
		break;
	}
	
}

static uint8_t *get_char(uint8_t *msg, char *mbuf, int bufmax) {
	if (msg[0] == '\n') {
		mbuf[0] = '\n';
		mbuf[1] = '\0';
		return msg +1;
	}
	
	uint8_t *p = advance_char(msg, nact->encoding);
	while (msg < p)
		*mbuf++ = *msg++;
	*mbuf = '\0';
	
	return msg;
}

static void is_in_icon() {
	
}


static void cb_keyrelease(agsevent_t *e) {
	switch (e->code) {
	case AGSEVENT_BUTTON_LEFT:
#if 0
		if (is_in_icon()) {
			do_icon(e->mousex, e->mousey);
			break;
		}
#endif
		// fall through
	case AGSEVENT_BUTTON_RIGHT:
		if (night.zhiding) {
			// unhide();
			break;
		}
		night.waitkey = e->code;
		break;
	}
	
}

static void cb_mousemove(agsevent_t *e) {
	// 音声mute/メッセージスキップ/メッセージ枠消去の領域に
	// マウスが移動したら、その部分のアイコンを変化させる
}

// メッセージ表示時に、キー入力を促すアニメーションの設定
static void setup_hakanim() {
}

// メッセージ表示時のキー入力を促すアニメーション
static void hakanim(int i) {
	sprite_t *sp;
	bool show;
	
	sp = night.sp[SPNO_MSG_KEYANIM];
	if (sp == NULL) return;
	show = sp->show;
	if (i%2) {
		sp->curcg = sp->cg1;
	} else {
		sp->curcg = sp->cg2;
	}
	sp->show = true;
	nt_sp_updateme(sp);
	nt_sp_update_clipped();
	
	sp->show = show;
}

void ntmsg_update(sprite_t *sp, SDL_Rect *r) {
	SDL_Rect sp_rect = {0, 0, sp->width, sp->height};
	int sx = 0;
	int sy = 0;
	int dx = sp->cur.x;
	int dy = sp->cur.y;
	int w = sp->width;
	int h = sp->height;
	if (!ags_clipCopyRect(&sp_rect, r, &sx, &sy, &dx, &dy, &w, &h)) {
		return;
	}

	SDL_SetSurfaceBlendMode(sp->u.msg.canvas, SDL_BLENDMODE_BLEND);
	SDL_SetSurfaceAlphaMod(sp->u.msg.canvas, sp->blendrate);
	SDL_BlitSurface(sp->u.msg.canvas, &(SDL_Rect){sx, sy, w, h}, main_surface, &(SDL_Rect){dx, dy, w, h});

	SACT_DEBUG("do update no=%d, sx=%d, sy=%d, w=%d, h=%d, dx=%d, dy=%d",
		sp->no, sx, sy, w, h, dx, dy);
}

static void ntmsg_clear(int wNum) {
	sprite_t *sp = night.sp[wNum];

	sp->u.msg.dspcur.x = 0;
	sp->u.msg.dspcur.y = 0;
	
	night.msgbuf[0]  = '\0';

	// Clear the canvas
	SDL_FillRect(sp->u.msg.canvas, NULL, 0);

	nt_sp_updateme(sp);
}