From 69b8a3b3b3fe731016cc7b8c2fd6b9f7aa9b3bf8 Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 18 May 2016 10:40:56 +0000 Subject: [PATCH] In Pixmap and BitmapFont, use std::vector instead of std::unique_ptr. MSVC 2013 keeps having aggravating bugs where its unique_ptr implementation wants to use deleted functions. In this case the worst that could happen is one copy per entity once during initialization (which should in principle be elided anyway because a return value is a prvalue... no idea if MSVC does actually do that). --- src/glhelper.cpp | 7 +++---- src/resource.cpp | 10 +++++----- src/resource.h | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/glhelper.cpp b/src/glhelper.cpp index 9dd1c08..08564b7 100644 --- a/src/glhelper.cpp +++ b/src/glhelper.cpp @@ -575,7 +575,7 @@ void ssglDrawPixmap(const Pixmap &pixmap, bool flip) { int format = pixmap.hasAlpha ? GL_RGBA : GL_RGB; glTexImage2D(GL_TEXTURE_2D, 0, format, pixmap.width, pixmap.height, 0, - format, GL_UNSIGNED_BYTE, pixmap.data.get()); + format, GL_UNSIGNED_BYTE, &pixmap.data[0]); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); @@ -612,8 +612,7 @@ static void LoadBitmapFont() { BuiltinBitmapFont.AddGlyph(0xE006, LoadPNG("fonts/private/6-stipple-dash.png")); BuiltinBitmapFont.AddGlyph(0xE007, LoadPNG("fonts/private/7-stipple-zigzag.png")); // Unifont doesn't have a glyph for U+0020. - std::unique_ptr blank(new uint8_t[8*16*3] {}); - BuiltinBitmapFont.AddGlyph(0x20, Pixmap({ 8, 16, 8*3, false, std::move(blank) })); + BuiltinBitmapFont.AddGlyph(0x20, Pixmap({ 8, 16, 8*3, false, std::vector(8*16*3) })); } void ssglInitializeBitmapFont() @@ -628,7 +627,7 @@ void ssglInitializeBitmapFont() glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, BitmapFont::TEXTURE_DIM, BitmapFont::TEXTURE_DIM, - 0, GL_ALPHA, GL_UNSIGNED_BYTE, BuiltinBitmapFont.texture.get()); + 0, GL_ALPHA, GL_UNSIGNED_BYTE, &BuiltinBitmapFont.texture[0]); } int ssglBitmapCharWidth(char32_t codepoint) { diff --git a/src/resource.cpp b/src/resource.cpp index 764a32e..a271da9 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -73,7 +73,7 @@ void Pixmap::Clear() { } RgbaColor Pixmap::GetPixel(size_t x, size_t y) const { - uint8_t *pixel = &data[y * stride + x * GetBytesPerPixel()]; + const uint8_t *pixel = &data[y * stride + x * GetBytesPerPixel()]; if(hasAlpha) { return RgbaColor::From(pixel[0], pixel[1], pixel[2], pixel[3]); @@ -94,7 +94,7 @@ static Pixmap ReadPNGIntoPixmap(png_struct *png_ptr, png_info *info_ptr) { if(stride % 4 != 0) stride += 4 - stride % 4; pixmap.stride = stride; - pixmap.data = std::unique_ptr(new uint8_t[pixmap.stride * pixmap.height]); + pixmap.data = std::vector(pixmap.stride * pixmap.height); uint8_t **rows = png_get_rows(png_ptr, info_ptr); for(size_t y = 0; y < pixmap.height; y++) { memcpy(&pixmap.data[pixmap.stride * y], rows[y], @@ -264,7 +264,7 @@ static uint8_t *BitmapFontTextureRow(uint8_t *texture, uint16_t position, size_t BitmapFont BitmapFont::From(std::string &&unifontData) { BitmapFont font = {}; font.unifontData = std::move(unifontData); - font.texture = std::unique_ptr(new uint8_t[TEXTURE_DIM * TEXTURE_DIM]); + font.texture = std::vector(TEXTURE_DIM * TEXTURE_DIM); return font; } @@ -279,7 +279,7 @@ void BitmapFont::AddGlyph(char32_t codepoint, const Pixmap &pixmap) { glyphs.emplace(codepoint, std::move(glyph)); for(size_t y = 0; y < pixmap.height; y++) { - uint8_t *row = BitmapFontTextureRow(texture.get(), glyph.position, y); + uint8_t *row = BitmapFontTextureRow(&texture[0], glyph.position, y); for(size_t x = 0; x < pixmap.width; x++) { if(pixmap.GetPixel(x, y).ToPackedInt() != 0) { row[x] = 255; @@ -348,7 +348,7 @@ const BitmapFont::Glyph &BitmapFont::GetGlyph(char32_t codepoint) { // Fill in the texture (one texture byte per glyph bit). for(size_t y = 0; y < 16; y++) { - uint8_t *row = BitmapFontTextureRow(texture.get(), glyph.position, y); + uint8_t *row = BitmapFontTextureRow(&texture[0], glyph.position, y); for(size_t x = 0; x < 16; x++) { if(glyphBits[y] & (1 << (15 - x))) { row[x] = 255; diff --git a/src/resource.h b/src/resource.h index 96052cf..68d97b3 100644 --- a/src/resource.h +++ b/src/resource.h @@ -26,7 +26,7 @@ public: size_t height; size_t stride; bool hasAlpha; - std::unique_ptr data; + std::vector data; static Pixmap FromPNG(const uint8_t *data, size_t size); static Pixmap FromPNG(FILE *f); @@ -49,7 +49,7 @@ public: std::string unifontData; std::map glyphs; - std::unique_ptr texture; + std::vector texture; uint16_t nextPosition; static BitmapFont From(std::string &&unifontData);