-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathColorizingDMD_save.cpp
12377 lines (11914 loc) · 542 KB
/
ColorizingDMD_save.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ColorizingDMD: Program to edit cROM colorized roms
* Programmed in plain C with Visual Studio 2022 by Zedrummer, 2022
*
* Linked to the project Visual Pinball Engine for Unity and, as such, is licensed under the GNU General Public License v3.0 https://github.com/freezy/VisualPinball.Engine/blob/master/LICENSE
*
* Uses OpenGL Immediate Mode for display in 2D in window as this is by far fast enough to make it works at 100+ FPS even on a low end computer with any dedicated GPU
*
*/
#pragma region Includes
#include "framework.h"
#include "ColorizingDMD.h"
#include <strsafe.h>
#include <gdiplus.h>
using namespace Gdiplus;
#include <CommCtrl.h>
#include "resource.h"
#include <shlwapi.h>
#include "cRom.h"
#include <tchar.h>
#include <direct.h>
#include "OGL_Immediate_2D.h"
#include <windowsx.h>
#include <math.h>
#include <shlobj_core.h>
#include <crtdbg.h>
#include "LiteZip.h"
#include <opencv2/opencv.hpp>
using namespace cv;
#include "gifenc.h"
#include "dmddevice.h"
#include "crc32.h"
#pragma endregion Includes
#pragma region Global_Variables
#define MAJ_VERSION 1
#define MIN_VERSION 34
#define PATCH_VERSION 1
static TCHAR szWindowClass[] = _T("ColorizingDMD");
static TCHAR szWindowClass2[] = _T("ChildWin");
static TCHAR szWindowClass3[] = _T("Image");
HINSTANCE hInst; // current instance
HWND hWnd = NULL, hwTB = NULL, hwTB2 = NULL, hwTB3 = NULL, hPal = NULL, hPal2 = NULL, hPal3 = NULL, hMovSec = NULL, hColSet = NULL, hSprites = NULL, hImages=NULL, hStatus = NULL, hStatus2 = NULL, hStatus3 = NULL;
UINT ColSetMode = 0, preColRot = 0, acColRot = 0; // 0- displaying colsets, 1- displaying colrots
GLFWwindow * glfwframe, * glfwframestrip,*glfwsprites,*glfwspritestrip, * glfwimages; // handle+context of our window
bool fDone = false;
HACCEL hAccelTable;
bool Update_Toolbar = false; // Do we need to recreate toolbar?
bool Update_Toolbar2 = false; // Do we need to recreate toolbar?
UINT ScrWframe = 500, ScrHframe = 200; // Frame draw window dimensions
UINT8 SelFrameColor = 0; // variables for changing color frames
DWORD timeSelFrame = 0;
UINT Edit_Mode = 0; // 0- editing original frame, 1- editing colorized frame
UINT Comparison_Mode = 0; // 0- no mask mode, 1- exclusion mask mode, 2- horizontal moving rectangle inclusion mask
bool isLoadedProject = false; // is there a project loaded?
HIMAGELIST g_hImageList = NULL, g_hImageListD = NULL;
bool Night_Mode = false;
//char DumpDir[MAX_PATH] = "D:\\visual pinball\\VPinMame\\dmddump\\";
//bool Ask_for_SaveDir = true;
bool NewProj; // is this a new project and we have to ask for the destination path even with a Ctrl+S
char Dir_Dumps[MAX_PATH], Dir_Images[MAX_PATH], Dir_Serum[MAX_PATH], Dir_GIFs[MAX_PATH], Dir_VP[MAX_PATH]; // different paths we need to keep
cRom_struct MycRom = { "",0,0,0,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL };
cRP_struct MycRP = { "",{FALSE},{0},0,0,{0},FALSE,0,FALSE };
COLORREF PrevColors[16]={0};
UINT8 originalcolors[16 * 3];
UINT8 acEditColors[16] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
UINT noColSel = 0; // # color selected either for the mask color (original mode) or for the drawing color (colorized mode)
UINT noSprSel = 0; // # color selected for drawing the sprite
UINT noColMod = 0; // # color clicked for modification
UINT8 Draw_Extra_Surface[256 * 64]; // surface to draw over the frames (show circle, rectangle, etc... before they are really drawn to the fram)
UINT8 Draw_Extra_Surface2[MAX_SPRITE_SIZE * MAX_SPRITE_SIZE]; // surface to draw over the frames (show circle, rectangle, etc... before they are really drawn to the fram)
//UINT8 acDynaColSet = 0; // current 4-color set in Dyna4Cols used for dynamic content
bool Color_Pipette = false; // are we picking the color?
int frame_zoom = 1; // zoom multiplier to display the frame
int offset_frame_x, offset_frame_y; // offset in pixels of the top left corner of the frame in the client area
int sprite_zoom = 1; // zoom multiplier to display the sprite
int offset_sprite_x, offset_sprite_y; // offset in pixels of the top left corner of the sprite in the client area
UINT TxCircle = (UINT) - 1; // LED imitation circle texture ID
UINT TxFrameStrip[2] = { (UINT)-1, (UINT)-1 }; // Framebuffer texture for the strip displaying the frames ID
UINT TxSpriteStrip[2] = { (UINT)-1, (UINT)-1 }; // Framebuffer texture for the strip displaying the sprites ID
UINT TxChiffres, TxcRom; // Number texture ID
UINT TxImage = (UINT)-1; // Image texture ID
UINT width_image, height_image; // width and height of the loaded image
UINT XSelection = 0, YSelection = 0, WSelection = 0, HSelection = 0; // size of the selection in the main window in LEDs
UINT NSelection = 0; // how many pixels selected
float SelRatio = 1; // ratio WSelection/HSelection
int crop_reductioni = 0, crop_reductionf = 0; // number of offset pixels of the crop frame in the the image window
UINT initcropwidth = 120, initcropheight = 0; // size of the crop frame in original image size with no reduction
//UINT crop_offsetx = 0, crop_offsety = 0; // offset of the crop frame in the image window
/*UINT crop_sizeW = 0, crop_sizeH = 0; // size of the crop frame in the window*/
UINT crop_iOsizeW = 0, crop_iOsizeH = 0; // size of the crop frame in the window
UINT crop_fOsizeW = 0, crop_fOsizeH = 0; // size of the crop frame in the window
int crop_ioffsetx = 0, crop_ioffsety = 0, crop_foffsetx = 0, crop_foffsety = 0; // Validated position of centers for initial and final crop zones
int crop_iwidth = 0, crop_iheight = 0, crop_fwidth = 0, crop_fheight = 0; // validated size of initial and final crop zones (crop_xwidth=0 the zone is not set)
/*UINT crop_scroll_mode = 0; // 0- no scroll mode, 1- horizontal mode, 2- vertical mode
bool crop_scroll_invert = false;
int crop_scroll_extend = 0; // extending when in scroll mode*/
UINT image_posx = 0, image_posy = 0; // position of the image in the image screen
UINT image_sizeW = 10, image_sizeH = 10; // size of the image in the image screen
bool image_mousepressed = false; // is button pressed on the image
bool image_source_format_video = false; // is the file loaded a video?
cv::VideoCapture image_video_cap; // the video file opened
long image_video_frame_rate = 60; // number of frames per second in the video
UINT8 image_video_hour, image_video_minute, image_video_second, image_video_frame; // time selected for the video
UINT8 image_video_nhours, image_video_nminutes, image_video_nseconds, image_video_nframes; // length of the video in HMSF
bool image_loaded = false; // is there an image loaded
cv::Mat image_mat, image_org_mat; // the image loaded
int image_brightness = 0, image_contrast = 0;
char image_path[MAX_PATH]; // path to the image file
unsigned char image_precolsel = 0, image_ncolsel = 1; // number of color selected in image mode
UINT acFSText = 0; // current texture displayed on the frame strip
UINT acSSText = 0; // current texture displayed on the sprite strip
UINT8 Raw_Digit_Def[RAW_DIGIT_W * RAW_DIGIT_H * 10]; // buffer for raw numbers
UINT8* pFrameStrip = NULL; // pointer to the memory to draw the frame strips
UINT8* pSpriteStrip = NULL; // pointer to the memory to draw the sprite strips
UINT SliderWidth, PosSlider; // Width of the frame strip slider and pos of the slider on the bar
UINT ScrW, ScrH; // client size of the main window
UINT ScrW2, ScrH2; // client size of the sprite window
UINT ScrW3, ScrH3; // client size of the image window
int MonWidth = 1920; // X resolution of the monitor
int PreFrameInStrip = 0; // First frame to display in the frame strip
UINT NFrameToDraw = 0; // how many frames to draw in the strip
UINT FS_LMargin = 0; // left margin (same as right) before the first frame in the strip
int PreSpriteInStrip = 0; // First sprite to display in the frame strip
UINT NSpriteToDraw = 0; // how many sprites to draw in the strip
UINT SS_LMargin = 0; // left margin (same as right) before the first sprite in the strip
UINT acFrame = 0, prevFrame = 0; // current frame displayed and previously selected frame
UINT acSprite = 0, prevSprite=0; // current sprite displayed
UINT acDetSprite = 0; // current detection zone active for the sprite
unsigned int nSelFrames = 1; // number of selected frames
#define MAX_SEL_FRAMES 1024 // Max number of selected frames (has consequences on the undo/redo buffer size!)
unsigned int SelFrames[MAX_SEL_FRAMES]={0}; // list of selected frames
const UINT8 SelColor[3] = { 100,150,255 };
const UINT8 acColor[3] = { 255,255,255 };
const UINT8 SameColor[3] = { 0,180,0 };
const UINT8 UnselColor[3] = { 255,50,50 };
const UINT8 SectionColor[3] = { 255,50,255 };
bool MultiSelectionActionWarning = false; // is there a messagebox each time to confirm an action on a multi selection?
bool isDrawAllSel = false; // do we draw on all the selected frames?
bool isMaskAllSel = false; // do we modify the masks of all the selected frames?
DWORD timeLPress = 0, timeRPress = 0, timeUPress = 0, timeDPress = 0; // timer to know how long the key has been pressed
UINT32 Mouse_Mode = 0; // 0- no mouse activity, 1 - drawing the comparison mask (point), 2 - drawing the comparison/detection area mask (rectangle or magic wand), 3 - drawing the colorized frame (point), 4 - drawing the colorized frame (others), 5 - drawing the dynamic mask (point), 6 - drawing the dynamic mask (others), 7 - drawing the copy mask (point), 8 - drawing the copy mask (others)
bool isDel_Mode; // are we deleting from draw/mask?
//bool MouseActionCancelled = false; // do we cancelled the edit action while mouse clicked?
int MouseIniPosx, MouseIniPosy; // position of the mouse when starting to draw
int MouseFinPosx, MouseFinPosy; // position of the mouse when moving to draw
int MouseFrSliderlx; // previous position on the slider
bool MouseFrSliderLPressed = false; // mouse pressed on the frame strip slider
UINT8* UndoSave; // [MAX_SEL_FRAMES * 256 * 64 * MAX_UNDO] ; // Buffer to save things for the undo
int UndoAvailable = 0; // how many actions to undo available
UINT UndoAction[MAX_UNDO]; // To know which action to do in case of undo: see enum SA_XXXXXXXX in cRom.h
UINT8* RedoSave;// [MAX_SEL_FRAMES * 256 * 64 * MAX_UNDO] ; // Buffer to save things for the redo
int RedoAvailable = 0; // how many actions to redo available
UINT RedoAction[MAX_UNDO]; // To know which action to do in case of redo: see enum SA_XXXXXXXX in cRom.h
bool isAddToMask = true; // true if the selection is add to mask, false if the selection is removed from mask
HANDLE hStdout; // for log window
bool DrawCommonPoints = true; // Do we display the common points of the selected frames (in original frame mode)?
HWND hCBList = NULL; // the combobox list to subclass for right click
#define MAX_SAME_FRAMES 10000
int nSameFrames = 0;
int SameFrames[MAX_SAME_FRAMES];
bool UpdateFSneeded = false; // Do we need to update the frame strip
bool UpdateSSneeded = false; // Do we need to update the sprite strip
//bool UpdateFSneededwhenBreleased = false; // Do we need to update the frame strip after the mouse button has been released
UINT8 Sprite_Mode = 0; // sprite drawing mode: 0- point, 1- line, 2- rectangle, 3- circle, 4- fill
bool SpriteFill_Mode = false; // sprite drawing filled rectangle and circle
UINT8 acDynaSet = 0; // current dynamic color set used
UINT8 mselcol; // flashing color %
UINT8 Copy_Mask[256 * 64] = { 0 }; // mask for copy operations
UINT8 Copy_Col[256 * 64]; // colors of copy
UINT8 Copy_Colo[256 * 64]; // original colors of copy
UINT8 Copy_Dyna[256 * 64]; // dynamic mask for copy
UINT Paste_Width, Paste_Height; // Dimensions for paste
UINT8 Paste_Mask[256 * 64]; // mask copy of the copy for paste
UINT8 Paste_Col[256 * 64]; // colors of copy
UINT8 Paste_Colo[256 * 64]; // original colors of copy
UINT8 Paste_Dyna[256 * 64]; // dynamic mask for copy
int Copy_From_Frame = -1; // which frame to copy from
bool Copy_Mode = false; // do we add to copy mask?
bool Copy_Available = false; // Is there any content to paste?
int paste_offsetx, paste_offsety, paste_centerx, paste_centery; // position of the copy in the new frame, and position of the middle of the copied area
bool Paste_Mode = false; // are we waiting for a click in the frame to paste
int Paste_Mirror = 0; // are we flipping the copied area horizontally and/or vertically 0- no, 1- horizontally, 2- vertically, 3- both
HCURSOR hcColPick, hcArrow, hcPaste; // pick color cursor, standard arrow cursor, paste cursor
GLFWcursor *glfwpastecur, *glfwdropcur, *glfwarrowcur; // glfw cursors
bool Start_Gradient = false; // are we creating a color gradient?
UINT8 Ini_Gradient_Color, Fin_Gradient_Color; // what is the first and last colors for the gradient
bool Start_Gradient2 = false; // are we creating a color gradient for filling?
UINT8 Draw_Grad_Ini = 0, Draw_Grad_Fin = 63; // colors for draw in gradient
bool Start_Col_Exchange = false; // are we chganging the position of 2 colors in the palette
UINT8 Pre_Col_Exchange = 0; // first color selected for exchange
sColRot MyColRot; // color rotation structure
bool Ident_Pushed = false;
bool filter_time = false, filter_allmask = false, filter_color = false;
int filter_length = 15, filter_ncolor = 16;
int statusBarHeight;
bool BlocPause = true;
//sImage MyImage = { "",0,0,0.0f,0,0 };
#pragma endregion Global_Variables
#pragma region Undo_Tools
void EraseFirstSavedAction(bool isUndo)
{
UINT* pAc;
UINT8* pBuffer;
if (isUndo)
{
pAc = UndoAction;
pBuffer = UndoSave;
UndoAvailable--;
}
else
{
pAc = RedoAction;
pBuffer = RedoSave;
RedoAvailable--;
}
for (UINT ti = 0; ti < MAX_UNDO - 1; ti++)
pAc[ti] = pAc[ti + 1];
memcpy(pBuffer, &pBuffer[MAX_SEL_FRAMES * 256 * 64], MAX_SEL_FRAMES * 256 * 64 * (MAX_UNDO - 1));
}
UINT8* SaveGetBuffer(bool isUndo)
{
if (isUndo)
{
if (UndoAvailable == MAX_UNDO) EraseFirstSavedAction(true);
return &UndoSave[MAX_SEL_FRAMES * 256 * 64 * UndoAvailable];
}
else
{
if (RedoAvailable == MAX_UNDO) EraseFirstSavedAction(false);
return &RedoSave[MAX_SEL_FRAMES * 256 * 64 * RedoAvailable];
}
}
void SaveSetAction(bool isUndo, UINT action)
{
if (isUndo)
{
UndoAction[UndoAvailable] = action;
UndoAvailable++;
SendMessage(hwTB, TB_ENABLEBUTTON, BM_UNDO, MAKELONG(1, 0));
}
else
{
RedoAction[RedoAvailable] = action;
RedoAvailable++;
SendMessage(hwTB, TB_ENABLEBUTTON, BM_REDO, MAKELONG(1, 0));
}
}
UINT8* RecoverGetBuffer(bool isUndo)
{
if (isUndo) return &UndoSave[MAX_SEL_FRAMES * 256 * 64 * (UndoAvailable - 1)];
else return &RedoSave[MAX_SEL_FRAMES * 256 * 64 * (RedoAvailable - 1)];
}
void RecoverAdjustAction(bool isUndo)
{
if (isUndo)
{
UndoAvailable--;
if (UndoAvailable == 0) SendMessage(hwTB, TB_ENABLEBUTTON, BM_UNDO, MAKELONG(0, 0));
}
else
{
RedoAvailable--;
if (RedoAvailable == 0) SendMessage(hwTB, TB_ENABLEBUTTON, BM_REDO, MAKELONG(0, 0));
}
}
void SaveDrawFrames(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer= SaveGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) memcpy(&pBuffer[ti * MycRom.fWidth * MycRom.fHeight],
&MycRom.cFrames[SelFrames[ti] * MycRom.fWidth * MycRom.fHeight], MycRom.fWidth * MycRom.fHeight);
SaveSetAction(isUndo, SA_DRAW);
}
void RecoverDrawFrames(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer= RecoverGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) memcpy(&MycRom.cFrames[SelFrames[ti] * MycRom.fWidth * MycRom.fHeight],
&pBuffer[ti * MycRom.fWidth * MycRom.fHeight], MycRom.fWidth * MycRom.fHeight);
RecoverAdjustAction(isUndo);
}
void SaveSelection(bool isUndo)
{
// Save selected frame list for undo or redo action before changing it
UINT8* pBuffer = SaveGetBuffer(isUndo);
*((unsigned int*)pBuffer) = nSelFrames;
*((UINT*)(&pBuffer[4])) = acFrame;
memcpy(&pBuffer[8], SelFrames, nSelFrames * sizeof(int));
SaveSetAction(isUndo, SA_SELECTION);
}
void RecoverSelection(bool isUndo)
{
// Undo or redo a saved selection
UINT8* pBuffer = RecoverGetBuffer(isUndo);
nSelFrames = *((unsigned int*)pBuffer);
acFrame = *((UINT*)(&pBuffer[4]));
InitColorRotation();
if (acFrame >= PreFrameInStrip + NFrameToDraw) PreFrameInStrip = acFrame - NFrameToDraw + 1;
if ((int)acFrame < PreFrameInStrip) PreFrameInStrip = acFrame;
memcpy(SelFrames, &pBuffer[8], nSelFrames * sizeof(int));
UpdateNewacFrame();
RecoverAdjustAction(isUndo);
}
void SaveDynaColor(bool isUndo)
{
// Save selected frame list for undo or redo action before changing it
UINT8* pBuffer = SaveGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) memcpy(&pBuffer[ti * MAX_DYNA_SETS_PER_FRAME * MycRom.noColors],
&MycRom.Dyna4Cols[SelFrames[ti] * MAX_DYNA_SETS_PER_FRAME * MycRom.noColors], MAX_DYNA_SETS_PER_FRAME * MycRom.noColors);
SaveSetAction(isUndo, SA_DYNACOLOR);
}
void RecoverDynaColor(bool isUndo)
{
// Undo or redo a saved selection
UINT8* pBuffer = RecoverGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) memcpy(&MycRom.Dyna4Cols[SelFrames[ti] * MAX_DYNA_SETS_PER_FRAME * MycRom.noColors],
&pBuffer[ti * MAX_DYNA_SETS_PER_FRAME * MycRom.noColors], MAX_DYNA_SETS_PER_FRAME * MycRom.noColors);
RecoverAdjustAction(isUndo);
}
void SaveCompMask(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8 ti = MycRom.CompMaskID[acFrame];
if (ti == 255) return;
UINT8* pBuffer = SaveGetBuffer(isUndo);
memcpy(pBuffer, &MycRom.CompMasks[ti * MycRom.fWidth * MycRom.fHeight], MycRom.fWidth * MycRom.fHeight);
SaveSetAction(isUndo, SA_COMPMASK);
}
void RecoverCompMask(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
UINT8 ti = MycRom.CompMaskID[acFrame];
memcpy(&MycRom.CompMasks[ti * MycRom.fWidth * MycRom.fHeight], pBuffer, MycRom.fWidth * MycRom.fHeight);
RecoverAdjustAction(isUndo);
}
void SaveDuration(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT* pBuffer = (UINT*)SaveGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) pBuffer[ti] = MycRP.FrameDuration[SelFrames[ti]];
SaveSetAction(isUndo, SA_COMPMASK);
}
void RecoverDuration(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT* pBuffer = (UINT*)RecoverGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) MycRP.FrameDuration[SelFrames[ti]] = pBuffer[ti];
RecoverAdjustAction(isUndo);
}
void SavePalette(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) memcpy(&pBuffer[ti * MycRom.ncColors * 3],
&MycRom.cPal[SelFrames[ti] * MycRom.ncColors * 3], MycRom.ncColors * 3);
SaveSetAction(isUndo, SA_PALETTE);
}
void RecoverPalette(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) memcpy(&MycRom.cPal[SelFrames[ti] * MycRom.ncColors * 3],
&pBuffer[ti * MycRom.ncColors * 3], MycRom.ncColors * 3);
for (UINT ti = IDC_COL1; ti <= IDC_COL16; ti++) InvalidateRect(GetDlgItem(hwTB, ti), NULL, TRUE);
for (UINT ti = IDC_DYNACOL1; ti <= IDC_DYNACOL16; ti++) InvalidateRect(GetDlgItem(hwTB, ti), NULL, TRUE);
RecoverAdjustAction(isUndo);
}
void SaveMaskID(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) pBuffer[ti] = MycRom.CompMaskID[SelFrames[ti]];
SaveSetAction(isUndo, SA_MASKID);
}
void RecoverMaskID(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) MycRom.CompMaskID[SelFrames[ti]] = pBuffer[ti];
UpdateMaskList();
CheckSameFrames();
RecoverAdjustAction(isUndo);
}
void SaveShapeMode(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) pBuffer[ti] = MycRom.ShapeCompMode[SelFrames[ti]];
SaveSetAction(isUndo, SA_SHAPEMODE);
}
void RecoverShapeMode(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++) MycRom.ShapeCompMode[SelFrames[ti]] = pBuffer[ti];
if (MycRom.ShapeCompMode[acFrame] == TRUE) Button_SetCheck(GetDlgItem(hwTB, IDC_SHAPEMODE), BST_CHECKED); else Button_SetCheck(GetDlgItem(hwTB, IDC_SHAPEMODE), BST_UNCHECKED);
CheckSameFrames();
RecoverAdjustAction(isUndo);
}
void SaveColorSets(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
memcpy(pBuffer, MycRP.ColSets, MAX_COL_SETS * 16); // copy the col sets
memcpy(&pBuffer[MAX_COL_SETS * 16], MycRP.nameColSet, MAX_COL_SETS * 64); // copy their names
memcpy(&pBuffer[MAX_COL_SETS * (16 + 64)], MycRP.activeColSet,MAX_COL_SETS); // copy the active one
SaveSetAction(isUndo, SA_SHAPEMODE);
}
void RecoverColorSets(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
memcpy(MycRP.ColSets, pBuffer, MAX_COL_SETS * 16);
memcpy(MycRP.nameColSet, &pBuffer[MAX_COL_SETS * 16], MAX_COL_SETS * 64);
memcpy(MycRP.activeColSet, &pBuffer[MAX_COL_SETS * (16 + 64)], MAX_COL_SETS);
RecoverAdjustAction(isUndo);
}
void SaveEditColor(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
memcpy(pBuffer, acEditColors, 16);
SaveSetAction(isUndo, SA_EDITCOLOR);
}
void RecoverEditColor(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
memcpy(acEditColors, pBuffer, 16);
if (Edit_Mode == 1)
{
for (UINT ti = IDC_COL1; ti <= IDC_COL16; ti++) InvalidateRect(GetDlgItem(hwTB, ti), NULL, TRUE);
}
RecoverAdjustAction(isUndo);
}
void SaveCopyMask(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
memcpy(pBuffer, Copy_Mask, 256 * 64);
memcpy(&pBuffer[256 * 64], Copy_Col, 256 * 64);
memcpy(&pBuffer[2 * 256 * 64], Copy_Colo, 256 * 64);
memcpy(&pBuffer[3 * 256 * 64], Copy_Dyna, 256 * 64);
SaveSetAction(isUndo, SA_COPYMASK);
}
void RecoverCopyMask(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
memcpy(Copy_Mask, pBuffer, 256 * 64);
memcpy(Copy_Col, &pBuffer[256 * 64], 256 * 64);
memcpy(Copy_Colo, &pBuffer[2 * 256 * 64], 256 * 64);
memcpy(Copy_Dyna, &pBuffer[3 * 256 * 64], 256 * 64);
GetSelectionSize(&XSelection, &YSelection, &WSelection, &HSelection, &NSelection);
RecoverAdjustAction(isUndo);
}
void SaveDynaMask(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
for (UINT32 ti = 0; ti < nSelFrames; ti++) memcpy(&pBuffer[ti * MycRom.fWidth * MycRom.fHeight],
&MycRom.DynaMasks[SelFrames[ti] * MycRom.fWidth * MycRom.fHeight], MycRom.fWidth * MycRom.fHeight);
SaveSetAction(isUndo, SA_DYNAMASK);
}
void RecoverDynaMask(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
for (UINT32 ti = 0; ti < nSelFrames; ti++) memcpy(&MycRom.DynaMasks[SelFrames[ti] * MycRom.fWidth * MycRom.fHeight],
&pBuffer[ti * MycRom.fWidth * MycRom.fHeight], MycRom.fWidth * MycRom.fHeight);
RecoverAdjustAction(isUndo);
}
void SaveSpriteCol(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
memcpy(pBuffer, &MycRP.Sprite_Edit_Colors[acSprite * 16], 16);
SaveSetAction(isUndo, SA_SPRITECOLOR);
}
void RecoverSpriteCol(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
memcpy(&MycRP.Sprite_Edit_Colors[acSprite * 16], pBuffer, 16);
RecoverAdjustAction(isUndo);
}
void SaveFrameSprites(bool isUndo)
{
// Save selected frame content for undo or redo action before drawing
UINT8* pBuffer = SaveGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++)
{
memcpy(&pBuffer[ti * MAX_SPRITES_PER_FRAME], &MycRom.FrameSprites[SelFrames[ti] * MAX_SPRITES_PER_FRAME], MAX_SPRITES_PER_FRAME);
}
SaveSetAction(isUndo, SA_FRAMESPRITES);
}
void RecoverFrameSprites(bool isUndo)
{
// Undo or redo a saved frame before drawing
UINT8* pBuffer = RecoverGetBuffer(isUndo);
for (UINT ti = 0; ti < nSelFrames; ti++)
{
memcpy(&MycRom.FrameSprites[SelFrames[ti] * MAX_SPRITES_PER_FRAME], &pBuffer[ti * MAX_SPRITES_PER_FRAME], MAX_SPRITES_PER_FRAME);
}
RecoverAdjustAction(isUndo);
}
void SaveAcSprite(bool isUndo)
{
UINT8* pBuffer = SaveGetBuffer(isUndo);
*((UINT*)pBuffer) = acSprite;
SaveSetAction(isUndo, SA_ACSPRITE);
}
void RecoverAcSprite(bool isUndo)
{
UINT8* pBuffer = RecoverGetBuffer(isUndo);
acSprite = *((UINT*)pBuffer);
if (acSprite >= MycRom.nSprites) acSprite = MycRom.nSprites - 1;
RecoverAdjustAction(isUndo);
}
void SaveSprite(bool isUndo)
{
UINT8* pBuffer = SaveGetBuffer(isUndo);
memcpy(pBuffer, &MycRom.SpriteDescriptions[acSprite * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE], MAX_SPRITE_SIZE * MAX_SPRITE_SIZE * sizeof(UINT16));
memcpy(&pBuffer[MAX_SPRITE_SIZE * MAX_SPRITE_SIZE * sizeof(UINT16)], &MycRom.SpriteDetAreas[acSprite * 4 * MAX_SPRITE_DETECT_AREAS], 4 * MAX_SPRITE_DETECT_AREAS * sizeof(UINT16));
SaveSetAction(isUndo, SA_SPRITE);
}
void RecoverSprite(bool isUndo)
{
UINT8* pBuffer = RecoverGetBuffer(isUndo);
memcpy(&MycRom.SpriteDescriptions[acSprite * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE], pBuffer, MAX_SPRITE_SIZE * MAX_SPRITE_SIZE * sizeof(UINT16));
memcpy(&MycRom.SpriteDetAreas[acSprite * 4 * MAX_SPRITE_DETECT_AREAS], &pBuffer[MAX_SPRITE_SIZE * MAX_SPRITE_SIZE * sizeof(UINT16)], 4 * MAX_SPRITE_DETECT_AREAS * sizeof(UINT16));
RecoverAdjustAction(isUndo);
}
void SaveColRot(bool isUndo)
{
UINT8* pBuffer = SaveGetBuffer(isUndo);
for (UINT32 ti = 0; ti < nSelFrames; ti++) memcpy(pBuffer, &MycRom.ColorRotations[SelFrames[ti] * 3 * MAX_COLOR_ROTATION], 3 * MAX_COLOR_ROTATION);
SaveSetAction(isUndo, SA_COLROT);
}
void RecoverColRot(bool isUndo)
{
UINT8* pBuffer = RecoverGetBuffer(isUndo);
for (UINT32 ti = 0; ti < nSelFrames; ti++) memcpy(&MycRom.ColorRotations[SelFrames[ti] * 3 * MAX_COLOR_ROTATION], pBuffer, 3 * MAX_COLOR_ROTATION);
RecoverAdjustAction(isUndo);
}
void SaveSprites(bool isUndo)
{
UINT8* pBuffer = SaveGetBuffer(isUndo);
((UINT32*)pBuffer)[0] = MycRom.nSprites;
memcpy(&pBuffer[sizeof(UINT32)], MycRom.SpriteDescriptions, sizeof(UINT16) * MycRom.nSprites * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE);
memcpy(&pBuffer[sizeof(UINT32) + sizeof(UINT16) * MycRom.nSprites * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE], MycRom.SpriteDetAreas, sizeof(UINT16) * MycRom.nSprites * 4 * MAX_SPRITE_DETECT_AREAS);
SaveSetAction(isUndo, SA_SPRITES);
}
void RecoverSprites(bool isUndo)
{
UINT8* pBuffer = RecoverGetBuffer(isUndo);
UINT32 tns = MycRom.nSprites;
MycRom.nSprites = ((UINT32*)pBuffer)[0];
if (tns != MycRom.nSprites)
{
MycRom.SpriteDescriptions = (UINT16*)realloc(MycRom.SpriteDescriptions, sizeof(UINT16) * MycRom.nSprites * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE);
MycRom.SpriteDetAreas = (UINT16*)realloc(MycRom.SpriteDetAreas, sizeof(UINT16) * MycRom.nSprites * 4 * MAX_SPRITE_DETECT_AREAS);
}
memcpy(MycRom.SpriteDescriptions, &pBuffer[sizeof(UINT32)], sizeof(UINT16) * MycRom.nSprites * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE);
memcpy(MycRom.SpriteDetAreas, &pBuffer[sizeof(UINT32) + sizeof(UINT16) * MycRom.nSprites * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE], sizeof(UINT16) * MycRom.nSprites * 4 * MAX_SPRITE_DETECT_AREAS);
UpdateSpriteList();
InvalidateRect(hwTB2, NULL, TRUE);
if (acSprite >= MycRom.nSprites) acSprite = MycRom.nSprites - 1;
}
void SaveSections(bool isUndo)
{
UINT8* pBuffer = SaveGetBuffer(isUndo);
*((UINT32*)pBuffer) = MycRP.nSections;
memcpy(&pBuffer[4], MycRP.Section_Names, MAX_SECTIONS * SIZE_SECTION_NAMES);
memcpy(&pBuffer[4 + MAX_SECTIONS * SIZE_SECTION_NAMES], MycRP.Section_Firsts, sizeof(UINT32) * MAX_SECTIONS);
SaveSetAction(isUndo, SA_SECTIONS);
}
void RecoverSections(bool isUndo)
{
UINT8* pBuffer = RecoverGetBuffer(isUndo);
MycRP.nSections = *((UINT32*)pBuffer);
memcpy(MycRP.Section_Names, &pBuffer[4], MAX_SECTIONS * SIZE_SECTION_NAMES);
memcpy(MycRP.Section_Firsts, &pBuffer[4 + MAX_SECTIONS * SIZE_SECTION_NAMES], sizeof(UINT32) * MAX_SECTIONS);
RecoverAdjustAction(isUndo);
UpdateSectionList();
}
void SaveAction(bool isUndo, int action)
{
// 0- draw to frames, 1- change selection, 2- change comp mask shape, 3- change palette, 4- change mask colors, 5- change moving comparison pattern
switch (action)
{
case SA_DRAW: // save before drawing
SaveDrawFrames(isUndo);
break;
case SA_SELECTION: // save before changing selection
SaveSelection(isUndo);
break;
case SA_COMPMASK: // save before changing comp mask
SaveCompMask(isUndo);
break;
case SA_PALETTE: // save before changing palette
SavePalette(isUndo);
break;
case SA_MOVCOMPPATTERN: // save before changing moving comparison pattern
// SaveMovCompPattern(isUndo);
case SA_EDITCOLOR:
SaveEditColor(isUndo);
break;
case SA_DYNACOLOR:
SaveDynaColor(isUndo);
break;
case SA_MASKID:
SaveMaskID(isUndo);
break;
case SA_SHAPEMODE:
SaveShapeMode(isUndo);
break;
case SA_COLSETS:
SaveColorSets(isUndo);
break;
case SA_COPYMASK:
SaveCopyMask(isUndo);
break;
case SA_DYNAMASK:
SaveDynaMask(isUndo);
break;
case SA_SPRITECOLOR:
SaveSpriteCol(isUndo);
break;
case SA_ACSPRITE:
SaveAcSprite(isUndo);
break;
case SA_SPRITE:
SaveSprite(isUndo);
break;
case SA_SPRITES:
SaveSprites(isUndo);
break;
case SA_FRAMESPRITES:
SaveFrameSprites(isUndo);
break;
case SA_SECTIONS:
SaveSections(isUndo);
break;
case SA_COLROT:
SaveColRot(isUndo);
break;
case SA_DURATION:
SaveDuration(isUndo);
break;
}
UpdateFSneeded = true;
UpdateSSneeded = true;
if (UndoAvailable > 0)
{
EnableWindow(GetDlgItem(hwTB, IDC_UNDO), TRUE);
EnableWindow(GetDlgItem(hwTB2, IDC_UNDO), TRUE);
}
else
{
EnableWindow(GetDlgItem(hwTB, IDC_UNDO), FALSE);
EnableWindow(GetDlgItem(hwTB2, IDC_UNDO), FALSE);
}
if (RedoAvailable > 0)
{
EnableWindow(GetDlgItem(hwTB, IDC_REDO), TRUE);
EnableWindow(GetDlgItem(hwTB2, IDC_REDO), TRUE);
}
else
{
EnableWindow(GetDlgItem(hwTB, IDC_REDO), FALSE);
EnableWindow(GetDlgItem(hwTB2, IDC_REDO), FALSE);
}
}
void RecoverAction(bool isUndo)
{
int action;
if (isUndo)
{
if (UndoAvailable == 0) return;
action = UndoAction[UndoAvailable - 1];
}
else
{
if (RedoAvailable == 0) return;
action = RedoAction[RedoAvailable - 1];
}
switch (action)
{
case SA_DRAW: // save before drawing
SaveDrawFrames(!isUndo);
RecoverDrawFrames(isUndo);
break;
case SA_SELECTION: // save before changing selection
SaveSelection(!isUndo);
RecoverSelection(isUndo);
break;
case SA_COMPMASK: // save before changing comp mask
SaveCompMask(!isUndo);
RecoverCompMask(isUndo);
break;
case SA_PALETTE: // save before changing palette
SavePalette(!isUndo);
RecoverPalette(isUndo);
break;
case SA_MOVCOMPPATTERN: // save before changing moving comparison pattern
//SaveMovCompPattern(!isUndo);
//RecoverMovCompPattern(isUndo);
break;
case SA_EDITCOLOR:
SaveEditColor(!isUndo);
RecoverEditColor(isUndo);
break;
case SA_DYNACOLOR:
SaveDynaColor(!isUndo);
RecoverDynaColor(isUndo);
for (UINT ti = IDC_DYNACOL1; ti <= IDC_DYNACOL16; ti++) InvalidateRect(GetDlgItem(hwTB, ti), NULL, TRUE);
break;
case SA_MASKID:
SaveMaskID(!isUndo);
RecoverMaskID(isUndo);
break;
case SA_SHAPEMODE:
SaveShapeMode(!isUndo);
RecoverShapeMode(isUndo);
break;
case SA_COLSETS:
SaveColorSets(!isUndo);
RecoverColorSets(isUndo);
break;
case SA_COPYMASK:
SaveCopyMask(!isUndo);
RecoverCopyMask(isUndo);
break;
case SA_DYNAMASK:
SaveDynaMask(!isUndo);
RecoverDynaMask(isUndo);
break;
case SA_SPRITECOLOR:
SaveSpriteCol(!isUndo);
RecoverSpriteCol(isUndo);
break;
case SA_ACSPRITE:
SaveAcSprite(!isUndo);
RecoverAcSprite(isUndo);
break;
case SA_SPRITE:
SaveSprite(!isUndo);
RecoverSprite(isUndo);
break;
case SA_FRAMESPRITES:
SaveFrameSprites(!isUndo);
RecoverFrameSprites(isUndo);
break;
case SA_SPRITES:
SaveSprites(!isUndo);
RecoverSprites(isUndo);
break;
case SA_SECTIONS:
SaveSections(!isUndo);
RecoverSections(isUndo);
break;
case SA_COLROT:
SaveColRot(!isUndo);
RecoverColRot(isUndo);
break;
case SA_DURATION:
SaveDuration(!isUndo);
RecoverDuration(isUndo);
break;
}
UpdateFSneeded = true;
UpdateSSneeded = true;
if (UndoAvailable > 0)
{
EnableWindow(GetDlgItem(hwTB, IDC_UNDO), TRUE);
EnableWindow(GetDlgItem(hwTB2, IDC_UNDO), TRUE);
}
else
{
EnableWindow(GetDlgItem(hwTB, IDC_UNDO), FALSE);
EnableWindow(GetDlgItem(hwTB2, IDC_UNDO), FALSE);
}
if (RedoAvailable > 0)
{
EnableWindow(GetDlgItem(hwTB, IDC_REDO), TRUE);
EnableWindow(GetDlgItem(hwTB2, IDC_REDO), TRUE);
}
else
{
EnableWindow(GetDlgItem(hwTB, IDC_REDO), FALSE);
EnableWindow(GetDlgItem(hwTB2, IDC_REDO), FALSE);
}
}
#pragma endregion Undo_Tools
#pragma region Debug_Tools
void cprintf(const char* format,...) // write to the console
{
char tbuf[5000];
va_list argptr;
va_start(argptr, format);
vsprintf_s(tbuf,1024, format, argptr);
va_end(argptr);
char tbuf2[512];
SYSTEMTIME lt;
GetLocalTime(<);
sprintf_s(tbuf2, 512, "%02d:%02d: %s\n\r", lt.wHour,lt.wMinute, tbuf);
WriteFile(hStdout, tbuf2, (DWORD)strlen(tbuf2), NULL, NULL);
}
void AffLastError(char* lpszFunction)
{
// Retrieve the system error message for the last-error code
char* lpMsgBuf;
char* lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (char*)LocalAlloc(LMEM_ZEROINIT,
(strlen((LPCSTR)lpMsgBuf) + strlen((LPCSTR)lpszFunction) + 40) * sizeof(char));
StringCchPrintfA(lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(char),
"%s failed with error %d: %s",
lpszFunction, dw, lpMsgBuf);
cprintf(lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
#pragma endregion Debug_Tools
#pragma region Memory_Tools
void Del_Buffer_Element(UINT8* pBuf, UINT* pnElt, UINT noElt, UINT Elt_Size)
{
/* Erase the noElt-th element of the buffer pointed by pBuf.
* Before the function, the buffer contains *pnElt element and each element is Elt_Size byte long.
* Shift all the elements after noElt to the left and reduce the buffer size with realloc. *pnElt is decremetend at the end.*/
if (noElt >= (*pnElt)) return;
if ((*pnElt) == 1)
{
free(pBuf);
pBuf = NULL;
(*pnElt) = 0;
return;
}
if (noElt < ((*pnElt) - 1)) memcpy(&pBuf[noElt * Elt_Size], &pBuf[(noElt + 1) * Elt_Size], ((*pnElt) - 1 - noElt) * Elt_Size);
pBuf = (UINT8*)realloc(pBuf, ((*pnElt) - 1) * Elt_Size);
(*pnElt)--;
}
bool Add_Buffer_Element(UINT8* pBuf, UINT* pnElt, UINT Elt_Size, UINT8* pSrc)
{
/* Add an element to the buffer pointed by pBuf.
* Before the function, the buffer contains *pnElt element and each element is Elt_Size byte long.
* Increase the buffer size by Elt_Size bytes using realloc then copy the new element from pSrc copiing Elt_Size bytes at the end of the buffer.
* if pSrc==NULL, fill the memory with 0s
* *pnElt is incremented at the end*/
pBuf = (UINT8*)realloc(pBuf, ((*pnElt) + 1) * Elt_Size);
if (!pBuf)
{
cprintf("Unable to increase the buffer size in Add_Buffer_Element");
return false;
}
if (pSrc != NULL) memcpy(&pBuf[(*pnElt) * Elt_Size], pSrc, Elt_Size); else memset(&pBuf[(*pnElt) * Elt_Size], 0, Elt_Size);
(*pnElt)++;
return true;
}
#pragma endregion Memory_Tools
#pragma region Color_Tools
void CopyColAndDynaCol(UINT fromframe, UINT toframe)
{
for (UINT ti = 0; ti < MycRom.ncColors * 3; ti++)
{
MycRom.cPal[toframe * 3 * MycRom.ncColors + ti] = MycRom.cPal[fromframe * 3 * MycRom.ncColors + ti];
}
for (UINT ti = 0; ti < MAX_DYNA_SETS_PER_FRAME; ti++)
{
for (UINT tj = 0; tj < MycRom.noColors; tj++)
{
MycRom.Dyna4Cols[toframe * MAX_DYNA_SETS_PER_FRAME * MycRom.noColors + ti * MycRom.noColors + tj] = MycRom.Dyna4Cols[fromframe * MAX_DYNA_SETS_PER_FRAME * MycRom.noColors + ti * MycRom.noColors + tj];
}
}
}
bool Is_Used_Color(UINT nofr, UINT8 nocol)
{
//if (nofr >= MycRom.nFrames) return false;
// check if a color is used in a frame cFrame or dynamasks
for (UINT ti = 0; ti < MycRom.fWidth * MycRom.fHeight; ti++)
{
UINT8 quelset = MycRom.DynaMasks[nofr * MycRom.fWidth * MycRom.fHeight + ti];
if (quelset == 255)
{
if (MycRom.cFrames[nofr * MycRom.fWidth * MycRom.fHeight + ti] == nocol) return true;
}