-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·499 lines (440 loc) · 12.3 KB
/
main.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
#include <iostream>
#include <fstream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
// Defines for the mode
#define SPRITE 0
#define PLAYFIELD 1
// A little function for pausing
void Pause();
void LoadFileIntoMem(char * FileName);
void RunScripts();
char FileName[256]; // Stores filename
bool Mode = PLAYFIELD; // Sets graphic mode. 0 = sprite, 1 = playfield
bool Reflected; // Reflected or not
char * FileMem; // File memory
char ASMFileName[256]; // File name to save ASM code in
// BMP class
// Only supports 24 bit bmps for now
class BMP
{
public:
bool LoadFromMem(); // Loads info from memory
unsigned long ReadHexLine(int Min, int Max); // Send min and max spots and return total
void RenderSprite(); // Makes the sprite graphics
void RenderPlayfield(); // Makes the playfield graphics
private:
string MagicNum; // Used to determine OS version
long Size; // Size of file
long DataOffset; // Offset to data info
long HeaderBytes; // Bytes left in header
long Width, Height; // Width and height
int ColorPlanes; // Number of color planes
int BitsPerPix; // Number of bits/pixel
int Compression; // Type of compression
long RawSize; // Raw size of BMP data
};
unsigned long BMP::ReadHexLine(int Min, int Max)
{
long Total = 0;
for (int i=Max; i > Min; --i) // We have to work backwards
{
long Temp;
if (FileMem[i] < 0)
Temp = 256-abs(FileMem[i]);
else
Temp = FileMem[i];
Total += Temp<<(8*(i-(Min+1))); // Adds up the file size via temp
}
return Total;
}
bool BMP::LoadFromMem()
{
// Loads magic number, lame way :D
MagicNum = FileMem[0]; MagicNum += FileMem[1];
// Check the magic number
if (MagicNum != "BM")
{
cout << "Error: Invalid BMP file.\n";
return false;
}
Size = ReadHexLine(0x1, 0x5); // File size
DataOffset = ReadHexLine(0x9, 0xD); // Data offset
HeaderBytes = ReadHexLine(0xD, 0x11); // Bytes left in header
Width = ReadHexLine(0x11, 0x15); // Width
Height = ReadHexLine(0x15, 0x19); // Height
ColorPlanes = ReadHexLine(0x19, 0x1B); // Color planes
BitsPerPix = ReadHexLine(0x1B, 0x1C); // Bits/Pix
Compression = ReadHexLine(0x1D, 0x21); // Compression
RawSize = ReadHexLine(0x21, 0x25); // Size of raw data
if (BitsPerPix != 24)
{
cout << "Error: Requested BMP format not supported.\nPlease use a 24 bit BMP.\n";
return false;
}
// Makes sure the width is proper
if (Width != 8 && Mode == SPRITE) // Must be 8 for sprite
{
cout << "Error: File must be 8 pixels for compiling a sprite.\nWidth in current BMP: " << Width << " pixels.\n";
return false;
}
else if (Width != 40 && Mode == PLAYFIELD) // Must be 40 for playfield
{
cout << "Error: File must be 40 pixels for compiling a playfield.\nWidth in current BMP: " << Width << " pixels.\n";
return false;
}
// Makes sure the height is 192 or less
if (Height > 192)
{
cout << "Error: Height of BMP must be 192 or less.\nCurrent height is: " << Height << " pixels.\n";
return false;
}
// Displays the info
cout << "\nFile information:\n";
cout << "Filesize: " << Size << " bytes.\n";
cout << "Offset to data: " << DataOffset << " bytes.\n";
cout << "Width: " << Width << " pixels.\n";
cout << "Height: " << Height << " pixels.\n";
cout << "Bits per pixel: " << BitsPerPix << ".\n";
cout << "Color planes: " << ColorPlanes << ".\n";
cout << "Size of raw data: " << RawSize << " bytes.\n";
cout << "Bytes per row: " << RawSize/Height;
// Load was successful, yay!
return true;
}
void BMP::RenderSprite()
{
int CountToWidth = 0;
int SubPixelCount = 0;
bool ASMPlayer[8]; // Stores the binary line
int Pixel[3];
ofstream ASMCodeFile;
ASMCodeFile.open(ASMFileName);
// Top part
ASMCodeFile << ";;; Generated with GeekyLink's BMP to Atari 2600 Graphics compiler\n;;; http://www.gekinzuku.com\n";
ASMCodeFile << "PlayerGraphic\n";
// Loops through the data
for (int i=DataOffset; i < Size+1; ++i)
{
unsigned int Temp;
if (FileMem[i] < 0)
Temp = 256-abs(FileMem[i]);
else
Temp = FileMem[i];
++CountToWidth;
// This skips over the 4 byte alignment padding
if (CountToWidth == Width*3+1)
{
ASMCodeFile << "\t.byte #%";
for (int x=0;x<8;x++)
{
ASMCodeFile << ASMPlayer[x];
}
ASMCodeFile << "\n";
i+= (RawSize/Height) - CountToWidth;
CountToWidth=0;
continue;
}
Pixel[SubPixelCount] = Temp; // Stores the pixel colors for comparing
++SubPixelCount;
if (SubPixelCount == 3)
{
if (Pixel[0] != 255 || Pixel[1] != 255 || Pixel[2] != 255) // Sees if pixel is not white
{
// cout << "X,Y: " << CountToWidth/3-1 << "," << (i-DataOffset)/(Width*3+2) << ": Pixel not white.\n";
// cout << Pixel[0] << "," << Pixel[1] << "," << Pixel[2] << endl;
ASMPlayer[CountToWidth/3-1] = 1;
}
else ASMPlayer[CountToWidth/3-1] = 0;
//cout << CountToWidth/3 << endl;
SubPixelCount=0;
//cout << CountToWidth/3 << endl;
}
}
}
void BMP::RenderPlayfield()
{
int CountToWidth = 0;
int SubPixelCount = 0;
bool ASMPlayfield[40][192]; // Stores the binary line
int Pixel[3];
// Loops through the data
for (int i=DataOffset; i < Size+1; ++i)
{
unsigned int Temp;
if (FileMem[i] < 0)
Temp = 256-abs(FileMem[i]);
else
Temp = FileMem[i];
++CountToWidth;
// This skips over the 4 byte alignment padding
if (CountToWidth == Width*3+1)
{
i+= (RawSize/Height) - CountToWidth;
CountToWidth=0;
continue;
}
Pixel[SubPixelCount] = Temp; // Stores the pixel colors for comparing
++SubPixelCount;
if (SubPixelCount == 3)
{
int Y = (i-DataOffset)/(RawSize/Height);
if (Pixel[0] != 255 || Pixel[1] != 255 || Pixel[2] != 255) // Sees if pixel is not white
{
// cout << "X,Y: " << CountToWidth/3-1 << "," << (i-DataOffset)/(Width*3+2) << ": Pixel not white.\n";
// cout << Pixel[0] << "," << Pixel[1] << "," << Pixel[2] << endl;
ASMPlayfield[CountToWidth/3-1][Y] = 1;
//cout << endl << CountToWidth/3-1 << "," << Y << endl;
}
else ASMPlayfield[CountToWidth/3-1][Y] = 0;
//cout << CountToWidth/3 << endl;
SubPixelCount=0;
//cout << CountToWidth/3 << endl;
}
}
ofstream ASMCodeFile;
ASMCodeFile.open(ASMFileName);
// Top part
ASMCodeFile << ";;; Generated with GeekyLink's BMP to Atari 2600 Graphics compiler\n;;; http://www.gekinzuku.com\n";
if (Reflected)
ASMCodeFile << ";;; Graphic is reflected\n";
else
ASMCodeFile << ";;; Graphic is not reflected\n";
ASMCodeFile << "PF0TitleLeft"; // Playfield 0 left
for (int y=0; y<Height; ++y)
{
ASMCodeFile << "\n\t.byte #%";
for (int x=3; x>=0; --x) {
ASMCodeFile << ASMPlayfield[x][y];
}
ASMCodeFile << "0000";
}
ASMCodeFile << "\nPF0TitleRight";// Playfield 0 Right
for (int y=0; y<Height; ++y)
{
ASMCodeFile << "\n\t.byte #%";
if (Reflected)
{
for (int x=36; x<=39; ++x) {
ASMCodeFile << ASMPlayfield[x][y];
}
}
else
{ for (int x=23; x>=20; --x) {
ASMCodeFile << ASMPlayfield[x][y];
}
}
ASMCodeFile << "0000";
}
ASMCodeFile << "\nPF1TitleLeft"; // Playfield 1 left
for (int y=0; y<Height; ++y)
{
ASMCodeFile << "\n\t.byte #%";
for (int x=4; x<=11; ++x) {
ASMCodeFile << ASMPlayfield[x][y];
}
}
ASMCodeFile << "\nPF1TitleRight"; // Playfield 1 right
for (int y=0; y<Height; ++y)
{
ASMCodeFile << "\n\t.byte #%";
if (Reflected) {
for (int x=35; x>=28; --x) {
ASMCodeFile << ASMPlayfield[x][y];
}
}
else {
for (int x=24; x<=31; ++x) {
ASMCodeFile << ASMPlayfield[x][y];
}
}
}
ASMCodeFile << "\nPF2TitleLeft"; // Playfield 2 left
for (int y=0; y<Height; ++y)
{
ASMCodeFile << "\n\t.byte #%";
for (int x=19; x>=12; --x) {
ASMCodeFile << ASMPlayfield[x][y];
}
}
ASMCodeFile << "\nPF2TitleRight"; // Playfield 2 right
for (int y=0; y<Height; ++y)
{
ASMCodeFile << "\n\t.byte #%";
if (Reflected) {
for (int x=20; x<=27; ++x) {
ASMCodeFile << ASMPlayfield[x][y];
}
}
else {
for (int x=39; x>=32; --x) {
ASMCodeFile << ASMPlayfield[x][y];
}
}
}
}
// Start of program
int main (int argc, char **argv)
{
cout << "***********************\n";
cout << "** BMP to Atari 2600 **\n";
cout << "** Compiler V1.2 **\n";
cout << "**-------------------**\n";
cout << "** Programmer: **\n";
cout << "** GeekyLink **\n";
cout << "***********************\n";
cout << "Converts a 24bit BMP to 6502 compatible ASM for use in Atari 2600 games.\n";
cout << "http://www.gekinzuku.com\n\n";
/* check we have command line options */
if (argc == 4)
{
strcpy(FileName, argv[1]);
strcpy(ASMFileName, argv[2]);
if (argv[3][1] == 'p')
Mode = PLAYFIELD;
else if (argv[3][1] == 's')
Mode = SPRITE;
else {
cout << "Error: invalid selection for flag 3. Please use -p or -s.";
cin.ignore();
exit(0);
}
Reflected = false;
RunScripts();
}
else if (argc == 5)
{
strcpy(FileName, argv[1]);
strcpy(ASMFileName, argv[2]);
if (argv[3][1] == 'p')
Mode = PLAYFIELD;
else if (argv[3][1] == 's')
Mode = SPRITE;
else {
cout << "Error: invalid selection for flag 3. Please use -p or -s.";
cin.ignore();
exit(0);
}
if (argv[4][1] == 'r')
Reflected = true;
else if (argv[4][1] == 'n')
Reflected = false;
else {
cout << "Error: invalid selection for flag 4. Please use -n or -r.";
cin.ignore();
exit(0);
}
RunScripts();
exit(0);
}
else if (argc > 1) // If flags but not the right amount
{
cout << "When using flags, the usage is:\nBmpTo2600 image.bmp code.asm -p/s -r/n";
cout << "\n\nImage must be a 24 bit BMP.\n";
cout << "-p is a playfield, -s is a sprite\n";
cout << "-r is a reflected playfield, -n is normal.\n";
cin.ignore();
exit(0);
}
// Asks user if image is a sprite or playfield
char SelectMode = '0';
while (SelectMode == '0')
{
cout << "Select mode:\n";
cout << "1. Sprite\n2. Playfield\nMode: ";
cin.ignore ( cin.rdbuf()->in_avail() );
cin >> SelectMode;
if (SelectMode != '1' && SelectMode != '2')
{
SelectMode = '0';
cout << "Error, invalid selection. Try Again.\n\n";
}
}
// Stores it in the mode var
if (SelectMode == '1')
Mode = SPRITE;
else
{
Mode = PLAYFIELD;
SelectMode = '0';
while (SelectMode == '0')
{
cout << "Select reflected:\n";
cout << "1. Reflected\n2. Not reflected\nChoice: ";
cin.ignore ( cin.rdbuf()->in_avail() );
cin >> SelectMode;
if (SelectMode != '1' && SelectMode != '2')
{
SelectMode = '0';
cout << "Error, invalid selection. Try Again.\n\n";
}
}
if (SelectMode == '1')
Reflected = true;
else Reflected = false;
}
cin.ignore();
// Gets filename
cout << "\nEnter FileName of BMP: ";
cin.ignore ( cin.rdbuf()->in_avail() );
cin.getline(FileName, 256);
cout << "Enter Filename to save ASM code in: ";
cin.ignore ( cin.rdbuf()->in_avail() );
cin.getline(ASMFileName, 256);
RunScripts();
Pause();
return 0;
}
// Loads the file, and sets up the conversion
void RunScripts()
{
// Loads the file
cout << "\nLoading image: " << FileName << endl;
LoadFileIntoMem(FileName);
// Puts info into bmp class
BMP Image;
if (!Image.LoadFromMem())
{
Pause();
exit(1);
}
// Mode to render
if (Mode == SPRITE) Image.RenderSprite();
else Image.RenderPlayfield();
cout << "\n\n" << ASMFileName << " created successfully!\n";
delete[] FileMem;
}
// Loads file into the FileMemory
void LoadFileIntoMem(char * FileName)
{
ifstream::pos_type FileSize; // File size
// Reading in the file
ifstream Image;
Image.open(FileName, ios::in|ios::binary|ios::ate); // Opens file
// Did image open correctly?
if (Image.is_open())
{
// Loads the file into memory for easy reading
FileSize = Image.tellg();
FileMem = new char [FileSize];
Image.seekg(0, ios::beg);
Image.read(FileMem, FileSize);
Image.close();
}
else // Tell user of the error, and exit
{
cout << "Error: The file was either misspelled or does not exist.\n";
Pause();
delete[] FileMem;
exit(1);
}
}
// Pauses the screen, just cause it irritates me typing it :P
void Pause()
{
cout << "Press Enter to continue...\n";
cin.ignore ( cin.rdbuf()->in_avail() );
cin.get();
}