Use SDL_bool instead an int return code in the SDL API

Most SDL functions used to indicate success or failure using an int return code. These functions have been changed to return SDL_bool.

Here is a coccinelle patch to change code that previously compared the return value to 0 and changes it to a boolean test:
@ bool_return_type  @
identifier func =~ "^(SDL_AddEventWatch|SDL_AddHintCallback|SDL_AddSurfaceAlternateImage|SDL_AddVulkanRenderSemaphores|SDL_BindAudioStream|SDL_BindAudioStreams|SDL_BlitSurface|SDL_BlitSurface9Grid|SDL_BlitSurfaceScaled|SDL_BlitSurfaceTiled|SDL_BlitSurfaceTiledWithScale|SDL_BlitSurfaceUnchecked|SDL_BlitSurfaceUncheckedScaled|SDL_CaptureMouse|SDL_ClearAudioStream|SDL_ClearClipboardData|SDL_ClearComposition|SDL_ClearError|SDL_ClearProperty|SDL_ClearSurface|SDL_CloseIO|SDL_CloseStorage|SDL_ConvertAudioSamples|SDL_ConvertEventToRenderCoordinates|SDL_ConvertPixels|SDL_ConvertPixelsAndColorspace|SDL_CopyFile|SDL_CopyProperties|SDL_CopyStorageFile|SDL_CreateDirectory|SDL_CreateStorageDirectory|SDL_CreateWindowAndRenderer|SDL_DateTimeToTime|SDL_DestroyWindowSurface|SDL_DetachVirtualJoystick|SDL_DisableScreenSaver|SDL_EnableScreenSaver|SDL_EnumerateDirectory|SDL_EnumerateProperties|SDL_EnumerateStorageDirectory|SDL_FillSurfaceRect|SDL_FillSurfaceRects|SDL_FlashWindow|SDL_FlipSurface|SDL_FlushAudioStream|SDL_FlushRenderer|SDL_GL_DestroyContext|SDL_GL_GetAttribute|SDL_GL_GetSwapInterval|SDL_GL_LoadLibrary|SDL_GL_MakeCurrent|SDL_GL_SetAttribute|SDL_GL_SetSwapInterval|SDL_GL_SwapWindow|SDL_GetAudioDeviceFormat|SDL_GetAudioStreamFormat|SDL_GetCameraFormat|SDL_GetClosestFullscreenDisplayMode|SDL_GetCurrentRenderOutputSize|SDL_GetCurrentTime|SDL_GetDXGIOutputInfo|SDL_GetDateTimeLocalePreferences|SDL_GetDisplayBounds|SDL_GetDisplayUsableBounds|SDL_GetGDKDefaultUser|SDL_GetGDKTaskQueue|SDL_GetGamepadSensorData|SDL_GetGamepadTouchpadFinger|SDL_GetHapticEffectStatus|SDL_GetJoystickBall|SDL_GetMasksForPixelFormat|SDL_GetPathInfo|SDL_GetRectUnion|SDL_GetRectUnionFloat|SDL_GetRenderClipRect|SDL_GetRenderColorScale|SDL_GetRenderDrawBlendMode|SDL_GetRenderDrawColor|SDL_GetRenderDrawColorFloat|SDL_GetRenderLogicalPresentation|SDL_GetRenderLogicalPresentationRect|SDL_GetRenderOutputSize|SDL_GetRenderSafeArea|SDL_GetRenderScale|SDL_GetRenderVSync|SDL_GetRenderViewport|SDL_GetSensorData|SDL_GetStorageFileSize|SDL_GetStoragePathInfo|SDL_GetSurfaceAlphaMod|SDL_GetSurfaceBlendMode|SDL_GetSurfaceClipRect|SDL_GetSurfaceColorKey|SDL_GetSurfaceColorMod|SDL_GetTextInputArea|SDL_GetTextureAlphaMod|SDL_GetTextureAlphaModFloat|SDL_GetTextureBlendMode|SDL_GetTextureColorMod|SDL_GetTextureColorModFloat|SDL_GetTextureScaleMode|SDL_GetTextureSize|SDL_GetWindowAspectRatio|SDL_GetWindowBordersSize|SDL_GetWindowMaximumSize|SDL_GetWindowMinimumSize|SDL_GetWindowPosition|SDL_GetWindowRelativeMouseMode|SDL_GetWindowSafeArea|SDL_GetWindowSize|SDL_GetWindowSizeInPixels|SDL_GetWindowSurfaceVSync|SDL_HideCursor|SDL_HideWindow|SDL_Init|SDL_InitHapticRumble|SDL_InitSubSystem|SDL_LoadWAV|SDL_LoadWAV_IO|SDL_LockAudioStream|SDL_LockProperties|SDL_LockSurface|SDL_LockTexture|SDL_LockTextureToSurface|SDL_MaximizeWindow|SDL_MinimizeWindow|SDL_MixAudio|SDL_OpenURL|SDL_OutOfMemory|SDL_PauseAudioDevice|SDL_PauseAudioStreamDevice|SDL_PauseHaptic|SDL_PlayHapticRumble|SDL_PremultiplyAlpha|SDL_PremultiplySurfaceAlpha|SDL_PushEvent|SDL_PutAudioStreamData|SDL_RaiseWindow|SDL_ReadStorageFile|SDL_ReadSurfacePixel|SDL_ReadSurfacePixelFloat|SDL_RegisterApp|SDL_ReloadGamepadMappings|SDL_RemovePath|SDL_RemoveStoragePath|SDL_RemoveTimer|SDL_RenamePath|SDL_RenameStoragePath|SDL_RenderClear|SDL_RenderCoordinatesFromWindow|SDL_RenderCoordinatesToWindow|SDL_RenderFillRect|SDL_RenderFillRects|SDL_RenderGeometry|SDL_RenderGeometryRaw|SDL_RenderLine|SDL_RenderLines|SDL_RenderPoint|SDL_RenderPoints|SDL_RenderPresent|SDL_RenderRect|SDL_RenderRects|SDL_RenderTexture|SDL_RenderTexture9Grid|SDL_RenderTextureRotated|SDL_RenderTextureTiled|SDL_RequestAndroidPermission|SDL_RestoreWindow|SDL_ResumeAudioDevice|SDL_ResumeAudioStreamDevice|SDL_ResumeHaptic|SDL_RumbleGamepad|SDL_RumbleGamepadTriggers|SDL_RumbleJoystick|SDL_RumbleJoystickTriggers|SDL_RunHapticEffect|SDL_SaveBMP|SDL_SaveBMP_IO|SDL_SendAndroidMessage|SDL_SendGamepadEffect|SDL_SendJoystickEffect|SDL_SendJoystickVirtualSensorData|SDL_SetAppMetadata|SDL_SetAppMetadataProperty|SDL_SetAudioDeviceGain|SDL_SetAudioPostmixCallback|SDL_SetAudioStreamFormat|SDL_SetAudioStreamFrequencyRatio|SDL_SetAudioStreamGain|SDL_SetAudioStreamGetCallback|SDL_SetAudioStreamInputChannelMap|SDL_SetAudioStreamOutputChannelMap|SDL_SetAudioStreamPutCallback|SDL_SetBooleanProperty|SDL_SetClipboardData|SDL_SetClipboardText|SDL_SetCursor|SDL_SetFloatProperty|SDL_SetGamepadLED|SDL_SetGamepadMapping|SDL_SetGamepadPlayerIndex|SDL_SetGamepadSensorEnabled|SDL_SetHapticAutocenter|SDL_SetHapticGain|SDL_SetJoystickLED|SDL_SetJoystickPlayerIndex|SDL_SetJoystickVirtualAxis|SDL_SetJoystickVirtualBall|SDL_SetJoystickVirtualButton|SDL_SetJoystickVirtualHat|SDL_SetJoystickVirtualTouchpad|SDL_SetLinuxThreadPriority|SDL_SetLinuxThreadPriorityAndPolicy|SDL_SetLogPriorityPrefix|SDL_SetMemoryFunctions|SDL_SetNumberProperty|SDL_SetPaletteColors|SDL_SetPointerProperty|SDL_SetPointerPropertyWithCleanup|SDL_SetPrimarySelectionText|SDL_SetRenderClipRect|SDL_SetRenderColorScale|SDL_SetRenderDrawBlendMode|SDL_SetRenderDrawColor|SDL_SetRenderDrawColorFloat|SDL_SetRenderLogicalPresentation|SDL_SetRenderScale|SDL_SetRenderTarget|SDL_SetRenderVSync|SDL_SetRenderViewport|SDL_SetScancodeName|SDL_SetStringProperty|SDL_SetSurfaceAlphaMod|SDL_SetSurfaceBlendMode|SDL_SetSurfaceColorKey|SDL_SetSurfaceColorMod|SDL_SetSurfaceColorspace|SDL_SetSurfacePalette|SDL_SetSurfaceRLE|SDL_SetTLS|SDL_SetTextInputArea|SDL_SetTextureAlphaMod|SDL_SetTextureAlphaModFloat|SDL_SetTextureBlendMode|SDL_SetTextureColorMod|SDL_SetTextureColorModFloat|SDL_SetTextureScaleMode|SDL_SetThreadPriority|SDL_SetWindowAlwaysOnTop|SDL_SetWindowAspectRatio|SDL_SetWindowBordered|SDL_SetWindowFocusable|SDL_SetWindowFullscreen|SDL_SetWindowFullscreenMode|SDL_SetWindowHitTest|SDL_SetWindowIcon|SDL_SetWindowKeyboardGrab|SDL_SetWindowMaximumSize|SDL_SetWindowMinimumSize|SDL_SetWindowModalFor|SDL_SetWindowMouseGrab|SDL_SetWindowMouseRect|SDL_SetWindowOpacity|SDL_SetWindowPosition|SDL_SetWindowRelativeMouseMode|SDL_SetWindowResizable|SDL_SetWindowShape|SDL_SetWindowSize|SDL_SetWindowSurfaceVSync|SDL_SetWindowTitle|SDL_SetiOSAnimationCallback|SDL_ShowAndroidToast|SDL_ShowCursor|SDL_ShowMessageBox|SDL_ShowSimpleMessageBox|SDL_ShowWindow|SDL_ShowWindowSystemMenu|SDL_StartTextInput|SDL_StartTextInputWithProperties|SDL_StopHapticEffect|SDL_StopHapticEffects|SDL_StopHapticRumble|SDL_StopTextInput|SDL_SyncWindow|SDL_TimeToDateTime|SDL_TryLockMutex|SDL_TryLockRWLockForReading|SDL_TryLockRWLockForWriting|SDL_TryWaitSemaphore|SDL_UnlockAudioStream|SDL_UpdateHapticEffect|SDL_UpdateNVTexture|SDL_UpdateTexture|SDL_UpdateWindowSurface|SDL_UpdateWindowSurfaceRects|SDL_UpdateYUVTexture|SDL_Vulkan_CreateSurface|SDL_Vulkan_LoadLibrary|SDL_WaitConditionTimeout|SDL_WaitSemaphoreTimeout|SDL_WarpMouseGlobal|SDL_WriteStorageFile|SDL_WriteSurfacePixel|SDL_WriteSurfacePixelFloat)$";
@@
(
  func(
  ...
  )
- == 0
|
- func(
+ !func(
  ...
  )
- < 0
|
- func(
+ !func(
  ...
  )
- != 0
|
- func(
+ !func(
  ...
  )
- == -1
)
This commit is contained in:
Sam Lantinga 2024-08-22 17:33:49 -07:00
parent d9a5ed75b9
commit 9ff3446f03
726 changed files with 11062 additions and 11472 deletions

View file

@ -73,7 +73,7 @@ typedef struct IOStreamWindowsData
#define READAHEAD_BUFFER_SIZE 1024
static int SDLCALL windows_file_open(IOStreamWindowsData *iodata, const char *filename, const char *mode)
static bool SDLCALL windows_file_open(IOStreamWindowsData *iodata, const char *filename, const char *mode)
{
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT)
UINT old_error_mode;
@ -100,13 +100,13 @@ static int SDLCALL windows_file_open(IOStreamWindowsData *iodata, const char *fi
w_right = (a_mode || SDL_strchr(mode, '+') || truncate) ? GENERIC_WRITE : 0;
if (!r_right && !w_right) {
return -1; // inconsistent mode
return false; // inconsistent mode
}
// failed (invalid call)
iodata->data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
if (!iodata->data) {
return -1;
return false;
}
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT)
// Do not open a dialog box if failure
@ -147,12 +147,12 @@ static int SDLCALL windows_file_open(IOStreamWindowsData *iodata, const char *fi
SDL_free(iodata->data);
iodata->data = NULL;
SDL_SetError("Couldn't open %s", filename);
return -2; // failed (CreateFile)
return false; // failed (CreateFile)
}
iodata->h = h;
iodata->append = a_mode ? true : false;
return 0; // ok
return true; // ok
}
static Sint64 SDLCALL windows_file_size(void *userdata)
@ -190,7 +190,8 @@ static Sint64 SDLCALL windows_file_seek(void *userdata, Sint64 offset, SDL_IOWhe
windowswhence = FILE_END;
break;
default:
return SDL_SetError("windows_file_seek: Unknown value for 'whence'");
SDL_SetError("windows_file_seek: Unknown value for 'whence'");
return -1;
}
windowsoffset.QuadPart = offset;
@ -276,7 +277,7 @@ static size_t SDLCALL windows_file_write(void *userdata, const void *ptr, size_t
return bytes;
}
static int SDLCALL windows_file_close(void *userdata)
static SDL_bool SDLCALL windows_file_close(void *userdata)
{
IOStreamWindowsData *iodata = (IOStreamWindowsData *) userdata;
if (iodata->h != INVALID_HANDLE_VALUE) {
@ -285,7 +286,7 @@ static int SDLCALL windows_file_close(void *userdata)
}
SDL_free(iodata->data);
SDL_free(iodata);
return 0;
return SDL_TRUE;
}
#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
@ -351,12 +352,14 @@ static Sint64 SDLCALL stdio_seek(void *userdata, Sint64 offset, SDL_IOWhence whe
stdiowhence = SEEK_END;
break;
default:
return SDL_SetError("Unknown value for 'whence'");
SDL_SetError("Unknown value for 'whence'");
return -1;
}
#if defined(FSEEK_OFF_MIN) && defined(FSEEK_OFF_MAX)
if (offset < (Sint64)(FSEEK_OFF_MIN) || offset > (Sint64)(FSEEK_OFF_MAX)) {
return SDL_SetError("Seek offset out of range");
SDL_SetError("Seek offset out of range");
return -1;
}
#endif
@ -366,11 +369,13 @@ static Sint64 SDLCALL stdio_seek(void *userdata, Sint64 offset, SDL_IOWhence whe
if (is_noop || fseek(iodata->fp, (fseek_off_t)offset, stdiowhence) == 0) {
const Sint64 pos = ftell(iodata->fp);
if (pos < 0) {
return SDL_SetError("Couldn't get stream offset");
SDL_SetError("Couldn't get stream offset");
return -1;
}
return pos;
}
return SDL_SetError("Error seeking in datastream");
SDL_SetError("Error seeking in datastream");
return -1;
}
static size_t SDLCALL stdio_read(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
@ -393,10 +398,10 @@ static size_t SDLCALL stdio_write(void *userdata, const void *ptr, size_t size,
return bytes;
}
static int SDLCALL stdio_close(void *userdata)
static SDL_bool SDLCALL stdio_close(void *userdata)
{
IOStreamStdioData *iodata = (IOStreamStdioData *) userdata;
int status = 0;
bool status = true;
if (iodata->autoclose) {
if (fclose(iodata->fp) != 0) {
status = SDL_SetError("Error writing to datastream");
@ -469,7 +474,8 @@ static Sint64 SDLCALL mem_seek(void *userdata, Sint64 offset, SDL_IOWhence whenc
newpos = iodata->stop + offset;
break;
default:
return SDL_SetError("Unknown value for 'whence'");
SDL_SetError("Unknown value for 'whence'");
return -1;
}
if (newpos < iodata->base) {
newpos = iodata->base;
@ -505,10 +511,10 @@ static size_t SDLCALL mem_write(void *userdata, const void *ptr, size_t size, SD
return mem_io(userdata, iodata->here, ptr, size);
}
static int SDLCALL mem_close(void *userdata)
static SDL_bool SDLCALL mem_close(void *userdata)
{
SDL_free(userdata);
return 0;
return SDL_TRUE;
}
// Functions to create SDL_IOStream structures from various data sources
@ -596,7 +602,7 @@ SDL_IOStream *SDL_IOFromFile(const char *file, const char *mode)
// Try to open the file from the asset system
void *iodata = NULL;
if (Android_JNI_FileOpen(&iodata, file, mode) < 0) {
if (!Android_JNI_FileOpen(&iodata, file, mode)) {
return NULL;
}
@ -624,7 +630,7 @@ SDL_IOStream *SDL_IOFromFile(const char *file, const char *mode)
return NULL;
}
if (windows_file_open(iodata, file, mode) < 0) {
if (!windows_file_open(iodata, file, mode)) {
windows_file_close(iodata);
return NULL;
}
@ -769,7 +775,7 @@ static size_t SDLCALL dynamic_mem_read(void *userdata, void *ptr, size_t size, S
return mem_io(&iodata->data, ptr, iodata->data.here, size);
}
static int dynamic_mem_realloc(IOStreamDynamicMemData *iodata, size_t size)
static bool dynamic_mem_realloc(IOStreamDynamicMemData *iodata, size_t size)
{
size_t chunksize = (size_t)SDL_GetNumberProperty(SDL_GetIOProperties(iodata->stream), SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER, 0);
if (!chunksize) {
@ -781,7 +787,7 @@ static int dynamic_mem_realloc(IOStreamDynamicMemData *iodata, size_t size)
size_t length = (chunks * chunksize);
Uint8 *base = (Uint8 *)SDL_realloc(iodata->data.base, length);
if (!base) {
return -1;
return false;
}
size_t here_offset = (iodata->data.here - iodata->data.base);
@ -798,7 +804,7 @@ static size_t SDLCALL dynamic_mem_write(void *userdata, const void *ptr, size_t
IOStreamDynamicMemData *iodata = (IOStreamDynamicMemData *) userdata;
if (size > (size_t)(iodata->data.stop - iodata->data.here)) {
if (size > (size_t)(iodata->end - iodata->data.here)) {
if (dynamic_mem_realloc(iodata, size) < 0) {
if (!dynamic_mem_realloc(iodata, size)) {
return 0;
}
}
@ -807,7 +813,7 @@ static size_t SDLCALL dynamic_mem_write(void *userdata, const void *ptr, size_t
return mem_io(&iodata->data, iodata->data.here, ptr, size);
}
static int SDLCALL dynamic_mem_close(void *userdata)
static SDL_bool SDLCALL dynamic_mem_close(void *userdata)
{
const IOStreamDynamicMemData *iodata = (IOStreamDynamicMemData *) userdata;
void *mem = SDL_GetPointerProperty(SDL_GetIOProperties(iodata->stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
@ -815,7 +821,7 @@ static int SDLCALL dynamic_mem_close(void *userdata)
SDL_free(mem);
}
SDL_free(userdata);
return 0;
return true;
}
SDL_IOStream *SDL_IOFromDynamicMem(void)
@ -871,17 +877,17 @@ SDL_IOStream *SDL_OpenIO(const SDL_IOStreamInterface *iface, void *userdata)
return iostr;
}
int SDL_CloseIO(SDL_IOStream *iostr)
SDL_bool SDL_CloseIO(SDL_IOStream *iostr)
{
int retval = 0;
bool result = true;
if (iostr) {
if (iostr->iface.close) {
retval = iostr->iface.close(iostr->userdata);
result = iostr->iface.close(iostr->userdata);
}
SDL_DestroyProperties(iostr->props);
SDL_free(iostr);
}
return retval;
return result;
}
// Load all the data from an SDL data stream