From c6aae4cde48e5baa302c79e82f6e67473981ef84 Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 16 Jul 2018 11:05:06 +0000 Subject: [PATCH] Start using C++ in-place member initialization. We should make good use of in-place member initialization. Many new classes have constructors that effectively do nothing but default-initialize POD members, and when adding new members, it is very easy to miss initializing them. With in-place initialization, the code is more compact, the diffs are nicer, and it's harder to miss them. This commit only converts render/ and platform/ to use in-place member initialization, since there was a bug in CairoRenderer, but we should convert the entire codebase. --- src/render/gl3shader.h | 14 ++++++------ src/render/render.h | 48 ++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/render/gl3shader.h b/src/render/gl3shader.h index 86e87b451..5b110e632 100644 --- a/src/render/gl3shader.h +++ b/src/render/gl3shader.h @@ -67,7 +67,7 @@ class Vector4f { class Shader { public: - GLuint program; + GLuint program = 0; void Init(const std::string &vertexRes, const std::string &fragmentRes, @@ -104,7 +104,7 @@ class MeshRenderer { Shader lightShader; Shader fillShader; - Shader *selectedShader; + Shader *selectedShader = NULL; void Init(); void Clear(); @@ -152,8 +152,8 @@ class EdgeRenderer { Shader shader; - const StippleAtlas *atlas; - StipplePattern pattern; + const StippleAtlas *atlas = NULL; + StipplePattern pattern = {}; void Init(const StippleAtlas *atlas); void Clear(); @@ -192,8 +192,8 @@ class OutlineRenderer { Shader shader; - const StippleAtlas *atlas; - StipplePattern pattern; + const StippleAtlas *atlas = NULL; + StipplePattern pattern = {}; void Init(const StippleAtlas *atlas); void Clear(); @@ -242,7 +242,7 @@ class IndexedMeshRenderer { Shader colShader; Shader pointShader; - Shader *selectedShader; + Shader *selectedShader = NULL; void Init(); void Clear(); diff --git a/src/render/render.h b/src/render/render.h index 07eeca4db..ca838c4ae 100644 --- a/src/render/render.h +++ b/src/render/render.h @@ -135,11 +135,10 @@ class Canvas { bool Equals(const Fill &other) const; }; - IdList strokes; - IdList fills; - BitmapFont bitmapFont; + IdList strokes = {}; + IdList fills = {}; + BitmapFont bitmapFont = {}; - Canvas() : strokes(), fills(), bitmapFont() {} virtual void Clear(); hStroke GetStroke(const Stroke &stroke); @@ -197,7 +196,7 @@ class BatchCanvas : public Canvas { class UiCanvas { public: std::shared_ptr canvas; - bool flip; + bool flip = false; void DrawLine(int x1, int y1, int x2, int y2, RgbaColor color, int width = 1, int zIndex = 0); @@ -216,17 +215,14 @@ class UiCanvas { // A canvas that performs picking against drawn geometry. class ObjectPicker : public Canvas { public: - Camera camera; + Camera camera = {}; // Configuration. - Point2d point; - double selRadius; + Point2d point = {}; + double selRadius = 0.0; // Picking state. - double minDistance; - int maxZIndex; - uint32_t position; - - ObjectPicker() : camera(), point(), selRadius(), - minDistance(), maxZIndex(), position() {} + double minDistance = 0.0; + int maxZIndex = 0; + uint32_t position = 0; const Camera &GetCamera() const override { return camera; } @@ -261,18 +257,17 @@ class ObjectPicker : public Canvas { // on the CPU. class SurfaceRenderer : public Canvas { public: - Camera camera; - Lighting lighting; + Camera camera = {}; + Lighting lighting = {}; // Chord tolerance, for converting beziers to pwl. - double chordTolerance; + double chordTolerance = 0.0; // Render lists. handle_map edges; handle_map beziers; - SMesh mesh; + SMesh mesh = {}; // State. - BBox bbox; + BBox bbox = {}; - SurfaceRenderer() : camera(), lighting(), chordTolerance(), mesh(), bbox() {} void Clear() override; // Canvas interface. @@ -323,15 +318,13 @@ class SurfaceRenderer : public Canvas { class CairoRenderer : public SurfaceRenderer { public: - cairo_t *context; + cairo_t *context = NULL; // Renderer configuration. - bool antialias; + bool antialias = false; // Renderer state. struct { hStroke hcs; - } current; - - CairoRenderer() : context(), current() {} + } current = {}; void SelectStroke(hStroke hcs); void MoveTo(Vector p); @@ -353,8 +346,9 @@ class CairoRenderer : public SurfaceRenderer { // An offscreen renderer based on OpenGL framebuffers. class GlOffscreen { public: - unsigned int framebuffer; - unsigned int colorRenderbuffer, depthRenderbuffer; + unsigned int framebuffer = 0; + unsigned int colorRenderbuffer = 0; + unsigned int depthRenderbuffer = 0; std::vector data; bool Render(int width, int height, std::function renderFn);