forked from catmirrors/xlvns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgLvns.c
184 lines (162 loc) · 4.3 KB
/
mgLvns.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
/*
* LEAF Visual Novel System For MGL2
* (c) Copyright 2001 TF <[email protected]>
* All rights reserverd.
*
* ORIGINAL LVNS (c) Copyright 1996-1999 LEAF/AQUAPLUS Inc.
*
* $Id: mgLvns.c,v 1.6 2001/08/17 16:55:52 tf Exp $
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "Lvns.h"
static void read_convert_table(Lvns *);
static void open_leafpack(Lvns *);
void
mgLvnsInit(Lvns *lvns)
{
srandom((unsigned)getpid());
switch (lvns->version) {
case 1:
dprintf((stderr, "LVNS1: Shizuku\n"));
SizukuInitialize(lvns);
break;
case 2:
dprintf((stderr, "LVNS2: Kizuato\n"));
KizuatoInitialize(lvns);
break;
case 3:
dprintf((stderr, "LVNS3: To Heart\n"));
ToHeartInitialize(lvns);
break;
default:
fprintf(stderr, "Unknown version of LVNS: %d.\n", lvns->version);
exit(1);
}
/*
* オフスクリーン生成
*/
lvns->background = mglimage_new(WIDTH, HEIGHT, 0, 0);
lvns->vram = mglimage_new(WIDTH, HEIGHT, 0, 0);
/*
* set system depended functions
*/
mgLvnsCoreSetLvns(lvns);
mglimage_init(LVNS->width, LVNS->height);
read_convert_table(lvns);
open_leafpack(lvns);
#ifdef FONT_PLUS
{
char* buf;
char fname[ 128];
if( lvns->font_file == NULL){
/* フォントファイルが指定されていない場合 */
;;
} else if( strcmp( lvns->font_file , "PAK FILE") == 0){
/* PAKファイルから読み込む場合 */
if( (buf = LvnsLoadData(lvns, "KNJ_ALL.KNJ", NULL)) != NULL){
/* 読めた場合 */
if( ( lvns->font = lvnsfont_new( buf)) == NULL){
perror( "Lvns fontload. Use system font.");
}
} else {
/* 読めなかった場合 */
strcpy( fname, lvns->data_path);
strcat( fname, "/");
strcat( fname, "KNJ_ALL.KNJ");
if( ( lvns->font = lvnsfont_load( fname)) == NULL){
perror( "Lvns fontload. Use system font.");
}
}
} else {
/* フォントファイルが指定されている場合 */
if( ( lvns->font = lvnsfont_load( lvns->font_file)) == NULL){
perror( "Lvns fontload. Use system font.");
}
}
}
#endif FONT_PLUS
}
void
mgLvnsDelete(Lvns * lvns)
{
leafpack_delete(lvns->leafpack);
if (lvns->scnpack != lvns->leafpack) {
leafpack_delete(lvns->scnpack);
}
mglimage_delete(lvns->background);
mglimage_delete(lvns->vram);
}
static void
read_convert_table(Lvns *lvns)
{
FILE *fp;
char path[PATH_LEN];
size_t size;
int i;
snprintf(path, sizeof(path), "%s/%s", lvns->data_path,
LVNS->fonttable_name);
dprintf((stderr, "font table: %s\n", LVNS->fonttable_name));
if ((fp = fopen(path, "r")) == NULL) {
perror(path);
exit(1);
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
rewind(fp);
/*
* Read leaf->euc table
*/
lvns->leaf_to_euc = malloc(size);
if (fread(lvns->leaf_to_euc, 1, size, fp) != size) {
perror(LVNS->fonttable_name);
free(lvns->leaf_to_euc);
exit(1);
}
fclose(fp);
/*
* create jis->leaf table
*/
size /= 2; /* # of chars. */
lvns->jis_to_leaf = (u_short *)
malloc(sizeof(u_short) * 94 * 94);
for (i = 0; i < 94 * 94; i++) {
lvns->jis_to_leaf[i] = 0;
}
for (i = 0; i < size; i++) {
int code =
((lvns->leaf_to_euc[i * 2] & 0x7f) - 33) * 94 +
(lvns->leaf_to_euc[i * 2 + 1] & 0x7f) - 33;
lvns->jis_to_leaf[code] = i;
}
}
static void
open_leafpack(Lvns *lvns)
{
char path[PATH_LEN];
/*
* open CG and sound package
*/
snprintf(path, sizeof(path), "%s/%s", lvns->data_path,
LVNS->leafpack_name);
if ((lvns->leafpack = leafpack_new(path)) == NULL) {
fprintf(stderr, "Can't open package %s.\n", LVNS->leafpack_name);
exit(1);
}
/*
* open scenario package
*/
if ((LVNS->scnpack_name)) {
snprintf(path, sizeof(path), "%s/%s", lvns->data_path,
LVNS->scnpack_name);
if ((lvns->scnpack = leafpack_new(path)) == NULL) {
fprintf(stderr, "Can't open package %s.\n", LVNS->scnpack_name);
exit(1);
}
} else {
lvns->scnpack = lvns->leafpack;
}
}