~amaurycarvalho/nmsxtiles/trunk

« back to all changes in this revision

Viewing changes to screenw.cpp

  • Committer: Amaury Carvalho
  • Date: 2020-03-19 21:21:28 UTC
  • Revision ID: amauryspires@gmail.com-20200319212128-mpz9vo6xqm83vuxf
Commit on 19/03/2020 06:21:28  -03 by amaury

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <screenw.h>
 
2
#include "dlgbankrange.h"
 
3
#include <QDebug>
 
4
#include <QFile>
 
5
#include <labelbtn.h>
 
6
#include <QPixmap>
 
7
#include <supportfuncs.h>
 
8
#include <fstream>
 
9
 
 
10
#include <string.h>
 
11
 
 
12
using namespace std;
 
13
 
 
14
extern int PletterMain( int dataSize, unsigned char *pData, string destfilename );
 
15
 
 
16
 
 
17
const int CScreenW::PAL_ALL = 100;
 
18
 
 
19
CScreenW::CScreenW()
 
20
{
 
21
        setupUi( this );        
 
22
}
 
23
 
 
24
 
 
25
void CScreenW::Initialize()
 
26
{
 
27
        int i;
 
28
        int x, y;
 
29
        QPalette pal;
 
30
        QWidget *pTab;
 
31
        QImage image;
 
32
 
 
33
                
 
34
        m_pLblScreen = new CLabelBtn( m_pFrmScreen, 32*16, 24*16 );
 
35
        m_pLblScreen->setGeometry( 3, 3, 32*16, 24*16 );
 
36
        m_pLblScreen->setMouseTracking( true );
 
37
        m_pLblScreen->show();
 
38
 
 
39
        m_pLblSelection = new CLabelBtn( m_pFrmScreen, 0, 0 );
 
40
        m_pLblSelection->setGeometry( 0, 0, 0, 0 );
 
41
        m_pLblSelection->setFrameShape( QFrame::Box );
 
42
        m_pLblSelection->setFrameShadow( QFrame::Raised );
 
43
        pal = m_pLblSelection->palette();
 
44
        pal.setColor( QPalette::Dark, Qt::green );
 
45
        m_pLblSelection->setPalette( pal );
 
46
 
 
47
 
 
48
 
 
49
        connect( m_pLblScreen, SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnScreenClickLeft( int, int ) ) );
 
50
        connect( m_pLblScreen, SIGNAL( onReleasedLeft( int, int ) ), this, SLOT( OnScreenReleaseLeft( int, int ) ) );
 
51
        connect( m_pLblScreen, SIGNAL( onReleasedRight( int, int ) ), this, SLOT( OnScreenReleaseRight( int, int ) ) );
 
52
        connect( m_pLblScreen, SIGNAL( onMouseMoveEvent( int, int ) ), this, SLOT( OnScreenMouseMoveEvent( int, int ) ) );
 
53
 
 
54
        
 
55
        m_pTabBank = new QTabWidget( this );
 
56
        m_pTabBank->setGeometry( QRect( 5, 420, 596, 184 ) );
 
57
 
 
58
        for( i = 0; i < 3; i++ )
 
59
        {
 
60
                pTab = new QWidget();
 
61
                m_pTabBank->addTab( pTab, QString());
 
62
 
 
63
                m_pTabBank->setTabText( m_pTabBank->indexOf( pTab), QString( "Bank %1" ).arg( i ) );
 
64
                
 
65
                m_pFrmBank[i] = new QFrame( pTab );
 
66
                m_pFrmBank[i]->setGeometry( QRect( 5, 10, 591, 194) );
 
67
                m_pFrmBank[i]->setFrameShape( QFrame::StyledPanel );
 
68
                m_pFrmBank[i]->setFrameShadow( QFrame::Raised );
 
69
 
 
70
                m_pLblBank[i] = new CLabelBtn( m_pFrmBank[i], 32*18, 8*18 );
 
71
                m_pLblBank[i]->setGeometry( 0, 0, m_pFrmBank[i]->width(), m_pFrmBank[i]->height() );
 
72
                
 
73
                m_pLblBank[i]->show();
 
74
        }
 
75
        
 
76
        connect( m_pTabBank, SIGNAL( currentChanged( int ) ), this, SLOT( OnBankChanged( int ) ) );
 
77
        connect( m_pLblBank[0], SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnBank0Click( int, int ) ) );
 
78
        connect( m_pLblBank[1], SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnBank1Click( int, int ) ) );
 
79
        connect( m_pLblBank[2], SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnBank2Click( int, int ) ) );
 
80
 
 
81
        m_pLblBank[0]->setMouseTracking( true );
 
82
        m_pLblBank[1]->setMouseTracking( true );
 
83
        m_pLblBank[2]->setMouseTracking( true );
 
84
                /*
 
85
        connect( m_pLblBank[0], SIGNAL( onReleasedLeft( int, int ) ), this, SLOT( OnBankReleaseLeft( int, int ) ) );
 
86
        connect( m_pLblBank[1], SIGNAL( onReleasedLeft( int, int ) ), this, SLOT( OnBankReleaseLeft( int, int ) ) );
 
87
        connect( m_pLblBank[2], SIGNAL( onReleasedLeft( int, int ) ), this, SLOT( OnBankReleaseLeft( int, int ) ) );
 
88
*/
 
89
        connect( m_pLblBank[0], SIGNAL( onDoubleClickedLeft( int, int ) ), this, SLOT( OnBank0DoubleClick( int, int ) ) );
 
90
        connect( m_pLblBank[1], SIGNAL( onDoubleClickedLeft( int, int ) ), this, SLOT( OnBank1DoubleClick( int, int ) ) );
 
91
        connect( m_pLblBank[2], SIGNAL( onDoubleClickedLeft( int, int ) ), this, SLOT( OnBank2DoubleClick( int, int ) ) );
 
92
        
 
93
        connect( m_pGrOneBank,   SIGNAL( clicked() ), this, SLOT( OnOneBank() ) );
 
94
                
 
95
        m_pLblPalette = new CLabelBtn( m_pFrmPalette, 40, 96 );
 
96
        m_pLblPalette->setGeometry( 0, 0, m_pFrmPalette->width(), m_pFrmPalette->height() );
 
97
 
 
98
        connect( m_pLblPalette, SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnPaletteClick( int, int ) ) );
 
99
 
 
100
        m_pLblPalBack = new CLabelBtn( m_pFrmDesigner, 16, 10 );
 
101
        m_pLblPalBack->setGeometry( m_pLblPalBackC->x(), m_pLblPalBackC->y() + 25, 16, 10 );
 
102
        
 
103
        m_pLblPalFore = new CLabelBtn( m_pFrmDesigner, 16, 10 );
 
104
        m_pLblPalFore->setGeometry( m_pLblPalForeC->x(), m_pLblPalForeC->y() + 25, 16, 10 );
 
105
 
 
106
        connect( m_pLblPalBack, SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnPalBackClick( int, int ) ) );
 
107
        connect( m_pLblPalFore, SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnPalForeClick( int, int ) ) );
 
108
        
 
109
        m_pLblTile = new CLabelBtn( m_pFrmTile, 96, 96 );
 
110
        m_pLblTile->setGeometry( 2, 2, m_pFrmTile->width(), m_pFrmTile->height() );
 
111
        
 
112
        connect( m_pLblTile, SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnTileClickLeft( int, int ) ) );
 
113
        connect( m_pLblTile, SIGNAL( onClickedRight( int, int ) ), this, SLOT( OnTileClickRight( int, int ) ) );
 
114
        
 
115
        m_pLblColors = new CLabelBtn( m_pFrmColors, 96, 12 );
 
116
        m_pLblColors->setGeometry( 3, 3, 12*16, 16 );
 
117
        
 
118
        image = QImage( m_pLblColors->width(), m_pLblColors->height(), QImage::Format_RGB32 );
 
119
        
 
120
        for( x = 0; x < 16; x++ ) CSupportFuncs::SetPixelRect( &image, x*12, 0, CSupportFuncs::COLORS_TABLE[x], 12, 16 );
 
121
        
 
122
        m_pLblColors->put_Image( image );
 
123
        
 
124
        connect( m_pLblColors, SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnColorsClick( int, int ) ) );
 
125
 
 
126
        connect( m_pBtnFromBank, SIGNAL( clicked() ), this, SLOT( OnFromBank() ) );
 
127
        connect( m_pBtnToBank, SIGNAL( clicked() ), this, SLOT( OnToBank() ) );
 
128
 
 
129
        connect( m_pRadBack, SIGNAL( clicked() ), this, SLOT( OnRadBack() ) );
 
130
        connect( m_pRadFore, SIGNAL( clicked() ), this, SLOT( OnRadFore() ) );
 
131
        
 
132
        m_SelColor = QPoint( 1, 0 );
 
133
        
 
134
        for( x = 0; x < 256*3; x++ ) m_Screen[x] = 0;
 
135
        for( y = 0; y < 3; y++ ) for( x = 0; x < 256; x++ )     m_TilesBank[x][y] = CTile();
 
136
        
 
137
        m_pRadFore->setChecked( true );
 
138
        
 
139
        connect( m_pGrTileClear, SIGNAL( clicked() ), this, SLOT( OnTileClear() ) );
 
140
        connect( m_pGrTileReorder, SIGNAL( clicked() ), this, SLOT( OnTileReorder() ) );
 
141
        connect( m_pGrTileShiftDown, SIGNAL( clicked() ), this, SLOT( OnTileShiftDown() ) );
 
142
        connect( m_pGrTileShiftUp, SIGNAL( clicked() ), this, SLOT( OnTileShiftUp() ) );                
 
143
        connect( m_pGrTileShiftLeft, SIGNAL( clicked() ), this, SLOT( OnTileShiftLeft() ) );
 
144
        connect( m_pGrTileShiftRight, SIGNAL( clicked() ), this, SLOT( OnTileShiftRight() ) );
 
145
        connect( m_pGrTileFlipHorizontal, SIGNAL( clicked() ), this, SLOT( OnTileFlipHorizontal() ) );
 
146
        connect( m_pGrTileFlipVertical, SIGNAL( clicked() ), this, SLOT( OnTileFlipVertical() ) );
 
147
        connect( m_pGrTileInvertColors, SIGNAL( clicked() ), this, SLOT( OnTileInvertColors() ) );
 
148
        connect( m_pGrTileRotate, SIGNAL( clicked() ), this, SLOT( OnTileRotate() ) );
 
149
        
 
150
        setMouseTracking( true );
 
151
 
 
152
        for( x = 0; x < 32; x++ )
 
153
        {
 
154
                m_pColsLabels[x] = new QLabel( QString( "%1" ).arg( x ), this );
 
155
                m_pColsLabels[x]->setGeometry( 16*x+26, 5, 16, 16 );
 
156
                m_pColsLabels[x]->setAlignment( Qt::AlignCenter );
 
157
            m_pColsLabels[x]->setFrameShape(QFrame::Box);
 
158
            m_pColsLabels[x]->setFrameShadow(QFrame::Raised);
 
159
        }
 
160
 
 
161
        for( x = 0; x < 24; x++ )
 
162
        {
 
163
                m_pRowsLabels[x] = new QLabel( QString( "%1" ).arg( x%8 ), this );
 
164
                m_pRowsLabels[x]->setGeometry( 6, 16*x+26, 16, 16 );    
 
165
                m_pRowsLabels[x]->setAlignment( Qt::AlignCenter );
 
166
            m_pRowsLabels[x]->setFrameShape(QFrame::Box);
 
167
            m_pRowsLabels[x]->setFrameShadow(QFrame::Raised);
 
168
        }
 
169
        
 
170
        m_LastGridValue = QPoint( 0, 0 );
 
171
 
 
172
        //
 
173
        m_CursorImage = QImage( 16, 16, QImage::Format_RGB32 ); //TODO - 1 No membre
 
174
        CSupportFuncs::SetPixelRect( &m_CursorImage, 0, 0, CSupportFuncs::COLORS_TABLE[15], 16, 16 );
 
175
        m_pLblCursor[0] = new CLabelBtn( m_pFrmScreen, 15, 1 );
 
176
        m_pLblCursor[0]->setGeometry( 0, 0, 0, 0 );
 
177
        m_pLblCursor[0]->put_Image( m_CursorImage );
 
178
 
 
179
        m_pLblCursor[1] = new CLabelBtn( m_pFrmScreen, 1, 15 );
 
180
        m_pLblCursor[1]->setGeometry( 0, 0, 0, 0 );
 
181
        m_pLblCursor[1]->put_Image( m_CursorImage );
 
182
 
 
183
        m_pLblCursor[2] = new CLabelBtn( m_pFrmScreen, 15, 11 );
 
184
        m_pLblCursor[2]->setGeometry( 0, 0, 0, 0 );
 
185
        m_pLblCursor[2]->put_Image( m_CursorImage );
 
186
 
 
187
        m_pLblCursor[3] = new CLabelBtn( m_pFrmScreen, 1, 15 );
 
188
        m_pLblCursor[3]->setGeometry( 0, 0, 0, 0 );
 
189
        m_pLblCursor[3]->put_Image( m_CursorImage );
 
190
 
 
191
        for( i = 0; i < 256; i++ )
 
192
        {
 
193
                m_pGrBackgroundTiles0->addItem( QString( "%1" ).arg( i ) );
 
194
                m_pGrBackgroundTiles1->addItem( QString( "%1" ).arg( i ) );
 
195
                m_pGrBackgroundTiles2->addItem( QString( "%1" ).arg( i ) );
 
196
        }
 
197
/*
 
198
 
 
199
        m_pLblBankTile = new CLabelBtn( this, 16, 16 );
 
200
        m_pLblBankTile->setGeometry( 0, 0, 16, 16 );
 
201
        m_pLblBankTile->setPixmap( QPixmap( 16, 16 ) );
 
202
//      m_pLblBankTile->setMouseTracking( true );
 
203
        m_pLblBankTile->setVisible( false );
 
204
        //m_pLblBankTile->show();
 
205
        */
 
206
        /*
 
207
        connect( m_pLblBankTile, SIGNAL( onMouseMoveEvent( int, int ) ), this, SLOT( OnBankTileMouseMoveEvent( int, int ) ) );
 
208
        connect( m_pLblBankTile, SIGNAL( onReleasedLeft( int, int ) ), this, SLOT(   OnBankTileReleaseLeft( int, int ) ) );
 
209
        connect( m_pLblBankTile, SIGNAL( onClickedLeft( int, int ) ), this, SLOT( OnBankTileClickLeft( int, int ) ) );
 
210
        connect( m_pLblBankTile, SIGNAL( onDoubleClickedLeft( int, int ) ), this, SLOT( OnBankTileDoubleClickLeft( int, int ) ) );
 
211
        m_LastBankTilePos = QPoint( -1, -1 );
 
212
*/
 
213
 
 
214
        //
 
215
 
 
216
//      m_DragDrop = false;
 
217
 
 
218
        m_Selecting = false;
 
219
 
 
220
        m_SelectionRect = QRect( 0, 0, 0, 0 );
 
221
        m_LastClickPos = QPoint( -1, -1 );
 
222
        
 
223
        m_LastTilesBlockW = m_pGrTilesBlockW->text().toLongLong();
 
224
        m_LastTilesBlockH = m_pGrTilesBlockH->text().toLongLong();
 
225
 
 
226
        m_pFrmBackgroundTiles->setVisible( false );
 
227
 
 
228
        m_Close = false;
 
229
}
 
230
 
 
231
void CScreenW::UpdateBank( int tile, int bank, bool updateScreen )
 
232
{
 
233
        PaintTile( &m_BankImages[bank], m_TilesBank[tile][bank], (tile%32)*18+1, (tile/32)*18+1, 2 );
 
234
 
 
235
        m_pLblBank[bank]->put_Image( m_BankImages[bank] ); 
 
236
        
 
237
        if( updateScreen ) UpdateScreen();
 
238
}
 
239
 
 
240
void CScreenW::InitBanks()
 
241
{
 
242
        int i, j;
 
243
        
 
244
        
 
245
        for( j = 0; j < 3; j++ ) 
 
246
        {
 
247
                m_BankImages[j] = m_pLblBank[j]->pixmap()->toImage();
 
248
 
 
249
                PaintGrid( &m_BankImages[j], 16, 16 );
 
250
 
 
251
                for( i = 0; i < 256; i++ ) 
 
252
                {
 
253
                        PaintTile( &m_BankImages[j], m_TilesBank[i][j], (i%32)*18+1, (i/32)*18+1, 2 );
 
254
                }
 
255
 
 
256
                m_pLblBank[j]->put_Image( m_BankImages[j] );
 
257
        }
 
258
 
 
259
        UpdateScreen();
 
260
        
 
261
        OnBank0Click( 1, 1 );
 
262
}
 
263
 
 
264
void CScreenW::UpdateScreen()
 
265
{
 
266
        int i;
 
267
        int row, col;
 
268
        QImage image;
 
269
        
 
270
        
 
271
        image = QImage( 32*16, 24*16, QImage::Format_RGB32 );
 
272
        
 
273
        for( i = 0; i < 3; i++ )
 
274
        {
 
275
                for( row = 0; row < 8; row++ )
 
276
                {
 
277
                        for( col = 0; col < 32; col++ )
 
278
                        {
 
279
                                PaintTile( &image, m_TilesBank[ m_Screen[(i*8+row)*32+col] ][ i ], col*16, (i*8+row)*16, 2 );
 
280
                        }
 
281
                }
 
282
        }
 
283
        m_pLblScreen->put_Image( image );
 
284
}
 
285
 
 
286
 
 
287
void CScreenW::CopyTile()
 
288
{
 
289
        if( m_ScreenSelection.size() ) m_ScreenSelectionCopy = m_ScreenSelection;
 
290
        else 
 
291
        {
 
292
                m_CopyTile = m_TilesBank[m_SelTile.x()][m_SelTile.y()];
 
293
                m_ScreenSelectionCopy.clear();
 
294
        }
 
295
}
 
296
 
 
297
void CScreenW::PasteTile()
 
298
{
 
299
        int xI;
 
300
        int x, y;
 
301
        int i, j;
 
302
        QList<int> s;
 
303
 
 
304
 
 
305
        if( m_ScreenSelectionCopy.size() )
 
306
        {
 
307
                xI = m_MouseX / 16;
 
308
                y = m_MouseY / 16;
 
309
        
 
310
                if( m_ScreenSelectionCopy.size() == m_SelectionRect.height() && m_ScreenSelectionCopy.at( 0 ).size() == m_SelectionRect.width() )
 
311
                {
 
312
                        for( i = 0; i < m_ScreenSelectionCopy.size(); i++ )
 
313
                        {
 
314
                                s =  m_ScreenSelectionCopy.at( i );
 
315
 
 
316
                                if( ( i + y ) > 23 ) break;
 
317
 
 
318
                                for( j = 0, x = xI; j < s.size(); j++ )
 
319
                                {
 
320
                                        if( ( j + x ) > 31 ) break;
 
321
 
 
322
                                        m_Screen[(i+y)*32+j+x] = s.at( j );
 
323
                                }
 
324
                        }
 
325
                }
 
326
                else
 
327
                {
 
328
                        for( y = m_SelectionRect.top(), j = 0; y <= m_SelectionRect.bottom(); y++, j++ )
 
329
                        {
 
330
                                if( j == m_ScreenSelectionCopy.size() ) j = 0;
 
331
 
 
332
                                s = m_ScreenSelectionCopy.at( j );
 
333
 
 
334
                                for( x = m_SelectionRect.left(), i = 0; x <= m_SelectionRect.right(); x++, i++ )
 
335
                                {
 
336
                                        if( i == m_ScreenSelectionCopy.at( j ).size() ) i = 0;
 
337
 
 
338
                                        m_Screen[y*32+x] = s.at( i );
 
339
                                }
 
340
                        }
 
341
                }
 
342
                UpdateScreen();
 
343
        }
 
344
        else
 
345
        {
 
346
                if( m_ScreenSelection.size() )
 
347
                {
 
348
                        for( y = m_SelectionRect.top(); y <= m_SelectionRect.bottom(); y++ )
 
349
                        {
 
350
                                for( x = m_SelectionRect.left(); x <= m_SelectionRect.right(); x++ )
 
351
                                {
 
352
                                        m_Screen[y*32+x] = m_SelTile.x();
 
353
                                }
 
354
                        }
 
355
                        UpdateScreen();
 
356
                }
 
357
                else
 
358
                {
 
359
                        m_TilesBank[m_SelTile.x()][m_SelTile.y()] = m_CopyTile;
 
360
                        UpdateBank( m_SelTile.x(), m_SelTile.y() );
 
361
                }
 
362
        }
 
363
}
 
364
 
 
365
void CScreenW::PasteTileColors()
 
366
{
 
367
        int y;
 
368
        
 
369
        
 
370
        for( y = 0; y < 8; y++ )
 
371
        {
 
372
                m_TilesBank[m_SelTile.x()][m_SelTile.y()].SetForeColor( y, m_CopyTile.GetForeColor( y ) );
 
373
                m_TilesBank[m_SelTile.x()][m_SelTile.y()].SetBackColor( y, m_CopyTile.GetBgColor( y ) );
 
374
        }
 
375
        UpdateBank( m_SelTile.x(), m_SelTile.y() );
 
376
}
 
377
 
 
378
void CScreenW::OnBank0DoubleClick( int x, int y )
 
379
{
 
380
        OnBankDoubleClick( x, y, 0 );
 
381
}
 
382
 
 
383
void CScreenW::OnBank1DoubleClick( int x, int y )
 
384
{
 
385
        OnBankDoubleClick( x, y, 1 );
 
386
}
 
387
 
 
388
void CScreenW::OnBank2DoubleClick( int x, int y )
 
389
{
 
390
        OnBankDoubleClick( x, y, 2 );
 
391
}
 
392
 
 
393
void CScreenW::OnBankDoubleClick( int /*x*/, int /*y*/, int /*bank*/ )
 
394
{
 
395
        m_pLblSelection->setGeometry( 0, 0, 0, 0 ); //TODO - 1 Class CSelection
 
396
        m_ScreenSelection.clear();
 
397
 
 
398
        OnFromBank();
 
399
}
 
400
 
 
401
 
 
402
void CScreenW::OnBankTileDoubleClickLeft( int x, int y )
 
403
{
 
404
        OnFromBank();
 
405
}
 
406
/*
 
407
void CScreenW::OnBankTileClickLeft( int x, int y )
 
408
{
 
409
 
 
410
        int mX, mY;
 
411
 
 
412
 
 
413
//      mX = m_pLblBankTile->x() - m_pTabBank->x() - m_pFrmBank[0]->x() - 4;
 
414
//      mY = m_pLblBankTile->y() - m_pTabBank->y() - m_pFrmBank[0]->y() - 25;
 
415
 
 
416
        qDebug()<<"CScreenW::OnBankTileClickedLEft currentTab="<<m_pTabBank->currentIndex();
 
417
 
 
418
        OnBankClick( mX, mY, m_pTabBank->currentIndex() );
 
419
}
 
420
*/
 
421
void CScreenW::OnBank0Click( int x, int y )
 
422
{
 
423
        OnBankClick( x, y, 0 );
 
424
}
 
425
 
 
426
void CScreenW::OnBank1Click( int x, int y )
 
427
{
 
428
        OnBankClick( x, y, 1 );
 
429
}
 
430
 
 
431
void CScreenW::OnBank2Click( int x, int y )
 
432
{
 
433
        OnBankClick( x, y, 2 );
 
434
}
 
435
 
 
436
void CScreenW::OnBankClick( int x, int y, int bank )
 
437
{
 
438
    m_pLblSelection->setGeometry( 0, 0, 0, 0 );
 
439
    m_ScreenSelection.clear();
 
440
 
 
441
    CSupportFuncs::SetBorderImage( &m_BankImages[m_SelTile.y()], (m_SelTile.x()%32)*18 + 1, (m_SelTile.x()/32)*18 + 1, 18*m_LastTilesBlockW-1, 18*m_LastTilesBlockH-1, 0xC0C0C0 );
 
442
    m_pLblBank[m_SelTile.y()]->put_Image( m_BankImages[m_SelTile.y()] );
 
443
 
 
444
    m_SelTile = QPoint( (y/18)*32+(x/18), bank );
 
445
    CSupportFuncs::SetBorderImage( &m_BankImages[bank], (m_SelTile.x()%32)*18 + 1, (m_SelTile.x()/32)*18 + 1, 18*m_pGrTilesBlockW->text().toLongLong()-1, 18*m_pGrTilesBlockH->text().toLongLong()-1, 0xffff00 );
 
446
 
 
447
    m_LastTilesBlockW = m_pGrTilesBlockW->text().toLongLong();
 
448
    m_LastTilesBlockH = m_pGrTilesBlockH->text().toLongLong();
 
449
 
 
450
    m_pLblBank[bank]->put_Image( m_BankImages[bank] );//TODO - 1 Serveix?
 
451
 
 
452
    m_pLblShowTile->setText( QString( "%1" ).arg( m_SelTile.x() ) );
 
453
}
 
454
 
 
455
void CScreenW::OnPaletteClick( int x, int y )
 
456
{
 
457
        m_SelColor = QPoint( x/(int)20, y/(int)12 );
 
458
 
 
459
        if( m_SelColor.x() == 0 ) m_pRadBack->click();
 
460
        else m_pRadFore->click();
 
461
}
 
462
 
 
463
void CScreenW::OnPalBackClick( int, int )
 
464
{
 
465
        m_SelColor = QPoint( 0, PAL_ALL );
 
466
        m_pRadBack->click();
 
467
}
 
468
 
 
469
void CScreenW::OnPalForeClick( int, int )
 
470
{
 
471
        m_SelColor = QPoint( 1, PAL_ALL );
 
472
        m_pRadFore->click();
 
473
}
 
474
 
 
475
 
 
476
void CScreenW::OnColorsClick( int x, int /*y*/ )
 
477
{
 
478
        int c;
 
479
        int i;
 
480
        
 
481
        
 
482
        c = x / 12;
 
483
 
 
484
        if( m_SelColor.x() == 0 ) 
 
485
        {
 
486
                if( m_SelColor.y() != PAL_ALL ) m_TileDesign.SetBackColor( m_SelColor.y(), c );
 
487
                else for( i = 0; i < 8; i++ ) m_TileDesign.SetBackColor( i, c );
 
488
        }
 
489
        else 
 
490
        {
 
491
                if( m_SelColor.y() != PAL_ALL ) m_TileDesign.SetForeColor( m_SelColor.y(), c );
 
492
                else for( i = 0; i < 8; i++ ) m_TileDesign.SetForeColor( i, c );
 
493
        }
 
494
        
 
495
        SetTileDesign();        
 
496
}
 
497
 
 
498
void CScreenW::OnTileClickLeft( int x, int y )
 
499
{
 
500
        x /= 12;
 
501
        y /= 12;
 
502
        
 
503
        if( m_pRadBack->isChecked() ) 
 
504
        {
 
505
                m_TileDesign.SetBackPixel( x, y );
 
506
                m_SelColor = QPoint( 0, y );
 
507
        }
 
508
        else 
 
509
        {
 
510
                m_TileDesign.SetForePixel( x, y );
 
511
                m_SelColor = QPoint( 1, y );
 
512
        }
 
513
         
 
514
        SetTileDesign();
 
515
}
 
516
 
 
517
void CScreenW::OnTileClickRight( int x, int y )
 
518
{
 
519
         x /= 12;
 
520
         y /= 12;
 
521
         
 
522
         if( !m_pRadBack->isChecked() ) m_TileDesign.SetBackPixel( x, y );
 
523
         else m_TileDesign.SetForePixel( x, y );
 
524
         
 
525
         SetTileDesign();
 
526
}
 
527
 
 
528
void CScreenW::OnFromBank()
 
529
{
 
530
        CopyToTileDesign( m_SelTile.x(), m_SelTile.y() );
 
531
}
 
532
//TODO - 1 class CUndo
 
533
void CScreenW::Undo()
 
534
{
 
535
        if( m_Undo.size() == 0 ) return;
 
536
 
 
537
        if( m_Undo.at( 0 ) == "TOBANK" )
 
538
        {
 
539
                if( m_Undo.at( 1 ) == "ALLBANKS" )
 
540
                {
 
541
                        m_TilesBank[ m_Undo.at( 2 ).toLongLong() ] [ 0 ] = m_UndoTile;          
 
542
                        m_TilesBank[ m_Undo.at( 2 ).toLongLong() ] [ 1 ] = m_UndoTile;
 
543
                        m_TilesBank[ m_Undo.at( 2 ).toLongLong() ] [ 2 ] = m_UndoTile;
 
544
                        UpdateBank( m_Undo.at( 2 ).toLongLong(), 0 );
 
545
                }
 
546
                else if( m_Undo.at( 1 ) == "ONEBANK" )
 
547
                {
 
548
                        m_TilesBank[ m_Undo.at( 2 ).toLongLong() ] [ m_Undo.at( 3 ).toLongLong() ] = m_UndoTile;
 
549
                        UpdateBank( m_Undo.at( 2 ).toLongLong(), m_Undo.at( 3 ).toLongLong() );
 
550
                }
 
551
                else Q_ASSERT( false );
 
552
 
 
553
                m_Undo.clear();
 
554
        }
 
555
        else if( m_Undo.at( 0 ) == "SCREENCLICK" )
 
556
        {
 
557
                m_Screen[ m_Undo.at( 2 ).toLongLong() *32 + m_Undo.at( 1 ).toLongLong() ] = m_Undo.at( 3 ).toLongLong();
 
558
                
 
559
                UpdateScreen();
 
560
 
 
561
                m_Undo.clear();
 
562
        }
 
563
        else
 
564
        {
 
565
                Q_ASSERT( false );
 
566
        }
 
567
}
 
568
 
 
569
void CScreenW::OnToBank()
 
570
{
 
571
        if( m_pGrOneBank->isChecked() )
 
572
        {
 
573
                m_Undo.clear();
 
574
                m_Undo<<"TOBANK"<<"ALLBANKS"<<m_SelTile.x();
 
575
                m_UndoTile = m_TilesBank[ m_SelTile.x() ] [0 ];
 
576
 
 
577
                m_TilesBank[ m_SelTile.x() ][ 0 ] = m_TileDesign;
 
578
                m_TilesBank[ m_SelTile.x() ][ 1 ] = m_TileDesign;
 
579
                m_TilesBank[ m_SelTile.x() ][ 2 ] = m_TileDesign;
 
580
                UpdateBank( m_SelTile.x(), 0 );
 
581
        } 
 
582
        else 
 
583
        {
 
584
                m_Undo.clear();
 
585
                m_Undo<<"TOBANK"<<"ONEBANK"<<m_SelTile.x()<<m_SelTile.y();
 
586
                m_UndoTile = m_TilesBank[ m_SelTile.x() ] [ m_SelTile.y() ];
 
587
 
 
588
                m_TilesBank[ m_SelTile.x() ][ m_SelTile.y() ] = m_TileDesign;   
 
589
                UpdateBank( m_SelTile.x(), m_SelTile.y() );
 
590
        }
 
591
}
 
592
 
 
593
void CScreenW::OnScreenReleaseRight( int x, int y )
 
594
{
 
595
        m_pLblSelection->setGeometry( 0, 0, 0, 0 );
 
596
        m_ScreenSelection.clear();
 
597
 
 
598
        x = x / 16;
 
599
        y = y / 16;
 
600
 
 
601
        m_Undo.clear();
 
602
        m_Undo<<"SCREENCLICK"<<x<<y<<m_Screen[y*32+x];
 
603
 
 
604
        m_Screen[y*32 + x] = GetBackgroundTile( y/8 );
 
605
        
 
606
        UpdateScreen();
 
607
}
 
608
//TODO - 1 Deshabilitat. Per mes endavant.
 
609
int CScreenW::GetBackgroundTile( int y )
 
610
{
 
611
    if( m_pGrOneBank->isChecked() ) return m_pGrBackgroundTiles0->currentText().toLongLong();
 
612
 
 
613
    switch( y )
 
614
    {
 
615
        case 0: return m_pGrBackgroundTiles0->currentText().toLongLong();
 
616
        case 1: return m_pGrBackgroundTiles1->currentText().toLongLong();
 
617
        case 2: return m_pGrBackgroundTiles2->currentText().toLongLong();
 
618
        default: Q_ASSERT( false ); return 0;
 
619
    }
 
620
}
 
621
 
 
622
void CScreenW::OnScreenClickLeft( int x, int y )
 
623
{
 
624
        m_ScreenSelection.clear();
 
625
 
 
626
        m_Selecting = true;
 
627
 
 
628
        m_pLblCursor[0]->setVisible( false );
 
629
        m_pLblCursor[1]->setVisible( false );
 
630
        m_pLblCursor[2]->setVisible( false );
 
631
        m_pLblCursor[3]->setVisible( false );
 
632
 
 
633
        m_LastClickPos = QPoint( x, y );
 
634
}
 
635
 
 
636
void CScreenW::OnScreenReleaseLeft( int x, int y )
 
637
{
 
638
        int tmp;
 
639
        int i, j;
 
640
        int xI, yI;
 
641
        int xE, yE;
 
642
        QList<int> r;
 
643
 
 
644
 
 
645
        m_Selecting = false;
 
646
        m_ScreenSelection.clear();
 
647
        //TODO - 1 xI<xE yI < yE
 
648
        if( m_LastClickPos.x() >= 0 && ( abs( m_LastClickPos.x() - x ) >= 8 || abs( m_LastClickPos.y() - y ) >= 8 ) )
 
649
        {
 
650
                xI = m_LastClickPos.x() / 16 * 16;
 
651
                yI = m_LastClickPos.y() / 16 * 16;
 
652
                xE = ( x / 16 * 16 );
 
653
                yE = ( y / 16 * 16 );
 
654
 
 
655
                if( xI > xE )
 
656
                {
 
657
                        tmp = xE;
 
658
                        xE = xI;
 
659
                        xI = tmp;
 
660
                }
 
661
 
 
662
                if( yI > yE )
 
663
                {
 
664
                        tmp = yE;
 
665
                        yE = yI;
 
666
                        yI = tmp;
 
667
                }
 
668
                m_pLblSelection->setGeometry( xI + 3, yI + 2, abs( xI - xE ), abs( yI - yE ) );
 
669
                m_pLblSelection->show();
 
670
 
 
671
                xI = xI / 16;
 
672
                xE = xE / 16;
 
673
                yI = yI /16;
 
674
                yE = yE / 16;
 
675
 
 
676
                m_SelectionRect = QRect( xI, yI, xE - xI, yE - yI );
 
677
 
 
678
                for( i = yI; i < yE; i++ )
 
679
                {
 
680
                        r.clear();
 
681
 
 
682
                        for( j = xI; j < xE; j++ )
 
683
                        {
 
684
                                r<<m_Screen[i*32+j];                                    
 
685
 
 
686
                        }
 
687
                        m_ScreenSelection<<r;
 
688
                }
 
689
 
 
690
                return;
 
691
        }
 
692
 
 
693
        m_pLblSelection->setVisible( false );
 
694
 
 
695
        x = x / 16;
 
696
        y = y / 16;
 
697
 
 
698
        if( ( y / 8 ) != m_SelTile.y() && !m_pGrOneBank->isChecked() ) return;
 
699
        
 
700
        m_Undo.clear();
 
701
        m_Undo<<"SCREENCLICK"<<x<<y<<m_Screen[y*32+x];
 
702
 
 
703
        for( i = 0; i < m_pGrTilesBlockH->text().toLongLong(); i++ )
 
704
        {
 
705
                for( j = 0; j < m_pGrTilesBlockW->text().toLongLong(); j++ )
 
706
                {
 
707
            if( ( m_SelTile.x() + (i*32) + j ) < 256 ) m_Screen[(y+i)*32+x+j] = m_SelTile.x()+(i*32)+j;
 
708
                        else Q_ASSERT( false );
 
709
                }
 
710
        }
 
711
 
 
712
        UpdateScreen();
 
713
}
 
714
 
 
715
void CScreenW::OnBankChanged( int index )
 
716
{
 
717
        OnBankClick( ( m_SelTile.x() % 32 )*18, ( m_SelTile.x() / 32 )*18, index );
 
718
}
 
719
 
 
720
void CScreenW::CopyToTileDesign( int index, int bank )
 
721
{
 
722
        m_TileDesign = m_TilesBank[index][bank];
 
723
        SetTileDesign();
 
724
}
 
725
 
 
726
void CScreenW::SetTileDesign()
 
727
{
 
728
        int y;
 
729
        QImage image;
 
730
        
 
731
        
 
732
        image = QImage( 96, 96, QImage::Format_RGB32 );
 
733
        
 
734
        PaintTile( &image, m_TileDesign, 0, 0, 12 );
 
735
 
 
736
        m_pLblTile->put_Image( image );
 
737
 
 
738
        image = QImage( 42, 98, QImage::Format_RGB32 );
 
739
 
 
740
        for( y = 0; y < 8; y++ )
 
741
        {
 
742
                CSupportFuncs::SetPixelRect( &image, 0, y*12, CSupportFuncs::COLORS_TABLE[m_TileDesign.GetBgColor( y )], 20, 12 );
 
743
                CSupportFuncs::SetPixelRect( &image, 20, y*12, CSupportFuncs::COLORS_TABLE[m_TileDesign.GetForeColor( y )], 20, 12 );
 
744
                
 
745
                CSupportFuncs::SetBorderImage( &image, 0, y*12, 20, 12, 0xC0C0C0 );
 
746
                CSupportFuncs::SetBorderImage( &image, 20, y*12, 20, 12, 0xC0C0C0 );
 
747
        }
 
748
        m_pLblPalette->put_Image( image );
 
749
                
 
750
}
 
751
 
 
752
void CScreenW::PaintTile( QImage *pImage, CTile &tile, int posX, int posY, int dotSize )
 
753
{
 
754
        int x, y;
 
755
        
 
756
        
 
757
        for( y = 0; y < 8; y++ )
 
758
        {
 
759
                for( x = 0; x < 8; x++ )
 
760
                {
 
761
                        CSupportFuncs::SetPixelRect( pImage, posX + x*dotSize, posY + y*dotSize, CSupportFuncs::COLORS_TABLE[tile.GetPixelColor( x, y )], dotSize, dotSize );
 
762
                }
 
763
        }
 
764
}
 
765
 
 
766
void CScreenW::PaintGrid( QImage *pImage, int gridWidth, int gridHeight )
 
767
{
 
768
        int i, j;
 
769
        
 
770
        
 
771
        for( i = 1; i < pImage->height(); i+= ( gridHeight+ 2 ) )
 
772
        {
 
773
                for( j = 0; j < pImage->width(); j++ ) 
 
774
                {
 
775
                        pImage->setPixel( j, i-1, 0xC0C0C0 );   
 
776
                        pImage->setPixel( j, i+gridHeight, 0xC0C0C0 );  
 
777
                }
 
778
        }
 
779
        
 
780
        for( i = 1; i < pImage->width(); i+= ( gridWidth + 2 ) )
 
781
        {
 
782
                for( j = 0; j < pImage->height(); j++ ) 
 
783
                {
 
784
                        pImage->setPixel( i-1, j, 0xC0C0C0 );   
 
785
                        pImage->setPixel( i+gridWidth, j, 0xC0C0C0 );   
 
786
                }
 
787
        }
 
788
        
 
789
 
 
790
}
 
791
 
 
792
void CScreenW::OnRadBack()
 
793
{
 
794
        m_SelColor = QPoint( 0, m_SelColor.y() );
 
795
}
 
796
 
 
797
void CScreenW::OnRadFore()
 
798
{
 
799
        m_SelColor = QPoint( 1, m_SelColor.y() );
 
800
}
 
801
 
 
802
void CScreenW::OnTileClear()
 
803
{
 
804
        m_TileDesign.Reset();
 
805
        SetTileDesign();
 
806
}
 
807
 
 
808
void CScreenW::OnTileReorder()
 
809
{
 
810
        m_TileDesign.Reorder();
 
811
        SetTileDesign();
 
812
}
 
813
 
 
814
void CScreenW::OnTileRotate()
 
815
{
 
816
        m_TileDesign.Rotate();
 
817
        SetTileDesign();
 
818
}
 
819
 
 
820
void CScreenW::OnTileInvertColors()
 
821
{
 
822
        int i;
 
823
 
 
824
        for( i = 0; i < 8; i++ ) m_TileDesign.InvertRow( i );
 
825
        SetTileDesign();
 
826
}
 
827
 
 
828
void CScreenW::OnTileShiftDown()
 
829
{
 
830
        m_TileDesign.ShiftDown();
 
831
        SetTileDesign();
 
832
}
 
833
 
 
834
void CScreenW::OnTileShiftUp()
 
835
{
 
836
        m_TileDesign.ShiftUp();
 
837
        SetTileDesign();
 
838
}
 
839
 
 
840
void CScreenW::OnTileShiftLeft()
 
841
{
 
842
        m_TileDesign.ShiftLeft();
 
843
        SetTileDesign();
 
844
}
 
845
 
 
846
void CScreenW::OnTileShiftRight()
 
847
{
 
848
        m_TileDesign.ShiftRight();
 
849
        SetTileDesign();        
 
850
}
 
851
 
 
852
void CScreenW::OnTileFlipHorizontal()
 
853
{
 
854
        m_TileDesign.FlipHorizontal();
 
855
        SetTileDesign();        
 
856
}
 
857
 
 
858
void CScreenW::OnTileFlipVertical()
 
859
{
 
860
        m_TileDesign.FlipVertical();
 
861
        SetTileDesign();
 
862
}
 
863
/*
 
864
void CScreenW::OnBankReleaseLeft( int, int )
 
865
{
 
866
        m_DragDrop = false;
 
867
}
 
868
*/
 
869
void CScreenW::OnBankTileMouseMoveEvent( int x, int y )
 
870
{
 
871
    /*
 
872
        int difX;
 
873
        int difY;
 
874
        int modd;
 
875
 
 
876
 
 
877
        difX = m_LastBankTilePos.x() - x;
 
878
        difY = m_LastBankTilePos.y() - y;
 
879
 
 
880
        m_pLblBankTile->setGeometry( m_pLblBankTile->x() - difX, m_pLblBankTile->y() - difY, m_pLblBankTile->width(), m_pLblBankTile->height() );
 
881
 
 
882
        m_LastBankTilePos = QPoint( x + difX, y + difY );
 
883
 
 
884
        OnScreenMouseMoveEvent( m_pLblBankTile->x() - m_pFrmScreen->x(), m_pLblBankTile->y() - m_pFrmScreen->y() );
 
885
*/
 
886
}
 
887
/*
 
888
void CScreenW::OnBank0MouseMoveEvent( int x, int y )
 
889
{
 
890
        OnBankMouseMoveEvent( x, y, 0 );
 
891
}
 
892
 
 
893
void CScreenW::OnBank1MouseMoveEvent( int x, int y )
 
894
{
 
895
        OnBankMouseMoveEvent( x, y, 1 );
 
896
}
 
897
 
 
898
void CScreenW::OnBank2MouseMoveEvent( int x, int y )
 
899
{
 
900
        OnBankMouseMoveEvent( x, y, 2 );
 
901
}
 
902
 
 
903
void CScreenW::OnBankMouseMoveEvent( int x, int y, int bank )
 
904
{
 
905
        int mX, mY;
 
906
        int bX, bY;
 
907
 
 
908
 
 
909
        bX = mX = ( x / 18 ) * 18;
 
910
        mX += ( m_pTabBank->x() + m_pFrmBank[bank]->x() + 4 ); //4 Bordes
 
911
 
 
912
        bY = mY = ( y / 18 ) * 18;
 
913
        mY += ( m_pTabBank->y() + m_pFrmBank[bank]->y() + 25 ); //25 Pestanyes
 
914
 
 
915
        m_pLblBankTile->setGeometry( mX, mY, 17, 17 );
 
916
 
 
917
        m_pLblBankTile->put_Image( m_BankImages[bank].copy( bX, bY, 17, 17 ) );
 
918
        m_pLblBankTile->setVisible( true );
 
919
}
 
920
*/
 
921
void CScreenW::OnScreenMouseMoveEvent( int x, int y )
 
922
{
 
923
    QPalette pal;
 
924
    int ox, oy;
 
925
 
 
926
        if( x < 0 || y < 0 ) return;
 
927
 
 
928
    m_MouseX = x;
 
929
    m_MouseY = y;
 
930
 
 
931
    x /= 16;
 
932
    y /= 16;
 
933
 
 
934
    if( x > 31 || y >23 ) return;
 
935
 
 
936
    if( m_LastGridValue.x() != x || m_LastGridValue.y() != y )
 
937
    {
 
938
        pal = m_pColsLabels[m_LastGridValue.x()]->palette();
 
939
        pal.setColor( QPalette::Dark, Qt::gray );
 
940
        m_pColsLabels[m_LastGridValue.x()]->setPalette( pal );
 
941
        pal.setColor( QPalette::Dark, Qt::green );
 
942
        m_pColsLabels[x]->setPalette( pal );
 
943
 
 
944
                Q_ASSERT( m_pRowsLabels[m_LastGridValue.y()] );
 
945
        pal = m_pRowsLabels[m_LastGridValue.y()]->palette();
 
946
        pal.setColor( QPalette::Dark, Qt::gray );
 
947
        m_pRowsLabels[m_LastGridValue.y()]->setPalette( pal );
 
948
        pal.setColor( QPalette::Dark, Qt::green );
 
949
                Q_ASSERT( m_pRowsLabels[y] );
 
950
        m_pRowsLabels[y]->setPalette( pal );
 
951
        m_LastGridValue = QPoint( x, y );
 
952
 
 
953
        m_pLblShowTileBank->setText( QString( "%1" ).arg( m_Screen[ y*32+x] ) );
 
954
    }
 
955
 
 
956
    if( m_Selecting )
 
957
    {
 
958
        if( m_LastClickPos.x() > m_MouseX) ox = m_MouseX;
 
959
        else ox = m_LastClickPos.x();
 
960
 
 
961
        if( m_LastClickPos.y() > m_MouseY ) oy = m_MouseY;
 
962
        else oy = m_LastClickPos.y();
 
963
 
 
964
        m_pLblSelection->setGeometry( ox, oy, abs( m_MouseX - m_LastClickPos.x() ), abs( m_MouseY - m_LastClickPos.y() ) );
 
965
        m_pLblSelection->setVisible( true );
 
966
    }
 
967
    else
 
968
    {
 
969
        m_pLblCursor[0]->setGeometry( x*16+1 + 3,  y*16+1, 14, 1 );
 
970
        m_pLblCursor[1]->setGeometry( x*16+20, y*16+1 + 2,  1, 16 );
 
971
        m_pLblCursor[2]->setGeometry( x*16+1 + 3,  y*16+21, 14, 1 );
 
972
        m_pLblCursor[3]->setGeometry( x*16+1,  y*16+1 + 2,  1, 16 );
 
973
 
 
974
        m_pLblCursor[0]->setVisible( true );
 
975
        m_pLblCursor[1]->setVisible( true );
 
976
        m_pLblCursor[2]->setVisible( true );
 
977
        m_pLblCursor[3]->setVisible( true );
 
978
    }
 
979
}
 
980
 
 
981
void CScreenW::NewScreen()
 
982
{
 
983
        int i;
 
984
                
 
985
        for( i = 0; i < 32*24; i++ ) m_Screen[i] = 0;
 
986
        
 
987
        UpdateScreen();
 
988
}
 
989
 
 
990
bool CScreenW::LoadScreen( QString fileName )
 
991
{
 
992
        int i;
 
993
        QString line;
 
994
        QFile hFile;
 
995
        
 
996
        
 
997
        hFile.setFileName( fileName );
 
998
        hFile.open( QIODevice::ReadOnly );
 
999
        QTextStream str( &hFile );
 
1000
 
 
1001
        if( hFile.atEnd() ) 
 
1002
        {
 
1003
                hFile.close();
 
1004
                return false;
 
1005
        }
 
1006
 
 
1007
        line = str.readLine();
 
1008
 
 
1009
        if( line != "SCREEN" )
 
1010
        {
 
1011
                hFile.close();
 
1012
                return false;
 
1013
        }
 
1014
 
 
1015
        for( i = 0; i < 32*24; i++ )
 
1016
        {
 
1017
                line = str.readLine();
 
1018
                m_Screen[i] = line.toInt();
 
1019
        }
 
1020
 
 
1021
        hFile.close();
 
1022
 
 
1023
        UpdateScreen();
 
1024
        
 
1025
        return true;
 
1026
}
 
1027
 
 
1028
bool CScreenW::SaveScreen( QString fileName )
 
1029
{
 
1030
        int i;
 
1031
        QFile hFile;
 
1032
        
 
1033
 
 
1034
        hFile.setFileName( fileName );
 
1035
        hFile.open( QIODevice::WriteOnly );
 
1036
 
 
1037
        QTextStream str( &hFile );
 
1038
        
 
1039
        str<<"SCREEN"<<endl;
 
1040
        
 
1041
        for( i = 0; i < 32*24; i++ ) str<<m_Screen[i]<<endl;
 
1042
 
 
1043
        hFile.flush();
 
1044
        hFile.close();
 
1045
        
 
1046
        return true;
 
1047
}
 
1048
 
 
1049
bool CScreenW::ExportScreenBin( QString fileName, int initX, int initY, int w, int h )
 
1050
{
 
1051
        int x, y;
 
1052
        int addX, addY;
 
1053
    int wi, he;
 
1054
        QFile hFile;
 
1055
        
 
1056
 
 
1057
        hFile.setFileName( fileName );
 
1058
        hFile.open( QIODevice::WriteOnly );
 
1059
 
 
1060
        QDataStream str( &hFile );
 
1061
 
 
1062
        addX = m_pGrTilesBlockW->text().toLongLong();
 
1063
        addY = m_pGrTilesBlockH->text().toLongLong();
 
1064
 
 
1065
    wi = initX + w;
 
1066
    if( wi > 32 ) wi = 32;
 
1067
 
 
1068
    he = initY + h;
 
1069
    if( he > 24 ) he = 24;
 
1070
 
 
1071
    for( y = initY; y < he; )
 
1072
 
 
1073
        {
 
1074
        for( x = initX; x < wi; )
 
1075
                {
 
1076
                        str<<(unsigned char)m_Screen[y*32+x];
 
1077
                        x+= addX;
 
1078
                }
 
1079
                y+= addY;
 
1080
        }
 
1081
 
 
1082
        hFile.flush();
 
1083
        hFile.close();
 
1084
        
 
1085
        return true;    
 
1086
}
 
1087
 
 
1088
bool CScreenW::ExportScreenPNG( QString fileName )
 
1089
{
 
1090
    int h;
 
1091
    int xT, yT;
 
1092
    int i, y, x;
 
1093
    int tileId;
 
1094
    CTile tile;
 
1095
    QImage image;
 
1096
 
 
1097
 
 
1098
    image = QImage( 256, 192, QImage::Format_RGB32 );
 
1099
 
 
1100
    for( yT = 0, i = -1; yT < 192; yT+=8 )
 
1101
    {
 
1102
        for( xT = 0; xT < 256; xT+=8 )
 
1103
        {
 
1104
            i++;
 
1105
            tileId = m_Screen[i];
 
1106
            for( y = 0, tile = m_TilesBank[tileId%256][i/256]; y < 8; y++ )
 
1107
            {
 
1108
                for( x = 0; x < 8; x++ )
 
1109
                {
 
1110
                    image.setPixel( xT + x, yT + y, CSupportFuncs::COLORS_TABLE[tile.GetPixelColor(x, y)]);
 
1111
 
 
1112
                }
 
1113
            }
 
1114
        }
 
1115
    }
 
1116
 
 
1117
    image.save( fileName );
 
1118
}
 
1119
 
 
1120
 
 
1121
bool CScreenW::ExportScreenBinPletter( QString fileName, int initX, int initY, int w, int h )
 
1122
{
 
1123
        int s;
 
1124
        int x, y;
 
1125
        int addX, addY;
 
1126
    int wi, he;
 
1127
        unsigned char buffer[256*3];
 
1128
 
 
1129
        addX = m_pGrTilesBlockW->text().toLongLong();
 
1130
        addY = m_pGrTilesBlockH->text().toLongLong();
 
1131
        s = 0;
 
1132
 
 
1133
    wi = initX + w;
 
1134
    if( wi > 32 ) wi = 32;
 
1135
 
 
1136
    he = initY + h;
 
1137
    if( he > 24 ) he = 24;
 
1138
 
 
1139
    for( y = initY; y < he; )
 
1140
        {
 
1141
        for( x = initX; x < wi; )
 
1142
                {
 
1143
                        buffer[s] = (unsigned char)m_Screen[y*32+x];
 
1144
                        x+= addX;
 
1145
                        s++;
 
1146
                }
 
1147
                y+= addY;
 
1148
        }
 
1149
 
 
1150
        PletterMain( s, buffer, fileName.toStdString() );
 
1151
 
 
1152
    return true; //TODO - 2
 
1153
}
 
1154
 
 
1155
bool CScreenW::ExportScreenData( QString fileName, bool hexa, int initX, int initY, int w, int h )
 
1156
{
 
1157
        int addX, addY;
 
1158
        int x, y;
 
1159
    int wi, he;
 
1160
        QFile hFile;
 
1161
    QString tmp;
 
1162
        
 
1163
 
 
1164
        hFile.setFileName( fileName );
 
1165
        hFile.open( QIODevice::WriteOnly );
 
1166
 
 
1167
        QTextStream str( &hFile );
 
1168
        
 
1169
        str<<"SCREEN:"<<"\r\n"<<"db ";
 
1170
 
 
1171
        addX = m_pGrTilesBlockW->text().toLongLong();
 
1172
        addY = m_pGrTilesBlockH->text().toLongLong();
 
1173
 
 
1174
    wi = initX + w;
 
1175
    if( wi > 32 ) wi = 32;
 
1176
 
 
1177
    he = initY + h;
 
1178
    if( he > 24 ) he = 24;
 
1179
 
 
1180
    for( y = initY; y < he; )
 
1181
    {
 
1182
        for( x = initX; x < wi; )
 
1183
                {
 
1184
            if( hexa ) str<<QString( "0x%1" ).arg( m_Screen[y*32+x], 0, 16 ).rightJustified( 4, ' ' );
 
1185
            else str<<QString( "%1" ).arg( m_Screen[y*32+x] ).rightJustified( 3, ' ' );
 
1186
 
 
1187
            if( (y*32+x+1)%32 == 0 || x == ( wi - 1 ) )
 
1188
                        {
 
1189
                                str<<"\r\n";
 
1190
                if( (y*32+x+1) != 256*3 && !( y == (he-1) && x == (wi-1) )) str<<"db ";
 
1191
                        }
 
1192
            else if( !( x == (wi - 1) && y == (he - 1) ) ) str<<",";
 
1193
 
 
1194
                        x+= addX;
 
1195
                }
 
1196
                y+= addY;
 
1197
        }
 
1198
 
 
1199
 
 
1200
        str<<"\r\n";
 
1201
 
 
1202
        hFile.flush();
 
1203
        hFile.close();
 
1204
        
 
1205
        return true;
 
1206
}
 
1207
 
 
1208
void CScreenW::NewTiles()
 
1209
{
 
1210
        int b, i;
 
1211
        
 
1212
        for( b = 0; b < 3; b++ )
 
1213
        {
 
1214
                for( i = 0; i < 256; i++ ) m_TilesBank[i][b] = CTile();
 
1215
        }
 
1216
        InitBanks();
 
1217
}
 
1218
 
 
1219
bool CScreenW::LoadTiles( QString fileName )
 
1220
{
 
1221
        int i;
 
1222
        int x, y, b;
 
1223
        QString line;
 
1224
        QFile hFile;
 
1225
        
 
1226
        
 
1227
        hFile.setFileName( fileName );
 
1228
        hFile.open( QIODevice::ReadOnly );
 
1229
        QTextStream str( &hFile );
 
1230
 
 
1231
        if( hFile.atEnd() ) 
 
1232
        {
 
1233
                hFile.close();
 
1234
                return false;
 
1235
        }
 
1236
        
 
1237
        if( str.readLine() != "TILES" )
 
1238
        {
 
1239
                hFile.close();
 
1240
                return false;
 
1241
        }
 
1242
 
 
1243
        for( b = 0; b < 3; b++ )
 
1244
        {
 
1245
                line = str.readLine();
 
1246
 
 
1247
                if( line != QString( "BANK%1" ).arg( b ) )
 
1248
                {
 
1249
                        hFile.close();
 
1250
                        return false;
 
1251
                }
 
1252
                for( i = 0; i < 256; i++ )
 
1253
                {
 
1254
                        for( y = 0; y < 8; y++ ) m_TilesBank[i][b].put_BgColor( y, str.readLine().toInt() );
 
1255
                        
 
1256
                        for( y = 0; y < 8; y++ ) m_TilesBank[i][b].put_ForeColor( y, str.readLine().toInt() );
 
1257
                        
 
1258
                        for( y = 0; y < 8; y++ )
 
1259
                        {
 
1260
                                for( x = 0; x < 8; x++ ) m_TilesBank[i][b].put_Pixel( x, y, str.readLine().toInt() );
 
1261
                        }
 
1262
                }
 
1263
        }
 
1264
 
 
1265
        hFile.close();
 
1266
        
 
1267
        InitBanks();
 
1268
 
 
1269
        return true;
 
1270
}
 
1271
 
 
1272
bool CScreenW::LoadTilesLibrary( QString fileName, int bankOr, int xOr, int yOr, int width, int height, int bankDest, int xDest, int yDest )
 
1273
{
 
1274
    int i;
 
1275
    int vX, vY;
 
1276
    int x, y, b;
 
1277
    int dX, dY;
 
1278
    QString line;
 
1279
    QFile hFile;
 
1280
 
 
1281
 
 
1282
    hFile.setFileName( fileName );
 
1283
    hFile.open( QIODevice::ReadOnly );
 
1284
    QTextStream str( &hFile );
 
1285
 
 
1286
    //TODO - 1 Indicar error
 
1287
    if( hFile.atEnd() )
 
1288
    {
 
1289
        hFile.close();
 
1290
        return false;
 
1291
    }
 
1292
 
 
1293
    if( str.readLine() != "TILES" )
 
1294
    {
 
1295
        hFile.close();
 
1296
        return false;
 
1297
    }
 
1298
 
 
1299
    for( b = 0; b < 3; b++ )
 
1300
    {
 
1301
        line = str.readLine();
 
1302
 
 
1303
        if( line != QString( "BANK%1" ).arg( b ) )
 
1304
        {
 
1305
            hFile.close();
 
1306
            return false;
 
1307
        }
 
1308
 
 
1309
        for( i = 0; i < 256; i++ )
 
1310
        {
 
1311
            vX = i % 32;
 
1312
            vY = i / 32;
 
1313
 
 
1314
            dX = vX - xOr + xDest;
 
1315
            dY = vY - yOr + yDest;
 
1316
 
 
1317
            if( vX >= xOr && vX < ( xOr + width ) && vY >= yOr && vY < ( yOr + height ) && bankOr == b && dX >= 0 && dX < 32 && dY >=0 && dY < 8 )
 
1318
            {
 
1319
                qDebug()<<"CScreenW::LoadTilesLibrary dX="<<dX<<" dY="<<dY;
 
1320
                for( y = 0; y < 8; y++ ) m_TilesBank[dY*32+dX][bankDest].put_BgColor( y, str.readLine().toInt() );
 
1321
 
 
1322
                for( y = 0; y < 8; y++ ) m_TilesBank[dY*32+dX][bankDest].put_ForeColor( y, str.readLine().toInt() );
 
1323
 
 
1324
                for( y = 0; y < 8; y++ )
 
1325
                {
 
1326
                   for( x = 0; x < 8; x++ ) m_TilesBank[dY*32+dX][bankDest].put_Pixel( x, y, str.readLine().toInt() );
 
1327
                }
 
1328
            }
 
1329
            else
 
1330
            {
 
1331
                for( y = 0; y < 8*2; y++ ) str.readLine();
 
1332
                for( y = 0; y < 8*8; y++ ) str.readLine();
 
1333
            }
 
1334
        }
 
1335
    }
 
1336
 
 
1337
    hFile.close();
 
1338
 
 
1339
    InitBanks();
 
1340
 
 
1341
    return true;
 
1342
 
 
1343
}
 
1344
 
 
1345
bool CScreenW::SaveTiles( QString fileName )
 
1346
{
 
1347
        int i, b;
 
1348
        int x, y;
 
1349
        QFile hFile;
 
1350
        
 
1351
 
 
1352
        hFile.setFileName( fileName );
 
1353
        hFile.open( QIODevice::WriteOnly );
 
1354
 
 
1355
        QTextStream str( &hFile );
 
1356
        
 
1357
        str<<"TILES"<<endl;
 
1358
        
 
1359
        for( b = 0; b < 3; b++ )
 
1360
        {
 
1361
                str<<"BANK"<<b<<endl;
 
1362
                
 
1363
                for( i = 0; i < 256; i++ )
 
1364
                {
 
1365
                        for( x = 0; x < 8; x++ ) str<<m_TilesBank[i][b].get_BgColor(x)<<endl;
 
1366
                        for( x = 0; x < 8; x++ ) str<<m_TilesBank[i][b].get_ForeColor(x)<<endl;
 
1367
                        for( y = 0; y < 8; y++ )
 
1368
                        {
 
1369
                                for( x = 0; x < 8; x++ ) str<<m_TilesBank[i][b].get_Pixel( x, y )<<endl;
 
1370
                        }
 
1371
                }
 
1372
        }
 
1373
        
 
1374
        hFile.flush();
 
1375
        hFile.close();
 
1376
        
 
1377
        return true;
 
1378
}
 
1379
 
 
1380
void CScreenW::GroupTiles()
 
1381
{
 
1382
        int blacked;
 
1383
        int i, j, b;
 
1384
        int index;
 
1385
        CTile groupBank[256];
 
1386
 
 
1387
 
 
1388
        for( b = 0; b < 3; b++ )
 
1389
        {
 
1390
                for( i = 0; i < 256; i++ ) groupBank[b] = CTile();
 
1391
 
 
1392
                for( i = 0, index = 0, blacked = false; i < 256; i++ )
 
1393
                {
 
1394
                        if( !m_TilesBank[i][b].IsBlack() || !blacked )
 
1395
                        {
 
1396
                                if( m_TilesBank[i][b].IsBlack() ) blacked = true;
 
1397
 
 
1398
                                groupBank[index] = m_TilesBank[i][b];
 
1399
 
 
1400
                                for( j = 0; j < 256; j++ ) if( m_Screen[j+256*b] == i ) m_Screen[j+256*b] = index;
 
1401
 
 
1402
                                index++;
 
1403
                        }
 
1404
                }
 
1405
 
 
1406
                for( i = 0; i < 256; i++ ) m_TilesBank[i][b] = groupBank[i];
 
1407
        }
 
1408
 
 
1409
        InitBanks();
 
1410
}
 
1411
 
 
1412
 
 
1413
void CScreenW::ExportTilesPNG( QString fileName )
 
1414
{
 
1415
    int h;
 
1416
    int xT, yT;
 
1417
    int i, y, x;
 
1418
    CTile tile;
 
1419
    QImage image;
 
1420
 
 
1421
 
 
1422
    if( m_pGrOneBank->isChecked() ) h = 64;
 
1423
    else h = 64*3;
 
1424
 
 
1425
    image = QImage( 256, h, QImage::Format_RGB32 );
 
1426
 
 
1427
    for( yT = 0, i = -1; yT < h; yT+=8 )
 
1428
    {
 
1429
        for( xT = 0; xT < 256; xT+=8 )
 
1430
        {
 
1431
            i++;
 
1432
            for( y = 0, tile = m_TilesBank[i%256][i/256]; y < 8; y++ )
 
1433
            {
 
1434
                for( x = 0; x < 8; x++ )
 
1435
                {
 
1436
                    image.setPixel( xT + x, yT + y, CSupportFuncs::COLORS_TABLE[tile.GetPixelColor(x, y)]);
 
1437
 
 
1438
                }
 
1439
            }
 
1440
        }
 
1441
    }
 
1442
 
 
1443
    image.save( fileName );
 
1444
}
 
1445
 
 
1446
 
 
1447
bool CScreenW::ExportTilesBin( QString fileName )
 
1448
{
 
1449
        int i, j;
 
1450
        int b;
 
1451
        QFile hFile;
 
1452
        int nLoops;
 
1453
        CDlgBankRange dlgBankRange;
 
1454
 
 
1455
 
 
1456
        dlgBankRange.exec();
 
1457
        
 
1458
        if( dlgBankRange.result() == 0 ) //TODO - 1 MsgBox
 
1459
        {
 
1460
                return false;
 
1461
        }
 
1462
        
 
1463
        hFile.setFileName( fileName + QString( ".til" ) );
 
1464
        hFile.open( QIODevice::WriteOnly );
 
1465
 
 
1466
        if( m_pGrOneBank->isChecked() ) nLoops = 1;
 
1467
        else nLoops = 3;
 
1468
 
 
1469
        QDataStream str( &hFile );
 
1470
        
 
1471
        for( b = 0; b < nLoops; b++ )
 
1472
        {
 
1473
                for( j = dlgBankRange.get_FromTile(); j <  dlgBankRange.get_ToTile() + 1; j++ )
 
1474
                {
 
1475
                        if( dlgBankRange.get_BankOption() == 0 || dlgBankRange.get_BankOption() == (b + 1) )
 
1476
                        {
 
1477
                                for( i = 0; i < 8; i++ )
 
1478
                                {
 
1479
 
 
1480
                                        str<<(unsigned char)m_TilesBank[j][b].GetRowPattern( i );
 
1481
                                }
 
1482
                        }
 
1483
                }
 
1484
        }
 
1485
        
 
1486
        hFile.flush();
 
1487
        hFile.close();
 
1488
        
 
1489
        hFile.setFileName( fileName + QString( ".col" ) );
 
1490
        hFile.open( QIODevice::WriteOnly );
 
1491
 
 
1492
        QDataStream strCol( &hFile );
 
1493
        
 
1494
        for( b = 0; b < nLoops; b++ )
 
1495
        {
 
1496
                for( j = dlgBankRange.get_FromTile(); j < dlgBankRange.get_ToTile() + 1; j++ )
 
1497
                {
 
1498
                        if( dlgBankRange.get_BankOption() == 0 || dlgBankRange.get_BankOption() == (b + 1) )
 
1499
                        {
 
1500
                                for( i = 0; i < 8; i++ )
 
1501
                                {
 
1502
                                        strCol<<(unsigned char)m_TilesBank[j][b].GetRowColor( i );
 
1503
                                }
 
1504
                        }
 
1505
                }
 
1506
        }
 
1507
        
 
1508
        hFile.flush();
 
1509
        hFile.close();
 
1510
        
 
1511
        return true;
 
1512
}
 
1513
 
 
1514
void CScreenW::ExportTilesBinPletter( QString fileName )
 
1515
{
 
1516
        long sz;
 
1517
    int i, b, j;
 
1518
    int nLoops;
 
1519
    unsigned char buffer[256*3*8];
 
1520
        CDlgBankRange dlgBankRange;
 
1521
 
 
1522
 
 
1523
        dlgBankRange.exec();
 
1524
 
 
1525
        if( dlgBankRange.result() == 0 ) //TODO - 1 MsgBox
 
1526
        {
 
1527
                return;
 
1528
        }
 
1529
 
 
1530
    if( m_pGrOneBank->isChecked() ) nLoops = 1;
 
1531
    else nLoops = 3;
 
1532
 
 
1533
 
 
1534
        for( b = 0, sz = 0; b < nLoops; b++ )
 
1535
    {
 
1536
                for( j = dlgBankRange.get_FromTile(); j < dlgBankRange.get_ToTile() + 1; j++ )
 
1537
        {
 
1538
                        if( dlgBankRange.get_BankOption() == 0 || dlgBankRange.get_BankOption() == (b + 1) )
 
1539
                        {
 
1540
                                //for( i = 0; i < 8; i++ ) buffer[b*256*8+j*8+i] = m_TilesBank[j][b].GetRowPattern( i );
 
1541
                                for( i = 0; i < 8; i++, sz++ ) buffer[sz] = m_TilesBank[j][b].GetRowPattern( i );
 
1542
                        }
 
1543
        }
 
1544
    }
 
1545
 
 
1546
        PletterMain( sz/*b*256*8*/, buffer, QString( fileName + QString( ".til" ) ).toStdString() );
 
1547
 
 
1548
        for( b = 0, sz = 0; b < nLoops; b++ )
 
1549
    {
 
1550
                for( j = dlgBankRange.get_FromTile(); j < dlgBankRange.get_ToTile() + 1; j++ )
 
1551
        {
 
1552
                        if( dlgBankRange.get_BankOption() == 0 || dlgBankRange.get_BankOption() == (b + 1) )
 
1553
                        {
 
1554
                                for( i = 0; i < 8; i++, sz++ ) buffer[sz] = m_TilesBank[j][b].GetRowColor( i );
 
1555
                        }
 
1556
        }
 
1557
    }
 
1558
 
 
1559
        PletterMain( sz/*b*256*8*/, buffer, QString( fileName + QString( ".col" ) ).toStdString() );
 
1560
}
 
1561
 
 
1562
bool CScreenW::ExportTilesData( QString fileName, bool hexa )
 
1563
{
 
1564
        int i, j;
 
1565
        int b;
 
1566
        QFile hFile;
 
1567
        
 
1568
 
 
1569
        hFile.setFileName( fileName );
 
1570
        hFile.open( QIODevice::WriteOnly );
 
1571
 
 
1572
        QTextStream str( &hFile );
 
1573
        
 
1574
        for( b = 0; b < 3; b++ )
 
1575
        {
 
1576
                str<<QString( "BANK_PATTERN_%1:").arg( b )<<endl;
 
1577
 
 
1578
                for( j = 0; j < 256; j++ )
 
1579
                {
 
1580
                        str<<"db ";
 
1581
                        
 
1582
                        for( i = 0; i < 8; i++ ) 
 
1583
                        {
 
1584
                                if( hexa ) str<<QString( "0x%1" ).arg( m_TilesBank[j][b].GetRowPattern( i ), 0, 16 );
 
1585
                                else str<<m_TilesBank[j][b].GetRowPattern( i );
 
1586
 
 
1587
                                if( i != 7 ) str<<", ";
 
1588
                        }
 
1589
                        str<<endl;
 
1590
                }
 
1591
        }
 
1592
        
 
1593
        str<<endl<<endl;
 
1594
        
 
1595
        for( b = 0; b < 3; b++ )
 
1596
        {
 
1597
                str<<QString( "BANK_COLOR_%1:").arg( b )<<endl;
 
1598
 
 
1599
                for( j = 0; j < 256; j++ )
 
1600
                {
 
1601
                        str<<"db ";
 
1602
                        for( i = 0; i < 8; i++ ) 
 
1603
                        {
 
1604
                                if( hexa ) str<<QString( "0x%1" ).arg( m_TilesBank[j][b].GetRowColor( i ), 0, 16 );
 
1605
                                else str<<m_TilesBank[j][b].GetRowColor( i );
 
1606
 
 
1607
                                if( i != 7 ) str<<", ";
 
1608
                        }
 
1609
                        str<<endl;
 
1610
                }
 
1611
        }
 
1612
        
 
1613
        hFile.flush();
 
1614
        hFile.close();
 
1615
        
 
1616
        return true;
 
1617
}
 
1618
 
 
1619
bool CScreenW::DeduceColorPalette( unsigned int color, unsigned int palette[16] ) {
 
1620
    int i;
 
1621
    unsigned int tmp;
 
1622
    unsigned int paltmp[3];
 
1623
 
 
1624
 
 
1625
    if( color >= 9000000 && color <= 17000000 ) { //RED 6, 8, 9
 
1626
 
 
1627
        paltmp[0] = palette[6];
 
1628
        paltmp[1] = palette[8];
 
1629
        paltmp[2] = palette[9];
 
1630
 
 
1631
        for( i = 2; i >=0; i-- ) {
 
1632
            if( color == paltmp[i] ) break;
 
1633
 
 
1634
            if( color > paltmp[i] ) {
 
1635
                tmp = paltmp[i];
 
1636
                paltmp[i] = color;
 
1637
                color = tmp;
 
1638
                i = 3;
 
1639
            }
 
1640
        }
 
1641
 
 
1642
        palette[6] = paltmp[0];
 
1643
        palette[8] = paltmp[1];
 
1644
        palette[9] = paltmp[2];
 
1645
    }
 
1646
 
 
1647
    return true; //TODO - -1 treure true
 
1648
 
 
1649
}
 
1650
 
 
1651
bool CScreenW::ImportTilesPNGAdv( QString fileName ) {
 
1652
    int x, y, i, b;
 
1653
    int fore, back;
 
1654
    unsigned int color;
 
1655
    unsigned int rawColor;
 
1656
    QImage image;
 
1657
    unsigned int palette[16];
 
1658
 
 
1659
    //Llegir fitxer i muntar paleta
 
1660
    if( !image.load( fileName ) ) return false;
 
1661
    for( i = 0; i < 16; i++ ) palette[i] = 0;
 
1662
 
 
1663
    for( y = 0; y < image.height(); y++ ) {
 
1664
 
 
1665
        for( x = 0; x < image.width(); x++) {
 
1666
            color = image.pixel( x, y );
 
1667
            if( color > 0xff000000 ) color -= 0xff000000; //Treu transparencia si hi ha
 
1668
 
 
1669
            DeduceColorPalette( color, palette );
 
1670
        }
 
1671
 
 
1672
    }
 
1673
 
 
1674
    if( !image.load( fileName ) ) return false;
 
1675
 
 
1676
    if( image.width() < 256 || image.height() < 64 ) return false;
 
1677
 
 
1678
    for( b = 0; b < 3; b++ )
 
1679
    {
 
1680
        if( b > 0 && image.height() <= 64 ) break;
 
1681
 
 
1682
        for( y = 0; y < 8*8; y++ )
 
1683
        {
 
1684
            for( x = 0; x < 32; x++ )
 
1685
            {
 
1686
                fore = back = -1;
 
1687
 
 
1688
                for( i = 0; i < 8; i++ )
 
1689
                {
 
1690
                    rawColor = image.pixel( x*8+i, y+b*64 );
 
1691
                    if( rawColor > 0xff000000 ) rawColor -= 0xff000000; //Treu transparencia si hi ha
 
1692
 
 
1693
                    color = GetColor( rawColor, palette );
 
1694
 
 
1695
                    if( fore == -1  )
 
1696
                    {
 
1697
                        fore = color;
 
1698
                        m_TilesBank[(y/8)*32+x][b].put_ForeColor( y%8, fore );
 
1699
                        m_TilesBank[(y/8)*32+x][b].put_Pixel( i, y%8, 1 );
 
1700
                    }
 
1701
                    else if( fore == color )
 
1702
                    {
 
1703
                        m_TilesBank[(y/8)*32+x][b].put_Pixel( i, y%8, 1 );
 
1704
                    }
 
1705
                    else if( back == -1 )
 
1706
                    {
 
1707
                        back = color;
 
1708
                        m_TilesBank[(y/8)*32+x][b].put_BgColor( y%8, back );
 
1709
                        m_TilesBank[(y/8)*32+x][b].put_Pixel( i, y%8, 0 );
 
1710
                    }
 
1711
                    else if( back == color )
 
1712
                    {
 
1713
                        m_TilesBank[(y/8)*32+x][b].put_Pixel( i, y%8, 0 );
 
1714
                    }
 
1715
                    else //Three colors on a row!
 
1716
                    {
 
1717
                        m_TilesBank[(y/8)*32+x][b].put_ForeColor( y%8, fore );
 
1718
                    }
 
1719
                }
 
1720
            }
 
1721
        }
 
1722
    }
 
1723
 
 
1724
    InitBanks();
 
1725
 
 
1726
    return true;
 
1727
}
 
1728
 
 
1729
bool CScreenW::ImportTiles( QString fileName, QString paletteFileName )
 
1730
{
 
1731
        int i;
 
1732
        int color;
 
1733
        int x, y, b;
 
1734
        int fore, back;
 
1735
        QImage image;
 
1736
        unsigned int palette[16];
 
1737
        
 
1738
        
 
1739
    if( paletteFileName == "" ) //Palette used by nMSXtiles
 
1740
    {
 
1741
        for( i = 0; i < 16; i++ ) palette[i] = CSupportFuncs::COLORS_TABLE[i];
 
1742
    }
 
1743
    else
 
1744
    {
 
1745
        if( !image.load( paletteFileName ) ) return false;
 
1746
 
 
1747
        if( image.width() < 16  ) return false;
 
1748
 
 
1749
        for( i = 0; i < 16; i++ ) palette[i] = (unsigned int )image.pixel( i, 0 );
 
1750
    }
 
1751
 
 
1752
    if( !image.load( fileName ) ) return false;
 
1753
 
 
1754
    if( image.width() < 256 || image.height() < 64 ) return false;
 
1755
 
 
1756
    for( b = 0; b < 3; b++ )
 
1757
    {
 
1758
        if( b > 0 && image.height() <= 64 ) break;
 
1759
 
 
1760
        for( y = 0; y < 8*8; y++ )
 
1761
        {
 
1762
            for( x = 0; x < 32; x++ )
 
1763
            {
 
1764
                fore = back = -1;
 
1765
 
 
1766
                for( i = 0; i < 8; i++ )
 
1767
                {
 
1768
                    color = GetColor( image.pixel( x*8+i, y+b*64 ), palette );
 
1769
 
 
1770
                    if( fore == -1  )
 
1771
                    {
 
1772
                        fore = color;
 
1773
                        m_TilesBank[(y/8)*32+x][b].put_ForeColor( y%8, fore );
 
1774
                        m_TilesBank[(y/8)*32+x][b].put_Pixel( i, y%8, 1 );
 
1775
                    }
 
1776
                    else if( fore == color )
 
1777
                    {
 
1778
                        m_TilesBank[(y/8)*32+x][b].put_Pixel( i, y%8, 1 );
 
1779
                    }
 
1780
                    else if( back == -1 )
 
1781
                    {
 
1782
                        back = color;
 
1783
                        m_TilesBank[(y/8)*32+x][b].put_BgColor( y%8, back );
 
1784
                        m_TilesBank[(y/8)*32+x][b].put_Pixel( i, y%8, 0 );
 
1785
                    }
 
1786
                    else if( back == color )
 
1787
                    {
 
1788
                        m_TilesBank[(y/8)*32+x][b].put_Pixel( i, y%8, 0 );
 
1789
                    }
 
1790
                    else //Three colors on a row!
 
1791
                    {
 
1792
                        m_TilesBank[(y/8)*32+x][b].put_ForeColor( y%8, fore );
 
1793
                    }
 
1794
                }
 
1795
            }
 
1796
        }
 
1797
    }
 
1798
 
 
1799
    InitBanks();
 
1800
 
 
1801
    return true;
 
1802
}
 
1803
 
 
1804
bool CScreenW::ImportPolkaTiles( QString fileName )
 
1805
{
 
1806
        //7 bytes cabecera
 
1807
        //256*8 Patterns
 
1808
        //256*8 back|fore
 
1809
        int i, b;
 
1810
        int x, y;
 
1811
        int value;
 
1812
    unsigned char buffer[256*8];
 
1813
    ifstream myFile( qPrintable( fileName ), ios::in | ios::binary );
 
1814
 
 
1815
 
 
1816
        NewTiles();
 
1817
 
 
1818
    myFile.read ((char*)buffer, 7);
 
1819
    if (!myFile) return false;
 
1820
 
 
1821
    myFile.read( (char*)buffer, 2048 );
 
1822
 
 
1823
        for( i = 0; i < 256; i++ )
 
1824
        {
 
1825
                for( y = 0; y < 8; y++ )
 
1826
                {
 
1827
                        for( x = 0; x < 8; x++ )
 
1828
                        {
 
1829
                                value = ( buffer[ i*8 + y ] >> x ) & 0x1;
 
1830
                                m_TilesBank[i][0].put_Pixel( x, y, value );
 
1831
                        }
 
1832
                }
 
1833
        }
 
1834
 
 
1835
    myFile.read( (char*)buffer, 2048 );
 
1836
 
 
1837
        for( i = 0; i < 256; i++ )
 
1838
        {
 
1839
                for( y = 0; y < 8; y++ )
 
1840
                {
 
1841
                        m_TilesBank[i][0].put_BgColor( y, ( buffer[i*8 + y] >> 4 ) & 0xF );
 
1842
                        m_TilesBank[i][0].put_ForeColor( y, ( buffer[i*8 + y] ) & 0xF );
 
1843
                }
 
1844
        }
 
1845
 
 
1846
        for( b = 0; b < 3; b++ ) for( i = 0; i < 32*8; i++ ) m_Screen[i+b*32*8]=i;
 
1847
 
 
1848
        InitBanks();
 
1849
 
 
1850
        return true;
 
1851
}
 
1852
 
 
1853
bool CScreenW::ExportPolkaTiles( QString fileName )
 
1854
{
 
1855
        FILE *pFile;
 
1856
        int i, y, x;
 
1857
        unsigned char buffer[256*8];
 
1858
        
 
1859
 
 
1860
        if( !( pFile = fopen( qPrintable( fileName ), "wb" ) ) ) return false;
 
1861
 
 
1862
        sprintf( (char*)buffer, "%s", "POLKA" );
 
1863
        buffer[5] = 1;
 
1864
        buffer[6] = 0;
 
1865
        fwrite( buffer, sizeof( unsigned char ), 7, pFile );
 
1866
 
 
1867
        for( i = 0; i < 256; i++ )
 
1868
        {
 
1869
                for( y = 0; y < 8; y++ )
 
1870
                {
 
1871
                        buffer[ i*8 + y ] = 0;
 
1872
 
 
1873
                        for( x = 0; x < 8; x++ )
 
1874
                        {
 
1875
                                buffer[ i*8 + y ] |= ( m_TilesBank[i][0].get_Pixel( x, y )<<x );
 
1876
                        }
 
1877
                }
 
1878
        }
 
1879
 
 
1880
        fwrite( buffer, sizeof( unsigned char ), 2048, pFile );
 
1881
 
 
1882
        for( i = 0; i < 256; i++ )
 
1883
        {
 
1884
                for( y = 0; y < 8; y++ )
 
1885
                {
 
1886
                        buffer[i*8+y] = ( (m_TilesBank[i][0].get_BgColor(y )<<4 ) | (m_TilesBank[i][0].get_ForeColor(y) ) );
 
1887
                }
 
1888
        }
 
1889
 
 
1890
        fwrite( buffer, sizeof( unsigned char), 2048, pFile );
 
1891
 
 
1892
        buffer[0] = 0; //Num of Palettes
 
1893
 
 
1894
        fwrite( buffer, sizeof( unsigned char ), 1, pFile );
 
1895
//      [0][0][0][0]
 
1896
        //[17][6]
 
1897
        //[51][7]
 
1898
        //[23][1][39][3]
 
1899
        //[97][1][39][6][113][1]
 
1900
        //[115][3][97][6]
 
1901
        //[100][6][17][4][101][2][85][5][119][7]
 
1902
        buffer[ 0] = buffer[1] = buffer[2] = buffer[3] = 0;
 
1903
        buffer[ 4] = 17;        buffer[ 5] = 6;
 
1904
        buffer[ 6] = 51;        buffer[ 7] = 7;
 
1905
        buffer[ 8] = 23;        buffer[ 9] = 1;
 
1906
        buffer[10] = 39;        buffer[11] = 3;
 
1907
        buffer[12] = 97;        buffer[13] = 1;
 
1908
        buffer[14] = 39;        buffer[15] = 6;
 
1909
        buffer[16] =113;        buffer[17] = 1;
 
1910
        buffer[18] =115;        buffer[19] = 3;
 
1911
        buffer[20] = 97;        buffer[21] = 6;
 
1912
        buffer[22] =100;        buffer[23] = 6;
 
1913
        buffer[24] = 17;        buffer[25] = 4;
 
1914
        buffer[26] =101;        buffer[27] = 2;
 
1915
        buffer[28] = 85;        buffer[29] = 5;
 
1916
        buffer[30] =119;        buffer[31] = 7;
 
1917
 
 
1918
        fwrite( buffer, sizeof( unsigned char ), 32, pFile );
 
1919
 
 
1920
        for( i = 0; i < 256; i++ ) buffer[i] = 1;
 
1921
 
 
1922
        fwrite( buffer, sizeof( unsigned char ), 256, pFile );
 
1923
 
 
1924
        fclose( pFile );
 
1925
 
 
1926
        return true;
 
1927
}
 
1928
 
 
1929
void CScreenW::Fill1to255()
 
1930
{
 
1931
    int b;
 
1932
    int i;
 
1933
 
 
1934
 
 
1935
    for( b = 0; b < 3; b++ ) for( i = 0; i < 32*8; i++ ) m_Screen[i+b*32*8]=i;
 
1936
    UpdateScreen();
 
1937
}
 
1938
 
 
1939
int CScreenW::GetColor( unsigned int color, unsigned int palette[] )
 
1940
{
 
1941
        int i;
 
1942
        
 
1943
        for( i = 0; i < 16; i++ )
 
1944
        {
 
1945
                if( color == palette[i] ) return i;
 
1946
        }
 
1947
        //Color not in palette
 
1948
        return 0;
 
1949
}
 
1950
 
 
1951
void CScreenW::Close()
 
1952
{
 
1953
        m_Close = true;
 
1954
        close();
 
1955
}
 
1956
 
 
1957
void CScreenW::closeEvent( QCloseEvent *pEvent )
 
1958
{
 
1959
        if( m_Close ) pEvent->accept();
 
1960
        else pEvent->ignore();
 
1961
}
 
1962
 
 
1963
void CScreenW::OnOneBank()
 
1964
{
 
1965
        int i;
 
1966
        
 
1967
        
 
1968
        m_pTabBank->setCurrentIndex( 0 );
 
1969
                
 
1970
        for( i = 0; i < 256; i++ )
 
1971
        {
 
1972
                m_TilesBank[i][1] = m_TilesBank[i][0];
 
1973
                m_TilesBank[i][2] = m_TilesBank[i][0];
 
1974
        }
 
1975
        
 
1976
        m_pTabBank->setTabEnabled( 1, !m_pGrOneBank->isChecked() );
 
1977
        m_pTabBank->setTabEnabled( 2, !m_pGrOneBank->isChecked() );
 
1978
 
 
1979
        m_pGrBackgroundTiles1->setVisible( !m_pGrOneBank->isChecked() );
 
1980
        m_pGrBackgroundTiles2->setVisible( !m_pGrOneBank->isChecked() );
 
1981
        m_pGrLblBackgroundTiles1->setVisible( !m_pGrOneBank->isChecked() );
 
1982
        m_pGrLblBackgroundTiles2->setVisible( !m_pGrOneBank->isChecked() );
 
1983
 
 
1984
        InitBanks();
 
1985
}
 
1986
 
 
1987
 
 
1988
void CScreenW::SetOneBank( bool value )
 
1989
{
 
1990
        m_pGrOneBank->setChecked( value );
 
1991
        if( value ) OnOneBank();
 
1992
}
 
1993
 
 
1994
bool CScreenW::GetOneBank()
 
1995
{
 
1996
        return m_pGrOneBank->isChecked();
 
1997
}
 
1998