1 /*
2  * sactamask.c: SACTEFAM.KLD Ÿ��
3  *
4  * Copyright (C) 1997-1998 Masaki Chikama (Wren) <chikama@kasumi.ipl.mech.nagoya-u.ac.jp>
5  *               1998-                           <masaki-c@is.aist-nara.ac.jp>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21 */
22 /* $Id: sactamask.c,v 1.1 2003/04/22 16:29:52 chikama Exp $ */
23 
24 #include "config.h"
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <glib.h>
35 
36 #include "portab.h"
37 #include "system.h"
38 #include "LittleEndian.h"
39 #include "imput.h"
40 #include "sact.h"
41 #include "surface.h"
42 #include "ngraph.h"
43 #include "sprite.h"
44 #include "counter.h"
45 
46 static surface_t *smask_get(int no);
47 static surface_t *smask_mul(surface_t *sf, int val);
48 
49 
50 
51 struct ecopyparam {
52 	int sttime;
53 	int curtime;
54 	int edtime;
55 	int curstep;
56 	int oldstep;
57 };
58 typedef struct ecopyparam ecopyparam_t;
59 static ecopyparam_t ecp;
60 
61 // SACTEFAM.KLD ���ɤ߹���
smask_init(char * path)62 int smask_init(char *path) {
63 	struct stat sbuf;
64 	int i, fd;
65 	char *adr;
66 	SACTEFAM_t *am;
67 
68 	if (0 > (fd = open(path, O_RDONLY))) {
69 		WARNING("open: %s\n", strerror(errno));
70 		return NG;
71 	}
72 
73 	if (0 > fstat(fd, &sbuf)) {
74 		WARNING("fstat: %s\n", strerror(errno));
75 		close(fd);
76 		return NG;
77 	}
78 
79 	if (MAP_FAILED == (adr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))) {
80 		WARNING("mmap: %s\n", strerror(errno));
81 		close(fd);
82 		return NG;
83 	}
84 
85 	am = &sact.am;
86 	am->mapadr = adr;
87 	am->size = sbuf.st_size;
88 	am->fd = fd;
89 
90 	am->datanum = LittleEndian_getDW(adr, 0);
91 	am->no = g_new(int, am->datanum);
92 	am->offset = g_new(int, am->datanum);
93 
94 	for (i = 0; i < am->datanum; i++) {
95 		am->no[i] = LittleEndian_getDW(adr, 16 + i * 16);
96 		am->offset[i] = LittleEndian_getDW(adr, 16 + i * 16 + 8);
97 	}
98 
99 	return OK;
100 }
101 
102 // �����ֹ�� alphamask �ե�������ߤ���
smask_get(int no)103 static surface_t *smask_get(int no) {
104 	int i;
105 	SACTEFAM_t *am = &sact.am;
106 
107 	for (i = 0; i < am->datanum; i++) {
108 		if (am->no[i] == no) break;
109 	}
110 
111 	if (i == am->datanum) return NULL;
112 
113 	return sf_getcg(am->mapadr + am->offset[i]);
114 }
115 
116 // �١����ˤʤ�ޥ����� alpha �ͤ���礷�Ƽ��Ф�
smask_mul(surface_t * sf,int val)117 static surface_t *smask_mul(surface_t *sf, int val) {
118 	surface_t *out = sf_create_alpha(sf->width, sf->height);
119 	BYTE *src = sf->alpha;
120 	BYTE *dst = out->alpha;
121 	int pix = sf->width * sf->height;
122 
123 	while(pix--) {
124 		int i = (*src - val) * 16;
125 		if (i < 0)        *dst = 255; // �����ͤ����礭���Τϥ��ԡ�
126 		else if (i > 255) *dst = 0;   // �����ͤ��⾮�����Τ�̵��
127 		else              *dst = 255-i; // ����ʳ����ͤ�16��
128 		src++; dst++;
129 	}
130 
131 	return out;
132 }
133 
134 /**
135  * �ޥ����Ĥ����̹���
136  */
sp_eupdate_amap(int index,int time,int cancel)137 int sp_eupdate_amap(int index, int time, int cancel) {
138 	surface_t *mask, *mask2;
139 	surface_t *sfsrc, *sfdst;
140 	int key;
141 
142 	mask = smask_get(index);
143 	if (mask == NULL) {
144 		sp_update_all(TRUE);
145 		return OK;
146 	}
147 
148 	// ���ߤ� sf0 ������
149 	sfsrc = sf_dup(sf0);
150 	sp_update_all(FALSE);
151 	sfdst = sf_dup(sf0);
152 	sf_copyall(sf0, sfsrc);
153 
154 	ecp.sttime = ecp.curtime = get_high_counter(SYSTEMCOUNTER_MSEC);
155 	ecp.edtime = ecp.curtime + time*10;
156 	ecp.oldstep = 0;
157 
158 	while ((ecp.curtime = get_high_counter(SYSTEMCOUNTER_MSEC)) < ecp.edtime) {
159 		int curstep = 255 * (ecp.curtime - ecp.sttime)/ (ecp.edtime - ecp.sttime);
160 		// ���ˤʤ�ޥ�����alpha�ͤ�16�ܤ����ߤ����Ȥ���������Ф�
161 		mask2 = smask_mul(mask, curstep);
162 
163 		gre_BlendUseAMap(sf0, 0, 0, sfsrc, 0, 0, sfdst, 0, 0, sfsrc->width, sfsrc->height, mask2, 0, 0, 255);
164 		ags_updateFull();
165 
166 		key = sys_keywait(10, cancel);
167 		if (cancel && key) break;
168 
169 		// ����ޥ�������
170 		sf_free(mask2);
171 	}
172 
173 	sf_copyall(sf0, sfdst);
174 	ags_updateFull();
175 	sf_free(sfsrc);
176 	sf_free(sfdst);
177 
178 	sf_free(mask);
179 	return OK;
180 }
181