Skip to content

Commit

Permalink
Start using C++ in-place member initialization.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
whitequark committed Jul 16, 2018
1 parent 501a482 commit c6aae4c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
14 changes: 7 additions & 7 deletions src/render/gl3shader.h
Expand Up @@ -67,7 +67,7 @@ class Vector4f {

class Shader {
public:
GLuint program;
GLuint program = 0;

void Init(const std::string &vertexRes,
const std::string &fragmentRes,
Expand Down Expand Up @@ -104,7 +104,7 @@ class MeshRenderer {

Shader lightShader;
Shader fillShader;
Shader *selectedShader;
Shader *selectedShader = NULL;

void Init();
void Clear();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -242,7 +242,7 @@ class IndexedMeshRenderer {
Shader colShader;
Shader pointShader;

Shader *selectedShader;
Shader *selectedShader = NULL;

void Init();
void Clear();
Expand Down
48 changes: 21 additions & 27 deletions src/render/render.h
Expand Up @@ -135,11 +135,10 @@ class Canvas {
bool Equals(const Fill &other) const;
};

IdList<Stroke, hStroke> strokes;
IdList<Fill, hFill> fills;
BitmapFont bitmapFont;
IdList<Stroke, hStroke> strokes = {};
IdList<Fill, hFill> fills = {};
BitmapFont bitmapFont = {};

Canvas() : strokes(), fills(), bitmapFont() {}
virtual void Clear();

hStroke GetStroke(const Stroke &stroke);
Expand Down Expand Up @@ -197,7 +196,7 @@ class BatchCanvas : public Canvas {
class UiCanvas {
public:
std::shared_ptr<Canvas> canvas;
bool flip;
bool flip = false;

void DrawLine(int x1, int y1, int x2, int y2, RgbaColor color, int width = 1,
int zIndex = 0);
Expand All @@ -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; }

Expand Down Expand Up @@ -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<hStroke, SEdgeList> edges;
handle_map<hStroke, SBezierList> beziers;
SMesh mesh;
SMesh mesh = {};
// State.
BBox bbox;
BBox bbox = {};

SurfaceRenderer() : camera(), lighting(), chordTolerance(), mesh(), bbox() {}
void Clear() override;

// Canvas interface.
Expand Down Expand Up @@ -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);
Expand All @@ -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<uint8_t> data;

bool Render(int width, int height, std::function<void()> renderFn);
Expand Down

0 comments on commit c6aae4c

Please sign in to comment.