3D Scans and Web Presentation

Here is a 3D Scan done with Polycam on my phone:

There you go.

Visual Studio C++ IDE settings

Or makefiles to anyone else…

Two main sections. There is the IDE settings themselves and also the projects Configuration Properties. Note that debugging is in with the IDE and not with the compiler configuration.

IDE Settings

Menu -> Tools -> Options…

C/C++

Scroll Bars: Use map mode for vertical scroll bar, Wide

Debugging

General -> Enable Just My Code (Turn off for stepping through the STL implementation.

Configuration Properties

These can also be set via putting all the values in a .props file. More on that later.

Menu -> Project -> Properties

Only use x64 (Debug and Release).

C/C++ -> Language -> C++ Language Standard -> ISO C++ 17 Standard (/std:c++17)

C/C++ -> General -> Warning Level -> EnableAllWarnings (/Wall)

C/C++ -> General -> Additional Include Directories -> “C:\…\cpp\include”

C/C++ -> General -> Treat Warnings as Errors -> Yes (/WX)

  • Note this is confusing as there are other locations that will also work.
  • Include Directories corresponds to the environment variable INCLUDE.
  •  Additional Include Directories are passed via a command line argument (i.e. the \I option).

C/C++ Precompiled Headers -> Not Using Precompiled Headers

TODO: Lots of other changes to get a minimal command line. I would like to justify each option. Currently at this:

/JMC 
/permissive- 
/ifcOutput "x64\Debug\" 
/GS 
/Wall 
/Zc:wchar_t 
/ZI 
/Gm- 
/Od 
/sdl 
/Fd"x64\Debug\vc142.pdb" 
/Zc:inline 
/fp:precise 
/D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" 
/errorReport:prompt 
/WX 
/Zc:forScope 
/RTC1 
/Gd 
/MDd 
/FC 
/Fa"x64\Debug\" 
/EHsc 
/nologo 
/Fo"x64\Debug\" 
/Fp"x64\Debug\includeTests.pch" 
/diagnostics:column 

C++ Coding Standards

The following is not intended to generate a holy war, but merely a place for me to remind myself of the decisions I took along the way to get to a consistent style for any new code that I write.

IDE

Tabs set to 4, and set it to replace all tabs with spaces. Shift-Tab will backspace a tabs worth of spaces. Using spaces is so the code will look the same when cut and paste into other environments, like these blogs, email, and also online code links to godbolt.org

Compiler

-Wall and then turn off using pragma for any that are not needed.

Use C++ 11 for a simpler life and good compatibility. I am unlikely to use any of the new features. OK so there are a few nice features. I will list them here and if they grow useful enough the default could be moved to C++ 17.

Update: So using structs for partial template specialisation is a nice clean way to do it, but there is a lot of extra code to get to a series of key inline functions that do the special part that is needed depending on the template parameters.. It also make the code harder to write. First you write the code as you would and then remove the core differences into these inline functions. I also found that many cases these are not in critical performance paths so having the extra branch would not be a problem on C++11. So I am moving over to setting C++17 by default from here on in.

  • constexpr if statement
  • Default Template Definition with out <> – Class template argument deduction

if constexpr

This allows for compile time inclusion of code. This is like replacing the preprocessor and having macros.

inline static member variable for a class

This is much more convienant than having to put the declaration in the class and then the static assignment at the end, outside of the class.

inline static const char* emptyConstString = "";

Class template argument deduction (CTAD)

This allows for a class like cString<allocator=heap> and then call it without the template brackets.

enum allocator::source { heap, stack };
// ...
template<int source = allocator::source::heap>
// ...
cString<> usingDefaultAlloc;
cString<allocator::source::stack> fasterAlloc;
// and replace it with
cString usingDefaultAlloc;
cString<allocator::source::stack> fasterAlloc;

However note that it is easier to create specific names for them. Use nameClass for the main class and then the default can be just name

using name= nameClass;

Braces

Allman style with forced braces even for one line. Collapsing code into one neat little line is very tempting, but key reasons not to is for code readability and allowing for a place to step inside the debugger. Vertical space is not that critical if the code is clear and, simple and broken down to low Cyclomatic Complexity. I keep trying and can never get on with K&R style and its variants.

There is an exception so as to prove the above being true.

For code using if constexpr() where it is a one liner selection, that can be without the braces.

if constexpr_(isStack)
    newBlock = alloca(_poolBlockSize);
else
    newBlock = malloc(_poolBlockSize);

Identifiers

Take your pick from: camelCase, PascalCase, snake_case, SCREAMING_SNAKE_CASE.

My preference is camelCase as that is easier to type the snake_case, but the later feels more C++. Modern C++ have moved away from SCREAMING_SNAKE_CASE and I think that is a good move.

I tried snake_case for a while and the underscore is not that much of an issue because of the autocomplete. I still feel that camelCase is more readable and natural. It also takes up less space on a line.

camelCase it is then.

Hungarian or English?

I grew up with strong Hungarian notation and it makes sense in C where the typing is very weak. For modern C++ there is a view that this is not necessary any more as the compiler will warn of any conversions. That leaves member variable and static/global variables. While the m_ approach is nice, if you do that, then why not for everything.

My solution was arrived at by realising the member and static variables are to some degree special and that this can follow the direction indicated by the reserved cases for identifiers (__example, _ANOTHER). Thus, while using underscore is frowned upon, I like the notation to show the intent that these things are ‘part of inner code’. The same approach is to be used for static variables in the class or struct scope and also for local functions that clash with standard library versions.

class entity
{
    static constexpr size_t     _maxEntities = 1000;
    entity(char* name)
    {
        _name = name;
    };
    char    _name[8];
};

inline float _floor(const float& a);

For true global variable, use a single global struct call ‘g’. This allows for a central way to create a warning that these values must be written to very carefully.

struct globals
{
    int     count = 0;
    bool    isGameRunning = false;
}
struct globals g;
...
g.count++;    // Not thread safe.

camelCase can also be used for filenames. This is in contradiction to keeping all filenames lowercase to help with any accidental file use under windows due to case-insensitivity. But for consistency sake and a nice look it will be used here as well.

hashTable.h
imageScaler.h
vectorTests.cpp

Naming Conventions

size_t  n;          // For bytes in a buffer
int     i;          // For loops etc only
size_t  index;      // For array index - don't use 'i'
size_t  length;     // For char* only
size_t  size;       // For number of items in classes
size_t  capacity;   // For current max number of items
type    rhs;        // For operators overloads with lhs and rhs

Use nullptr over NULL to get the type information for the compiler.

Alignment

alignas
alignof
size_of

Align classes and structures to 8 bytes unless they only contain char. This is because all compiles will be 64bit.

If using SSEM then align to 16 bytes and make sure the class or struct is a multiple of 16 using packing.

Project Include Files

I am generating a set of one file includes for various core functionality. EG, Vectors, Intrinsics, Maths, Lists, HashTable, Allocator etc. these are to be used in any project I work on and I have been temporarily using:

#include    "../include/maths.h"

Which is just terrible. The reason is that I am using Visual Studio IDE and I don’t really want to have any changes from the default configuration of the IDE. However I am starting to build up a list of critical things that are needed. So from here on in I will just list them in an IDE set up post and then I add things as needed.

Comments from Various Sources

I’m a contract videogame programmer, so the answer is: I follow whatever the company guidelines call for. Typically, videogame code doesn’t use RTTI or exceptions, and follow CamelCase naming rules, m_ member variables, s_ static variables, and tab=4s. It’s remarkably consistent across the industry, for some reason. BoarsLair

I learned about how Plan 9 C code had non-traditional scheme of #include files where they don’t put #ifdef wrappers in each .h file to allow multiple inclusion and .h files don’t include other .h files. As a result .c files have to include every .h file they need and in the right order. It’s a bit of a pain and no other modern C++ codebase I know of maintains such discipline. But it’s my project so I did it and I keep doing it. It prevents circular dependencies between .h files and doesn’t inflate C++ build times because of careless including the same files over and over again. Chris/Krzysztof Kowalczyk

Hash Functions for Pointers in C++

I was told on Stack Overflow:

“A pointer is just a pointer.”

Well I don’t think it is intended to be in C++. Pass by reference a struct and it knows all about it. But what about char* this is not really pointer to a char, it is a pointer to a string. There is implicit, but clear information about the intended use. The size is defined. It is just not stored.

Continue reading “Hash Functions for Pointers in C++”

Member Function Partial Template Specialisation

I was messing around implementing a hash function to be able to do the equivalent of std::map_unordered. I realised that I would like to have some functions specialised but that most of them would be OK use the default implementation. I tried to do this and found the compiler would not let me. Google helped out and I found that this is not in the language. So I thought that this must have a community accepted idiom to achieve the same thing. This is the story of that journey.

Continue reading “Member Function Partial Template Specialisation”

Buying a New Keyboard – The Mechanical Renascence

So you have an old Microsoft Wireless Desktop 3000 Keyboard and Mouse. You get used to it and it becomes your ‘personal standard’ for keyboards. It has full travel keypress and all the keys laid out the way you want them – the large Enter Key and the Media Mute key. But then it gets old and breaks and they don’t make that version anymore, so you look for the closest replacement. In this case the cheaper Microsoft Wireless Keyboard 2000. It’s OK and you put up with it, but the mouse starts to break as well. What to do…

Microsoft Wireless Desktop 3000 Reviews | TechSpot
Microsoft Wireless Desktop 3000 Keyboard (with the full size UK Enter Key)
Continue reading “Buying a New Keyboard – The Mechanical Renascence”

Easy to Make Mistakes in Class Inheritance

This started as play with simple class inheritance to work out the syntax and idiomatic way to use them in C++. I just kept typing things that I thought would work and the compiler would slowly teach me what was wrong.

Continue reading “Easy to Make Mistakes in Class Inheritance”

Learning C++

This is a series of things that I found out when learning C++. The approach is going to non-standard and likely to annoy any normal C++ professional. I am interested in languages in general and so I will be doing all my early code without and STL at all. I want to learn what features the language offers before starting to use the standard libraries. To this end I try to do do simple version of the features that are already available. That way I can see how these features are implemented in the standard libraries. To me that feels like a important advantage in learning the language. I did try to read the code for the libraries but it has a lot of very nuanced code that I will only get to understand much later on. The code is still interesting to step through however as it does offer clues as to how to unpick various implementations.

Continue reading “Learning C++”

Streaming a Window Without Hardware

Given what OBS does (screen grab and encode) it would seem that there should be a simple way to capture a window and create a local video stream from it. This could then be viewed remotely served from your home PC, or used as an input in the composition for a stream scene.

This is clearly done using a HW video capture device, but that is expensive and given everything is already digital, seems like an odd detour.

An example use case for this is if you were using a separate Windows laptop of tablet for drawing and wanted to be able include it in a Twitch stream. Web cameras do this and use the RTSP protocol. So I went looking for a simple way to set this up on a new machine.

Continue reading “Streaming a Window Without Hardware”