forked from catmirrors/xlvns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LvnsController.m
390 lines (335 loc) · 7.93 KB
/
LvnsController.m
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
#import "LvnsController.h"
#define TICKSPERSEC 1.0
@implementation LvnsController
// NSView メソッド群
/* 再描画 */
- (void) drawRect : (NSRect) arBounds
{
[image draw : arBounds];
[[NSGraphicsContext currentContext] flushGraphics];
}
// イベント処理
- (void)mouseMoved:(NSEvent *)theEvent
{
}
- (void)mouseDown:(NSEvent *)theEvent
{
[self Select : self];
}
- (void)rightMouseDown:(NSEvent *)theEvent
{
[self Cancel : self];
}
- (void)keyDown:(NSEvent *)theEvent
{
}
// actions
- (IBAction) Select : (id) sender
{
LvnsSelect(lvns);
}
- (IBAction) Cancel : (id) sender
{
LvnsCancel(lvns);
}
- (IBAction) ImageMode : (id) sender
{
}
- (IBAction) SkipTillSelect : (id) sender
{
}
- (IBAction) SkipTillSelectForce : (id) sender
{
}
- (IBAction) CursorUp : (id) sender
{
LvnsCursorUp(lvns);
}
- (IBAction) CursorDown : (id) sender
{
LvnsCursorDown(lvns);
}
// privates
// Lvns System Depend Functions
/**
* 全画面をリフレッシュして実際の画面に反映させる
*/
static void
FlushWindow(Lvns *lvns)
{
LvnsController *self = lvns->system_depend;
[self setNeedsDisplay : true];
//[self lockFocus];
//[self->image draw : NSMakeRect(0,0,WIDTH,HEIGHT)];
//[self unlockFocus];
}
/**
* 画面を部分的にリフレッシュして実際の画面に反映させる
*/
static void
FlushWindowArea(Lvns *lvns, int x, int y, int w, int h)
{
LvnsController *self = lvns->system_depend;
[self setNeedsDisplayInRect : NSMakeRect(x,y,w,h)];
//[self lockFocus];
//[self->image draw : NSMakeRect(x,HEIGHT-y-1,w,h)];
//[self unlockFocus];
}
/**
* 全体を描画(フラッシュ無し)
*/
static void
DrawWindow(Lvns *lvns)
{
LvnsController *self = lvns->system_depend;
[self->image copy : lvns->vram : 0 : 0];
}
/**
* 部分描画(フラッシュ無し)
*/
static void
DrawWindowArea(Lvns *lvns, int x, int y, int w, int h, int x2, int y2)
{
LvnsController *self = lvns->system_depend;
[self->image copyArea : lvns->vram : x : y : w : h : x2 : y2];
}
/**
* 全画面画面消去(フラッシュ無し)
*/
static void
ClearWindow(Lvns *lvns)
{
LvnsController *self = lvns->system_depend;
[self->image clearArea : 0 : 0 : WIDTH : HEIGHT : lvns->vram->black];
}
/**
* 部分画面消去(フラッシュ無し)
*/
static void
ClearWindowArea(Lvns *lvns, int x, int y, int w, int h)
{
LvnsController *self = lvns->system_depend;
[self->image clearArea : x : y : w : h : lvns->vram->black];
}
/**
* 特定のインデックスにカラーを割り当てる
*/
static void
SetPaletteIndex(Lvns *lvns, int index, int r, int g, int b)
{
LvnsController *self = lvns->system_depend;
[self->image setPalette : index r:r g:g b:b];
}
/**
* パレット設定(通常)
* 全部を設定する…
*/
static void
MySetPalette(Lvns *lvns)
{
LvnsController *self = lvns->system_depend;
[self->image setPal : lvns->vram];
}
/**
* パレット設定(multiple)
*/
static void
SetPaletteMulti(Lvns *lvns, int par16)
{
LvnsController *self = lvns->system_depend;
[self->image setPalMulti : lvns->vram : par16];
}
/**
* パレット設定(screen)
*/
static void
SetPaletteScreen(Lvns *lvns, int par16)
{
LvnsController *self = lvns->system_depend;
[self->image setPalScreen : lvns->vram : par16];
}
/*
* マスクパターンでウインドウに対して描画を行う(フラッシュ無し)
*/
static void
DrawWindowMask(Lvns *lvns, int x, int y, int state)
{
LvnsController *self = lvns->system_depend;
[self->image copyMaskUnit : lvns->vram : x : y : x : y : state ];
}
/*
* 矩形パターンでウィンドウに対して描画を行う(フラッシュ無し)
*/
static void
DrawWindowSquareMask(Lvns *lvns, int x, int y, int state)
{
LvnsController *self = lvns->system_depend;
[self->image copySquareMaskUnit : lvns->vram : x : y : x : y : state];
}
/*
* 表示されている画面のオフセットをずらす(振動用)
*/
static void
SetDispOffset(Lvns *lvns, int xoff, int yoff, int maxoff)
{
}
/*
* 文字パターン表示 (雫/痕 用)
*/
static void
PutPattern(Lvns *lvns, int x, int y, int index, u_char *data)
{
LvnsController *self = lvns->system_depend;
[self->image putPattern24_2 : x : y : index : data];
}
/*
* 文字パターン表示 (ToHeart用)
*/
static void
PutPattern2(Lvns *lvns, int x, int y, int index, u_char *data)
{
LvnsController *self = lvns->system_depend;
[self->image putPattern24 : x : y : index : data];
}
/*
* タイマをリセットする
* @param no タイマ番号
*/
static void
ResetTimer(Lvns *lvns, int no)
{
LvnsController *self = lvns->system_depend;
gettimeofday(&self->timer[no], NULL);
}
/*
* タイマ値を取得する
* @param no タイマ番号
* @return タイマ値 (msec単位)
*/ // シンクロ
// シンクロ
static long
GetTimer(Lvns *lvns, int no)
{
LvnsController *self = lvns->system_depend;
struct timeval current;
gettimeofday(¤t, NULL);
return (current.tv_sec - self->timer[no].tv_sec) * 1000 +
(current.tv_usec - self->timer[no].tv_usec) / 1000;
}
/**
* 矩形を描画する
* @param x,y,w,h 位置とサイズ
* @param idx 色のインデックス値
*/
static void
DrawBox(Lvns *lvns, int x, int y, int w, int h, int idx)
{
}
#include <sys/types.h>
#include <sys/stat.h>
/**
* 指定したファイルの更新日付を取得する
*/
static time_t
GetFileTime(Lvns *lvns, const char *path)
{
struct stat sb;
if (stat(path, &sb) < 0) {
return 0;
}
return sb.st_mtime;
}
- (void) flip
{
int wait, sa;
wait = wait_count / 60;
if (wait > 0) {
u_long tw;
sa = [NSDate timeIntervalSinceReferenceDate] - wait_clock;
if (sa > 0) wait -= sa;
if (wait > 0) {
NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow: wait];
[NSThread sleepUntilDate : date];
[date release];
}
tw = [NSDate timeIntervalSinceReferenceDate];
sa = tw - wait_clock;
if (sa > 0)
wait_count -= sa * 60;
else
wait_count = 0;
wait_clock = tw;
}
wait_count += TICKSPERSEC;
if (wait_count < 0) {
wait_count = 0;
}
}
/*
* 描画タイミング待ち処理
*/
static void
Flip(Lvns *lvns)
{
LvnsController *self = lvns->system_depend;
[self flip];
}
/**
* メインエンジン
*/
- (void) runMain : (id) param
{
LvnsMain(lvns);
}
extern int system_type;
/**
* 初期化処理
*/
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
char *home = getenv("HOME");
lvns = LvnsNew();
if (home) {
LvnsSetSaveDataPath(lvns, home);
}
//LvnsSetDataPath(lvns, );
/* リソースの反映 */
lvns->demo_mode = False;
lvns->seen_mode = False;
lvns->latitude_dark = 11;
lvns->key_click_fast = True;
/* システム依存関数群の設定 */
lvns->flushWindow = FlushWindow;
lvns->flushWindowArea = FlushWindowArea;
lvns->drawWindow = DrawWindow;
lvns->drawWindowArea = DrawWindowArea;
lvns->clearWindow = ClearWindow;
lvns->clearWindowArea = ClearWindowArea;
lvns->setPaletteIndex = SetPaletteIndex;
lvns->setPalette = MySetPalette;
lvns->setPaletteMulti = SetPaletteMulti;
lvns->setPaletteScreen = SetPaletteScreen;
lvns->drawWindowMask = DrawWindowMask;
lvns->drawWindowSquareMask = DrawWindowSquareMask;
lvns->setDispOffset = SetDispOffset;
lvns->putPattern = PutPattern;
lvns->putPattern2 = PutPattern2;
lvns->resetTimer = ResetTimer;
lvns->getTimer = GetTimer;
lvns->drawBox = DrawBox;
lvns->getFileTime = GetFileTime;
lvns->flip = Flip;
LvnsInitialize(lvns, system_type, self);
//サイズ決定
{
NSSize size = { WIDTH, HEIGHT };
[mainWindow setContentSize : size];
[mainWindow setContentView : self];
image = [[NSLvnsImage alloc] init : size];
}
wait_clock = [NSDate timeIntervalSinceReferenceDate];
wait_count = TICKSPERSEC;
// スレッドを分けて本体を動かす
[NSThread detachNewThreadSelector: @selector(runMain:) toTarget:self withObject:self];
}
@end