From cca7589486cbd4cfd5acc5a46617aa27e08e242a Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 13 Feb 2014 01:58:50 +0100 Subject: [PATCH] Cleanup. --- include/GLFW/glfw3.h | 27 ++++++++++++++++----------- src/cocoa_window.m | 4 ++-- src/internal.h | 2 +- src/win32_window.c | 43 ++++++++++++++++--------------------------- src/window.c | 7 ++++--- src/x11_window.c | 4 ++-- tests/icons.c | 32 ++++++++++++-------------------- 7 files changed, 53 insertions(+), 66 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 8d2303224..ddeff96a2 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -1916,23 +1916,28 @@ GLFWAPI void glfwGetFramebufferSize(GLFWwindow* window, int* width, int* height) */ GLFWAPI void glfwGetWindowFrameSize(GLFWwindow* window, int* left, int* top, int* right, int* bottom); -/*! @brief Sets the application icons to use for the given window. +/*! @brief Sets the icons for the specified window. + * + * This function sets the icons to be used by the specified window. + * * @param[in] window The window to set the icons for. - * @param[in] icons A pointer to the first element of an array of GLFWimage structs. - * @param[in] numicons The number of icons in the given array. - * @ingroup window + * @param[in] icons An array of @ref GLFWimage structs. + * @param[in] count The number of icons in the array. * * @note This function may only be called from the main thread. * - * @note From all the given icons GLFW will automatically pick the most appropriate - * size for the different locations in which the application icon can occur. For - * example on Windows, if a larger and a smaller icon are given the larger icon - * will be used for the ALT-TAB screen and the smaller for the taskbar. + * @note From all the given icons GLFW will automatically pick the most + * appropriate size for the different locations in which the application icon + * can occur. For example on Windows, if a larger and a smaller icon are given + * the larger icon will be used for the Alt+Tab screen and the smaller for the + * taskbar. * - * @note If the icon does not exactly fit the operating systems requirements for the - * icon size the icon will be automatically resized. + * @note If the icon does not exactly fit the operating systems requirements + * for the icon size the icon will be automatically resized. + * + * @ingroup window */ -GLFWAPI void glfwSetWindowIcons(GLFWwindow* window, GLFWimage* icons, int numicons); +GLFWAPI void glfwSetWindowIcons(GLFWwindow* window, GLFWimage* icons, int count); /*! @brief Iconifies the specified window. * diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 19bc93c2c..dfeea242a 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -1042,9 +1042,9 @@ void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, *bottom = contentRect.origin.y - frameRect.origin.y; } -void _glfwPlatformSetWindowIcons(_GLFWwindow* window, GLFWimage *icons, int numicons) +void _glfwPlatformSetWindowIcons(_GLFWwindow* window, GLFWimage* icons, int count) { - /* TODO: implement this */ + // TODO: Implement this } void _glfwPlatformIconifyWindow(_GLFWwindow* window) diff --git a/src/internal.h b/src/internal.h index 2b1d3df73..6576dcdd7 100644 --- a/src/internal.h +++ b/src/internal.h @@ -542,7 +542,7 @@ void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, int* left, int* top, i /*! @copydoc glfwSetWindowIcons * @ingroup platform */ -void _glfwPlatformSetWindowIcons(_GLFWwindow* window, GLFWimage* icons, int numicons); +void _glfwPlatformSetWindowIcons(_GLFWwindow* window, GLFWimage* icons, int count); /*! @copydoc glfwIconifyWindow * @ingroup platform diff --git a/src/win32_window.c b/src/win32_window.c index 337f87b7d..c99139f17 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -759,15 +759,11 @@ static HICON createIcon(GLFWimage* image) // first we need to convert RGBA to BGRA (yay Windows!) // we also need to convert lines, because Windows wants bottom-to-top RGBA - BGRAData = malloc(image->width * image->height * 4); - if (!BGRAData) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, NULL); - return NULL; - } + BGRAData = calloc(1, image->width * image->height * 4); - for (i = 0; i < image->width * image->height; i++) { - unsigned char *dst = BGRAData + 4 * i; + for (i = 0; i < image->width * image->height; i++) + { + unsigned char* dst = BGRAData + 4 * i; unsigned char *src = image->pixels + 4 * i; dst[0] = src[2]; // copy blue channel @@ -801,19 +797,17 @@ static HICON createIcon(GLFWimage* image) // Chooses the best fitting image given the images and desired size // -static GLFWimage* bestFit(GLFWimage *icons, const int numicons, const int targetWidth, const int targetHeight) +static GLFWimage* bestFit(GLFWimage* icons, const int count, const int targetWidth, const int targetHeight) { - GLFWimage *curIcon = icons; - GLFWimage *bestIcon = curIcon; + GLFWimage* curIcon = icons; + GLFWimage* bestIcon = curIcon; const double targetRatio = (double) targetWidth / targetHeight; - while (curIcon < icons + numicons) + while (curIcon < icons + count) { // always use exact match if (curIcon->width == targetWidth && curIcon->height == targetHeight) - { return curIcon; - } // at least wide or high enough, ratio preferably as close as possible if (curIcon->width >= targetWidth || curIcon->height >= targetHeight) @@ -824,14 +818,10 @@ static GLFWimage* bestFit(GLFWimage *icons, const int numicons, const int target double bestDelta = targetRatio - bestRatio; if (curDelta < 0) - { curDelta = -curDelta; - } - + if (bestDelta < 0) - { bestDelta = -bestDelta; - } // if our ratio is closer OR if the best icon so far isn't large // enough we'll become the new best icon @@ -847,17 +837,16 @@ static GLFWimage* bestFit(GLFWimage *icons, const int numicons, const int target else if (bestIcon->width < targetWidth && bestIcon->height < targetHeight) { if (curIcon->width * curIcon->height > bestIcon->width * bestIcon->height) - { bestIcon = curIcon; - } } - - ++curIcon; + + curIcon++; } - + return bestIcon; } + ////////////////////////////////////////////////////////////////////////// ////// GLFW internal API ////// ////////////////////////////////////////////////////////////////////////// @@ -1059,13 +1048,13 @@ void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, *bottom = rect.bottom - height; } -void _glfwPlatformSetWindowIcons(_GLFWwindow* window, GLFWimage *icons, int numicons) +void _glfwPlatformSetWindowIcons(_GLFWwindow* window, GLFWimage* icons, int count) { GLFWimage* normalicon; GLFWimage* smallicon; - normalicon = bestFit(icons, numicons, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)); - smallicon = bestFit(icons, numicons, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)); + normalicon = bestFit(icons, count, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)); + smallicon = bestFit(icons, count, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)); SendMessage(window->win32.handle, WM_SETICON, ICON_BIG, (LPARAM) createIcon(normalicon)); SendMessage(window->win32.handle, WM_SETICON, ICON_SMALL, (LPARAM) createIcon(smallicon)); diff --git a/src/window.c b/src/window.c index 3bb4e5359..428b26e41 100644 --- a/src/window.c +++ b/src/window.c @@ -513,17 +513,18 @@ GLFWAPI void glfwGetWindowFrameSize(GLFWwindow* handle, _glfwPlatformGetWindowFrameSize(window, left, top, right, bottom); } -GLFWAPI void glfwSetWindowIcons(GLFWwindow* handle, GLFWimage* icons, int numicons) +GLFWAPI void glfwSetWindowIcons(GLFWwindow* handle, GLFWimage* icons, int count) { _GLFWwindow* window = (_GLFWwindow*) handle; _GLFW_REQUIRE_INIT(); - if (numicons < 1) + if (count < 1) { + _glfwInputError(GLFW_INVALID_VALUE, "No icons specified"); return; } - _glfwPlatformSetWindowIcons(window, icons, numicons); + _glfwPlatformSetWindowIcons(window, icons, count); } GLFWAPI void glfwIconifyWindow(GLFWwindow* handle) diff --git a/src/x11_window.c b/src/x11_window.c index e25dc0af2..0f78fdae2 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -1645,9 +1645,9 @@ void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, XFree(extents); } -void _glfwPlatformSetWindowIcons(_GLFWwindow* window, GLFWimage *icons, int numicons) +void _glfwPlatformSetWindowIcons(_GLFWwindow* window, GLFWimage* icons, int count) { - /* TODO: implement this */ + // TODO: Implement this } void _glfwPlatformIconifyWindow(_GLFWwindow* window) diff --git a/tests/icons.c b/tests/icons.c index 819efa129..329ce3f90 100644 --- a/tests/icons.c +++ b/tests/icons.c @@ -34,7 +34,7 @@ #include // a simple glfw logo -const char * const logo[] = { +const char* const logo[] = { "................", "................", "...0000..0......", @@ -54,16 +54,17 @@ const char * const logo[] = { }; const unsigned char icon_colors[5][4] = { - {0x00, 0x00, 0x00, 0xff}, // black - {0xff, 0x00, 0x00, 0xff}, // red - {0x00, 0xff, 0x00, 0xff}, // green - {0xff, 0x00, 0xff, 0xff}, // blue - {0xff, 0xff, 0xff, 0xff} // white + { 0x00, 0x00, 0x00, 0xff }, // black + { 0xff, 0x00, 0x00, 0xff }, // red + { 0x00, 0xff, 0x00, 0xff }, // green + { 0xff, 0x00, 0xff, 0xff }, // blue + { 0xff, 0xff, 0xff, 0xff } // white }; static int cur_icon_color = 0; -static void set_icon(GLFWwindow* window, int icon_color) { +static void set_icon(GLFWwindow* window, int icon_color) +{ GLFWimage img; int x, y; char* pixels; @@ -73,17 +74,9 @@ static void set_icon(GLFWwindow* window, int icon_color) { img.height = 16; pixels = malloc(img.width * img.height * 4); - if (!pixels) + for (x = 0; x < 16; x++) { - glfwTerminate(); - - fprintf(stderr, "Failed to allocate memory.\n"); - exit(EXIT_FAILURE); - } - - for (x = 0; x < 16; ++x) - { - for (y = 0; y < 16; ++y) + for (y = 0; y < 16; y++) { // 15 - y because we need to flip the icon if (logo[15 - y][x] == '0') @@ -116,7 +109,6 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, } } - int main(int argc, char** argv) { GLFWwindow* window; @@ -146,6 +138,6 @@ int main(int argc, char** argv) } glfwTerminate(); - - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } +