Ver Fonte

Allows short names for primitive types (#2874)

1vanK há 3 anos atrás
pai
commit
d85c3444a9

+ 18 - 10
Source/Tools/BindingGenerator/ASUtils.cpp

@@ -42,28 +42,28 @@ string CppPrimitiveTypeToAS(const string& cppType)
     if (cppType == "bool")
         return "bool";
 
-    if (cppType == "char" || cppType == "signed char")
+    if (cppType == "char" || cppType == "signed char" || cppType == "i8")
         return "int8";
 
-    if (cppType == "unsigned char")
+    if (cppType == "unsigned char" || cppType == "u8")
         return "uint8";
 
-    if (cppType == "short")
+    if (cppType == "short" || cppType == "i16")
         return "int16";
 
-    if (cppType == "unsigned short")
+    if (cppType == "unsigned short" || cppType == "u16")
         return "uint16";
 
-    if (cppType == "int")
+    if (cppType == "int" || cppType == "i32")
         return "int";
 
-    if (cppType == "unsigned" || cppType == "unsigned int")
+    if (cppType == "unsigned" || cppType == "unsigned int" || cppType == "u32")
         return "uint";
 
-    if (cppType == "long long")
+    if (cppType == "long long" || cppType == "i64")
         return "int64";
 
-    if (cppType == "unsigned long long")
+    if (cppType == "unsigned long long" || cppType == "u64")
         return "uint64";
 
     if (cppType == "float")
@@ -165,16 +165,24 @@ bool IsKnownCppType(const string& name)
         "size_t",
         "char",
         "signed char",
+        "i8",
         "unsigned char",
+        "u8",
         "short",
+        "i16",
         "unsigned short",
+        "u16",
         "int",
-        "long",
+        "i32",
+        "long", // Size is compiler-dependent
         "unsigned",
         "unsigned int",
-        "unsigned long",
+        "u32",
+        "unsigned long", // Size is compiler-dependent
         "long long",
+        "i64",
         "unsigned long long",
+        "u64",
         "float",
         "double",
         "SDL_JoystickID",

+ 9 - 9
Source/Urho3D/AngelScript/Generated_Members.h

@@ -8961,7 +8961,7 @@ template <class T> void RegisterMembers_Audio(asIScriptEngine* engine, const cha
 {
     RegisterMembers_Object<T>(engine, className);
 
-    // void Audio::MixOutput(void* dest, unsigned samples)
+    // void Audio::MixOutput(void* dest, u32 samples)
     // Error: type "void*" can not automatically bind
 
     // void Audio::AddSoundSource(SoundSource* soundSource)
@@ -8979,16 +8979,16 @@ template <class T> void RegisterMembers_Audio(asIScriptEngine* engine, const cha
     engine->RegisterObjectMethod(className, "float GetMasterGain(const String&in) const", AS_METHODPR(T, GetMasterGain, (const String&) const, float), AS_CALL_THISCALL);
     engine->RegisterObjectMethod(className, "float get_masterGain(const String&in) const", AS_METHODPR(T, GetMasterGain, (const String&) const, float), AS_CALL_THISCALL);
 
-    // int Audio::GetMixRate() const
-    engine->RegisterObjectMethod(className, "int GetMixRate() const", AS_METHODPR(T, GetMixRate, () const, int), AS_CALL_THISCALL);
-    engine->RegisterObjectMethod(className, "int get_mixRate() const", AS_METHODPR(T, GetMixRate, () const, int), AS_CALL_THISCALL);
+    // i32 Audio::GetMixRate() const
+    engine->RegisterObjectMethod(className, "int GetMixRate() const", AS_METHODPR(T, GetMixRate, () const, i32), AS_CALL_THISCALL);
+    engine->RegisterObjectMethod(className, "int get_mixRate() const", AS_METHODPR(T, GetMixRate, () const, i32), AS_CALL_THISCALL);
 
     // Mutex& Audio::GetMutex()
     engine->RegisterObjectMethod(className, "Mutex& GetMutex()", AS_METHODPR(T, GetMutex, (), Mutex&), AS_CALL_THISCALL);
 
-    // unsigned Audio::GetSampleSize() const
-    engine->RegisterObjectMethod(className, "uint GetSampleSize() const", AS_METHODPR(T, GetSampleSize, () const, unsigned), AS_CALL_THISCALL);
-    engine->RegisterObjectMethod(className, "uint get_sampleSize() const", AS_METHODPR(T, GetSampleSize, () const, unsigned), AS_CALL_THISCALL);
+    // u32 Audio::GetSampleSize() const
+    engine->RegisterObjectMethod(className, "uint GetSampleSize() const", AS_METHODPR(T, GetSampleSize, () const, u32), AS_CALL_THISCALL);
+    engine->RegisterObjectMethod(className, "uint get_sampleSize() const", AS_METHODPR(T, GetSampleSize, () const, u32), AS_CALL_THISCALL);
 
     // float Audio::GetSoundSourceMasterGain(StringHash typeHash) const
     engine->RegisterObjectMethod(className, "float GetSoundSourceMasterGain(StringHash) const", AS_METHODPR(T, GetSoundSourceMasterGain, (StringHash) const, float), AS_CALL_THISCALL);
@@ -9037,8 +9037,8 @@ template <class T> void RegisterMembers_Audio(asIScriptEngine* engine, const cha
     engine->RegisterObjectMethod(className, "void SetMasterGain(const String&in, float)", AS_METHODPR(T, SetMasterGain, (const String&, float), void), AS_CALL_THISCALL);
     engine->RegisterObjectMethod(className, "void set_masterGain(const String&in, float)", AS_METHODPR(T, SetMasterGain, (const String&, float), void), AS_CALL_THISCALL);
 
-    // bool Audio::SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpolation = true)
-    engine->RegisterObjectMethod(className, "bool SetMode(int, int, bool, bool = true)", AS_METHODPR(T, SetMode, (int, int, bool, bool), bool), AS_CALL_THISCALL);
+    // bool Audio::SetMode(i32 bufferLengthMSec, i32 mixRate, bool stereo, bool interpolation = true)
+    engine->RegisterObjectMethod(className, "bool SetMode(int, int, bool, bool = true)", AS_METHODPR(T, SetMode, (i32, i32, bool, bool), bool), AS_CALL_THISCALL);
 
     // void Audio::Stop()
     engine->RegisterObjectMethod(className, "void Stop()", AS_METHODPR(T, Stop, (), void), AS_CALL_THISCALL);

+ 20 - 20
Source/Urho3D/Audio/Audio.cpp

@@ -45,12 +45,12 @@ namespace Urho3D
 
 const char* AUDIO_CATEGORY = "Audio";
 
-static const int MIN_BUFFERLENGTH = 20;
-static const int MIN_MIXRATE = 11025;
-static const int MAX_MIXRATE = 48000;
+static const i32 MIN_BUFFERLENGTH = 20;
+static const i32 MIN_MIXRATE = 11025;
+static const i32 MAX_MIXRATE = 48000;
 static const StringHash SOUND_MASTER_HASH("Master");
 
-static void SDLAudioCallback(void* userdata, Uint8* stream, int len);
+static void SDLAudioCallback(void* userdata, Uint8* stream, i32 len);
 
 Audio::Audio(Context* context) :
     Object(context)
@@ -72,7 +72,7 @@ Audio::~Audio()
     context_->ReleaseSDL();
 }
 
-bool Audio::SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpolation)
+bool Audio::SetMode(i32 bufferLengthMSec, i32 mixRate, bool stereo, bool interpolation)
 {
     Release();
 
@@ -89,15 +89,15 @@ bool Audio::SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpo
     desired.userdata = this;
 
     // SDL uses power of two audio fragments. Determine the closest match
-    int bufferSamples = mixRate * bufferLengthMSec / 1000;
-    desired.samples = (Uint16)NextPowerOfTwo((unsigned)bufferSamples);
-    if (Abs((int)desired.samples / 2 - bufferSamples) < Abs((int)desired.samples - bufferSamples))
+    i32 bufferSamples = mixRate * bufferLengthMSec / 1000;
+    desired.samples = (Uint16)NextPowerOfTwo((u32)bufferSamples);
+    if (Abs((i32)desired.samples / 2 - bufferSamples) < Abs((i32)desired.samples - bufferSamples))
         desired.samples /= 2;
 
     // Intentionally disallow format change so that the obtained format will always be the desired format, even though that format
     // is not matching the device format, however in doing it will enable the SDL's internal audio stream with audio conversion.
     // Also disallow channels change to avoid issues on multichannel audio device (5.1, 7.1, etc)
-    int allowedChanges = SDL_AUDIO_ALLOW_ANY_CHANGE & ~SDL_AUDIO_ALLOW_FORMAT_CHANGE & ~SDL_AUDIO_ALLOW_CHANNELS_CHANGE;
+    i32 allowedChanges = SDL_AUDIO_ALLOW_ANY_CHANGE & ~SDL_AUDIO_ALLOW_FORMAT_CHANGE & ~SDL_AUDIO_ALLOW_CHANNELS_CHANGE;
 
     if (stereo)
     {
@@ -127,12 +127,12 @@ bool Audio::SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpo
     }
 
     stereo_ = obtained.channels == 2;
-    sampleSize_ = (unsigned)(stereo_ ? sizeof(int) : sizeof(short));
+    sampleSize_ = (u32)(stereo_ ? sizeof(i32) : sizeof(i16));
     // Guarantee a fragment size that is low enough so that Vorbis decoding buffers do not wrap
-    fragmentSize_ = Min(NextPowerOfTwo((unsigned)mixRate >> 6u), (unsigned)obtained.samples);
+    fragmentSize_ = Min(NextPowerOfTwo((u32)mixRate >> 6u), (u32)obtained.samples);
     mixRate_ = obtained.freq;
     interpolation_ = interpolation;
-    clipBuffer_ = new int[stereo ? fragmentSize_ << 1u : fragmentSize_];
+    clipBuffer_ = new i32[stereo ? fragmentSize_ << 1u : fragmentSize_];
 
     URHO3D_LOGINFO("Set audio mode " + String(mixRate_) + " Hz " + (stereo_ ? "stereo" : "mono") + (interpolation_ ? " interpolated" : ""));
 
@@ -267,7 +267,7 @@ float Audio::GetSoundSourceMasterGain(StringHash typeHash) const
     return masterIt->second_.GetFloat() * typeIt->second_.GetFloat();
 }
 
-void SDLAudioCallback(void* userdata, Uint8* stream, int len)
+void SDLAudioCallback(void* userdata, Uint8* stream, i32 len)
 {
     auto* audio = static_cast<Audio*>(userdata);
     {
@@ -276,7 +276,7 @@ void SDLAudioCallback(void* userdata, Uint8* stream, int len)
     }
 }
 
-void Audio::MixOutput(void* dest, unsigned samples)
+void Audio::MixOutput(void* dest, u32 samples)
 {
     if (!playing_ || !clipBuffer_)
     {
@@ -287,14 +287,14 @@ void Audio::MixOutput(void* dest, unsigned samples)
     while (samples)
     {
         // If sample count exceeds the fragment (clip buffer) size, split the work
-        unsigned workSamples = Min(samples, fragmentSize_);
-        unsigned clipSamples = workSamples;
+        u32 workSamples = Min(samples, fragmentSize_);
+        u32 clipSamples = workSamples;
         if (stereo_)
             clipSamples <<= 1;
 
         // Clear clip buffer
-        int* clipPtr = clipBuffer_.Get();
-        memset(clipPtr, 0, clipSamples * sizeof(int));
+        i32* clipPtr = clipBuffer_.Get();
+        memset(clipPtr, 0, clipSamples * sizeof(i32));
 
         // Mix samples to clip buffer
         for (PODVector<SoundSource*>::Iterator i = soundSources_.Begin(); i != soundSources_.End(); ++i)
@@ -315,7 +315,7 @@ void Audio::MixOutput(void* dest, unsigned samples)
         while (clipSamples--)
             *destPtr++ = (short)Clamp(*clipPtr++, -32768, 32767);
         samples -= workSamples;
-        ((unsigned char*&)dest) += sampleSize_ * workSamples;
+        ((u8*&)dest) += sampleSize_ * workSamples;
     }
 }
 
@@ -343,7 +343,7 @@ void Audio::UpdateInternal(float timeStep)
     URHO3D_PROFILE(UpdateAudio);
 
     // Update in reverse order, because sound sources might remove themselves
-    for (unsigned i = soundSources_.Size() - 1; i < soundSources_.Size(); --i)
+    for (u32 i = soundSources_.Size() - 1; i < soundSources_.Size(); --i)
     {
         SoundSource* source = soundSources_[i];
 

+ 9 - 9
Source/Urho3D/Audio/Audio.h

@@ -48,7 +48,7 @@ public:
     ~Audio() override;
 
     /// Initialize sound output with specified buffer length and output mode.
-    bool SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpolation = true);
+    bool SetMode(i32 bufferLengthMSec, i32 mixRate, bool stereo, bool interpolation = true);
     /// Run update on sound sources. Not required for continued playback, but frees unused sound sources & sounds and updates 3D positions.
     void Update(float timeStep);
     /// Restart sound output.
@@ -72,11 +72,11 @@ public:
 
     /// Return byte size of one sample.
     /// @property
-    unsigned GetSampleSize() const { return sampleSize_; }
+    u32 GetSampleSize() const { return sampleSize_; }
 
     /// Return mixing rate.
     /// @property
-    int GetMixRate() const { return mixRate_; }
+    i32 GetMixRate() const { return mixRate_; }
 
     /// Return whether output is interpolated.
     /// @property
@@ -123,7 +123,7 @@ public:
     float GetSoundSourceMasterGain(StringHash typeHash) const;
 
     /// Mix sound sources into the buffer.
-    void MixOutput(void* dest, unsigned samples);
+    void MixOutput(void* dest, u32 samples);
 
 private:
     /// Handle render update event.
@@ -134,17 +134,17 @@ private:
     void UpdateInternal(float timeStep);
 
     /// Clipping buffer for mixing.
-    SharedArrayPtr<int> clipBuffer_;
+    SharedArrayPtr<i32> clipBuffer_;
     /// Audio thread mutex.
     Mutex audioMutex_;
     /// SDL audio device ID.
-    unsigned deviceID_{};
+    u32 deviceID_{};
     /// Sample size.
-    unsigned sampleSize_{};
+    u32 sampleSize_{};
     /// Clip buffer size in samples.
-    unsigned fragmentSize_{};
+    u32 fragmentSize_{};
     /// Mixing rate.
-    int mixRate_{};
+    i32 mixRate_{};
     /// Mixing interpolation flag.
     bool interpolation_{};
     /// Stereo flag.

+ 41 - 0
Source/Urho3D/Base/PrimitiveTypes.h

@@ -0,0 +1,41 @@
+//
+// Copyright (c) 2008-2022 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include <cstdint>
+
+// https://en.cppreference.com/w/cpp/language/types
+static_assert(sizeof(char) == 1);
+static_assert(sizeof(int) == 4);
+static_assert(sizeof(long) == 4 || sizeof(long) == 8); // (Win32, Win64, Unix32) || Unix64
+static_assert(sizeof(long long) == 8);
+
+// https://en.cppreference.com/w/cpp/types/integer
+typedef int8_t   i8;
+typedef uint8_t  u8;
+typedef int16_t  i16;
+typedef uint16_t u16;
+typedef int32_t  i32;
+typedef uint32_t u32;
+typedef int64_t  i64;
+typedef uint64_t u64;

+ 4 - 3
Source/Urho3D/Container/VectorBase.h

@@ -29,6 +29,7 @@
 #endif
 
 #include "../Base/Iter.h"
+#include "../Base/PrimitiveTypes.h"
 #include "../Container/Swap.h"
 
 namespace Urho3D
@@ -61,11 +62,11 @@ protected:
     static unsigned char* AllocateBuffer(unsigned size);
 
     /// Size of vector.
-    unsigned size_;
+    u32 size_;
     /// Buffer capacity.
-    unsigned capacity_;
+    u32 capacity_;
     /// Buffer.
-    unsigned char* buffer_;
+    u8* buffer_;
 };
 
 }