File: sactamask.c

package info (click to toggle)
xsystem35 1.7.3-pre5-10
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 7,368 kB
  • sloc: ansic: 51,007; sh: 12,055; asm: 863; makefile: 414; xml: 281; perl: 142
file content (180 lines) | stat: -rw-r--r-- 4,344 bytes parent folder | download | duplicates (4)
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
/*
 * sactamask.c: SACTEFAM.KLD Ÿ
 *
 * Copyright (C) 1997-1998 Masaki Chikama (Wren) <chikama@kasumi.ipl.mech.nagoya-u.ac.jp>
 *               1998-                           <masaki-c@is.aist-nara.ac.jp>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
*/
/* $Id: sactamask.c,v 1.1 2003/04/22 16:29:52 chikama Exp $ */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <glib.h>

#include "portab.h"
#include "system.h"
#include "LittleEndian.h"
#include "imput.h"
#include "sact.h"
#include "surface.h"
#include "ngraph.h"
#include "sprite.h"
#include "counter.h"

static surface_t *smask_get(int no);
static surface_t *smask_mul(surface_t *sf, int val);



struct ecopyparam {
	int sttime;
	int curtime;
	int edtime;
	int curstep;
	int oldstep;
};
typedef struct ecopyparam ecopyparam_t;
static ecopyparam_t ecp;

// SACTEFAM.KLD ɤ߹
int smask_init(char *path) {
	struct stat sbuf;
	int i, fd;
	char *adr;
	SACTEFAM_t *am;

	if (0 > (fd = open(path, O_RDONLY))) {
		WARNING("open: %s\n", strerror(errno));
		return NG;
	}
	
	if (0 > fstat(fd, &sbuf)) {
		WARNING("fstat: %s\n", strerror(errno));
		close(fd);
		return NG;
	}

	if (MAP_FAILED == (adr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))) {
		WARNING("mmap: %s\n", strerror(errno));
		close(fd);
		return NG;
	}
	
	am = &sact.am;
	am->mapadr = adr;
	am->size = sbuf.st_size;
	am->fd = fd;
	
	am->datanum = LittleEndian_getDW(adr, 0);
	am->no = g_new(int, am->datanum);
	am->offset = g_new(int, am->datanum);
	
	for (i = 0; i < am->datanum; i++) {
		am->no[i] = LittleEndian_getDW(adr, 16 + i * 16);
		am->offset[i] = LittleEndian_getDW(adr, 16 + i * 16 + 8);
	}
	
	return OK;
}

// ֹ alphamask եߤ
static surface_t *smask_get(int no) {
	int i;
	SACTEFAM_t *am = &sact.am;
	
	for (i = 0; i < am->datanum; i++) {
		if (am->no[i] == no) break;
	}

	if (i == am->datanum) return NULL;
	
	return sf_getcg(am->mapadr + am->offset[i]);
}

// ١ˤʤޥ alpha ͤ礷ƼФ
static surface_t *smask_mul(surface_t *sf, int val) {
	surface_t *out = sf_create_alpha(sf->width, sf->height);
	BYTE *src = sf->alpha;
	BYTE *dst = out->alpha;
	int pix = sf->width * sf->height;

	while(pix--) {
		int i = (*src - val) * 16;
		if (i < 0)        *dst = 255; // ͤ礭Τϥԡ
		else if (i > 255) *dst = 0;   // ͤ⾮Τ̵
		else              *dst = 255-i; // ʳͤ16
		src++; dst++;
	}
	
	return out;
}

/**
 * ޥĤ̹
 */
int sp_eupdate_amap(int index, int time, int cancel) {
	surface_t *mask, *mask2;
	surface_t *sfsrc, *sfdst;
	int key;
	
	mask = smask_get(index);
	if (mask == NULL) {
		sp_update_all(TRUE);
		return OK;
	}
	
	// ߤ sf0 򥻡
	sfsrc = sf_dup(sf0);
	sp_update_all(FALSE);
	sfdst = sf_dup(sf0);
	sf_copyall(sf0, sfsrc);
	
	ecp.sttime = ecp.curtime = get_high_counter(SYSTEMCOUNTER_MSEC);
	ecp.edtime = ecp.curtime + time*10;
	ecp.oldstep = 0;
	
	while ((ecp.curtime = get_high_counter(SYSTEMCOUNTER_MSEC)) < ecp.edtime) {
		int curstep = 255 * (ecp.curtime - ecp.sttime)/ (ecp.edtime - ecp.sttime);
		// ˤʤޥalphaͤ16ܤߤȤФ
		mask2 = smask_mul(mask, curstep);
		
		gre_BlendUseAMap(sf0, 0, 0, sfsrc, 0, 0, sfdst, 0, 0, sfsrc->width, sfsrc->height, mask2, 0, 0, 255);
		ags_updateFull();
		
		key = sys_keywait(10, cancel);
		if (cancel && key) break;
		
		// ޥ
		sf_free(mask2);
	}
	
	sf_copyall(sf0, sfdst);
	ags_updateFull();
	sf_free(sfsrc);
	sf_free(sfdst);
	
	sf_free(mask);
	return OK;
}