This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NESDLG.CPP
executable file
·598 lines (430 loc) · 11.9 KB
/
NESDLG.CPP
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "types.h"
#include "r2img.h"
#include "font.h"
#include "dd.h"
#include "mouse.h"
#include "message.h"
#include "gui.h"
#include "guimenu.h"
#include "guicolor.h"
#include "file.h"
#include "keyb.h"
#include "config.h"
#include "guivol.h"
#include "nesvideo.h"
#include "sound.h"
#include "nessound.h"
#include "slist.h"
#include "timing.h"
void loadrom(char *filename);
void enablegui();
//------------------------------
//dir listing box
#ifdef __WATCOMC__
#define USERENTRY
#endif
#ifdef _MSC_VER
#define USERENTRY __cdecl
#endif
#ifdef __BORLANDC__
#define USERENTRY _USERENTRY
#endif
typedef int (USERENTRY *qsortfuncptr)(const void *,const void *);
int filldirlistbox(char *filename,class GUIdirlistbox *b);
int USERENTRY dirlistcmp(char **a,char **b) {return stricmp(*a,*b);}
class GUIdirlistbox:public GUIstringlistbox
{
char path[128];
public:
void sortdirlist() //sort all entries in list
{
qsort((void *)getitems(),getnumitems(),4,(qsortfuncptr)dirlistcmp);
}
void setpath(char *_path)
{
strcpy(path,_path); //set path
freeitems();
enumdir(path,(DIRFUNCPTR)filldirlistbox,this); //fill dir list box
sortdirlist();
}
GUIdirlistbox(GUIrect *p,int x,int y,int xw,int iy,char *_path)
:GUIstringlistbox(p,x,y,xw,iy,10)
{
setpath(_path);
}
virtual char *getname() {return "guidirlist";}
virtual int acceptfocus() {return 1;}
};
int filldirlistbox(char *filename,GUIdirlistbox *b)
{
int num=b->getnumitems();
char **s=(char **)b->resizeitems(num+1);
s[num]=strdup(filename);
return 1;
}
//----------------------
// load rom
//----------------------
class loaddlg:public GUIcontents
{
GUIstringlistbox *list;
public:
loaddlg():GUIcontents(140,150)
{
// list=new GUIstringlistbox(this,5,5,width()-10,9,10);
list=new GUIdirlistbox(this,5,5,width()-10,14,"*.nes");
list->setsel(0);
}
virtual char *getname() {return "loadrom";}
int sendmessage(GUIrect *c,int guimsg)
{
if (guimsg==GUIMSG_OK || guimsg==GUIMSG_LISTBOXDBLCLICKED)
{
loadrom(list->getselptr());
return 1;
}
return 1;
};
virtual void draw(char *dest) {fill(CLR_BOX); GUIrect::draw(dest);}
virtual int acceptfocus() {return 1;}
//open functions
static DLGPOS pos; //saved last position of dialog
virtual ~loaddlg() {pos.close((GUIbox *)parent);}
};
DLGPOS loaddlg::pos;
void m_load()
{
loaddlg::pos.open(new GUIonebuttonbox(guiroot,"Load .NES ROM",new loaddlg(),"Load",0,0));
enablegui();
}
//--------------------------------------------------
//----------------------
// view pattern tables
//----------------------
//shows a particular pattern table
class patterntableview: public GUIrect
{
int pnum; //index to pattern table...
int attrib; //attrib to use
public:
patterntableview(GUIrect *_parent,int _pnum)
:GUIrect(_parent,0,0,16*8,16*8) {pnum=_pnum; attrib=0;}
virtual void draw(char *dest)
{
if (!nv) return;
if (!nv->sc.biggestsc) return;
patterntable &pt=nv->sc.biggestsc->ptn[pnum];
CLIP clip(dest,x1,y1,x2,y2);
for (int i=0; i<256; i++)
pt[i].draw_tile(dest,(i&15)*8,(i/16)*8,attrib);
}
void incattrib()
{
attrib++;
attrib&=7;
}
virtual GUIrect *click(mouse &m)
{
incattrib();
return 0;
}
virtual char *getname() {return "patterntable";}
};
class patterndlg:public GUIcontents
{
patterntableview *p1,*p2;
public:
patterndlg():GUIcontents(16*8,16*8*2)
{
p1=new patterntableview(this,0);
p2=new patterntableview(this,1);
p2->moveto(p1->x1,p1->y2);//+8);
}
virtual char *getname() {return "patterndlg";}
int sendmessage(GUIrect *c,int guimsg)
{return 1; };
// virtual void draw(char *dest) {fill(CLR_BOX); GUIrect::draw(dest);}
//open functions
static DLGPOS pos; //saved last position of dialog
virtual ~patterndlg() {pos.close((GUIbox *)parent);}
};
DLGPOS patterndlg::pos;
void m_patternview()
{
patterndlg::pos.open(new GUIbox(guiroot,"Pattern tables",new patterndlg(),0,0));
enablegui();
}
//-------------------------------------------------------------
//----------------------
// view name tables
//----------------------
#include "nes.h"
class namedlg:public GUIcontents
{
public:
namedlg():GUIcontents(32*2*8,30*8) {}
virtual char *getname() {return "nametabledlg";}
int sendmessage(GUIrect *c,int guimsg){ return 1; };
virtual void draw(char *dest)
{
if (!nv) return;
if (SCREENX<=320) return;
lrect clip={x1,y1,x2,y2};
nv->realntc[0]->s->blt(0,0,32*8,30*8,dest,x1,y1);
nv->realntc[1]->s->blt(0,0,32*8,30*8,dest,x1+32*8,y1);
// nv->realntc[0]->draw(x1,y1,&clip);
// nv->realntc[1]->draw(x1+32*8,y1,&clip);
// CLIP clip(dest,x1,y1,x2,y2);
//find screen pattern table to show...
// pattern *p=(pattern *)&nv->ptn[(ram[0x2000]&16) ? 1 : 0];
// ppu->nat[0].draw(dest,0,0,p);
// ppu->nat[(nv->mirroring==VMIRROR) ? 1 : 2].draw(dest,32*8,0,p);
// ppu->nat[2].draw(dest,0,3*8,p);
// ppu->nat[3].draw(dest,32*8,30*8,p);
}
virtual GUIrect *click(mouse &m)
{
int x=m.x-x1,y=m.y-y1;
// msg.printf(1,"nametable=%d (%d,%d)",x<32*8 ? 0 : ((nv->mirroring==VMIRROR) ? 1 : 2),
// x%(32*8),y);
return 0;
}
//open functions
static DLGPOS pos; //saved last position of dialog
virtual ~namedlg() {pos.close((GUIbox *)parent);}
};
DLGPOS namedlg::pos;
void m_nametableview()
{
namedlg::pos.open(new GUIbox(guiroot,"Name tables",new namedlg(),0,0));
enablegui();
}
//----------------------
// view palette
//----------------------
extern COLOR nespal[256];
//first index of nes color
#define CBASE 224
#define CSIZE 48
int cfontnum[3]={5,2,7}; //rgb fonts
class colorcomponent:public GUIrect
{
byte type; //0=r 1=g 2=b
byte val;
GUIstatictext *str;
GUIscrollbar *sb;
public:
colorcomponent(GUIrect *p,int x,int y,int xw,int yw,byte _type):
GUIrect(p,x,y,x+xw,y+yw), type(_type)
{
str=new GUIstatictext(this,cfontnum[type],"",0,4);
sb=new GUIhscrollbar(this,14,2,width()-16);
sb->setrange(0,0xFF);
val=0;
};
void setval(byte value)
{
if (value==val) return;
sb->setpos(value);
char s[10];
sprintf(s,"%02X",value);
str->settext(s);
val=value;
parent->sendmessage(this,value);
}
virtual void draw(char *dest)
{
setval(sb->getpos());
GUIrect::draw(dest);
}
virtual int acceptfocus() {return 1;}
};
class colorviewdlg:public GUIcontents
{
byte cnum; //color number 0-23
byte cidx; //color index 0-256
COLOR *c;
char indexstr[16];
colorcomponent *cc[3];
public:
void setcolor(byte colornum)
{
if (!nv) return;
cnum=colornum;
cidx=ppu->bgpal.c[cnum];
c=&nespal[cidx];
sprintf(indexstr,"%02X",cidx);
char s[32];
sprintf(s,"%s Color #%X",cnum<16 ? "Bg" : "Sprite",cnum&0xF);
if (parent) ((GUIbox *)parent)->settitle(s);
cc[0]->setval(c->r);
cc[1]->setval(c->g);
cc[2]->setval(c->b);
}
//update palette
void updatepalette()
{
for (int i=0; i<32; i++)
if (ppu->bgpal.c[i]==cidx) nv->palupdateidx[i]=1;
nv->paletteupdated=1;
// msg.printf(2,"pal updated");
}
colorviewdlg():GUIcontents(CSIZE+100,CSIZE)
{
indexstr[0]=0; c=0;
for (int i=0; i<3; i++)
cc[i]=new colorcomponent(this,CSIZE,CSIZE*i/3,width()-CSIZE,CSIZE/3,i);
}
virtual char *getname() {return "colorviewdlg";}
virtual int acceptfocus() {return 1;}
int sendmessage(GUIrect *t,int guimsg)
{
if (!c || !nv) return 0;
if (t==cc[0]) c->r=(byte)guimsg;
if (t==cc[1]) c->g=(byte)guimsg;
if (t==cc[2]) c->b=(byte)guimsg;
updatepalette();
return 1;
};
virtual void draw(char *dest)
{
if (!nv) return;
fill(CLR_BOX);
drawrect(dest,cnum+CBASE,x1+4,y1+4,CSIZE-8,CSIZE-8);
font[1]->drawcentered(indexstr,dest,x1+CSIZE/2,y1+CSIZE/2-5);
GUIrect::draw(dest);
}
//open functions
static DLGPOS pos; //saved last position of dialog
virtual ~colorviewdlg() {pos.close((GUIbox *)parent);}
};
DLGPOS colorviewdlg::pos;
void viewcolor(byte colornum)
{
if (!colorviewdlg::pos.opened)
colorviewdlg::pos.open(new GUIbox(guiroot,"",new colorviewdlg(),0,0));
((colorviewdlg *)colorviewdlg::pos.opened->contents)->setcolor(colornum);
enablegui();
}
int isviewcoloropen()
{
return colorviewdlg::pos.opened ? 1 : 0;
}
//---------------------------------
class paletteviewdlg:public GUIcontents
{
public:
paletteviewdlg():GUIcontents(16*16,16*2) {}
virtual char *getname() {return "paletteviewdlg";}
int sendmessage(GUIrect *c,int guimsg){ return 1; };
virtual void draw(char *dest)
{
if (!nv) return;
int x;
//bg pal
for (x=0; x<16; x++)
drawrect(dest,CBASE|x,x1+x*16,y1,16,16);
//sprite pal
for (x=0; x<16; x++)
drawrect(dest,CBASE|16|x,x1+x*16,y1+16,16,16);
}
virtual GUIrect *click(mouse &m)
{
if (!nv) return 0;
viewcolor(((m.x-x1)/16)+((m.y-y1)/16)*16);
return 0;
}
//open functions
static DLGPOS pos; //saved last position of dialog
virtual ~paletteviewdlg() {pos.close((GUIbox *)parent);}
};
DLGPOS paletteviewdlg::pos;
void m_paletteview()
{
paletteviewdlg::pos.open(new GUIbox(guiroot,"Palettes",new paletteviewdlg(),0,0));
enablegui();
}
///----------------------
class nestimingdlg:public GUIcontents
{
GUIintedit *hblank, *vblank,*vfps;
GUIstatictext *mhz;
public:
void setall()
{
hblank->set(HBLANKCYCLES);
vblank->set(VBLANKLINES);
vfps->set(TIMERSPEED);
}
nestimingdlg():GUIcontents(140,80)
{
hblank=new GUIintedit(this,"HBlank cycles: ",5,5,30,HBLANKCYCLES,10,999);
vblank=new GUIintedit(this,"VBlank lines: ",5,23,30,VBLANKLINES,1,999);
vfps=new GUIintedit(this,"Virtual FPS: ",5,41,30,TIMERSPEED,1,999);
new GUIstatictext(this,3,"6502 Mhz: ",30,60);
hblank->moverel((x2-10)-hblank->x2,0);
vblank->moverel((x2-10)-vblank->x2,0);
vfps->moverel ((x2-10)-vfps->x2,0);
}
virtual char *getname() {return "nestimingdlg";}
virtual int acceptfocus() {return 1;}
int sendmessage(GUIrect *c,int guimsg)
{
if (guimsg==GUIMSG_EDITCHANGED)
{
if (c==hblank) {HBLANKCYCLES=hblank->get();}
if (c==vblank) {VBLANKLINES=vblank->get();}
if (c==vfps) {settimerspeed(vfps->get());}
}
return 1;
};
virtual void draw(char *dest)
{
setall();
fill(CLR_BOX); GUIrect::draw(dest);
font[1]->printf(x1+85,y1+60,"%5.2f",((float)TIMERSPEED*CYCLESPERTICK)/1000000);
}
//open functions
static DLGPOS pos; //saved last position of dialog
virtual ~nestimingdlg() {pos.close((GUIbox *)parent);}
};
DLGPOS nestimingdlg::pos;
void m_nestimingview()
{
nestimingdlg::pos.open(new GUIbox(guiroot,"NES Timing",new nestimingdlg(),0,0));
enablegui();
}
//-----------------------------------------
//---------------------------------
class waveoutputdlg:public GUIcontents
{
public:
waveoutputdlg():GUIcontents(BLOCK_LENGTH/2,48) {}
virtual char *getname() {return "waveoutputdlg";}
int sendmessage(GUIrect *c,int guimsg){ return 1; };
virtual void draw(char *dest)
{
if (!ns) return;
short *t=ns->mixingbuf;
int yb=(y1+y2)/2;
for (int i=0; i<BLOCK_LENGTH/2; i++,t+=2)
{
int height=abs(*t)*32/0x2000+1;
if (*t<0) drawrect(dest,0x61,x1+i,yb-height,1,height);
else drawrect(dest,0x61,x1+i,yb,1,height);
}
}
//open functions
static DLGPOS pos; //saved last position of dialog
virtual ~waveoutputdlg() {pos.close((GUIbox *)parent);}
};
DLGPOS waveoutputdlg::pos;
void m_waveoutputview()
{
waveoutputdlg::pos.open(new GUIbox(guiroot,"Wave output",new waveoutputdlg(),0,0));
enablegui();
}