diff options
| author | Karl Schultz <karl@lunarg.com> | 2018-05-29 13:09:22 -0600 |
|---|---|---|
| committer | Karl Schultz <karl@lunarg.com> | 2018-05-30 17:09:07 -0600 |
| commit | b7940409945face40657080b6074fc5588a0c0ad (patch) | |
| tree | bf3f09afc62a76f2c2004bbba25de8e3bfb80a4c /cube/cube.cpp | |
| parent | 1956ee7fb6b2902c16d118a8f579a10ac083a963 (diff) | |
| download | usermoji-b7940409945face40657080b6074fc5588a0c0ad.tar.xz | |
build: Add cube(pp) to install target
- Modify cube and cubepp to obtain the texture image data from
an include file instead of reading a PPM file at runtime.
(This is the way it works for Android)
This removes the need to install an image file or otherwise
make an image file available in a repo build.
- Add cube and cubepp to the install target.
Note: The file handling code is left in place to make it easy
to add a "-texture_file <file>" option so a user can pass in
a texture file.
Fixes #5
Diffstat (limited to 'cube/cube.cpp')
| -rw-r--r-- | cube/cube.cpp | 54 |
1 files changed, 17 insertions, 37 deletions
diff --git a/cube/cube.cpp b/cube/cube.cpp index a7218617..9973fa0c 100644 --- a/cube/cube.cpp +++ b/cube/cube.cpp @@ -2301,58 +2301,38 @@ void Demo::update_data_buffer() { device.unmapMemory(swapchain_image_resources[current_buffer].uniform_memory); } +/* Convert ppm image data from header file into RGBA texture image */ +#include "lunarg.ppm.h" bool Demo::loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout *layout, int32_t *width, int32_t *height) { -#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)) - filename = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@(filename)].UTF8String; -#endif - - FILE *fPtr = fopen(filename, "rb"); - if (!fPtr) { + (void)filename; + char *cPtr; + cPtr = (char *)lunarg_ppm; + if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) { return false; } - - char header[256]; - char *cPtr = fgets(header, 256, fPtr); // P6 - if (cPtr == nullptr || strncmp(header, "P6\n", 3)) { - fclose(fPtr); - return false; - } - - do { - cPtr = fgets(header, 256, fPtr); - if (cPtr == nullptr) { - fclose(fPtr); - return false; - } - } while (!strncmp(header, "#", 1)); - - sscanf(header, "%" SCNd32 " %" SCNd32, width, height); - if (rgba_data == nullptr) { - fclose(fPtr); + while (strncmp(cPtr++, "\n", 1)) + ; + sscanf(cPtr, "%u %u", width, height); + if (rgba_data == NULL) { return true; } - - char *result = fgets(header, 256, fPtr); // Format - VERIFY(result != nullptr); - if (cPtr == nullptr || strncmp(header, "255\n", 3)) { - fclose(fPtr); + while (strncmp(cPtr++, "\n", 1)) + ; + if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) { return false; } - + while (strncmp(cPtr++, "\n", 1)) + ; for (int y = 0; y < *height; y++) { uint8_t *rowPtr = rgba_data; - for (int x = 0; x < *width; x++) { - size_t s = fread(rowPtr, 3, 1, fPtr); - (void)s; + memcpy(rowPtr, cPtr, 3); rowPtr[3] = 255; /* Alpha of 1 */ rowPtr += 4; + cPtr += 3; } - rgba_data += layout->rowPitch; } - - fclose(fPtr); return true; } |
