-
Notifications
You must be signed in to change notification settings - Fork 34
/
immediate2d.h
1485 lines (1143 loc) · 48.2 KB
/
immediate2d.h
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
#pragma once
///////////////////////////////////////////////////////////////////////////////
//
// Immediate2D - Created by Nicholas Piegdon
// https://github.com/npiegdon/immediate2d
//
// A drawing framework for Windows that makes simple graphics programming as
// much fun as the days when computers booted directly to a BASIC prompt.
//
// This is free and unencumbered software released into the public domain.
// See the full notice at the end of this file.
//
// CHANGELOG (see CHANGES.md for more detail)
// 2023-01, v2: API cleanup, single file, image loading/drawing, new examples
// 2017-11, v1: Initial release
//
///////////////////////////////////////////////////////////////////////////////
//
// Getting started:
//
// You'll need a recent Visual Studio with the "Desktop development with C++"
// workload selected during the install process.
//
// 1. Launch Visual Studio and choose "Create a new project".
// 2. For the project template, choose "Windows Desktop Wizard" from the list.
// 3. Name the project whatever you like and save it wherever you like.
//
// 4. Choose "Desktop Application (.exe)" from the "Application type" box.
// 5. Add a check mark to the "Empty Project" box!
// 6. Click OK.
//
// 7. Copy the following files from Immediate2D into your project's folder:
// - immediate2d.h
// - example1_helloWorld.cpp
//
// 8. Rename your example1_helloWorld.cpp to something like MyProject.cpp
// 9. Drag both of your new files from the folder to the "Source Files"
// entry in Visual Studio's "Solution Explorer" panel.
//
// You should be ready to go! Click the green play arrow (labeled "Local
// Windows Debugger") in the tool bar and after a few seconds you should see
// a dot in the middle of a black window. Congratulations!
//
// Double-click your MyProject.cpp file (or whatever you named it) in the
// "Source Files" list in the Solution Explorer panel and start writing code!
//
// Getting started without a template:
//
// If you don't want to start from the example file, all you need to do is
// add these two lines to the top of exactly one .cpp file in your project:
//
// #define IMM2D_IMPLEMENTATION
// #include "immediate2d.h"
//
// void run()
// {
// DrawPixel(10, 10, White);
// }
//
// By default, Immediate2D's virtual window is 160 pixels wide and 120 pixels
// tall, where each virtual pixel is scaled by 5x before being drawn.
//
// You can check these values at runtime by using the Width, Height, and
// PixelScale global variables.
//
// To change any of those, use additional #define lines, in the same file
// where you plan to #define IMM2D_IMPLEMENTATION, like this example:
//
// #define IMM2D_WIDTH 640
// #define IMM2D_HEIGHT 480
// #define IMM2D_SCALE 2
//
// #define IMM2D_IMPLEMENTATION
// #include "immediate2d.h"
//
// That would give you a window that was 640x480 where each virtual pixel is
// scaled 2x before being drawn to the screen. All three can be set
// independently. For example, you'd only need one line if you only wanted
// to change the scale.
//
///////////////////////////////////////////////////////////////////////////////
// Color
///////////////////////////////////////////////////////////////////////////////
// This line makes "Color" a synonym for "unsigned int", a positive number.
using Color = unsigned int;
// MakeColor returns a Color that can be used with the other drawing functions.
// The red, green, and blue parameters are color intensities between 0 and 255.
constexpr Color MakeColor(int red, int green, int blue);
// Here are some colors to get you started.
static const Color Transparent = 0U;
static const Color Black = MakeColor( 0, 0, 0);
static const Color Blue = MakeColor( 0, 0, 170);
static const Color Green = MakeColor( 0, 170, 0);
static const Color Cyan = MakeColor( 0, 170, 170);
static const Color Red = MakeColor(170, 0, 0);
static const Color Magenta = MakeColor(170, 0, 170);
static const Color Brown = MakeColor(170, 85, 0);
static const Color LightGray = MakeColor(170, 170, 170);
static const Color DarkGray = MakeColor( 85, 85, 85);
static const Color LightBlue = MakeColor( 85, 85, 170);
static const Color LightGreen = MakeColor( 85, 255, 85);
static const Color LightCyan = MakeColor( 85, 255, 255);
static const Color LightRed = MakeColor(255, 85, 85);
static const Color LightMagenta = MakeColor(255, 85, 255);
static const Color Yellow = MakeColor(255, 255, 85);
static const Color White = MakeColor(255, 255, 255);
// MakeColorHSB is like MakeColor but instead of using red, green, and blue
// values, you get to specify the hue. This is ideal for making rainbows!
//
// hue is an angle (in degrees) between 0 and 360.
// (0 is red, 30 is orange, 60 is yellow, 240 is blue...)
//
// saturation and brightness should be between 0 and 255.
//
// saturation=0 makes everything grayscale.
// saturation=255 will be the most vivid version of that color.
//
// brightness=0 is always black.
// brightness=255 is the brightest version of that color.
//
Color MakeColorHSB(int hue, int saturation, int brightness);
///////////////////////////////////////////////////////////////////////////////
// Math Helpers
///////////////////////////////////////////////////////////////////////////////
// The number of radians in a complete circle (equal to 2*pi).
static constexpr double Tau = 6.283185307179586476925286766559;
// To find out how many radians there are in 45 degrees, call Radians(45).
static constexpr double Radians(double degrees) { return degrees * Tau / 360.0; }
// To find out how many degrees there are in half a circle, call Degrees(Tau / 2).
static constexpr double Degrees(double radians) { return radians * 360.0 / Tau; }
///////////////////////////////////////////////////////////////////////////////
// Drawing
///////////////////////////////////////////////////////////////////////////////
// For all of the following drawing functions, x is between 0 and Width;
// y is between 0 and Height.
//
// In computer graphics, y starts at the top of the screen and goes downward
// as it increases. (This is upside-down from the way it's usually done in math
// classes and can be a little tricky to get used to!)
// Draws a single dot at (x, y) in the given color.
void DrawPixel(int x, int y, Color c);
// Draws a line from (x1, y1) to (x2, y2) with a given stroke thickness
// (in pixels) in the given color.
void DrawLine(int x1, int y1, int x2, int y2, int thickness, Color c);
// Draws a rectangle with upper-left corner at (x, y) with the given width
// and height. The fill color will cause the inside of the rectangle to filled
// with that color. The stroke color will be used to draw a single pixel
// border. To skip drawing either the border or filling the inside, set that
// color to Transparent.
void DrawRectangle(int x, int y, int width, int height, Color fill, Color stroke = Transparent);
// Draws a circle centered at (x, y) with a given radius (in pixels). Specify
// both the fill (inside) and stroke (single pixel border) colors. To skip
// drawing either the stroke or fill, set its color to Transparent.
void DrawCircle(int x, int y, int radius, Color fill, Color stroke = Transparent);
// Draws a piece of a circle centered at (x, y) with the given stroke thickness.
// The piece of the circle you get is determined by the start and end radians.
// Zero radians is at the right-hand side of the circle and they increase upward.
// 0 for startRadians and Tau/2 for endRadians would draw the top half of a circle.
void DrawArc(int x, int y, float radius, float thickness, Color stroke, float startRadians, float endRadians);
// Prints text at (x, y) using the given font and size (in points). It is normally
// left-justified but can be horizontally centered on x by passing true for centered.
void DrawString(int x, int y, const char *text, const char *fontName, int fontSizePt, const Color c, bool centered = false);
// Clears the screen to the given color (or Black if no color passed in).
void Clear(Color c = Black);
// Retrieves the color at the given location. Test against specific colors:
// if (ReadPixel(playerX, playerY) == Black) ...
//
Color ReadPixel(int x, int y);
// This makes "Image" a synonym for "int", a positive or negative number.
using Image = int;
const static Image InvalidImage = -1;
// Attempts to load and return an Image handle that can be used
// with DrawImage. Many image file extensions are supported.
//
// To find the image you requested, the following locations are
// searched, in this order:
// - Base64-encoded image data (any file format) passed in directly.
// - Resources embedded inside the app's .exe file.
// - Files in the current working directory.
//
// If the image wasn't found or there was some other problem loading it,
// this function will return InvalidImage.
//
// Try to call this only once per file you want to load. Keep the returned Image
// value around instead of calling this again and again. Images remain loaded in
// memory until your program ends, so calling this repeatedly for the same image
// (say, inside a loop) will continue using more memory until your app crashes.
Image LoadImage(const char *name);
// Draws an image (obtained using LoadImage) with its top-left corner at the
// provided (x, y) coordinates at 100% scale.
//
// Image formats with animation frames (like .gif) cycle through their animation
// automatically as time goes on. You just need to call DrawImage periodically
// so no frames are missed.
void DrawImage(int x, int y, Image i);
// Retrieves the width and height of an image (obtained using LoadImage).
int ImageWidth(Image i);
int ImageHeight(Image i);
// OPTIONAL! Anti-aliasing is a graphics technique to make your lines and
// circles appear with smooth/soft edges. These can be called at any time
// to change the way the other drawing functions behave.
void UseAntiAliasing();
void StopAntiAliasing();
// OPTIONAL! Things are normally drawn to the screen just as your drawing
// code is run. This works fine until you have very intricate scenes that
// require many drawing commands or when you want to animate something. At
// that point you may not want to see the intermediate drawing steps anymore.
//
// When you enable "double buffering", your drawing will take place on a
// different, hidden surface called the "back-buffer". You can draw as much
// as you like to the hidden back-buffer without any visible changes to the
// screen. Then, after you've composed your entire scene or updated your
// animation, you can "present" the scene all at once.
//
void UseDoubleBuffering(bool enabled);
// OPTIONAL! This isn't normally needed. But, if you've enabled double
// buffering, you MUST Present() whenever you would like to show the results
// of all your drawing code since the last time you called Present().
//
// Tip: If called at regular intervals, you can achieve an animation effect.
//
void Present();
///////////////////////////////////////////////////////////////////////////////
// Utility
///////////////////////////////////////////////////////////////////////////////
// When you are ready to end your program, you may call this to automatically
// close the window, too. If you don't, the window will simply show whatever
// was drawn most recently, forever.
//
// NOTE: If you're NOT ready to end your program, don't call this! As a side-
// effect, your run() function will be terminated immediately.
//
void CloseWindow();
// Saves your current drawing to a PNG image file on your desktop.
//
// Calling SaveImage() will give you a file called "image.png". If you include
// a number like SaveImage(26) then you'll get a file called "image_26.png".
//
void SaveImage(unsigned int suffix = 0);
// Generates a random true/false value (like a coin toss).
//
// This is useful when you need to make a decision:
// if (RandomBool()) goToTheStore();
// else stayHome();
//
bool RandomBool();
// Generates a random integer in the interval [low, high).
//
// This is useful for placing things on the screen:
// int x = RandomInt(0, Width);
// int y = RandomInt(0, Height);
//
// Or for generating random colors:
// Color c = MakeColor(RandomInt(0, 256), RandomInt(0, 256), RandomInt(0, 256));
//
int RandomInt(int low, int high);
// Generates a random "real" number between zero and one.
//
// This is useful whenever you need something that isn't a whole number. You
// could pick a random angle (in radians) around a circle like this:
// double theta = RandomDouble() * Tau;
//
// Or a random angle (in degrees):
// double theta = RandomDouble() * 360.0;
//
double RandomDouble();
// Delays your code for the given number of milliseconds. By drawing, then
// waiting, then drawing something else, you can make things animate.
//
// There are 1000 milliseconds in one second.
//
void Wait(int milliseconds);
///////////////////////////////////////////////////////////////////////////////
// Keyboard Input
///////////////////////////////////////////////////////////////////////////////
// Returns the most recently pressed keyboard character. For special, non-
// printable characters, test against the values in "Keys", below.
//
// NOTE: To avoid reporting the same key press forever, this function only
// returns the value ONCE and will subsequently report a value of zero
// until the next key is pressed. If you need to use this value more
// than once, store the result in a variable first!
//
char LastKey();
// Used to check the results of LastKey() against non-printable keys:
//
// // Save the value in a variable before testing it.
// const char key = LastKey();
// if (key == Enter) ...
// if (key == Left) ...
// if (key == Esc) ...
// if (key == '&') ...
// if (key == 'r') ...
// if (key == ' ') ...
//
enum Keys
{
Left = 17,
Up,
Right,
Down,
Backspace = 8,
Enter = 13,
Esc = 27,
Tab = 9,
};
// Sometimes immediate input (where only the most-recent keypress is available) isn't
// the best fit. If you don't want to miss any input, use this instead of LastKey.
// This is useful when you want to get typing input or fast arrow keys.
//
// Each call will return the next keypress in the stored list and then remove that
// entry from the list. Calling this when no more input is available will return 0.
//
// Up to one hundred key presses can be recorded internally without calling this before
// they'll begin to be lost the same way LastKey loses all but the most recent key.
//
// NOTE: This works completely independently from LastKey. The same input will be
// reported by both functions separately.
//
char LastBufferedKey();
// If you haven't called LastBufferedKey in a while and would like to ignore any input
// that has come in since the last call (or since your program started running), call
// this to wipe out the internal list of recorded input.
void ClearInputBuffer();
///////////////////////////////////////////////////////////////////////////////
// Mouse Input
///////////////////////////////////////////////////////////////////////////////
// In order to make good use of mouse input, you'll want to set up your
// program so that input is checked and drawing is performed repeatedly,
// in a loop. Game programmers usually call this the "game loop".
// Is the mouse button currently being held down?
bool LeftMousePressed();
bool RightMousePressed();
bool MiddleMousePressed();
// Retrieves the current mouse position (relative to the top-left corner of
// the drawing surface), or -1 if the mouse cursor isn't inside our window.
int MouseX();
int MouseY();
///////////////////////////////////////////////////////////////////////////////
// Music!
///////////////////////////////////////////////////////////////////////////////
// Play a music note (or rest) in the background. Notes (or rests) are played
// one by one, back-to-back as they're queued by this function. So you may
// send all the notes for a song and they'll be played in turn with the right
// timing automatically while your code continues to run normally.
//
// noteId 60 is middle C.
// noteId 61 is the C# just above that.
// noteId 62 is the D just above that.
//
// Use noteId 0 for a rest (no sound; just a pause in the music).
//
// noteId 21 is the lowest key on an 88 key piano.
// noteId 108 is the highest key on an 88 key piano.
//
// There are 1000 milliseconds in one second.
//
void PlayMusic(int noteId, int milliseconds);
// Clears all queued notes/rests. This is useful if you need to play
// something more important before your current song has ended.
void ResetMusic();
//
// Everything below this point is Immediate2D's implementation,
// which may be more challenging for beginners to read and follow.
//
extern const int Width;
extern const int Height;
extern const int PixelScale;
#ifdef IMM2D_IMPLEMENTATION
#include <map>
#include <mutex>
#include <deque>
#include <vector>
#include <string>
#include <atomic>
#include <memory>
#include <numeric>
#include <algorithm>
#ifndef IMM2D_WIDTH
#define IMM2D_WIDTH 160
#endif
#ifndef IMM2D_HEIGHT
#define IMM2D_HEIGHT 120
#endif
#ifndef IMM2D_SCALE
#define IMM2D_SCALE 5
#endif
#ifndef IMM2D_WINDOW_TITLE
#define IMM2D_WINDOW_TITLE "Immediate2D"
#endif
const int Width = IMM2D_WIDTH;
const int Height = IMM2D_HEIGHT;
const int PixelScale = IMM2D_SCALE;
// The standard library's min/max algorithms conflict with Microsoft's
// min/max macros, but if you define NOMINMAX, the Windows header omits them.
#define NOMINMAX
// This trims the Windows.h header down to improve compile times
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Shlobj.h>
#include <Shlwapi.h>
// This is all we need that is removed by WIN32_LEAN_AND_MEAN
#include <mmeapi.h>
#include <timeapi.h>
// GDI+ uses the min/max macros, so we have to work around disabling them.
namespace Gdiplus { using std::min; using std::max; }
#include <GdiPlus.h>
// This instructs Visual Studio to add these to the list of libraries we link against
#pragma comment(lib, "Ole32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdiplus.lib")
#pragma comment(lib, "Shell32.lib")
#pragma comment(lib, "Shlwapi.lib")
// The primary user-supplied function that we call on its own thread
extern void run();
//
// Immediate2D internal state
//
static bool imm2d_dirty{ true };
static bool imm2d_doubleBuffered{ false };
static std::atomic<char> imm2d_key{ 0 };
static std::atomic<bool> imm2d_quitting{ false };
static std::atomic<bool> imm2d_musicRunning{ true };
static std::atomic<bool> imm2d_mouseDown[3]{ false, false, false };
static std::atomic<int> imm2d_mouseX{ -1 }, imm2d_mouseY{ -1 };
static std::atomic<ULONGLONG> imm2d_runDuration{ 0 };
static std::mutex imm2d_bitmapLock;
static std::unique_ptr<Gdiplus::Bitmap> imm2d_bitmap, imm2d_bitmapOther;
static std::unique_ptr<Gdiplus::Graphics> imm2d_graphics, imm2d_graphicsOther;
static std::map<std::pair<std::string, int>, std::unique_ptr<Gdiplus::Font>> imm2d_fonts;
static std::mutex imm2d_mediaLock;
static std::vector<std::unique_ptr<Gdiplus::Bitmap>> imm2d_images;
static std::vector<std::pair<int, int>> imm2d_imageSizes;
static std::vector<uint32_t> imm2d_imageFrameCount;
static std::vector<size_t> imm2d_imageFrameStart;
static std::vector<uint32_t> imm2d_imageFrameCumulativeCentiSeconds;
static std::vector<uint32_t> imm2d_imageFrameSumMs;
static std::mutex imm2d_musicLock;
struct Imm2dMusicNote { uint8_t noteId; uint32_t duration; };
static std::deque<Imm2dMusicNote> imm2d_musicQueue;
static std::mutex imm2d_inputLock;
static std::deque<char> imm2d_inputBuffer;
void Present()
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (imm2d_doubleBuffered)
{
// This is more "offscreen composition" than double-buffering. We could probably add a
// an option to avoid this copy if the user really knew that they were going to redraw the
// entire screen each frame. But for now we assume immediate-mode drawing and make sure
// things are consistent instead of flickering each frame if you've got different sets of
// things drawn on each buffer
imm2d_graphicsOther->DrawImage(imm2d_bitmap.get(), 0, 0);
}
std::swap(imm2d_graphics, imm2d_graphicsOther);
std::swap(imm2d_bitmap, imm2d_bitmapOther);
imm2d_dirty = true;
}
void CloseWindow() { imm2d_quitting = true; }
char LastKey() { return imm2d_key.exchange(0); }
void Wait(int milliseconds) { ::Sleep(milliseconds); }
void UseDoubleBuffering(bool enabled)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
imm2d_doubleBuffered = enabled;
imm2d_dirty = true;
}
int MouseX() { return imm2d_mouseX; }
int MouseY() { return imm2d_mouseY; }
bool LeftMousePressed() { return imm2d_mouseDown[0]; }
bool RightMousePressed() { return imm2d_mouseDown[1]; }
bool MiddleMousePressed() { return imm2d_mouseDown[2]; }
static void imm2d_SetDirty()
{
if (imm2d_bitmapLock.try_lock()) throw std::runtime_error("SetDirty must be called while bitmapLock is held.");
if (!imm2d_doubleBuffered) imm2d_dirty = true;
}
static const std::wstring imm2d_ToWide(const char *utf8)
{
const int wlen = ::MultiByteToWideChar(CP_UTF8, 0, utf8, -1, nullptr, 0);
auto buffer = std::make_unique<wchar_t[]>(wlen);
const int success = ::MultiByteToWideChar(CP_UTF8, 0, utf8, -1, buffer.get(), wlen);
return success ? std::wstring(buffer.get()) : std::wstring();
}
char LastBufferedKey()
{
std::lock_guard<std::mutex> lock(imm2d_inputLock);
if (imm2d_inputBuffer.empty()) return 0;
char k = imm2d_inputBuffer.front();
imm2d_inputBuffer.pop_front();
return k;
}
void ClearInputBuffer()
{
std::lock_guard<std::mutex> lock(imm2d_inputLock);
imm2d_inputBuffer.clear();
}
static void imm2d_AddBufferedKey(char c)
{
std::lock_guard<std::mutex> lock(imm2d_inputLock);
// If we're not using this feature, keep list a reasonable size
imm2d_inputBuffer.push_back(c);
while (imm2d_inputBuffer.size() > 100) imm2d_inputBuffer.pop_front();
}
// Nice, fast, reasonably high-quality public-domain PRNG from http://xoroshiro.di.unimi.it/xoroshiro128plus.c
//
// For random coordinates/colors in a tight loop, this outperforms std::mt19937 by a mile
//
uint64_t imm2d_xoroshiro128plus(void) {
static uint64_t s[2] = { 1, static_cast<uint64_t>(time(NULL)) };
auto rotl = [](const uint64_t x, int k) { return (x << k) | (x >> (64 - k)); };
const uint64_t s0 = s[0];
uint64_t s1 = s[1];
const uint64_t result = s0 + s1;
s1 ^= s0;
s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
s[1] = rotl(s1, 36);
return result;
}
int RandomInt(int low, int high)
{
if (high <= low) return low;
return (imm2d_xoroshiro128plus() % (int64_t(high) - int64_t(low))) + low;
}
bool RandomBool()
{
return RandomInt(0, 1) == 1;
}
double RandomDouble()
{
union U { uint64_t i; double d; };
return U{ UINT64_C(0x3FF) << 52 | imm2d_xoroshiro128plus() >> 12 }.d - 1.0;
}
// GDI+ makes us work a little harder before we can save as a particular image type
static CLSID imm2d_GetEncoderClsid(const std::wstring &format)
{
UINT count{}, bytes{};
Gdiplus::GetImageEncodersSize(&count, &bytes);
// Something weird is going on here. The returned size isn't just count*sizeof(ImageCodecInfo).
auto codecs = std::make_unique<uint8_t[]>(bytes);
if (!codecs) return CLSID{};
Gdiplus::GetImageEncoders(count, bytes, reinterpret_cast<Gdiplus::ImageCodecInfo *>(codecs.get()));
for (UINT i = 0; i < count; ++i)
{
const auto &codec = reinterpret_cast<Gdiplus::ImageCodecInfo *>(codecs.get())[i];
if (wcscmp(codec.MimeType, format.c_str()) != 0) continue;
return codec.Clsid;
}
return CLSID{};
}
void SaveImage(unsigned int suffix)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
static std::wstring desktop;
if (desktop.empty())
{
wchar_t *desktopRaw = nullptr;
if (SHGetKnownFolderPath(FOLDERID_Desktop, 0, NULL, &desktopRaw) != S_OK) return;
desktop = desktopRaw;
CoTaskMemFree(desktopRaw);
}
std::wstring path = desktop + std::wstring(L"\\image");
if (suffix > 0) path += L"_" + std::to_wstring(suffix);
path += L".png";
static const CLSID png = imm2d_GetEncoderClsid(L"image/png");
imm2d_bitmap->Save(path.c_str(), &png, NULL);
}
// If your program is going to be writing every pixel every frame, this is a simple optimization
// over issuing Width*Height calls to DrawPixel. Instead of spending time on the thread-safety
// overhead Width*Height times, the entire operation is performed in bulk behind a single lock.
//
// The first element in "screen" is the top-left (0, 0) pixel. The next is (1, 0) and so on,
// wrapping automatically to the next row once you reach (Width - 1, 0) to (0, 1). The passed-
// in screen must have example Width*Height elements!
//
// To create a screen that is the right size (and fill it with a default color):
// std::vector<Color> screen(Width * Height, Black);
//
// To get the array index of a particular pixel:
// int index = y*Width + x;
//
// Which can then be used directly to set a color:
// screen[index] = LightBlue;
//
// And once you've placed every color value:
// Present(screen);
//
void Present(const std::vector<Color> &screen);
Color MakeColor(int r, int g, int b, int a)
{
return ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
}
constexpr Color MakeColor(int r, int g, int b)
{
return 0xFF000000 | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
}
Color MakeColorHSB(int hue, int sat, int val)
{
float h = std::min(1.0f, std::max(0.0f, (hue % 360) / 360.0f));
float s = std::min(1.0f, std::max(0.0f, sat / 255.0f));
float v = std::min(1.0f, std::max(0.0f, val / 255.0f));
if (s == 0)
{
int gray = int(v * 255);
return MakeColor(gray, gray, gray);
}
float var_h = h * 6;
int var_i = int(var_h);
float var_1 = v * (1 - s);
float var_2 = v * (1 - s * (var_h - var_i));
float var_3 = v * (1 - s * (1 - (var_h - var_i)));
float var_r;
float var_g;
float var_b;
switch (var_i)
{
case 0: var_r = v; var_g = var_3; var_b = var_1; break;
case 1: var_r = var_2; var_g = v; var_b = var_1; break;
case 2: var_r = var_1; var_g = v; var_b = var_3; break;
case 3: var_r = var_1; var_g = var_2; var_b = v; break;
case 4: var_r = var_3; var_g = var_1; var_b = v; break;
default: var_r = v; var_g = var_1; var_b = var_2; break;
}
return MakeColor(int(var_r * 255), int(var_g * 255), int(var_b * 255));
}
void imm2d_setAntiAliasing(bool enabled)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
imm2d_graphics->SetSmoothingMode(enabled ? Gdiplus::SmoothingModeAntiAlias : Gdiplus::SmoothingModeNone);
imm2d_graphicsOther->SetSmoothingMode(enabled ? Gdiplus::SmoothingModeAntiAlias : Gdiplus::SmoothingModeNone);
}
void UseAntiAliasing() { imm2d_setAntiAliasing(true); }
void StopAntiAliasing() { imm2d_setAntiAliasing(false); }
void DrawPixel(int x, int y, Color c)
{
if (x < 0 || x >= Width || y < 0 || y >= Height) return;
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
Gdiplus::BitmapData d;
Gdiplus::Rect r(x, y, 1, 1);
imm2d_bitmap->LockBits(&r, Gdiplus::ImageLockModeWrite, imm2d_bitmap->GetPixelFormat(), &d);
*reinterpret_cast<uint32_t *>(d.Scan0) = c;
imm2d_bitmap->UnlockBits(&d);
imm2d_SetDirty();
}
void Present(const std::vector<Color> &screen)
{
if (screen.size() != Width * Height) return;
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
Gdiplus::BitmapData d;
Gdiplus::Rect r(0, 0, Width, Height);
auto &b = imm2d_doubleBuffered ? imm2d_bitmapOther : imm2d_bitmap;
b->LockBits(&r, Gdiplus::ImageLockModeWrite, b->GetPixelFormat(), &d);
auto dstLine = reinterpret_cast<uint32_t *>(d.Scan0);
for (int y = 0; y < Height; ++y)
{
auto srcLine = &screen[Width * y];
memcpy(dstLine, srcLine, Width * sizeof(uint32_t));
dstLine += d.Stride / 4;
}
b->UnlockBits(&d);
imm2d_dirty = true;
}
Color ReadPixel(int x, int y)
{
if (x < 0 || x >= Width || y < 0 || y >= Height) return Black;
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return Black;
Gdiplus::Color c;
imm2d_bitmap->GetPixel(x, y, &c);
return c.GetValue();
}
void DrawLine(int x1, int y1, int x2, int y2, int thickness, Color c)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
Gdiplus::Color color(c);
Gdiplus::Pen p(c, (float)thickness);
p.SetStartCap(Gdiplus::LineCapRound);
p.SetEndCap(Gdiplus::LineCapRound);
imm2d_graphics->DrawLine(&p, x1, y1, x2, y2);
imm2d_SetDirty();
}
void DrawLine(float x1, float y1, float x2, float y2, int thickness, Color c)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
Gdiplus::Color color(c);
Gdiplus::Pen p(c, (float)thickness);
p.SetStartCap(Gdiplus::LineCapRound);
p.SetEndCap(Gdiplus::LineCapRound);
imm2d_graphics->DrawLine(&p, x1, y1, x2, y2);
imm2d_SetDirty();
}
void DrawCircle(int x, int y, int radius, Color fill, Color stroke)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
Gdiplus::Rect r(x - radius, y - radius, radius * 2, radius * 2);
if (fill != Transparent)
{
Gdiplus::SolidBrush brush{ Gdiplus::Color(fill) };
imm2d_graphics->FillEllipse(&brush, r);
}
if (stroke != Transparent)
{
Gdiplus::Pen pen{ Gdiplus::Color(stroke) };
imm2d_graphics->DrawEllipse(&pen, r);
}
imm2d_SetDirty();
}
void DrawCircle(float x, float y, float radius, Color fill, Color stroke)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
Gdiplus::RectF r(x - radius, y - radius, radius * 2, radius * 2);
if (fill != Transparent)
{
Gdiplus::SolidBrush brush{ Gdiplus::Color(fill) };
imm2d_graphics->FillEllipse(&brush, r);
}
if (stroke != Transparent)
{
Gdiplus::Pen pen{ Gdiplus::Color(stroke) };
imm2d_graphics->DrawEllipse(&pen, r);
}
imm2d_SetDirty();
}
void DrawArc(int x, int y, float radius, float thickness, Color c, float startRadians, float endRadians)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
Gdiplus::Color color(c);
Gdiplus::Pen p(c, thickness);
p.SetStartCap(Gdiplus::LineCapRound);
p.SetEndCap(Gdiplus::LineCapRound);
const auto s = static_cast<Gdiplus::REAL>(startRadians * 360.0 / Tau);
const auto e = static_cast<Gdiplus::REAL>(endRadians * 360.0 / Tau);
imm2d_graphics->DrawArc(&p, x - radius, y - radius, radius * 2, radius * 2, s, e - s);
imm2d_SetDirty();
}
void DrawRectangle(int x, int y, int width, int height, Color fill, Color stroke)
{
std::lock_guard<std::mutex> lock(imm2d_bitmapLock);
if (!imm2d_graphics) return;
// GDI+'s DrawRectangle and FillRectangle behave a little differently: One
// of them treats the end coordinates as inclusive and the other exclusive
const int adjustment = fill != Transparent ? 0 : -1;
Gdiplus::Rect r(x, y, width + adjustment, height + adjustment);
if (fill != Transparent)
{
Gdiplus::SolidBrush brush{ Gdiplus::Color(fill) };
imm2d_graphics->FillRectangle(&brush, r);
}
if (stroke != Transparent)
{
Gdiplus::Pen pen{ Gdiplus::Color(stroke) };
imm2d_graphics->DrawRectangle(&pen, r);
}
imm2d_SetDirty();
}
void DrawString(int x, int y, const char *text, const char *fontName, int fontPtSize, const Color c, bool centered)
{
if (!text || !fontName) return;
if (fontPtSize < 1 || !text[0]) return;