8000 Merge pull request #341 from Bigfoot71/master · RobLoach/raylib-cpp@6e4a852 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e4a852

Browse files
authored
Merge pull request #341 from Bigfoot71/master
Security and clarity improvements
2 parents c283b73 + 2a1738e commit 6e4a852

35 files changed

+276
-264
lines changed

include/AudioDevice.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AudioDevice {
1818
*
1919
* @throws raylib::RaylibException Throws if the AudioDevice failed to initialize.
2020
*/
21-
AudioDevice(bool lateInit = false) {
21+
explicit AudioDevice(bool lateInit = false) {
2222
if (!lateInit) {
2323
Init();
2424
}
@@ -34,7 +34,7 @@ class AudioDevice {
3434
*
3535
* @throws raylib::RaylibException Throws if the AudioDevice failed to initialize.
3636
*/
37-
void Init() {
37+
static void Init() {
3838
::InitAudioDevice();
3939
if (!IsReady()) {
4040
throw RaylibException("Failed to initialize AudioDevice");
@@ -44,12 +44,12 @@ class AudioDevice {
4444
/**
4545
* Close the audio device and context.
4646
*/
47-
void Close() { ::CloseAudioDevice(); }
47+
static void Close() { ::CloseAudioDevice(); }
4848

4949
/**
5050
* Check if audio device has been initialized successfully.
5151
*/
52-
bool IsReady() const { return ::IsAudioDeviceReady(); }
52+
static bool IsReady() { return ::IsAudioDeviceReady(); }
5353

5454
/**
5555
* Set master volume (listener).

include/AudioStream.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ namespace raylib {
1111
*/
1212
class AudioStream : public ::AudioStream {
1313
public:
14-
AudioStream(const ::AudioStream& music) { set(music); }
14+
AudioStream(const ::AudioStream& music)
15+
: ::AudioStream(music) {
16+
// Nothing.
17+
}
1518

1619
AudioStream(
1720
rAudioBuffer* buffer = nullptr,
@@ -34,7 +37,7 @@ class AudioStream : public ::AudioStream {
3437

3538
AudioStream(const AudioStream&) = delete;
3639

37-
AudioStream(AudioStream&& other) {
40+
AudioStream(AudioStream&& other) noexcept {
3841
set(other);
3942

4043
other.buffer = nullptr;
@@ -96,7 +99,7 @@ class AudioStream : public ::AudioStream {
9699
/**
97100
* Check if any audio stream buffers requires refill
98101
*/
99-
bool IsProcessed() const { return ::IsAudioStreamProcessed(*this); }
102+
[[nodiscard]] bool IsProcessed() const { return ::IsAudioStreamProcessed(*this); }
100103

101104
/**
102105
* Play audio stream
@@ -125,7 +128,7 @@ class AudioStream : public ::AudioStream {
125128
/**
126129
* Check if audio stream is playing
127130
*/
128-
bool IsPlaying() const { return ::IsAudioStreamPlaying(*this); }
131+
[[nodiscard]] bool IsPlaying() const { return ::IsAudioStreamPlaying(*this); }
129132

130133
/**
131134
* Stop audio stream
@@ -182,7 +185,7 @@ class AudioStream : public ::AudioStream {
182185
/**
183186
* Retrieve whether or not the audio stream is ready.
184187
*/
185-
bool IsValid() const { return ::IsAudioStreamValid(*this); }
188+
[[nodiscard]] bool IsValid() const { return ::IsAudioStreamValid(*this); }
186189

187190
/**
188191
* Load audio stream (to stream raw audio pcm data)

include/AutomationEventList.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ namespace raylib {
1111
*/
1212
class AutomationEventList : public ::AutomationEventList {
1313
public:
14-
AutomationEventList(const ::AutomationEventList& automationEventList) { set(automationEventList); }
14+
AutomationEventList(const ::AutomationEventList& automationEventList)
15+
: ::AutomationEventList(automationEventList) {
16+
// Nothing.
17+
}
1518

1619
/**
1720
* Load an empty automation events list.
@@ -27,7 +30,7 @@ class AutomationEventList : public ::AutomationEventList {
2730

2831
AutomationEventList(const AutomationEventList&) = delete;
2932

30-
AutomationEventList(AutomationEventList&& other) {
33+
AutomationEventList(AutomationEventList&& other) noexcept {
3134
set(other);
3235

3336
other.capacity = 0;

include/BoundingBox.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ class BoundingBox : public ::BoundingBox {
4141
/**
4242
* Detect collision between two boxes
4343
*/
44-
bool CheckCollision(const ::BoundingBox& box2) const { return CheckCollisionBoxes(*this, box2); }
44+
[[nodiscard]] bool CheckCollision(const ::BoundingBox& box2) const { return CheckCollisionBoxes(*this, box2); }
4545

4646
/**
4747
* Detect collision between box and sphere
4848
*/
49-
bool CheckCollision(::Vector3 center, float radius) const { return CheckCollisionBoxSphere(*this, center, radius); }
49+
[[nodiscard]] bool CheckCollision(::Vector3 center, float radius) const { return CheckCollisionBoxSphere(*this, center, radius); }
5050

5151
/**
5252
* Detect collision between ray and bounding box
5353
*/
54-
bool CheckCollision(const ::Ray& ray) const { return GetRayCollisionBox(ray, *this).hit; }
54+
[[nodiscard]] bool CheckCollision(const ::Ray& ray) const { return GetRayCollisionBox(ray, *this).hit; }
5555

5656
/**
5757
* Get collision information between ray and bounding box

include/Camera2D.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ namespace raylib {
1111
*/
1212
class Camera2D : public ::Camera2D {
1313
public:
14-
Camera2D(const ::Camera2D& camera) { set(camera); }
14+
Camera2D(const ::Camera2D& camera)
15+
: ::Camera2D(camera) {
16+
// Nothing.
17+
}
1518

16-
Camera2D() {}
19+
Camera2D() : ::Camera2D() {}
1720
Camera2D(::Vector2 offset, ::Vector2 target, float rotation = 0.0f, float zoom = 1.0f)
1821
: ::Camera2D{offset, target, rotation, zoom} {}
1922

@@ -40,17 +43,17 @@ class Camera2D : public ::Camera2D {
4043
/**
4144
* Returns camera 2d transform matrix
4245
*/
43-
Matrix GetMatrix() const { return ::GetCameraMatrix2D(*this); }
46+
[[nodiscard]] Matrix GetMatrix() const { return ::GetCameraMatrix2D(*this); }
4447

4548
/**
4649
* Returns the world space position for a 2d camera screen space position
4750
*/
48-
Vector2 GetScreenToWorld(::Vector2 position) const { return ::GetScreenToWorld2D(position, *this); }
51+
[[nodiscard]] Vector2 GetScreenToWorld(::Vector2 position) const { return ::GetScreenToWorld2D(position, *this); }
4952

5053
/**
5154
* Returns the screen space position for a 2d world space position
5255
*/
53-
Vector2 GetWorldToScreen(::Vector2 position) const { return ::GetWorldToScreen2D(position, *this); }
56+
[[nodiscard]] Vector2 GetWorldToScreen(::Vector2 position) const { return ::GetWorldToScreen2D(position, *this); }
5457
protected:
5558
void set(const ::Camera2D& camera) {
5659
offset = camera.offset;

include/Camera3D.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace raylib {
1111
*/
1212
class Camera3D : public ::Camera3D {
1313
public:
14-
Camera3D(const ::Camera3D& camera) { set(camera); }
14+
Camera3D(const ::Camera3D& camera) : ::Camera3D(camera) { }
1515

1616
/**
1717
* Create a new Camera3D.

include/Color.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,43 @@ class Color : public ::Color {
3636
/**
3737
* Get Color structure from hexadecimal value
3838
*/
39-
Color(unsigned int hexValue) { set(::GetColor(hexValue)); }
39+
explicit Color(unsigned int hexValue) : ::Color(::GetColor(hexValue)) { }
4040

41-
Color(void* srcPtr, int format) { set(::GetPixelColor(srcPtr, format)); }
41+
Color(void* srcPtr, int format) : ::Color(::GetPixelColor(srcPtr, format)) { }
4242

4343
/**
4444
* Returns hexadecimal value for a Color
4545
*/
46-
int ToInt() const { return ::ColorToInt(*this); }
46+
[[nodiscard]] int ToInt() const { return ::ColorToInt(*this); }
4747

4848
/**
4949
* Returns hexadecimal value for a Color
5050
*/
51-
operator int() const { return ::ColorToInt(*this); }
51+
explicit operator int() const { return ::ColorToInt(*this); }
5252

53-
std::string ToString() const { return TextFormat("Color(%d, %d, %d, %d)", r, g, b, a); }
53+
[[nodiscard]] std::string ToString() const { return TextFormat("Color(%d, %d, %d, %d)", r, g, b, a); }
5454

55-
operator std::string() const { return ToString(); }
55+
explicit operator std::string() const { return ToString(); }
5656

5757
/**
5858
* Returns color with alpha applied, alpha goes from 0.0f to 1.0f
5959
*/
60-
Color Fade(float alpha) const { return ::Fade(*this, alpha); }
60+
[[nodiscard]] Color Fade(float alpha) const { return ::Fade(*this, alpha); }
6161

6262
/**
6363
* Returns Color normalized as float [0..1]
6464
*/
65-
Vector4 Normalize() const { return ::ColorNormalize(*this); }
65+
[[nodiscard]] Vector4 Normalize() const { return ::ColorNormalize(*this); }
6666

6767
/**
6868
* Returns Color from normalized values [0..1]
6969
*/
70-
Color(::Vector4 normalized) { set(::ColorFromNormalized(normalized)); }
70+
explicit Color(::Vector4 normalized) : Color(::ColorFromNormalized(normalized)) { }
7171

7272
/**
7373
* Returns HSV values for a Color
7474
*/
75-
Vector3 ToHSV() const { return ::ColorToHSV(*this); }
75+
[[nodiscard]] Vector3 ToHSV() const { return ::ColorToHSV(*this); }
7676

7777
GETTERSETTER(unsigned char, R, r)
7878
GETTERSETTER(unsigned char, G, g)
@@ -206,7 +206,7 @@ class Color : public ::Color {
206206
/**
207207
* Returns color with alpha applied, alpha goes from 0.0f to 1.0f
208208
*/
209-
Color Alpha(float alpha) const { return ::ColorAlpha(*this, alpha); }
209+
[[nodiscard]] Color Alpha(float alpha) const { return ::ColorAlpha(*this, alpha); }
210210

211211
Color Lerp(::Color color2, float factor) {
212212
return ::ColorLerp(*this, color2, factor);
@@ -215,7 +215,7 @@ class Color : public ::Color {
215215
/**
216216
* Returns src alpha-blended into dst color with tint
217217
*/
218-
Color AlphaBlend(::Color dst, ::Color tint) const { return ::ColorAlphaBlend(dst, *this, tint); }
218+
[[nodiscard]] Color AlphaBlend(::Color dst, ::Color tint) const { return ::ColorAlphaBlend(dst, *this, tint); }
219219

220220
static Color LightGray() { return LIGHTGRAY; }
221221
static Color Gray() { return GRAY; }

include/Font.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ class Font : public ::Font {
2828
/**
2929
* Retrieves the default Font.
3030
*/
31-
Font() { set(::GetFontDefault()); }
31+
Font() : ::Font(::GetFontDefault()) { }
3232

33-
Font(const ::Font& font) { set(font); }
33+
Font(const ::Font& font) : ::Font(font) { }
3434

3535
/**
3636
* Loads a Font from the given file.
@@ -84,7 +84,7 @@ class Font : public ::Font {
8484

8585
Font(const Font&) = delete;
8686

87-
Font(Font&& other) {
87+
Font(Font&& other) noexcept {
8888
set(other);
8989

9090
other.baseSize = 0;
@@ -203,7 +203,7 @@ class Font : public ::Font {
203203
/**
204204
* Returns if the font is ready to be used.
205205
*/
206-
bool IsValid() const { return ::IsFontValid(*this); }
206+
[[nodiscard]] bool IsValid() const { return ::IsFontValid(*this); }
207207

208208
/**
209209
* Draw text using font and additional parameters.
@@ -286,33 +286,33 @@ class Font : public ::Font {
286286
/**
287287
* Measure string size for Font
288288
*/
289-
Vector2 MeasureText(const char* text, float fontSize, float spacing) const {
289+
[[nodiscard]] Vector2 MeasureText(const char* text, float fontSize, float spacing) const {
290290
return ::MeasureTextEx(*this, text, fontSize, spacing);
291291
}
292292

293293
/**
294294
* Measure string size for Font
295295
*/
296-
Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
296+
[[nodiscard]] Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
297297
return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
298298
}
299299

300300
/**
301301
* Get index position for a unicode character on font
302302
*/
303-
int GetGlyphIndex(int character) const { return ::GetGlyphIndex(*this, character); }
303+
[[nodiscard]] int GetGlyphIndex(int character) const { return ::GetGlyphIndex(*this, character); }
304304

305305
/**
306306
* Create an image from text (custom sprite font)
307307
*/
308-
::Image ImageText(const char* text, float fontSize, float spacing, ::Color tint) const {
308+
[[nodiscard]] ::Image ImageText(const char* text, float fontSize, float spacing, ::Color tint) const {
309309
return ::ImageTextEx(*this, text, fontSize, spacing, tint);
310310
}
311311

312312
/**
313313
* Create an image from text (custom sprite font)
314314
*/
315-
::Image ImageText(const std::string& text, float fontSize, float spacing, ::Color tint) const {
315+
[[nodiscard]] ::Image ImageText(const std::string& text, float fontSize, float spacing, ::Color tint) const {
316316
return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
317317
}
318318
protected:

0 commit comments

Comments
 (0)
0