diff --git a/imgui.cpp b/imgui.cpp index 5c6823e5..75298d46 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -377,7 +377,7 @@ static ImU32 crc32(const void* data, size_t data_size, ImU32 seed = 0) { ImU32 crc = i; for (ImU32 j = 0; j < 8; j++) - crc = (crc >> 1) ^ (-int(crc & 1) & polynomial); + crc = (crc >> 1) ^ (ImU32(-int(crc & 1)) & polynomial); crc32_lut[i] = crc; } } @@ -395,16 +395,14 @@ static size_t ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) int w = vsnprintf(buf, buf_size, fmt, args); va_end(args); buf[buf_size-1] = 0; - if (w == -1) w = (int)buf_size; - return w; + return (w == -1) ? buf_size : (size_t)w; } static size_t ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) { int w = vsnprintf(buf, buf_size, fmt, args); buf[buf_size-1] = 0; - if (w == -1) w = (int)buf_size; - return w; + return (w == -1) ? buf_size : (size_t)w; } static ImU32 ImConvertColorFloat4ToU32(const ImVec4& in) @@ -899,13 +897,13 @@ void ImGuiTextBuffer::append(const char* fmt, ...) return; const size_t write_off = Buf.size(); - if (write_off + len >= Buf.capacity()) + if (write_off + (size_t)len >= Buf.capacity()) Buf.reserve(Buf.capacity() * 2); Buf.resize(write_off + (size_t)len); va_start(args, fmt); - ImFormatStringV(&Buf[write_off] - 1, len+1, fmt, args); + ImFormatStringV(&Buf[write_off] - 1, (size_t)len+1, fmt, args); va_end(args); } @@ -1051,13 +1049,14 @@ static void LoadSettings() return; if (fseek(f, 0, SEEK_END)) return; - long f_size = ftell(f); - if (f_size == -1) + const long f_size_signed = ftell(f); + if (f_size_signed == -1) return; + size_t f_size = (size_t)f_size_signed; if (fseek(f, 0, SEEK_SET)) return; char* f_data = new char[f_size+1]; - f_size = (long)fread(f_data, 1, f_size, f); // Text conversion alter read size so let's not be fussy about return value + f_size = fread(f_data, 1, f_size, f); // Text conversion alter read size so let's not be fussy about return value fclose(f); if (f_size == 0) { @@ -3008,7 +3007,7 @@ void PushID(const void* ptr_id) window->IDStack.push_back(window->GetID(ptr_id)); } -void PushID(int int_id) +void PushID(const int int_id) { const void* ptr_id = (void*)(intptr_t)int_id; ImGuiWindow* window = GetCurrentWindow(); @@ -3362,7 +3361,7 @@ enum ImGuiPlotType static float PlotGetValue(const float* values, size_t stride, int idx) { - float v = *(float*)((unsigned char*)values + idx * stride); + const float v = *(float*)((unsigned char*)values + (size_t)idx * stride); return v; } @@ -3615,14 +3614,14 @@ void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int idx, int n) bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int idx, const char* new_text, int new_text_len) { char* buf_end = obj->Text + obj->BufSize; - const int text_len = (int)strlen(obj->Text); + const size_t text_len = strlen(obj->Text); if (new_text_len > buf_end - (obj->Text + text_len + 1)) return false; - memmove(obj->Text + idx + new_text_len, obj->Text + idx, text_len - idx); - memcpy(obj->Text + idx, new_text, new_text_len); - obj->Text[text_len + new_text_len] = 0; + memmove(obj->Text + (size_t)idx + new_text_len, obj->Text + (size_t)idx, text_len - (size_t)idx); + memcpy(obj->Text + (size_t)idx, new_text, (size_t)new_text_len); + obj->Text[text_len + (size_t)new_text_len] = 0; return true; } @@ -4065,7 +4064,7 @@ static bool Combo_ArrayGetter(void* data, int idx, const char** out_text) bool Combo(const char* label, int* current_item, const char** items, int items_count, int popup_height_items) { - bool value_changed = Combo(label, current_item, Combo_ArrayGetter, (void*)items, items_count, popup_height_items); + const bool value_changed = Combo(label, current_item, Combo_ArrayGetter, (void*)items, items_count, popup_height_items); return value_changed; } @@ -4138,7 +4137,7 @@ bool Combo(const char* label, int* current_item, bool (*items_getter)(void*, int ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x); ImGui::TextUnformatted(label, FindTextDisplayEnd(label)); - ImGui::PushID(id); + ImGui::PushID((int)id); bool menu_toggled = false; if (hovered) { @@ -4546,7 +4545,7 @@ float GetColumnOffset(int column_index) if (column_index < 0) column_index = window->DC.ColumnCurrent; - const ImGuiID column_id = ImGuiID(window->DC.ColumnsSetID + column_index); + const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(column_index); RegisterAliveId(column_id); const float default_t = column_index / (float)window->DC.ColumnsCount; const float t = (float)window->StateStorage.GetInt(column_id, (int)(default_t * 8096)) / 8096; // Cheaply store our floating point value inside the integer (could store an union into the map?) @@ -4562,7 +4561,7 @@ void SetColumnOffset(int column_index, float offset) if (column_index < 0) column_index = window->DC.ColumnCurrent; - const ImGuiID column_id = ImGuiID(window->DC.ColumnsSetID + column_index); + const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(column_index); const float t = (offset - window->DC.ColumnStartX) / (window->Size.x - g.Style.ScrollBarWidth - window->DC.ColumnStartX); window->StateStorage.SetInt(column_id, (int)(t*8096)); } @@ -4612,7 +4611,7 @@ void Columns(int columns_count, const char* id, bool border) { float x = window->Pos.x + GetColumnOffset(i); - const ImGuiID column_id = ImGuiID(window->DC.ColumnsSetID + i); + const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(i); const ImGuiAabb column_aabb(ImVec2(x-4,y1),ImVec2(x+4,y2)); if (IsClipped(column_aabb)) @@ -4839,7 +4838,7 @@ void ImDrawList::AddArc(const ImVec2& center, float rad, ImU32 col, int a_min, i if (tris) { - ReserveVertices((a_max-a_min) * 3); + ReserveVertices((unsigned int)(a_max-a_min) * 3); for (int a = a_min; a < a_max; a++) { AddVtx(center + circle_vtx[a % IM_ARRAYSIZE(circle_vtx)] * rad, col); @@ -4849,7 +4848,7 @@ void ImDrawList::AddArc(const ImVec2& center, float rad, ImU32 col, int a_min, i } else { - ReserveVertices((a_max-a_min) * 6); + ReserveVertices((unsigned int)(a_max-a_min) * 6); for (int a = a_min; a < a_max; a++) AddVtxLine(center + circle_vtx[a % IM_ARRAYSIZE(circle_vtx)] * rad, center + circle_vtx[(a+1) % IM_ARRAYSIZE(circle_vtx)] * rad, col); } @@ -4960,7 +4959,7 @@ void ImDrawList::AddCircle(const ImVec2& centre, float radius, ImU32 col, int nu if ((col >> 24) == 0) return; - ReserveVertices(num_segments*6); + ReserveVertices((unsigned int)num_segments*6); const float a_step = 2*PI/(float)num_segments; float a0 = 0.0f; for (int i = 0; i < num_segments; i++) @@ -4976,7 +4975,7 @@ void ImDrawList::AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, if ((col >> 24) == 0) return; - ReserveVertices(num_segments*3); + ReserveVertices((unsigned int)num_segments*3); const float a_step = 2*PI/(float)num_segments; float a0 = 0.0f; for (int i = 0; i < num_segments; i++) @@ -4998,17 +4997,17 @@ void ImDrawList::AddText(ImFont font, float font_size, const ImVec2& pos, ImU32 text_end = text_begin + strlen(text_begin); // reserve vertices for worse case - const int char_count = (int)(text_end - text_begin); - const int vtx_count_max = char_count * 6; + const unsigned int char_count = (unsigned int)(text_end - text_begin); + const unsigned int vtx_count_max = char_count * 6; const size_t vtx_begin = vtx_buffer.size(); ReserveVertices(vtx_count_max); font->RenderText(font_size, pos, col, clip_rect_stack.back(), text_begin, text_end, vtx_write); - // give unused vertices - vtx_buffer.resize(vtx_write - &vtx_buffer.front()); - const int vtx_count = (int)(vtx_buffer.size() - vtx_begin); - commands.back().vtx_count -= (vtx_count_max - vtx_count); + // give back unused vertices + vtx_buffer.resize((size_t)(vtx_write - &vtx_buffer.front())); + const size_t vtx_count = vtx_buffer.size() - vtx_begin; + commands.back().vtx_count -= (unsigned int)(vtx_count_max - vtx_count); vtx_write -= (vtx_count_max - vtx_count); } @@ -5049,8 +5048,10 @@ bool ImBitmapFont::LoadFromFile(const char* filename) return false; if (fseek(f, 0, SEEK_END)) return false; - if ((DataSize = (int)ftell(f)) == -1) + const long f_size = ftell(f); + if (f_size == -1) return false; + DataSize = (size_t)f_size; if (fseek(f, 0, SEEK_SET)) return false; if ((Data = (unsigned char*)malloc(DataSize)) == NULL) @@ -5058,7 +5059,7 @@ bool ImBitmapFont::LoadFromFile(const char* filename) fclose(f); return false; } - if ((int)fread(Data, 1, DataSize, f) != DataSize) + if (fread(Data, 1, DataSize, f) != DataSize) { fclose(f); free(Data); @@ -5069,7 +5070,7 @@ bool ImBitmapFont::LoadFromFile(const char* filename) return LoadFromMemory(Data, DataSize); } -bool ImBitmapFont::LoadFromMemory(const void* data, int data_size) +bool ImBitmapFont::LoadFromMemory(const void* data, size_t data_size) { Data = (unsigned char*)data; DataSize = data_size; @@ -5357,9 +5358,9 @@ static void SetClipboardTextFn_DefaultImpl(const char* text, const char* text_en } if (!text_end) text_end = text + strlen(text); - GImGui.PrivateClipboard = (char*)malloc(text_end - text + 1); - memcpy(GImGui.PrivateClipboard, text, text_end - text); - GImGui.PrivateClipboard[text_end - text] = 0; + GImGui.PrivateClipboard = (char*)malloc((size_t)(text_end - text) + 1); + memcpy(GImGui.PrivateClipboard, text, (size_t)(text_end - text)); + GImGui.PrivateClipboard[(size_t)(text_end - text)] = 0; } #endif @@ -5455,7 +5456,7 @@ void ShowTestWindow(bool* open) static bool no_scrollbar = false; static float fill_alpha = 0.65f; - const ImU32 layout_flags = (no_titlebar ? ImGuiWindowFlags_NoTitleBar : 0) | (no_border ? 0 : ImGuiWindowFlags_ShowBorders) | (no_resize ? ImGuiWindowFlags_NoResize : 0) | (no_move ? ImGuiWindowFlags_NoMove : 0) | (no_scrollbar ? ImGuiWindowFlags_NoScrollbar : 0); + const ImGuiWindowFlags layout_flags = (no_titlebar ? ImGuiWindowFlags_NoTitleBar : 0) | (no_border ? 0 : ImGuiWindowFlags_ShowBorders) | (no_resize ? ImGuiWindowFlags_NoResize : 0) | (no_move ? ImGuiWindowFlags_NoMove : 0) | (no_scrollbar ? ImGuiWindowFlags_NoScrollbar : 0); ImGui::Begin("ImGui Test", open, ImVec2(550,680), fill_alpha, layout_flags); ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); @@ -5620,7 +5621,7 @@ void ShowTestWindow(bool* open) static bool pause; static ImVector values; if (values.empty()) { values.resize(100); memset(&values.front(), 0, values.size()*sizeof(float)); } - static int values_offset = 0; + static size_t values_offset = 0; if (!pause) { // create dummy data at 60 hz @@ -5634,7 +5635,7 @@ void ShowTestWindow(bool* open) phase += 0.10f*values_offset; } } - ImGui::PlotLines("Frame Times", &values.front(), (int)values.size(), values_offset, "avg 0.0", -1.0f, 1.0f, ImVec2(0,70)); + ImGui::PlotLines("Frame Times", &values.front(), (int)values.size(), (int)values_offset, "avg 0.0", -1.0f, 1.0f, ImVec2(0,70)); ImGui::SameLine(); ImGui::Checkbox("pause", &pause); ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0,70)); @@ -5841,7 +5842,7 @@ void ShowTestWindow(bool* open) ImGui::SameLine(); if (ImGui::Button("Add 1000 lines")) { - for (size_t i = 0; i < 1000; i++) + for (int i = 0; i < 1000; i++) log.append("%i The quick brown fox jumps over the lazy dog\n", lines+i); lines += 1000; } diff --git a/imgui.h b/imgui.h index 667037c8..d1574b6e 100644 --- a/imgui.h +++ b/imgui.h @@ -622,7 +622,7 @@ struct ImBitmapFont #pragma pack(pop) unsigned char* Data; // Raw data, content of .fnt file - int DataSize; // + size_t DataSize; // bool DataOwned; // const FntInfo* Info; // (point into raw data) const FntCommon* Common; // (point into raw data) @@ -637,7 +637,7 @@ struct ImBitmapFont ImBitmapFont(); ~ImBitmapFont() { Clear(); } - bool LoadFromMemory(const void* data, int data_size); + bool LoadFromMemory(const void* data, size_t data_size); bool LoadFromFile(const char* filename); void Clear(); void BuildLookupTable();