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
/
Copy pathNESVIDEO.OLD
executable file
·477 lines (388 loc) · 9.69 KB
/
NESVIDEO.OLD
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
//functions for NES display
#include <string.h>
#include <stdlib.h>
#include "r2img.h"
#include "font.h"
#include "nesvideo.h"
#include "nes.h"
#include "gui.h"
#include "message.h"
#include "config.h"
#include "dd.h"
//color index from which all nes colors are based
#define CBASE 224
nesvideo *nv; //global nes video
#include "file.h"
//--------------------------
//palette stuff
#include "nespal.h"
void initializenespalette()
{
/* FILEIO f;
f.open("d:\\emu\\ines\\nesa.com");
f.setpos(0xB4e);
for (int i=0; i<0x40; i++)
{
char c;
f.read(&nespal[i],3); //read palette
f.read(&c,1);
msg.printf(2,"%X: %X %X %X",i,nespal[i].r,nespal[i].g,nespal[i].b);
}
f.close();
f.create("nes.pal");
f.write(nespal,256*3);
f.close();*/
FILEIO f;
f.open("anes.pal");
f.read(nespal,256*3);
f.close();
msg.printf(2,"NES Palette initialized");
}
//32 different indexes
void updatepalette(int palidx,byte paldata)
{
setpalettenum(CBASE+palidx,&nespal[paldata]);
// msg.printf(1,"palette[%d]=%X",palidx,paldata);
}
void nesvideo::refreshpalette()
{
if (!paletteupdated) return;
//update bg pals
for (int i=0; i<32; i++)
if (palupdateidx[i])
{
updatepalette(i,ppu->bgpal.c[i]);
palupdateidx[i]=0;
}
paletteupdated=0;
}
void nesvideo::resetpalette()
{
for (int i=0; i<32; i++) palupdateidx[i]=1;
paletteupdated=1;
}
//--------------------------------
//pattern stuff
void bitmap8x8::create(NES_pattern *np)
{
memset(s,0,8*8); //clear it
//create 256 color pattern out of nes bit pattern
for (int y=0; y<8; y++)
for (int x=0; x<8; x++)
{
if (np->low[y] &(0x80>>x)) s[y][x]|=1;
if (np->high[y]&(0x80>>x)) s[y][x]|=2;
}
}
void bitmap8x8::add_sprite(byte b)
{
char *t=(char *)s;
for (int i=0; i<64; i++,t++)
if (*t) *t+=b;
}
void bitmap8x8::add_tile(byte b)
{
char *t=(char *)s;
for (int i=0; i<64; i++,t++) *t+=b;
}
void bitmap8x8::draw_tile(char *dest,int x,int y)
{
// y-=8; //dont show top 2 lines
if (x<0 || y<0) return; //clip left
if (x+8>SCREENX || y+8>SCREENY) return; //clip bottomright
dest+=y*PITCH+x;
for (int i=0; i<8; i++,dest+=PITCH)
// for (int j=0; j<8; j++) dest[j]=s[i][j];
memcpy(dest,&s[i],8);
}
void bitmap8x8::draw_sprite(char *dest,int x,int y,int o)
{
// y-=8; //dont show top 2 lines
if (x<0 || y<0) return; //clip left
if (x+8>SCREENX || y+8>SCREENY) return; //clip bottomright
dest+=y*PITCH+x;
switch (o)
{
case 0: //normal
{
for (int i=0; i<8; i++,dest+=PITCH)
for (int j=0; j<8; j++)
if (s[i][j]) dest[j]=s[i][j];
}
break;
case 1: //flipx
{
for (int i=0; i<8; i++,dest+=PITCH)
for (int j=0; j<8; j++)
if (s[i][j]) dest[j^7]=s[i][j];
}
break;
case 2: //flipy
{
for (int i=7; i>=0; i--,dest+=PITCH)
for (int j=0; j<8; j++)
if (s[i][j]) dest[j]=s[i][j];
}
break;
case 3: //flipx|flipy
{
for (int i=7; i>=0; i--,dest+=PITCH)
for (int j=0; j<8; j++)
if (s[i][j]) dest[j^7]=s[i][j];
}
break;
}
}
void pattern::setpatternptr(int tablenum,int index)
{
np=&ppu->pt[tablenum].p[index];
updated=1;
}
//create pattern from NES_pattern
void pattern::refresh()
{
bitmap8x8 b;
b.create(np); //create bitmap
//create bg tiles
for (int i=0; i<4; i++)
{
tile[i]=b; //copy bitmap
tile[i].add_tile(CBASE+(i<<2)); //add attribute and cbase
}
//create sprites
for (i=0; i<4; i++)
{
sprite[i]=b; //copy bitmap
sprite[i].add_sprite(CBASE+16+(i<<2)); //add attribute and cbase
}
updated=0;
}
void nesvideo::resetpatterncache()
{
//reset patterns
for (int i=0; i<256; i++)
{
ptn[0][i].setpatternptr(0,i);
ptn[1][i].setpatternptr(1,i);
}
patternupdated=1;
}
//---------------------------------
//main video interface
void initattriblookup();
static DLGPOS dlgpos;
void cleardesktop();
nesvideo::nesvideo(char *romfile):
GUIcontents(256,224),maximized(0)
{
if (nv) delete nv->parent; //delete old video
xw=width(); yw=height();
initializenespalette();
initattriblookup();
resetpatterncache();
//wrap us up in box
GUImaximizebox *p=new GUImaximizebox(guiroot,romfile,this,0,0);
dlgpos.open(p);
if (SCREENX<=320) p->maximize();
msg.printf(2,"NES video initialized: %dx%d %d",xw,yw,sizeof(*this));
nv=this;
cleardesktop();
}
nesvideo::~nesvideo()
{
cfg->set(CFG_NOFILLEDDESKTOP,0); //force desktop to be drawn
msg.printf(2,"NES video destroyed");
nv=0;
freerom();
dlgpos.close((GUIbox *)parent);
}
//----------------------------------------------
extern byte scrollx,scrolly;
//rotation lookup
byte attriblookup[4][4];
void initattriblookup()
{
for (int x=0; x<4; x++)
for (int y=0; y<4; y++)
{
byte square=(x>>1) | (y&2);
attriblookup[x][y]=square*2;
}
// attriblookup[a][x][y]=(a>>(((x>>1)|((y>>1)<<1))*2))&3;
}
inline byte NES_attributetable::getat(int x,int y)
{
return (a[y/4][x/4]>>attriblookup[x&3][y&3])&3;
}
void nesvideo::drawbg(char *dest)
{
//find screen pattern table to show...
pattern (*p)[256]=&nv->ptn[(ram[0x2000]&16) ? 1 : 0];
//find name/attribute table to show
NES_natable *nat=&ppu->nat[ram[0x2000]&3];
//find name table to show
NES_nametable *nt=&nat->nt;
//find attribute table to show
NES_attributetable *at=&nat->at;
int i=0;
//lets draw this shit!!!
for (int y=1; y<29; y++)
for (int x=0; x<32; x++)
(*p)[nt->t[y][x]].tile[at->getat(x,y)].draw_tile(dest,x*8,y*8-8);
// dest[y*PITCH+x]=nt->t[y][x];
// (*p)[(i++)&0xFF].draw(dest,x*8,y*8);
// (*p)[0].draw(dest,x*8,y*8);
font[0]->printf(50,50,"scroll=%d,%d",scrollx,scrolly);
//font[0]->printf(50,60,"nametable=%d ptable=%d",ram[0x2000]&3,(ram[0x2000]&16) ? 1 : 0);
}
inline void NES_sprite::draw_8x8(char *dest,pattern *pt)
{
if (x<SCREENX && y<SCREENY)
pt[p].sprite[attrib].draw_sprite(dest,x,y+1-8,flipx|(flipy<<1));
}
inline void NES_sprite::draw_8x16(char *dest,pattern *pt,pattern *pt2)
{
if (x<SCREENX && y<SCREENY)
if (!(p&1)) //pattern table 1
{
if (!flipy)
{
pt[p].sprite[attrib].draw_sprite(dest,x,y+1-8,flipx);
pt[p+1].sprite[attrib].draw_sprite(dest,x,y+9-8,flipx);
} else
{
pt[p+1].sprite[attrib].draw_sprite(dest,x,y+1-8,flipx|2);
pt[p].sprite[attrib].draw_sprite(dest,x,y+9-8,flipx|2);
}
} else //pattern table 2
{
if (!flipy)
{
pt2[p-1].sprite[attrib].draw_sprite(dest,x,y+1-8,flipx);
pt2[p].sprite[attrib].draw_sprite(dest,x,y+9-8,flipx);
} else
{
pt2[p].sprite[attrib].draw_sprite(dest,x,y+1-8,flipx|2);
pt2[p-1].sprite[attrib].draw_sprite(dest,x,y+9-8,flipx|2);
}
}
}
void nesvideo::drawsprites_8x8(char *dest)
{
//find sprite pattern table to show...
pattern *p=(pattern *)&nv->ptn[(ram[0x2000]&8) ? 1 : 0];
//draw every sprite
for (int i=0; i<64; i++)
spritemem[i].draw_8x8(dest,p);
}
void nesvideo::drawsprites_8x16(char *dest)
{
pattern *p1,*p2;
//find sprite pattern tables to show...
p1=(pattern *)&nv->ptn[(ram[0x2000]&8) ? 1 : 0];
p2=(pattern *)&nv->ptn[(ram[0x2000]&8) ? 0 : 1];
//draw every sprite
for (int i=0; i<64; i++)
spritemem[i].draw_8x16(dest,p1,p2);
//font[0]->printf(50,50,"8x16");
//font[0]->printf(50,60,"ptable=%d",(ram[0x2000]&8) ? 1 : 0);
//font[0]->printf(50,50,"%d,%d",scrollx,scrolly);
}
//----------------------------------------------
int ccx,ccy;
int docolorcheck;
void cleardesktop()
{
#ifdef WIN95
if (nv) nv->forcedesktopfill=8;
#else
if (nv) nv->forcedesktopfill=1;
#endif
}
//update screen
void nesvideo::draw(char *dest)
{
cfg->set(CFG_NOFILLEDDESKTOP,maximized);
if (forcedesktopfill>0) {fill(0); forcedesktopfill--;}
else
//blank screen...
if (!(ram[0x2001]&8))
{
fill(0);
return;
}
//update patterns
if (patternupdated)
{
for (int i=0; i<256; i++)
{
if (ptn[0][i].updated) ptn[0][i].refresh();
if (ptn[1][i].updated) ptn[1][i].refresh();
}
patternupdated=0;
}
//force clipping
CLIP clip(dest,x1+(width()-xw)/2,y1+(height()-yw)/2,x1+(width()+xw)/2,y1+(height()+yw)/2);
//draw background
drawbg(dest);
if ((ram[0x2001]&16)) //show sprites
if (ram[0x2000]&32) //8x16 sprites ?
drawsprites_8x16(dest);
else drawsprites_8x8(dest);
// font[0]->printf(50,50,"hitflag=%d",spritemem[0].y);
// font[0]->printf(50,60,"0x2000=%X",ram[0x2000]);
/*
if (docolorcheck)
{
byte c=dest[ccy*PITCH+ccx];
c-=CBASE;
if (c>=0 && c<32)
{
COLOR *r=&nespal[ppu->bgpal.c[c]];
msg.printf(0,"(%d,%d): %s pixel=%X pal=%X RGB=%X,%X,%X",ccx,ccy,
c>=16 ? "sprite" : "bg",c&0xF,ppu->bgpal.c[c],
r->r,r->g,r->b);
}
docolorcheck=0;
} */
}
int nesvideo::keyhit(char kbscan,char key)
{
return 0;
}
#include "mouse.h"
GUIrect * nesvideo::click(mouse &m)
{
/* ccx=m.x-x1;
ccy=m.y-y1;
docolorcheck=1;*/
return 0;
}
//--------------------------
//GUI related bullshit
void enablegui();
void nesvideo::restore()
{
//msg.printf(2,"nesvideo restore");
cfg->set(CFG_NOFILLEDDESKTOP,0); //force desktop to be drawn
maximized=0;
resize(xw,yw);
enablegui();
cleardesktop();
}
void nesvideo::maximize()
{
//msg.printf(2,"nesvideo maximize");
cfg->set(CFG_NOFILLEDDESKTOP,1); //force desktop to not be drawn
moveto(0,0);
maximized=1;
resize(SCREENX,SCREENY);
cleardesktop();
}
void nesvideo::resize(int txw,int tyw)
{
//msg.printf(2,"nesvideo resize %d,%d",txw,tyw);
GUIrect::resize(txw,tyw);
if (maximized && parent)
((GUImaximizebox *)parent)->reposmaxbutton();
}