-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin32_geometer.c
1303 lines (1161 loc) · 40.2 KB
/
win32_geometer.c
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
#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#include <stdlib.h>
#include <windows.h>
#include <mmsystem.h>
#if SINGLE_EXECUTABLE
#include "geometer.c"
#else // SINGLE_EXECUTABLE
#include "geometer.h"
#endif // SINGLE_EXECUTABLE
#define USING_INPUT
#include <fonts.c>
#include <win32.h>
#include <live_edit/win32_live_edit.h>
#include "svg.h"
#define OGL_MSAA 8
#include <opengl/opengl.h>
#include <opengl/opengl_primitives.h>
/* #include "geometer_templibs.h" */
#if INTERNAL
#include <stdio.h>
#endif // INTERNAL
global_variable b32 GlobalRunning;
global_variable b32 GlobalPause;
global_variable f32 GlobalStroke = 2.5f;
global_variable WINDOWPLACEMENT GlobalWindowPosition = {sizeof(GlobalWindowPosition)};
#include <win32_gfx.h>
#include <win32_input.h>
char *STDC_VERSION = STR(__STDC_VERSION__);
typedef UPDATE_AND_RENDER(update_and_render);
typedef enum cursor_type
{
CURSOR_Normal = 0,
CURSOR_Basis,
CURSOR_Pan,
CURSOR_Select,
CURSOR_Draw,
CURSOR_Count
} cursor_type;
internal inline void
MemErrorOnFail(HWND WindowHandle, b32 Success)
{
if(!Success) MessageBox(WindowHandle, "Unable to allocate more memory. Save and quit.", "Memory Allocation Error", MB_ICONERROR);
}
#define FileErrorOnFail(hwnd, file, path) FileErrorOnFail_(hwnd, file, path, __func__, __LINE__)
internal inline void
FileErrorOnFail_(HWND WindowHandle, FILE *File, char *FilePath, char *FuncName, uint Line)
{
char Buf[1024];
ssnprintf(Buf, 1024, "%s(%u): Unable to open file at %s", FuncName, Line, FilePath);
if(!File) MessageBox(WindowHandle, Buf, "File Opening Error", MB_ICONERROR);
}
internal inline void
ChangeFilePath(state *State, char *FilePath, uint cchFilePath)
{
free(State->FilePath);
State->FilePath = FilePath;
State->cchFilePath = cchFilePath;
}
internal inline b32
FileHasName(state *State)
{
b32 Result = State->FilePath[0];
return Result;
}
internal inline b32
IsModified(state *State)
{
b32 Result = State->iCurrentAction != State->iSaveAction;
return Result;
}
internal OPENFILENAME
OpenFilenameDefault(HWND OwnerWindow, uint cchFilePath)
{
cchFilePath = CeilPow2(cchFilePath);
char *FilePath = calloc(cchFilePath, 1);
Assert(FilePath);
OPENFILENAME File = {0};
File.lStructSize = sizeof(OPENFILENAME);
File.hwndOwner = OwnerWindow;
// NOTE: Dialog box template?:
// File.hInstance;
File.lpstrFilter = "Geometer files (*.geo)\0*.geo\0" "All Files\0*.*\0";
/* lpstrCustomFilter; */
/* nMaxCustFilter; */
/* File.nFilterIndex; */
File.lpstrFile = FilePath;
File.nMaxFile = cchFilePath;
// NOTE: without path info
/* File.lpstrFileTitle; */
/* File.nMaxFileTitle; */
/* File.lpstrInitialDir; */
// NOTE: dialog box title
/* File.lpstrTitle; */
File.Flags = OFN_OVERWRITEPROMPT;// | OFN_EXPLORER;
/* File.nFileOffset; */
/* File.nFileExtension; */
File.lpstrDefExt = "geo";
/* File.lCustData; */
/* File.lpfnHook; */
/* File.lpTemplateName; */
return File;
}
internal inline size_t
ArenaAllocSize(u32 cElements, size_t ElementSize, u32 Factor, u32 Add)
{
size_t cBytesEl = (cElements + 1) * ElementSize; // Account for index 0
size_t Result = CeilPow2U64(Factor * cBytesEl + Add * ElementSize);
if(Result < ElementSize * cSTART_POINTS)
{ Result = ElementSize * cSTART_POINTS; }
return Result;
}
internal inline u64
ReadFileArrayToArena(FILE *File, memory_arena *Arena, u32 cElements, u32 ElementSize, u32 Factor, u32 Add)
{
size_t cBytesEl = cElements * ElementSize;
MemErrorOnFail(0, ArenaRealloc(Arena, ArenaAllocSize(cElements, ElementSize, Factor, Add)));
Arena->Used = cBytesEl + ElementSize; // Account for index 0
// TODO: check individual array size is right
Assert(Arena->Base);
size_t cElCheck = fread(Arena->Bytes + ElementSize, ElementSize, cElements, File);
Assert(cElements == cElCheck);
return cBytesEl;
}
internal inline u32
CRC32FileArray(u32 Continuation, u32 Tag, u32 Count, void *Array, size_t ElSize)
{
u32 Result = Continuation;
Result = CRC32(&Tag, sizeof(Tag), Result);
Result = CRC32(&Count, sizeof(Count), Result);
Result = CRC32(Array, Count * ElSize, Result);
return Result;
}
internal FILE *
OpenFileInCurrentWindow(state *State, char *FilePath, uint cchFilePath, HWND WindowHandle)
{
FILE *File = 0;
void *FileContents = 0;
if(FilePath)
{
File = fopen(FilePath, "r+b");
// TODO: handle nonexistant file names
FileErrorOnFail(WindowHandle, File, FilePath);
file_header FH;
LOG("\tCHECK ID");
DoAssert(fread(&FH, sizeof(FH), 1, File));
if(!(FH.ID[0]=='G'&&FH.ID[1]=='e'&&FH.ID[2]=='o'&&FH.ID[3]=='m'&&
FH.ID[4]=='e'&&FH.ID[5]=='t'&&FH.ID[6]=='e'&&FH.ID[7]=='r'))
{ goto open_error; }
// all in from now?
ChangeFilePath(State, FilePath, cchFilePath);
State->iCurrentAction = 0;
State->iLastAction = 0;
FileContents = malloc(FH.cBytes);
// TODO (ui): better indicate to user
u64 cBytesCheck = fread(FileContents, 1, FH.cBytes, File);
#define FileDataWarning(desc, title) MessageBox(WindowHandle, desc \
"\nThe file might be corrupted, or Geometer might have made an error.\n\n" \
"If your file looks correct you can continue, " \
"but I advise you to back up the existing file before saving over it " \
"(e.g. copy and paste the file in 'My Computer').", title, MB_ICONWARNING)
if (cBytesCheck < FH.cBytes) { FileDataWarning("There was less data in the file than expected.", "Unexpected quantity of data"); }
else if(cBytesCheck > FH.cBytes) { FileDataWarning("There was more data in the file than expected.", "Unexpected quantity of data"); }
u32 OpenCRC32 = CRC32(FileContents, FH.cBytes, 0);
if(OpenCRC32 != FH.CRC32) { FileDataWarning( "The validity check (CRC32) for opening this file failed." , "CRC32 check failed"); }
// TODO: do I want to continue if this fails?
b8 TypesFoundInFile[HEAD_Count] = {0};
switch(FH.FormatVersion)
{
case 1:
{
u32 ElType;
u32 cElements;
u8 *At = FileContents;
u8 *FinalAt = At + FH.cBytes;
for(uint iArray = 0; iArray < FH.cArrays; ++iArray)
{
ElType = *(u32 *)At; At += sizeof(ElType);
cElements = *(u32 *)At; At += sizeof(cElements);
TypesFoundInFile[ElType] = 1;
switch(ElType)
{
#define FILE_HEADER_ELEMENT(ID) \
size_t cBytesEl = cElements * HeaderElSizes[ElType]; \
void *Els = (void *)At; \
At += cBytesEl; \
if(At > FinalAt) FileDataWarning("Unexpected quantity of data or corrupted file. ("#ID")", "Unexpected quantity of data")
#define FILE_HEADER_ARENA(ID, mult, add) \
FILE_HEADER_ELEMENT(ID); \
MemErrorOnFail(0, ArenaRealloc(&State->ma## ID.Arena, ArenaAllocSize(cElements, sizeof(*State->ma## ID.Items), mult, add))); \
State->ma## ID.Used = (cElements + 1) * sizeof(*State->ma## ID.Items) /* Account for index 0 */
// Arenas
case HEAD_Points_v1:
{
FILE_HEADER_ARENA(Points, 2, 0);
for(u32 iEl = 0; iEl < cElements; ++iEl)
{ Pull(State->maPoints, iEl + 1) = ((v2 *)Els)[iEl]; }
} break;
case HEAD_PointStatus_v1:
{
FILE_HEADER_ARENA(PointStatus, 2, 0);
for(u32 iEl = 0; iEl < cElements; ++iEl)
{
Pull(State->maPointStatus, iEl + 1) = !!((u8 *)Els)[iEl];
}
} break;
case HEAD_Shapes_v1:
{
FILE_HEADER_ARENA(Shapes, 1, 2);
for(u32 iEl = 0; iEl < cElements; ++iEl)
{ Pull(State->maShapes, iEl + 1) = ((shape *)Els)[iEl]; }
} break;
case HEAD_Actions_v1:
case HEAD_Actions_v2:
{
FILE_HEADER_ARENA(Actions, 2, 0);
for(u32 iEl = 0; iEl < cElements; ++iEl)
{ // transfer the elements to state
action_v1 Action_v1 = {0};
action_v2 Action_v2 = {0};
switch(ElType)
{ // update type to most recent version
case HEAD_Actions_v1:
Action_v1 = ((action_v1 *)Els)[iEl];
Action_v2.Kind = Action_v1.Kind;
switch(USERIFY_ACTION(Action_v1.Kind)) // TODO (opt): Could make into a function ActionV1ToV2 - slightly more readable
{ // convert action_v1 to action_v2
case ACTION_Reset:
Action_v2.Reset.i = Action_v1.Reset.i;
Action_v2.Reset.cPoints = Action_v1.Reset.cPoints;
Action_v2.Reset.cShapes = Action_v1.Reset.cShapes;
break;
case ACTION_RemoveShape:
case ACTION_Segment:
case ACTION_Circle:
case ACTION_Arc:
Action_v2.Shape.i = Action_v1.Shape.i;
Action_v2.Shape.AllPoints = Action_v1.Shape.AllPoints;
Action_v2.Shape.iLayer = 1;
break;
case ACTION_Point:
case ACTION_RemovePt:
Action_v2.Point.ipo = Action_v1.Point.ipo;
Action_v2.Point.po = Action_v1.Point.po;
Action_v2.Point.iLayer = 1;
break;
default:
Assert(! "Trying to load unknown action type");
} // fallthrough
case HEAD_Actions_v2:
if(ElType == HEAD_Actions_v2) // only if directly switched to
{ Action_v2 = ((action_v2 *)Els)[iEl]; } // TODO (opt): maybe slightly faster with an unconditional jump
}
Pull(State->maActions, iEl + 1) = Action_v2;
}
} break;
// Arrays/Singles
case HEAD_Lengths_v1:
{
FILE_HEADER_ELEMENT(Lengths);
u32 NumberOfLengthsStored = cElements;
Assert(NumberOfLengthsStored == 26);
for(u32 iEl = 0; iEl < cElements; ++iEl)
{ State->LengthStores[iEl] = ((f32 *)Els)[iEl]; }
} break;
case HEAD_Basis_v1:
{
FILE_HEADER_ELEMENT(Basis);
Assert(cElements == 1);
State->Basis = DecompressBasis(*(compressed_basis *)Els);
} break;
case HEAD_Basis_v2:
{
FILE_HEADER_ELEMENT(Basis);
Assert(cElements == 1);
State->Basis = *(basis_v2 *)Els;
} break;
case HEAD_PointLayer_v1:
{
FILE_HEADER_ARENA(PointLayer, 2, 0);
for(u32 iEl = 0; iEl < cElements; ++iEl)
{
Pull(State->maPointLayer, iEl + 1) = ((uint *)Els)[iEl];
}
} break;
default:
{
// NOTE: unknown tag
MessageBox(WindowHandle, "Unexpected data or corrupted file. Try again.", "Filetype Error", MB_ICONERROR);
Assert(! "Unexpected data or corrupted file.");
ElType;
File = 0;
goto open_end;
} break;
#undef FILE_HEADER_ARENA
#undef FILE_HEADER_ARRAY
}
}
} break;
default: { goto open_error; }
}
u32 cPoints = (u32) Len(State->maPoints) - 1;
if(! TypesFoundInFile[HEAD_PointLayer_v1])
{
MemErrorOnFail(0, ArenaRealloc(&State->maPointLayer.Arena,
ArenaAllocSize(cPoints, sizeof(*State->maPointLayer.Items), 2, 0)));
State->maPointLayer.Used = (cPoints + 1) * sizeof(*State->maPointLayer.Items); /* Account for index 0 */
foreach(u32, Point, State->maPointLayer)
{ Pull(State->maPointLayer, iPoint) = 1; }
}
if(! TypesFoundInFile[HEAD_PointStatus_v1])
{
MemErrorOnFail(0, ArenaRealloc(&State->maPointStatus.Arena,
ArenaAllocSize(cPoints, sizeof(*State->maPointStatus.Items), 2, 0)));
State->maPointStatus.Used = (cPoints + 1) * sizeof(*State->maPointStatus.Items); /* Account for index 0 */
foreach(u8, Point, State->maPointStatus)
{ Pull(State->maPointStatus, iPoint) = 1; }
}
// fclose?
State->iLastPoint = (uint)Len(State->maPoints) - 1;
State->iLastShape = (uint)Len(State->maShapes) - 1;
State->iLastAction = (uint)Len(State->maActions) - 1;
State->iCurrentAction = State->iSaveAction = State->iLastAction;
uint cIntersects = CountShapeIntersects(State->maPoints.Items, State->maShapes.Items + 1, State->iLastShape);
MemErrorOnFail(0, ArenaRealloc(&State->maIntersects.Arena, ArenaAllocSize(cIntersects, sizeof(v2), 2, 0)));
RecalcAllIntersects(State);
#undef FileDataWarning
}
open_end:
Free(FileContents);
// TODO (rm): State->OpenFile = 0;
return File;
open_error:
MessageBox(WindowHandle, "Wrong filetype or corrupted file. Try again.\n\n"
"The filetype may be more recent than this version of Geometer can handle. "
"If so, please download and try opening this with the latest version.", "Filetype Error", MB_ICONERROR);
fclose(File);
File = 0;
goto open_end;
}
// TODO: error checking
internal u32
SaveToFile(state *State, HWND WindowHandle, char *FilePath)
{
BEGIN_TIMED_BLOCK;
#define PROCESS_DATA_ARRAY() \
DATA_PROCESS(HEAD_PointStatus_v1, State->iLastPoint, State->maPointStatus.Items + 1) \
DATA_PROCESS(HEAD_Points, State->iLastPoint, State->maPoints.Items + 1) \
DATA_PROCESS(HEAD_Shapes, State->iLastShape, State->maShapes.Items + 1) \
DATA_PROCESS(HEAD_Actions, State->iLastAction, State->maActions.Items + 1) \
DATA_PROCESS(HEAD_Lengths, cLengthStores, State->LengthStores) \
DATA_PROCESS(HEAD_Basis, One, &State->Basis) \
DATA_PROCESS(HEAD_PointLayer, State->iLastPoint, State->maPointLayer.Items + 1) \
// elementType cElements arraybase
#define DATA_PROCESS(a, b, c) +1
FILE *SaveFile = fopen(FilePath, "wb");
FileErrorOnFail(WindowHandle, SaveFile, FilePath);
file_header Header = {0};
Header.ID[0] = 'G';
Header.ID[1] = 'e';
Header.ID[2] = 'o';
Header.ID[3] = 'm';
Header.ID[4] = 'e';
Header.ID[5] = 't';
Header.ID[6] = 'e';
Header.ID[7] = 'r';
Header.FormatVersion = 1;
Header.cArrays = PROCESS_DATA_ARRAY(); // IMPORTANT: Keep updated when adding new arrays!
Header.CRC32 = 0; // edited in following macros
Header.cBytes = 0; // edited in following macros
Assert(Header.cArrays == 7);
#undef DATA_PROCESS
u32 Tag;
u32 One = 1; // To make it addressable
u32 cLengthStores = ArrayCount(State->LengthStores);
// CRC processing and byte count
#define DATA_PROCESS(tag, count, arraybase) \
Header.CRC32 = CRC32FileArray(Header.CRC32, tag, count, arraybase, sizeof(*(arraybase))); \
Header.cBytes += 2*sizeof(u32) + (count)*sizeof(*(arraybase));
PROCESS_DATA_ARRAY();
#undef DATA_PROCESS
// Write header to file
fwrite(&Header, sizeof(Header), 1, SaveFile);
// Write data to file
#define DATA_PROCESS(tag, count, arraybase) \
Tag = tag; \
fwrite(&Tag, sizeof(Tag), 1, SaveFile); \
fwrite(&count, sizeof(count), 1, SaveFile); \
fwrite((arraybase), sizeof((arraybase)[0]), count, SaveFile);
PROCESS_DATA_ARRAY();
#undef DATA_PROCESS
// TODO: use checksum to ensure written properly?
fclose(SaveFile);
END_TIMED_BLOCK;
return Header.CRC32;
}
internal void
ReallocateArenas(state *State, HWND WindowHandle)
{ LOG("REALLOCATION")
#define ArenaAssert(arena) Assert(arena->Used <= arena->Size)
// TODO: what to do if reallocation fails? Ensure no more shapes/points etc; Error message box: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx
v2_arena *maPoints = &State->maPoints;
u8_arena *maPointStatus = &State->maPointStatus;
v2_arena *maPointsOnScreen = &State->maPointsOnScreen;
v2_arena *maIntersects = &State->maIntersects;
uint_arena *maPointLayer = &State->maPointLayer;
uint_arena *maSelectedPoints = &State->maSelectedPoints;
shape_arena *maShapes = &State->maShapes;
shape_arena *maShapesNearScreen = &State->maShapesNearScreen;
action_arena *maActions = &State->maActions;
// NOTE: Can add multiple points per frame (intersections), but can't double
// NOTE: Realloc the next undo state if needed
ArenaAssert(maPoints);
ArenaAssert(maPointLayer);
ArenaAssert(maPointsOnScreen);
if(maPoints->Used >= maPoints->Size / 2)
{ LOG("Adding to points arena");
// NOTE: should all have exactly the same number of members
Assert(Len(*maPointLayer) == Len(*maPoints));
Assert(Cap(*maPointsOnScreen) == Cap(*maPoints));
Assert(Cap(*maSelectedPoints) == Cap(*maPoints));
MemErrorOnFail(WindowHandle, ArenaRealloc(&maPoints->Arena, maPoints->Size * 2));
MemErrorOnFail(WindowHandle, ArenaRealloc(&maPointStatus->Arena, maPointStatus->Size * 2));
MemErrorOnFail(WindowHandle, ArenaRealloc(&maPointLayer->Arena, maPointLayer->Size * 2));
MemErrorOnFail(WindowHandle, ArenaRealloc(&maPointsOnScreen->Arena, maPointsOnScreen->Size * 2));
MemErrorOnFail(WindowHandle, ArenaRealloc(&maSelectedPoints->Arena, maSelectedPoints->Size * 2));
}
// NOTE: Can only create 1 shape per frame
ArenaAssert(maShapes);
ArenaAssert(maShapesNearScreen);
ArenaAssert(maIntersects);
if(maIntersects->Used >= maIntersects->Size / 2)
{ MemErrorOnFail(WindowHandle, ArenaRealloc(&maIntersects->Arena, maIntersects->Size * 2)); }
if(maShapes->Used >= maShapes->Size)
{ LOG("Adding to shapes arena");
MemErrorOnFail(WindowHandle, ArenaRealloc(&maShapes->Arena, maShapes->Size * 2));
MemErrorOnFail(WindowHandle, ArenaRealloc(&maShapesNearScreen->Arena, maShapesNearScreen->Size * 2));
}
ArenaAssert(maActions);
// TODO: this will change once action = user action
if(maActions->Used >= maActions->Size/2 - sizeof(action))
{ LOG("Adding to actions arena");
MemErrorOnFail(WindowHandle, ArenaRealloc(&maActions->Arena, maActions->Size * 2));
}
#undef ArenaAssert
}
/// Open a new geometer window.
/// If FilePath is an empty string (""), open a blank file,
/// otherwise try to open the FilePath given.
/// Returns 0 on failure.
internal b32
Win32OpenGeometerWindow(char *FilePath)
{
size_t PathLen;
char *EXEPath = Win32GetEXEPath(&PathLen);
size_t FileLen = strlen(FilePath);
// TODO (opt): move to string pool / better string system
size_t SysLen = sizeof("start") + PathLen + FileLen + 2; // extra space and \0
char *SysCall = malloc(SysLen);
ssnprintf(SysCall, (int)SysLen, "start %s %s", EXEPath, FilePath);
// start call returns 0 on success
b32 Result = ! system(SysCall);
free(EXEPath);
free(SysCall);
return Result;
}
internal void
Save(state *State, HWND WindowHandle, b32 SaveAs)
{
BEGIN_TIMED_BLOCK;
char *SavePath = State->FilePath;
b32 Unnamed = ! FileHasName(State);
b32 ContinueSave = 1;
if(SaveAs || Unnamed) // explicitly requested or no current name
{
OPENFILENAME File = OpenFilenameDefault(WindowHandle, State->cchFilePath);
char *DialogTitle = SaveAs ? "Save as and open in new window" : 0;
SavePath = Win32GetSaveFilename(WindowHandle, &File, DialogTitle);
if(SavePath)
{ // for SaveAs, the new window will have the new file path
if(Unnamed && ! SaveAs)
{ ChangeFilePath(State, SavePath, File.nMaxFile); }
}
else
{
LOG("FILEPATH NOT FOUND");
ContinueSave = 0;
}
}
if(ContinueSave)
{
SaveToFile(State, WindowHandle, SavePath);
if(SaveAs)
{ // open the file just saved in a new window
Win32OpenGeometerWindow(SavePath);
free(SavePath); // only needed temporarily,
// ...as that file is no longer relevant after save
}
else
{
State->iSaveAction = State->iCurrentAction;
}
}
END_TIMED_BLOCK;
}
/// Confirms with user that they want to close if the file has been modified.
/// Saves if user wants to.
/// Returns 1 to continue with close or 0 to not close.
internal b32
Win32ConfirmFileClose(state *State, HWND WindowHandle)
{
b32 Result = 1;
while(IsModified(State))
{
uint ButtonResponse = MessageBox(WindowHandle, "Your file has been modified since you last saved.\n"
"Would you like to save before closing?", "Save Changes?", MB_YESNOCANCEL | MB_ICONWARNING);
if(ButtonResponse == IDYES)
{ // Save and confirm close
Save(State, WindowHandle, 0);
// loop back to check Modified has been set to 0
}
else if(ButtonResponse == IDNO)
{ // don't save and confirm close
break;
}
else // cancelled/unknown response
{ // don't close, return to program
Result = 0;
break;
}
}
return Result;
}
internal b32
ArcMoreThanSemiCircle(v2 poFocus, v2 poStart, v2 poEnd)
{
v2 DirStart = V2Sub(poStart, poFocus);
v2 DirEnd = V2Sub(poEnd, poFocus);
b32 Result = IsCCW(DirStart, DirEnd) ? 0 : 1;
return Result;
}
internal v2
CanvasToSVG(v2 P, aabb AABB)
{
v2 Result;
f32 Border = 10.f;
AABB.MinX -= Border;
AABB.MinY -= Border;
AABB.MaxX += Border;
AABB.MaxY += Border;
/* f32 Height = AABB.MaxY - AABB.MinY; */
// TODO (feature): scale to apparent zoom level? start at INITIAL_ZOOM of 1?
Result.X = P.X - AABB.MinX; // * invINITIAL_ZOOM;
Result.Y = AABB.MaxY - P.Y; // * invINITIAL_ZOOM;
return Result;
}
internal uint
FirstValidShape(state *State)
{
uint iFirstValidShape = 0;
for(uint iShape = 1; iShape <= State->iLastShape; ++iShape)
{
shape Shape = Pull(State->maShapes, iShape);
if(Shape.Kind != SHAPE_Free)
{
iFirstValidShape = iShape;
break;
}
}
return iFirstValidShape;
}
internal aabb
AABBOfAllShapes(v2 *Points, shape *Shapes, uint iFirstValidShape, uint iLastShape)
{
Assert(iFirstValidShape);
shape Shape = Shapes[iFirstValidShape];
aabb Result = AABBFromShape(Points, Shape);
for(uint iShape = iFirstValidShape + 1; iShape <= iLastShape; ++iShape)
{
Shape = Shapes[iShape];
aabb AABB = AABBFromShape(Points, Shape);
Result = AABBExpand(Result, AABB);
}
return Result;
}
internal void
ExportSVGToFile(state *State, char *FilePath)
{
v2 *Points = State->maPoints.Items;
shape *Shapes = State->maShapes.Items;
uint iLastShape = State->iLastShape;
uint iFirstValidShape = FirstValidShape(State);
if(iFirstValidShape)
{
aabb TotalAABB = AABBOfAllShapes(Points, Shapes, iFirstValidShape, iLastShape);
// TODO: have better automatic sizing for export
#if 0
// NOTE: svg is top down. I'm assuming bottom-up.
v2 Min = CanvasToSVG(V2(TotalAABB.MinX, TotalAABB.MaxY), TotalAABB);
v2 Max = CanvasToSVG(V2(TotalAABB.MaxX, TotalAABB.MinY), TotalAABB);
aabb BorderAABB = { Min.X, Min.Y, Max.X, Max.Y };
#endif
FILE *SVGFile = NewSVG(FilePath, "fill='none' stroke-width='1' stroke='black' stroke-linecap='round'");
/* AABBWidth(TotalAABB), AABBHeight(TotalAABB)); */
for(uint iShape = iFirstValidShape; iShape <= iLastShape; ++iShape)
{
shape Shape = Shapes[iShape];
if(Shape.Kind != SHAPE_Free)
{
/* SVGRect(SVGFile, StrokeWidth, AABB.MinX, AABB.MinY, AABB.MaxX-AABB.MinX, AABB.MaxY-AABB.MinY); */
switch(Shape.Kind)
{
case SHAPE_Circle:
{
circle Circle = Shape.Circle;
v2 poFocus = CanvasToSVG(Points[Circle.ipoFocus], TotalAABB);
v2 poRadius = CanvasToSVG(Points[Circle.ipoRadius], TotalAABB);
f32 Radius = Dist(poFocus, poRadius);
SVGCircle(SVGFile, poFocus.X, poFocus.Y, Radius);
} break;
case SHAPE_Arc:
{
arc Arc = Shape.Arc;
v2 poFocus = Points[Arc.ipoFocus];
v2 poStart = Points[Arc.ipoStart];
v2 poEnd = Points[Arc.ipoEnd];
f32 Radius = Dist(poFocus, poStart);
b32 LargeArc = ArcMoreThanSemiCircle(poFocus, poStart, poEnd);
poFocus = CanvasToSVG(poFocus, TotalAABB);
poStart = CanvasToSVG(poStart, TotalAABB);
poEnd = CanvasToSVG(poEnd, TotalAABB);
SVGArc(SVGFile, Radius, poStart.X, poStart.Y, poEnd.X, poEnd.Y, LargeArc);
} break;
case SHAPE_Segment:
{
line Line = Shape.Line;
v2 po1 = CanvasToSVG(Points[Line.P1], TotalAABB);
v2 po2 = CanvasToSVG(Points[Line.P2], TotalAABB);
SVGLine(SVGFile, po1.X, po1.Y, po2.X, po2.Y);
} break;
default:
{
Assert(! "not sure what shape is at index " && iShape);
}
}
}
}
/* SVGRect( AABB.xMin, AABB.yMin, AABB.xMax-AABB.xMin, AABB.yMax-AABB.yMin ); */
int CloseStatus = EndSVG(SVGFile);
Assert(CloseStatus == 0);
}
}
internal void
ExportSVG(state *State, HWND WindowHandle)
{
BEGIN_TIMED_BLOCK;
// TODO (fix): seems to set the title of the window
OPENFILENAME File = OpenFilenameDefault(WindowHandle, State->cchFilePath);
File.lpstrFilter = "Vector graphics (*.svg)\0*.svg\0" "All Files\0*.*\0";
char *DialogTitle = "Export SVG";
// malloc'd:
char *ExportPath = Win32GetSaveFilename(WindowHandle, &File, DialogTitle);
if(ExportPath)
{
ExportSVGToFile(State, ExportPath);
free(ExportPath);
}
// else has been cancelled (or an error message has already been presented)
END_TIMED_BLOCK;
}
internal void
FreeStateArenas(state *State)
{
Free(State->maPoints.Base);
Free(State->maPointLayer.Base);
Free(State->maPointStatus.Base);
Free(State->maShapes.Base);
Free(State->maActions.Base);
Free(State->maIntersects.Base);
Free(State->maPointsOnScreen.Base);
Free(State->maShapesNearScreen.Base);
}
internal void
AllocStateArenas(state *State)
{
State->maPointLayer.Arena = ArenaCalloc(sizeof(*State->maPointLayer.Items ) * cSTART_POINTS);
State->maPointStatus.Arena = ArenaCalloc(sizeof(*State->maPointLayer.Items ) * cSTART_POINTS);
State->maPoints.Arena = ArenaCalloc(sizeof(*State->maPoints.Items ) * cSTART_POINTS);
State->maShapes.Arena = ArenaCalloc(sizeof(*State->maShapes.Items ) * cSTART_POINTS);
State->maActions.Arena = ArenaCalloc(sizeof(*State->maActions.Items ) * cSTART_POINTS);
State->maIntersects.Arena = ArenaCalloc(sizeof(*State->maIntersects.Items ) * cSTART_POINTS);
State->maPointsOnScreen.Arena = ArenaCalloc(sizeof(*State->maPointsOnScreen.Items ) * cSTART_POINTS);
State->maSelectedPoints.Arena = ArenaCalloc(sizeof(*State->maSelectedPoints.Items ) * cSTART_POINTS);
State->maShapesNearScreen.Arena = ArenaCalloc(sizeof(*State->maShapesNearScreen.Items) * cSTART_POINTS);
}
internal void
HardReset(state *State, FILE *OpenFile)
{
if(OpenFile) { fclose(OpenFile); }
FreeStateArenas(State);
Free(State->FilePath);
state NewState = {0};
AllocStateArenas(&NewState);
ChangeFilePath(&NewState, calloc(1, 1), 1); // 1 byte set to 0 (empty string)
NewState.DefaultFont = State->DefaultFont;
NewState.maActions.Used = sizeof(action);
ResetNoAction(&NewState, 0);
*State = NewState;
}
internal FN(OpenGL, DrawCircleLine)
{
GLStrokeWidth(Draw->StrokeWidth);
glColor4f(Colour.R, Colour.G, Colour.B, Colour.A);
GLCircleLine(Centre, Radius);
}
internal FN(OpenGL, DrawCircleFill)
{
(void)Draw;
glColor4f(Colour.R, Colour.G, Colour.B, Colour.A);
GLCircleFill(Centre, Radius);
}
// TODO: other smears
internal FN(OpenGL, DrawCircleLineSmear)
{
colour StartColour = BLANK_COLOUR;
colour EndColour = PreMultiplyColour(Colour, 0.3f);
GLCircleLinearSmear(Centre, Radius, Draw->Smear, StartColour, EndColour);
}
internal FN(OpenGL, DrawArcLine)
{
GLStrokeWidth(Draw->StrokeWidth);
glColor4f(Colour.R, Colour.G, Colour.B, Colour.A);
GLArcCap(Centre, Radius, A, B);
}
internal FN(OpenGL, DrawSeg)
{
GLStrokeWidth(Draw->StrokeWidth);
glColor4f(Colour.R, Colour.G, Colour.B, Colour.A);
GLLineCap(Point1, Point2);
}
internal FN(OpenGL, DrawLine)
{
draw_buffer tDraw = *Draw;
GLStrokeWidth(tDraw.StrokeWidth);
glColor4f(Colour.R, Colour.G, Colour.B, Colour.A);
GLFullScreenLine(P, Dir, tDraw.Buffer.Width, tDraw.Buffer.Height);
}
internal FN(OpenGL, DrawRectLine)
{
GLStrokeWidth(Draw->StrokeWidth);
glColor4f(Colour.R,Colour.G, Colour.B, Colour.A);
GLRectMinMaxLine(vMin, vMax);
}
internal FN(OpenGL, DrawRectFill)
{
(void)Draw;
glColor4f(Colour.R,Colour.G, Colour.B, Colour.A);
GLRectMinMaxFill(vMin, vMax);
}
internal FN(OpenGL, ClipBuffer)
{
glScissor((GLint)Offset.X, (GLint)Offset.Y, (GLint)Size.X, (GLint)Size.Y);
return Buffer;
}
internal FN(OpenGL, ClearBuffer)
{
f32 W = (f32)Buffer.Width, H = (f32)Buffer.Height;
glViewport(0, 0, Buffer.Width, Buffer.Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.f, W,
0.f, H,
0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.f, 1.f, 1.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
// Software as well
{ memset(Buffer.Memory, 0x00, Buffer.Width * Buffer.Height * BytesPerPixel); }
}
internal FN(Software, DrawSeg)
{ DEBUGDrawLine(Draw->Buffer, Point1, Point2, Colour); }
internal FN(Software, DrawLine)
{ DrawFullScreenLine(Draw->Buffer, P, Dir, Colour); }
internal FN(Software, DrawCircleLine)
{ CircleLine(Draw->Buffer, Centre, Radius, Colour); }
internal FN(Software, DrawCircleFill)
{ CircleFill(Draw->Buffer, Centre, Radius, Colour); }
internal FN(Software, DrawCircleLineSmear)
{
#if 1
// do nothing
(void)Draw; (void)Radius; (void)Centre; (void)Colour;
#elif 0
// Multiple lines trailing behind
v2 PerpDir = Perp(Smear);
v2 Left = V2Sub(Centre, V2Mag(Radius, PerpDir));
v2 Right = V2Add(Centre, V2Mag(Radius, PerpDir));
for(int i = 0, N = 4; i <= N; ++i) {
f32 Frac = (f32)i / (f32)N;
Frac...
}
#else
DEBUGDrawLine(Draw->Buffer,
// multiple circles
/* int const Max = 4; */
/* image_buffer Buffer = Draw->Buffer; */
/* for(int i = 0; i <= Max; ++i) { */
/* v2 Offset = V2Mult((f32)i/(f32)Max, Smear); */
/* CircleLine(Buffer, V2Add(Centre, Offset), Radius, Colour); */
/* } */
#endif
}
internal FN(Software, DrawArcLine)
{ ArcLine(Draw->Buffer, Centre, Radius, A, B, Colour); }
internal FN(Software, DrawRectLine)
{ DrawRectangleLines(Draw->Buffer, vMin, vMax, Colour); }
internal FN(Software, DrawRectFill)
{ DrawRectangleFilled(Draw->Buffer, vMin, vMax, Colour); }
internal FN(Software, ClipBuffer)
{
Buffer.Memory = GetBufferLocation(Buffer, sizeof(u32), Offset);
Buffer.Width = (i32)Size.X;
Buffer.Height = (i32)Size.Y;
return Buffer;
}
internal FN(Software, ClearBuffer)
{ memset(Buffer.Memory, 0xFF, Buffer.Width * Buffer.Height * BytesPerPixel); }
int CALLBACK
WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode)
{
OPEN_LOG("win32_geometer_log", ".txt");
// UNUSED:
ShowCode; CommandLine; PrevInstance;
win32_window Window;
GlobalRunning = !! Win32BasicWindow(Instance, &Window, 960, 540, "Geometer", "Icon", "IconSmall");
/* Win32SetIcon(Window.Handle, GIcon32, cGIcon32, GIcon16, cGIcon16); */
LOG("ALLOC MEMORY");
#define MemSize (Kilobytes(64))
memory Memory = {0};
// TODO: Track memory use and realloc when necessary
Memory.PermanentStorageSize = MemSize;
Memory.TransientStorageSize = MemSize/2;
Memory.PermanentStorage = VirtualAlloc(0, Memory.PermanentStorageSize + Memory.TransientStorageSize,
MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
Memory.TransientStorage = (u8 *)Memory.PermanentStorage + Memory.PermanentStorageSize;
if(!Memory.PermanentStorage || !Memory.TransientStorage)
{
OutputDebugStringA("Memory not allocated properly");
GlobalRunning = 0;
}
state *State = (state *)Memory.PermanentStorage;
// Should already be cleared to 0, but just to be doubly sure...
state EmptyState = {0};
*State = EmptyState;
LOG("OPEN BLANK FILE");
HardReset(State, 0);
State->cchFilePath = 1024;
State->FilePath = calloc(State->cchFilePath, sizeof(char));
FILE *OpenedFile = 0;
char **argv = __argv;
uint argc = __argc;
if(argc > 1)
{
LOG("OPEN FILENAME");
uint ArgLen = (uint)strlen(argv[1]) + 1;
uint cchFilePath = State->cchFilePath >= ArgLen ? State->cchFilePath : CeilPow2(ArgLen);
char *FilePath = calloc(cchFilePath, 1);
strcpy(FilePath, argv[1]);
OpenedFile = OpenFileInCurrentWindow(State, FilePath, cchFilePath, Window.Handle);
}
ReallocateArenas(State, Window.Handle);
Win32LoadXInput();
// TODO: Pool with bitmap VirtualAlloc and font?
// TODO IMPORTANT: move to thin host layer so win32 can be updated at runtime
#if !SINGLE_EXECUTABLE
char *LibFnNames[] = {"UpdateAndRender"};
win32_library Lib = Win32Library(LibFnNames, ArrayCount(LibFnNames),
0, "geometer.dll", "geometer_temp.dll", "lock.tmp");
#endif // !SINGLE_EXECUTABLE
int ScreenWidth, ScreenHeight; // TODO: change to Window.ScreenWidth;
Win32ScreenResolution(Window.Handle, &ScreenWidth, &ScreenHeight);
win32_image_buffer Win32Buffer = {0};
Win32ResizeDIBSection(&Win32Buffer, ScreenWidth, ScreenHeight);
draw_buffer Draw = {0};
Draw.Buffer = *(image_buffer *) &Win32Buffer;
Draw.StrokeWidth = 2.f;
if(Win32CreateOpenGLContext(Window.Context))
{
Draw.Kind = DRAW_Hardware;
glEnable(GL_SCISSOR_TEST);
GLStrokeWidth(GlobalStroke);
GLEnablePrimitiveSmoothing();