diff options
| author | David Pinedo <david@lunarg.com> | 2015-04-21 14:45:16 -0600 |
|---|---|---|
| committer | Courtney Goeltzenleuchter <courtney@LunarG.com> | 2015-04-22 10:48:24 -0600 |
| commit | e8f49cad9078a58025aefa06bd179e2a2c09ee94 (patch) | |
| tree | f60f7a93df7f43975775f46521293b24268b29d3 | |
| parent | eb6b91c759e1a6fe02aa84874f7db1d42d642019 (diff) | |
| download | usermoji-e8f49cad9078a58025aefa06bd179e2a2c09ee94.tar.xz | |
demos: create console for vulkaninfo iff needed
| -rw-r--r-- | demos/CMakeLists.txt | 14 | ||||
| -rw-r--r-- | demos/vulkaninfo.c | 79 |
2 files changed, 83 insertions, 10 deletions
diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt index 68c51893..1d3d2275 100644 --- a/demos/CMakeLists.txt +++ b/demos/CMakeLists.txt @@ -59,10 +59,18 @@ else() set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DEXTERNAL_SPV") endif() -add_executable(vulkaninfo vulkaninfo.c) +if(UNIX) + add_executable(vulkaninfo vulkaninfo.c) +else() + add_executable(vulkaninfo WIN32 vulkaninfo.c) +endif() target_link_libraries(vulkaninfo ${LIBRARIES}) -add_executable(tri tri.c) +if(UNIX) + add_executable(tri tri.c) +else() + add_executable(tri WIN32 tri.c) +endif() target_link_libraries(tri ${LIBRARIES}) if(NOT WIN32) @@ -78,7 +86,7 @@ else() set_target_properties(libpngd PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/${LIB_DIR}/libpngd.lib) add_library(zlibd STATIC IMPORTED) set_target_properties(zlibd PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/${LIB_DIR}/zlibd.lib) - add_executable(cube cube.c) + add_executable(cube WIN32 cube.c) target_link_libraries(cube ${LIBRARIES} libpngd zlibd) endif() diff --git a/demos/vulkaninfo.c b/demos/vulkaninfo.c index ff1db50e..3b17c5d9 100644 --- a/demos/vulkaninfo.c +++ b/demos/vulkaninfo.c @@ -29,6 +29,8 @@ #ifdef _WIN32 #include <Windows.h> +#include <fcntl.h> +#include <io.h> #endif #include <vulkan.h> @@ -36,7 +38,27 @@ #define ERR(err) printf("%s:%d: failed with %s\n", \ __FILE__, __LINE__, vk_result_string(err)); -#define ERR_EXIT(err) do { ERR(err); exit(-1); } while (0) +#ifdef _WIN32 + +bool consoleCreated = false; + +#define WAIT_FOR_CONSOLE_DESTROY \ + do { \ + if (consoleCreated) \ + Sleep(INFINITE); \ + } while (0) +#else + #define WAIT_FOR_CONSOLE_DESTROY +#endif + + +#define ERR_EXIT(err) \ + do { \ + ERR(err); \ + fflush(stdout); \ + WAIT_FOR_CONSOLE_DESTROY; \ + exit(-1); \ + } while (0) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) @@ -683,9 +705,8 @@ int main(int argc, char **argv) err = vkCreateInstance(&inst_info, &inst); if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { printf("Cannot find a compatible Vulkan installable client driver " - "(ICD).\nExiting ...\n"); - fflush(stdout); - exit(1); + "(ICD).\n"); + ERR_EXIT(err); } else if (err) { ERR_EXIT(err); @@ -694,9 +715,8 @@ int main(int argc, char **argv) if (err) ERR_EXIT(err); if (gpu_count > MAX_GPUS) { - printf("Too many GPUS found \nExiting ...\n"); - fflush(stdout); - exit(1); + printf("Too many GPUS found \n"); + ERR_EXIT(VK_ERROR_UNKNOWN); } err = vkEnumeratePhysicalDevices(inst, &gpu_count, objs); if (err) @@ -719,10 +739,55 @@ int main(int argc, char **argv) } #ifdef _WIN32 + +// Create a console window with a large scrollback size to which to send stdout. +// Returns true if console window was successfully created, false otherwise. +bool SetStdOutToNewConsole() +{ + // don't do anything if we already have a console + if (GetStdHandle(STD_OUTPUT_HANDLE)) + return false; + + // allocate a console for this app + AllocConsole(); + + // redirect unbuffered STDOUT to the console + HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + int fileDescriptor = _open_osfhandle((intptr_t)consoleHandle, _O_TEXT); + FILE *fp = _fdopen( fileDescriptor, "w" ); + *stdout = *fp; + setvbuf( stdout, NULL, _IONBF, 0 ); + + // make the console window bigger + CONSOLE_SCREEN_BUFFER_INFO csbi; + SMALL_RECT r; + COORD bufferSize; + if (!GetConsoleScreenBufferInfo(consoleHandle, &csbi)) + return false; + bufferSize.X = csbi.dwSize.X; + bufferSize.Y = 1000; + if (!SetConsoleScreenBufferSize(consoleHandle, bufferSize)) + return false; + r.Left = r.Top = 0; + r.Right = csbi.dwSize.X-1; + r.Bottom = 60; + if (!SetConsoleWindowInfo(consoleHandle, true, &r)) + return false; + + // change the console window title + if (!SetConsoleTitle(TEXT("vulkaninfo"))) + return false; + + return true; +} + int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow) { char *argv = pCmdLine; + consoleCreated = SetStdOutToNewConsole(); main(1, &argv); fflush(stdout); + if (consoleCreated) + Sleep(INFINITE); } #endif |
