Quantcast
Channel: Bartek's coding blog
Viewing all 325 articles
Browse latest View live

The Matrix Updated

$
0
0

The Matrix demo project

Sometimes you can find interesting stuff in your past projects!
One day I was thinking about new post topics for the blog, but somehow, I got not much energy to do it. So, I just browsed through my very old projects (that are actually listed in my portfolio site). Memories came back and I decided maybe it’s time to refresh the ancient code files!

This time let’s go into Matrix!

Intro

Near the end of 2003, after me and my friends watching all the Matrix Movies, we got all crazy about the trilogy (which ended at that time). One friend suggested that maybe I could do some animation related to the ‘matrix rain’ from the movie intro. I said “Why not!”.

The below clip shows Matrix Reloaded intro:

In the animation we go from simple ‘matrix rain’ into a complex 3d scene and then into the first scene of the actual movie.

Matrix Reloaded intro sequence

There were (and still are) many screen savers and demos that are using the matrix effect idea. So I knew that implementing a simple rain of letters wouldn’t be that awesome. I needed something more. At the same time I realized that making full animation (like in the intro) is also beyond my capabilities - I couldn’t create that advanced clock/machinery 3d scene. But what if I could simplify this idea?

After some investigation, trials and errors I’ve decided to create a very simple 3d scene and put it behind the falling letters!

The showcase

So what we have here?

  • There is a really simple 3d clock animation (it shows the current hour)
  • Camera moves around
  • The whole scene is projected using the matrix effect as a postprocessing

How it works

We have the following core parts:

  • Setup
  • Matrix rain
  • 3d clock animation
  • Postprocessing effect that renders glyphs

Setup

The demo uses Allegro Game library 4.03 for rendering, implemented in Dev Cpp, Windows.

We need an off-screen bitmap with the resolution of scr_w/font_width x scr_h/font_height. For example 1600/8 x 900/8 = 200x112 pixels. Initially, I used just a 8x8 system font, but I also experimented with matrix style font. For me, the system font looked actually better for this effect than the matrix font.
Please note, that we also need another off-screen bitmap, a buffer, that will be used for double buffering.

Matrix rain

Each column of the little off-screen bitmap has a particle that moves from top to the bottom. Each particle has an initial velocity, there is no gravity here. The particle is renderer with a fading trail:

Falling lines

Click to see animating gif

The letters are falling from the top, but in the real effect, if I am correct, they can start in the middle of a window… so this might be worth checking.

3d clock animation

Most of 3d code (Matrix calculation, rotations, camera) is written from scratch and uses only basic Allegro methods:

  • set_projection_viewport - spef from allegro 4.4.2 - stores the correct projection viewport of the scene.
  • clip3d_f - spec from allegro 4.4.2 - this handles clipping, so I can just send my transformed vertices (before projection) and get clipped output.
  • persp_project_f - spec from allegro 4.4.2 - does the final perspective projection using my camera and screen settings.

Then, we render models in wire-frame mode only and only in the places covered by lines (not empty, black space), so do_line is invoked there to put pixels in proper places - it just check if existing color is not zero (not black) and then puts a pixel:

voidPutLetter(BITMAP *bmp,int x,int y,int c)
{
if(x >=0&& x < bmp->w && y >=0&& y < bmp->h)
{
if(bmp->line[y][x]>0)
bmp
->line[y][x]= c;
}
}

3d clock on top of falling lines

Postprocessing

The current state of the effect looks quite horrible on its own, the resolution is not acceptable, the wire-frame model is too simple. But with postprocessing it gets a bit better shape.

The current mini buffer is copied into the back buffer, but each pixel is replaced by a glyph:

for(i =0; i < map->w; i++)
{
for(j =0; j < map->h; j++)
{
txt
[0]= letter;
textout_ex
(back_buffer, matrix_font, txt,
i
*font_width, j*font_height,// x, y
map
->line[j][i],0);
}
}

the letter is a char code that will be shown on the screen. We have actually several options here:

  • random (originally implemented) - glyphs change every frame
  • based on pos + color (glyphs don’t change) - this is what you can see in the compiled youtube clip.
  • predefine: like when you press F2 you see ‘+Fen mode’ :)

Recompiling the old code

I was quite surprised that the original exe file worked well on my Win10 machine!. I could just double click on the file and play with the effect. Still, there were some problems with selecting a nice resolution.

Original matrix app window

The above picture shows a default Allegro GFX mode selector UI. As you can see most of this is for windows/monitors with 4:3 aspect ratio! No HD option unfortunately. And, what’s more problematic: I couldn’t enable fullscreen mode.

I decided that it would be nice if I could get this working in HD resolution or at least give more options of sizes. But how to recompile this thing? Do I have those old tools… ?

Here’s what I’ve gathered:

  • Allegro 5 contained a bit of breaking changes related to the version 4, so recompiling my old code (that used version 4.03 ) would be not that easy in the newest version.
  • I’ve downloaded version 4.2.1 and only a few minor updates were needed
  • DevCpp 4.9.2 is quite old and not updated any more, but you can grab Orwell DevCpp - http://orwelldevcpp.blogspot.com/

Somehow, after setting up the project one again, making sure you use proper compiler (MinGW, not TDM…) surprisingly I could play with the code!

Updates

I wanted to keep the old effect, but still some updates were made:

  • code was a bit improved, but please don’t use it to learn C++! It’s quite old, C style coding, lots of global variables, bad variable naming…. but it works :D
  • I’ve added option to pass window width and height as a command line params.
  • There is a new option to stop camera animation - F4
  • Show mini buffer - F5
  • Initially all the glyphs were random (so there was a lot of flickering), I’ve changed it a bit so it uses a glyph based on x/y/col value.

Future ideas:

  • Rewrite it to OpenGL… or maybe even WebGL. There is not much content to download, so it should be a tiny web app. We could use similar approach, render offscreen and then use postprocessing effect. Most of the things could be written in shaders. There are lots of such effects on shadertoy.
  • Find better, matrix font
  • Optimize: this is questionable actually. The effect runs pretty smoothly, even id Debug mode! I could optimize this as an exercise, but this wouldn’t be a huge gain.

Summary

GitHub Repo: https://github.com/fenbf/matrix - Please watch out for the code quality... it's really outdated! :)

Download the original exe file: link here

This was really great to play with this ancient project. It was more than 10 years (13 to be exact) when I’ve implemented the code. Fortunately, my programming skills improved and I write better code now. But in terms of creativity, at that time I was probably better at this. I’d love to return to writing such demos and little animations.


Visual Studio C++ Productivity Tips

$
0
0

Visual Studio Productivity Tips

Visual Studio is my main development environment. I’ve been using this tool probably since version 2003…2005. I am really happy that VS is getting more and more powerful these days and you can also use it on multiple-platforms (through VS Code, for web or cloud apps). What’s even better - it’s free for personal use or if you’re a small company (Community Version)! Today, I’ve prepared some productivity tips that might make your life a bit easier when developing an app in this Microsoft IDE.

I target native app development, but most of the rules should be generic to other languages as well.

Learn how to move in code

This is a very broad topic and probably the most important aspect on my list. When you learn programming you might work with simple projects with just several code files. In that scenario you have most of the code in your head. But, in a commercial projects you will end up with hundreds of files and classes. How to move efficiently is such complicated structures? You can easily forget about file hierarchy, let alone classes and subsystems.

Here are some basic steps:

Use go to definition/declaration

Probably, this is what I use the most often: switching between definition and declaration of a function, a variable or a class.

While you’re reading some code inside a method just by clicking on the method name (F12, Ctr+Alt+F12) you can go to its declaration or vice versa. Then, you can see where the function/method header is located, what class it’s part of.

This works not only for methods or types, but also for variables/members: in a long method, you can just go to definition of some variable to see where it’s declared.

Possible problem: sometimes this feature does not work well for large projects :) Intellisense is from time to time stubborn and does not want to cooperate. But if you’re patient you’ll reach the target eventually. Also Intellisense is greatly improved with each version of Visual Studio.

The most powerful thing: being able to navigate to anything you like. Maybe you want to check a function? or a class declaration? or some other symbol? In Visual Studio (Starting from version 2010) there is “Navigate To” command: Ctrl + COMMA

Navigate To in Visual Studio

Enter the short-cut and then start typing a symbol name you want to check. VS will dynamically look for it and will give you a list of found items. You can easily navigate to a particular line in the code.

Navigate to with symbol highlight

More about Navigate To:

I use that kind of navigation frequently. Sometimes I want to find something, but don’t know the proper type name. In that case I start typing some possible words that should match and usually I find what I was looking for.

In VisualAssist (described in the Plugins section) there is even more powerful tool: Find Symbol in Solution. This windows lets you quickly find any symbol in the solution, you can use prefixes, negative filters, multiple strings, etc.

See all references

This feature is very handy if you know what type/method you are looking for and you want to see where it’s being used. You can explore examples and recreate them in your solution.

At work, I often use this to track all the places that are related to some type/symbol. For example, when I need to add a similar type (some kind of a new attribute for a graphical element) I can search for all occurrences of some existing type and do the same for my new type: like recreate initialization, add to lists of supported types, register in UI, etc.

Recently, I’ve noticed that it might be nice to get something like “find some references”. This would be handy if you know that a function/code is used in hundreds of places (so search times might be huge), but you’re actually interested in any appearance, only a few would satisfy. This would work much faster. On the other hand, the IDE doesn’t know what items are most valuable to you… so it’s safer to return all the occurrences.

You might be also interested in using View Call Hierarchy feature, that shows calling hierarchy better that just finding all references.

Unfortunately, I must admit, I don’t have large RAM in my head and I constantly forget where was I two minutes ago in the project :) Luckily, I can navigate backwards and forwards to previously visited places - Ctrl+ - and Ctrl + Shift + -!

Navigate Backward And Forward

Alternatively, if you’re doing changes in two files, you can switch between them by using Ctrl+Tab. This will show the list of recently used and open files so you can open it immediately.

There are also bookmarks that can be leveraged. You might mark important places of the code and switch between those places. Still, I don’t use them much often. Moving back and forth to recently visited places seems to be creating sort of natural bookmarks and you don’t have to remember additional shortcuts. On the other hand using bookmarks might be a good exercise.

Other tools

  • View Call Hierarchy - Finding all references of a function might be ok, but VS offers more advanced feature: Call Hierarchy. This shows what other functions called your original method.
  • Class Hierarchy - Handy when you want to track what are the derived classes from you given class, or you want to track the common root of types. This feature is also connected to Object Browser.
  • Switch between header and implementation file - another basic tool - Ctrl+K, Ctrl+O. Especially useful when you write implementation of a class, and want to go to class declaration.
  • // TODO - Task List, mark important places in the code with // TODO: text and then you can see them in Task List Window.

Learn shortcuts

keyboard shortcuts

Bookmark one one this pages:

Print it, keep it near your desk and learn those shortcuts daily.

Enable enhanced scroll bar

Every time I return to a simpler IDE that hasn’t enhanced scroll bar feature I am a bit lost. Enhanced scroll bar adds a huge boost to the way you can move in a file. In simple cases, you might remember where your method is located: is it at the beginning, near the middle or at the end of a file… that might work for some time. But the advanced scroll bar adds a map where you can find your code much faster. It creates a bigger picture and thus your brain can memorize where the code is ‘visually’ located in a file. No more random scrollbar searches! :)

Enhanced scroll bar

Since most of lines have like 80…120 characters and you probably use a monitor with wide aspect ratio, then you should have some space on the right/left side in the editor. So even if you use very wide scrollbar it shouldn’t decrease the space for the code itself.

Also, the scrollbar shows some additional information like last modification, breakpoints, compiler errors and of course where is the currently edited line.

Read more about enhanced scroll bar at MSDN blog

Note that it’s great to have relatively small files. Otherwise your scrollbar map will just look like a normal, almost one colour, bar:

Enhanced scroll bar for a long file
There is a chance to still get some value of such map, but the shorter the file the better in my opinion.

In Productivity Tools For VS2013 there is a addition called “Structure Visualizer”. With “Show code structure in the margin” option you’ll get a nice visualization of code blocks in a file.
Code structure, Productivity Power Tools 2013
Above image comes from official plugin site: visualstudiogallery/Productivity Power Tools 2013

Install addons

Visual Studio is a very decent environment, but what if we could do better? Look for plugins that help with your daily tasks and fills some of the gaps that VS has.

VisualAssist

VisualAssist from Whole Tomato Software

I am a great fan of VisualAssist. For example, you can read some of my tips for this tool in my post from 2014: 3 Tools to Understand New Code from Visual Assist . VA is a complete set of tools for refactoring, code navigation, coding assistance… even debugging. Or to write this shortly: set of productivity tools.

Just take a look here:

It’s hard to tell when I am not using VA features - because it’s enabled all the time. But things like renaming a symbol name, extracting a method, class, documenting a function header, finding symbols, find base method declaration, class hierarchies… all of that are natural and really handy with VA. Also I am really impressed with the performance, rarely I need to wait for the results. As I mentioned before, you often need to wait for Intellisense, but with VA you don’t see such situations.

It’s not free (279$ for the full licence, 99$ for personal, 49$ for academic), but I highly encourage to save some money and buy the extension.

ReSharper for C++

ReSharper For C++ by JetBrains

I’ve never used it, but always wanted :) I need to allocate some time and experiment with the trial version.

Resharper is a well known extension for .NET developers, but eventually the company was able to produce similar version for C++! It’s also a really large set of productivity tools.

BTW: here is the comparison (made by JetBrains): VisualAssist vs ReSharper for C++

In terms of pricing you can use several models, for example € 199.00 for first year, € 159.00 second, … or use some discounts.

Productivity Power Tools for VS

Productivity Power Tools 2013
Productivity Power Tools 2015

This time it’s a completely free set of tools that highly improve work with VS. Even if you have Visual Assist or ReSharper the plugin can still bring some value.

Here is the full list of features:

  • Peek Help
  • Solution Explorer Errors
  • Structure Visualizer
  • Double click to maximize windows
  • Timestamp margin
  • Quick tasks – Edit Present On
  • Ctrl + Click to Peek Definition
  • HTML Copy improvements
  • Recently Closed Documents
  • Match Margin
  • Power Commands context menu cleanup
  • Syntactic Line Compression
  • Quick Tasks
  • Power Commands
  • Color printing
  • Middle-Click Scrolling
  • Organize Imports for Visual Basic
  • Custom Document Well
  • Tools Options Support
  • HTML Copy
  • Fix Mixed Tabs
  • Ctrl + Click Go To Definition
  • Align Assignments
  • Column Guides
  • Colorized Parameter Help

I especially like: Scrollbar markers (that shows additional code structure on the scrollbar), Recently Closed Documents, Document Tabs and some of the power commands.

Other interesting plugins

  • AnkhSVN - Subversion Support - khSVN open source (free) Subversion SourceControl Provider.
  • C++ Quick Fixes - VS 2015 only, add some help (in context menu) to fix some of the basic coding errors (like missing include, semicolon, using namespace, etc).
  • VS10x CodeMAP - displays a graphical nested representation of the current code editor window (C#, VB, C++).
  • Viasfora Add color to your Visual Studio Text Editor!
  • Image Watch - Provides a watch window for visualizing in-memory images (bitmaps) when debugging native C++ code.
  • DoxygenComments - extension that highlights Doxygen documentation tags inside C++, C# and JavaScript comments.
  • Visual C++ Refactoring - VS 2013 only, in VS 2015 more refactoring features were added.
  • GraphicalDebugging - VS 2015 only, the extension allows to view graphical representation of variables, e.g. Boost.Geometry models.
  • CodeMaid - an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, R, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
  • IncrediBuild - IncrediBuild speeds up builds by distributing C#, C/C++ compilations and other tasks across machines in the local network.

Some more suggestions in this old StackOverflow question: Recommended add-ons/plugins for Microsoft Visual Studio

Exercise

Like with every other tool practice is needed. Usually, after some time you’ll get used to any IDE and you find your own ways to be relatively productive. Still, this doesn’t mean it’s end of the story. Why not testing some other embedded tools, other techniques, key shortcuts, plugins, etc, etc… ?

Here are some suggestions for additional exercises for VS IDE:

  • Try to minimize mouse usage, do most of the stuff using only keyboard.
  • Learn one keyboard shortcut a day/week
  • I mention that I don’t use bookmarks, but as an exercise I could use them for a day or two and then decide if it improves my workflow or not.
  • Install ‘deep/large’ plugins like VisualAssist or ReSharper for C++ and (even trial versions) and play with them. Learn what tools and techniques do such plugins bring to the IDE.

Other programming IDE QT Creator, VIM, Eclipse

  • Use some other IDE! Do your pet project in some other IDE like QT Creator, Eclipse, etc, etc… or even use a command line + Vim. Maybe you’ll learn something that can be used in Visual Studio later?

Summary

Visual Studio small logo

Visual Studio is a very powerful IDE and it might be relatively hard to leverage all of its features. I hope that thanks to this article you’ve learn some techniques that will help when you work in the IDE. Since writing and reading code is probably the most often activity I think that ‘moving in code’ features are crucial to know. If you can quickly jump between files, locate classes, track variables, go to method definitions… (all via keyboard short-cuts of course! :)) then you can more focus on the actual job and not the problems with the tool.

I didn’t cover a lot of things: like build options, debugging, refactoring, windows layout, themes… maybe those are good topics for other articles. Let me know what would you like to read more.

Your turn

  • What are your tips for Visual Studio?
  • Do you use some additional tools to work with this IDE?
  • What is the most disappointing feature of VS?

Custom Deleters for C++ Smart Pointers

$
0
0

Custom deleters for C++ smart pointers

Let’s say we have the following code:

LegacyList* pMyList =newLegacyList();
...
pMyList
->ReleaseElements();
delete pMyList;

In order to fully delete an object we need to do some additional action.

How to make it more C++11? How to use unique_ptr or shared_ptr here?

Intro

We all know that smart pointers are really nice things and we should be using them instead of raw new and delete. But what if deleting a pointer is not only the thing we need to call before the object is fully destroyed? In our short example we have to call ReleaseElements() to completely clear the list.

Side Note: we could simply redesign LegacyList so that it properly clears its data inside its destructor. But for this exercise we need to assume that LegacyList cannot be changed (it’s some legacy, hard to fix code, or it might come from a third party library).

ReleaseElements is only my invention for this article. Other things might be involved here instead: logging, closing a file, terminating a connection, returning object to C style library… or in general: any resource releasing procedure, RAII.

To give more context to my example, let’s discuss the following use of LegacyList:

classWordCache{
public:
WordCache(){ m_pList =nullptr;}
~WordCache(){ClearCache();}

voidUpdateCache(LegacyList*pInputList){
ClearCache();
m_pList
= pInputList;
if(m_pList)
{
// do something with the list...
}
}

private:
voidClearCache(){
if(m_pList){
m_pList
->ReleaseElements();
delete m_pList;
m_pList
=nullptr;
}
}

LegacyList*m_pList;// owned by the object
};

You can play with the source code here:using Coliru online compiler.

This is a bit old style C++ class. The class owns the m_pList pointer, so it has to be cleared in the constructor. To make life easier there is ClearCache() method that is called from the destructor or from UpdateCache().

The main method UpdateCache() takes pointer to a list and gets ownership of that pointer. The pointer is deleted in the destructor or when we update the cache again.

Simplified usage:

WordCache myTestClass;

LegacyList* pList =newLegacyList();
// fill the list...
myTestClass
.UpdateCache(pList);

LegacyList* pList2 =newLegacyList();
// fill the list again
// pList should be deleted, pList2 is now owned
myTestClass
.UpdateCache(pList2);

With the above code there shouldn’t be any memory leaks, but we need to carefully pay attention what’s going on with the pList pointer. This is definitely not modern C++!

Let’s update the code so it’s modernized and properly uses RAII (smart pointers in these cases). Using unique_ptr or shared_ptr seems to be easy, but here we have a slight complication: how to execute this additional code that is required to fully delete LegacyList ?

What we need is a Custom Deleter

Custom Deleter for shared_ptr

I’ll start with shared_ptr because this type of pointer is more flexible and easier to use.

What should you do to pass a custom deleter? Just pass it when you create a pointer:

std::shared_ptr<int> pIntPtr(newint(10),
[](int*pi){delete pi;});// deleter

The above code is quite trivial and mostly redundant. If fact, it’s more or less a default deleter - because it’s just calling delete on a pointer. But basically, you can pass any callable thing (lambda, functor, function pointer) as deleter while constructing a shared pointer.

In the case of LegacyList let’s create a function:

voidDeleteLegacyList(LegacyList* p){
p
->ReleaseElements();
delete p;
}

The modernized class is super simple now:

classModernSharedWordCache{
public:
voidUpdateCache(std::shared_ptr<LegacyList> pInputList){
m_pList
= pInputList;
// do something with the list...
}

private:
std
::shared_ptr<LegacyList> m_pList;
};
  • No need for constructor - the pointer is initialized to nullptr by default
  • No need for destructor - pointer is cleared automatically
  • No need for helper ClearCache - just reset pointer and all the memory and resources are properly cleared.

When creating the pointer we need to pass that function:

ModernSharedWordCache mySharedClass;
std
::shared_ptr<LegacyList> ptr(newLegacyList(),
DeleteLegacyList)
mySharedClass
.UpdateCache(ptr);

As you can see there is no need to take care about the pointer, just create it (remember about passing a proper deleter) and that’s all.

Were is custom deleter stored?

When you use a custom deleter it won’t affect the size of your shared_ptr type. If you remember, that should be roughly 2 x sizeof(ptr) (8 or 16 bytes)… so where does this deleter hide?

shared_ptr consists of two things: pointer to the object and pointer to the control block (that contains reference counter for example). Control block is created only once per given pointer, so two shared_pointers (for the same pointer) will point to the same control block.

Inside control block there is a space for custom deleter and allocator.

Can I use make_shared?

Unfortunately you can pass a custom deleter only in the constructor of shared_ptr there is no way to use make_shared. This might be a bit of disadvantage, because as I described in Why create shared_ptr with make_shared? - from my old blog post, make_shared allocates the object and its control block for it next to each other in memory. Without make_shared you get two, probably separate, blocks of allocated mem.

Update: I got a very good comment on reddit: from quicknir saying that I am wrong in this point and there is something you can use instead of make_shared.

Indeed, you can use allocate_shared and leverage both the ability to have custom deleter and being able to share the same memory block. However, that requires you to write custom allocator, so I considered it to be too advanced for the original article.

Custom Deleter for unique_ptr

With unique_ptr there is a bit more complication. The main thing is that a deleter type will be part of unique_ptr type.

By default we get std::default_delete:

template<
class T,
classDeleter= std::default_delete<T>
>class unique_ptr;

Deleter is part of the pointer, heavy deleter (in terms of memory consumption) means larger pointer type.

What to chose as deleter?

What is best to use as a deleter? Let’s consider the following options:

  1. std::function
  2. Function pointer
  3. Stateless functor
  4. State-full functor
  5. Lambda

What is the smallest size of unique_ptr with the above deleter types? Can you guess? (Answer at the end of the article)

How to use?

For our example problem let’s use a functor:

structLegacyListDeleterFunctor{
voidoperator()(LegacyList* p){
p
->ReleaseElements();
delete p;
}
};

And here is a usage in the updated class:

classModernWordCache{
public:
using unique_legacylist_ptr =
std
::unique_ptr<LegacyList,
LegacyListDeleterFunctor>;

public:
voidUpdateCache(unique_legacylist_ptr pInputList){
m_pList
= std::move(pInputList);
// do something with the list...
}

private:
unique_legacylist_ptr m_pList
;
};

Code is a bit more complex than the version with `shared_ptr` - we need to define a proper pointer type. Below I show how to use that new class:

ModernWordCache myModernClass;
ModernWordCache::unique_legacylist_ptr pUniqueList(newLegacyList());
myModernClass
.UpdateCache(std::move(pUniqueList));

All we have to remember, since it’s a unique pointer, is to move the pointer rather than copy it.

Can I use make_unique?

Similarly as with shared_ptr you can pass a custom deleter only in the constructor of unique_ptr and thus you cannot use make_unique. Fortunately, make_unique is only for convenience (wrong!) and doesn’t give any performance/memory benefits over normal construction.

Update: I was too confident about make_unique :) There is always a purpose for such functions. Look here GotW #89 Solution: Smart Pointers - guru question 3:

make_unique is important because:
First of all:

Guideline: Use make_unique to create an object that isn’t shared (at
least not yet), unless you need a custom deleter or are adopting a raw
pointer from elsewhere.

Secondly:
make_unique gives exception safety: Exception safety and make_unique

So, by using a custom deleter we lose a bit of security. It’s worth knowig the risk behind that choice. Still, custom deleter with unique_ptr is far more better than playing with raw pointers.

Things to remember:
Custom Deleters give a lot of flexibility that improves resource
management in your apps.

Summary

In this post I’ve shown you how to use custom deleters with C++ smart pointer: shared_ptr and unique_ptr. Those deleters can be used in all the places wher ‘normal’delete ptr is not enough: when you wrap FILE*, some kind of a C style structure (SDL_FreeSurface, free(), destroy_bitmap from Allegro library, etc).
Remember that proper garbage collection is not only related to memory destruction, often some other actions needs to be invoked. With custom deleters you have that option.

Gist with the code is located here: fenbf/smart_ptr_deleters.cpp

Let me know what are your common problems with smart pointers?
What blocks you from using them?

References

Answer to the question about pointer size
1. std::function - heavy stuff, on 64 bit, gcc it showed me 40 bytes.
2. Function pointer - it’s just a pointer, so now unique_ptr contains two pointers: for the object and for that function… so 2*sizeof(ptr) = 8 or 16 bytes.
3. Stateless functor (and also stateless lambda) - it’s actually very tircky thing. You would probably say: two pointers… but it’s not. Thanks to empty base optimization - EBO the final size is just a size of one pointer, so the smallest possible thing.
4. State-full functor - if there is some state inside the functor then we cannot do any optimizations, so it will be the size of ptr + sizeof(functor)
5. Lambda (statefull) - similar to statefull functor

Google benchmark library

$
0
0

Some time ago I wrote about micro benchmarking libraries for C++ - here’s the link. I’ve described three libraries: Nonius, Hayai, Celero. But actually, I wanted to cover fourth one. Google Benchmark library was at that time not available for my Windows environment, so I couldn’t test it. Fortunately, under the original post I got a comment saying that the library is now ready for Visual Studio!

Let’s see how can we use it.

The library

Main github repo: github/google/benchmark
Discussion group: groups.google/forum/benchmark-discuss

Thanks to KindDragon commit: Support MSVC on appveyor we can now build the library under Visual Studio. Without any problems I was able to download the latest repo, use CMake to generate solution files, then build a proper version. To use it with your project, all you have to do is to link to the library itself and include one header file.

Simple example

In the original article I’ve used two experiments:

  • IntToStringConversionTest(count) - converts numbers 0…count-1 into a string and return vector of it.
  • DoubleToStringConversionTest(count) - converts numbers 0.12345… count-1+0.12345 into a string and then return vector of those strings.

The full example of benchmarks:

#include"benchmark/benchmark_api.h"
#include"../commonTest.h"

voidIntToString(benchmark::State& state){
while(state.KeepRunning()){
benchmark
::DoNotOptimize(
IntToStringConversionTest(state.range_x())
);
}
}
BENCHMARK
(IntToString)->Arg(TEST_NUM_COUNT1000);

voidDoubleToString(benchmark::State& state){
while(state.KeepRunning()){
benchmark
::DoNotOptimize(
DoubleToStringConversionTest(state.range_x())
);
}
}
BENCHMARK
(DoubleToString)->Arg(TEST_NUM_COUNT1000);

BENCHMARK_MAIN
()

Nice and simple! BENCHMARK macro is used to define a benchmark, then you can add invocation params. In the example above I’ve used Arg method. The parameter inside that method will be passed into the state object that is available to the benchmark function. In our example we can fetch the value as state.range_x(). This value is then translated into the size of the numbers vector.

Inside the benchmark function there is a while loop where the main code is executed. The library will automatically set the number of iterations.

As usually the application can be run in console mode with the following result:
Google Benchmark library results

We get a really simple output: benchmark name, time in nanoseconds (can be changed through Unit() method), CPU time, iterations invoked.

What are the nice features of the library?

  • Easy passing of custom values: Arg, ArgPair, Range, RangePair, Apply.
    • Values can be fetched as state.get_x(), state.get_y()
    • So you can create one or two dimensional problem space benchmarks.
  • Fixtures
  • Multithreaded benchmarks
  • Manual timing: useful when you execute code on GPU or other devices where standard CPU timing is not relevant.
  • Output formats: tabular, CSV, Json
  • Ability to insert custom label through state.SetLabel()
  • Labels for items processed and bytes processed thanks to state.SetItemsProcessed() and state.SetBytesProcessed()

Another output: with bytes processed, items processed, custom label and changed time units.

Advanced example

In the another post about micro benchmarking libraries I’ve used a bit more advanced example to test benchmark libraries. It’s my standard benchmark - vector of pointers vs vector of objects. Let’s see how can we implement that experiment with Google Benchmark.

Setup

What we’re going to test:

  • Particle class: holds 18 floats: 4 for pos, 4 for vel, 4 for acceleration, 4 for color, one for time, one for rotation. Plus there is a float buffer - we can change number of elements in that buffer.
    • Basic particle is 76 bytes
    • Larger particle is defined for 160 bytes.
  • We want to measure Update method on a vector of particles.
  • Five kind of containers:
    • vector<Particle>
    • vector<shared_ptr<Particle>> - randomized mem location
    • vector<shared_ptr<Particle>> - not randomized mem location
    • vector<unique_ptr<Particle>> - randomized mem location
    • vector<unique_ptr<Particle>> - not randomized mem location

Some code

Example code for vector<Particle:

template<classPart>
classParticlesObjVectorFixture:public::benchmark::Fixture{
public:
voidSetUp(const::benchmark::State& st){
particles
= std::vector<Part>(st.range_x());

for(auto&p : particles)
p
.generate();
}

voidTearDown(const::benchmark::State&){
particles
.clear();
}

std
::vector<Part> particles;
};

And the benchmark:

using P76Fix =ParticlesObjVectorFixture<Particle>;
BENCHMARK_DEFINE_F
(P76Fix,Obj)(benchmark::State& state){
while(state.KeepRunning()){
UpdateParticlesObj(particles);
}
}
BENCHMARK_REGISTER_F
(P76Fix,Obj)->Apply(CustomArguments);

using P160Fix =ParticlesObjVectorFixture<Particle160>;
BENCHMARK_DEFINE_F
(P160Fix,Obj)(benchmark::State& state){
while(state.KeepRunning()){
UpdateParticlesObj(particles);
}
}
BENCHMARK_REGISTER_F
(P160Fix,Obj)->Apply(CustomArguments);

With the above code we test for two kinds of particles: smaller - 76 bytes abd larger - 160 bytes. CustomArguments method generate number of particles in each benchmark invocation: 1k, 3k, 5k, 7k, 9k, 11k.

Results

In this blog post we focus on the library itself, but I wanted to cover one thing that was asked in the past: different size of a particle. I used only two kinds for now: 76 bytes and 160 bytes.

Results for 76 bytes:
Particle of the size 76bytes, updates

Randomized pointers are almost 76% slower than vector of objects.

Results for 160 bytes:
Particle of the size 160bytes, updates

Almost straight lines for the larger case! Randomized pointers are only 17% slower…. ok, so maybe not completely straight :)

Additionally, unique_ptr is also tested. And, as you can see, in terms of updates (access to the data) the speed is almost the same as for shared_ptr. The indirection is the problem and not the overhead of the smart pointer.

Summary

Repo with the code samples: github/fenbf/benchmarkLibsTest

I had no problems with using Google Benchmark library. In several minutes you can grasp style of building benchmarks. Multithreaded benchmarks, fixtures, automatic iteration estimation, CSV, or Json output formats, those are all solid features. I especially liked flexibility of passing parameters to the benchmark code. Other libraries that I’ve tested had some problems with passing a ‘problem space’ values into benchmark code. Celero was the easiest on in that area.
What’s missing for me is the lack of advanced results. The library provides only a mean time of the iterations. Still, in most cases that’s good enough.

In terms of the experiment: I got interesting results when measuring different size per particle. It’s a good base for the final future test. I’ll try to recreate my examples again with more diversity of object size. I expect to see a huge difference when the object is small, and small difference when the object is large.

Digitial Dragons 2016 day 1

$
0
0

Digitial Dragons 2016 Conference Logo
Official logo from digitaldragons.pl

May brings a special event into gaming industry in Cracow. For the fifth time Digital Dragons Conference is organized! I’ve just returned from the first day of the conference. Here are my notes.

About

As a reminder Digital Dragons Conference is about promoting games:

Digital Dragons is one of the biggest B2B event dedicated to digital
entertainment sector in East-Central Europe.
Digital Dragons promotes the games sector, one of the fastest growing sectors in the digital entertainment industry. Digital Dragons is where you can meet the most important players of Polish and global game development. Thanks to the presence of a vast number of games producers, publishers, venture capital funds, and representatives of the games industry media, Digital Dragons provides a perfect opportunity for business contacts, staff recruitment, and product promotion launches.

Extract fromdigitaldragons.pl/about.

I am really pleased to see that Digital Dragons is growing. In 2014 there was 822 people, in 2015 there were 1100 participants! The pace is decent so I wonder about the number from this year.

Here are some more facts and figures

Another great thing is to see more topics about programming. To be honest, I am not that much interested in B2B and marketing. I am looking for good tech reports, new programming techniques… and maybe a substitute of GDC? :) In 2016 there is a whole track of programming/tech presentations so I cannot complain.

The programme

Sessions

Notes from the sessions:

Rendering and Post Processing Pipeline of Bound

by Michał Olejnik (@olejplastic) from Plastic

Michał described how the rendering process - especially the postprocessing pipeline - works in their new game Bound. I’ve never heard of the game, but I must admit it has a unique visual style and sounds like a great game/experience.

See below:

One more article about the game: blog/playstation

  • They use dynamic, procedurally generated content.
  • Most of the time the geometry is just boxes with simple diffuse like materials.
  • In order not to lose details, the pipeline uses dynamic GI techniques: especially SSAO (SAO by McGuire, Mara)
  • Since colours and sharp edges are important part of the visuals, the engine cannot show any ugly aliasing effects. They use 4x MSAA, also with temporal reprojection.
  • 4 cascades for sunlight, separate shadow map for the main character.
  • HDR in RGBA16 render targets, Bloom, Depth of Field effect.
  • The game uses Screen space reflections. Also because of the style, developers aimed not on blurry glossy reflections, but pure mirror ones. They developed a very interesting and advanced technique that uses Hierarchical Z buffer to improve the speed of finding collisions with the geometry in screen space.
  • Volumetric lighting is achieved by using Bart Wroński techniques
  • The final composition is quite heavy because of using 4x MSAA, but they take advantage of FMASK texture on GCN/PS4. That gives a huge performance improvement over naive methods.

id Software’s Early Days

by John Romero (bio on wiki)

The legendary game developer has given a nice (but short) keynote about early days of game dev. I won’t go through the history - you can read it in the book Masters Of Doom: How Two Guys Created an Empire and Transformed Pop Culture or on wikipedia - Id Software.

Some interesting parts of the talk:

  • Lots of good tips for game development: like
  • Or another one:
  • Lots of stories about weather in different US States (and why you should the research first before moving to some other part of the country), doing games 24/7, development speed. They made two games every two months!
  • John Carmack invented a smooth scrolling effect that could be implemented on PC and that revolutionized the games. They could even sell engine to other companies.
  • Their code was super simple, C mostly.
  • Wolfenstein 3D was done in 4 months + 2 more months of the final touches.
  • Good tools are essential, so they invested in having the best machines they could. Even Cray workstations!
    • With less then 10 people in the team they created 28 games in 5.5 years!
    • That was done mostly without version control system! or at the beginning even without internet connection. Floppies everywhere!

From start to ship: Lifecycle of a game on Oculus

by Andy Borrell (@AndyBorrell), Oculus

It’s a shame to admit, but I’ve not played with any VR hardware that is out there. The presentation given me some interesting ideas and concepts behind creating games for VR platforms. For example I wasn’t aware that the movement in the game must be controlled in a really great detail. If something wrong is done, then the player will feel not comfortable and won’t enjoy the game. Andy given examples of two games:

AirMech Commander:

and Lucky’s Tale:

Designers and programmers need to be aware of the camera movement problems. The best way is to actually block the camera! In AirMech you see the battleground from the top and only when your main commander moves to the edge of the map it will also teleport camera to a different place. In Lucky’s Tale camera movement is super smooth, without much vertical movement and without rotations.

Also there is a really interesting problem of proper placement of audio sources. In VR we need to get great immersive experience. Human brain is aware, even subconsciously about all the sound effects (positioning, audio transfer delays, …) so they have to be recreated in the game.

The tools used in the development of Life Is Strange

by Sam Hocevar (@samhocevar) Dontnod Entertainment

Sam described how a relatively small gamedev studio with limited resources could achieve success with their game Life is Strange

  • The key is the automation
  • Automate as much as possible, invest in creating internal tools
  • Use open source/external tools if possible (to limit maintenance)
  • Nimp (internal) tool used heavily to automate build process, unify syntax between different versions of compilers, platforms. Additional wrapper on top of devenv from Visual Studio.
  • Buildbot (external) for build.
    • The company uses a lot of data analysis (datamining) on logs from the builds. They can find some compilation problem, bugs, warnings.
    • Since the tool is developed for more than 13 years not the knowledge/documentation is very well known and it’s easy to fix any problems with it.

Parkour. How to improve the freedom of movemet in FPP games in 20 simple steps.

by Bartosz Kulon from Techland

Moving in a city area is not an easy task, especially if zombies try to kill you all the time! Bartosz described some problems with creating realistic and fun experience for parkour style of movement in the game.

  • Ledges that the player could use to climb couldn't be placed manually. There were far too many objects in the game world. They used dynamic approach and each potential spot is found procedurally.
  • Some players jumped too early, some too late. In order to create a smooth experience the team needed to correct that.
  • Tweaks for the animation system and the proper display of hands/body position was needed as well. Hands coudn’t just grab the air - they have to be really on the geometry.
  • Some people could feel a bit sick after playing early prototypes. The developers focused on solving potential propblems with motion sickness (like giving a dot in the centre of the screen, adjusting camera movement so that it does not jump back and others)
  • When running through narrow places, or small obstacles the main character automatically jomps or manuver so that you don’t end up easily on some wall.

Debugging Multiplayer Games - Lessons from Warframe

by Maciej Siniło (http://msinilo.pl/) Digital Extremes

Maciej described some of the techniques he uses to find a bug in Warframe. The game has around 23mln users, and usually 50k players online at the same time. If I am correct, they got like 7..8 crash reports evey hour!

  • Updates for the game are released frequently, every week. Major patches every several weeks. Plus content.
  • They own a full technology stack, so they have control over most of the parts of the process.
  • Challanges: how to find a cause of the crash in such dynamic environment. We can make an analogy for debugging multithreaded code. There is a bug in one thread, but you don’t know what other threads were doing at the same time.
  • Knowledge is the key: it’s probably better to log more than less. You could just write some tools to reject/filter the data. It’s a problem when you have to little data.
  • Maciej mentioned a really good book:
    The Pentium Chronicles: The People, Passion, and Politics Behind Intel’s Landmark Chips
    . With one quote about using the data not your intuition.
  • They usually focus on X top frequent crashes.
  • Assembly knowledge, minidumps, custom exception handlers are they key things.
  • If you want to know what other side (client, server, threads) were doing sometimes you can make them to sent some packets of data from the crash handler.
  • Sometimes you can even find a compiler bug, like rot16 bug in VS 2010.
  • If you have too much time (or if it’s a Christmas time) you can for instance binary patch a third party library that causes a problem! :)

Gameplay Programming in a Rich and Immersive Open World - The Assassin’s Creed Syndicate Experience

by Bartłomiej Waszak (@bartwaszak), Ubisoft Quebec

Bartłomiej make a really nice introduction to techniques his company used to create open world in Assassin’s Creed. The presentation focused on horses, carriages and trains! He covered object model, wehicles, data driven approach, state machines, animation system, event handling and multithreading.

  • The world: London from the 19th Century.
  • Object model consist of entities and attached components (AI, physics, visual). Each object contains some sort of a state machine.
  • The world contains world components like Railway system
  • Usually one object means one entity, but for carriages they needed to use something else: at least three entities (main body, tractor, at least one horse). That messed up in the code a bit.
  • Train are modelled with a custom physics solution. Every wagon is an entity. The tracks are designed with bazier curves and with some special event points on it.
  • Thanks to a smart creation of carriage pool, with just 20 unique cars they can make an illusion of simulating 512 different objects!
  • Artists have a lots of controls and flexibility thanks to using curves in the editor for controlling all the dynamic properties.
  • Events are send directly to other objects or using observer/channel system. An object sends a message to the channel. Everyone interested in a channel need to subscribe and fetch messages from it.
  • Entities are updated in parallel but with some exceptions. Objects that are on the same hierarchy (like a carriage) are updated linearly (one by another) on the same thread, This saves a lot of problems with the syncing.
  • At the end Barłomiej showed a use case that crosses through all the mentioned systems: how to decouple two wagons! What’s the process from getting the user input, through applying animation, playing sounds, effects and do the physics.

Diversity of Ideas,

by Michał Staniszewski (@bonzajplc), Plastic

I was a bit tired at the end of the day. It was a pleasure to get such a powerful and refreshing presentation at the end!

Michał basically portrayed a system for inventing new ideas. It was sort of a philosophical, abstract mathematical presentation. As a base he told that most of the games currently seems to be recreating the same ideas over and over, so how to look for something else?

  • We should take from other media: theater, cinema, books.
  • What if ideas were just numbers?
  • One core idea is a prime number: a dog, a meadow, a cat…
  • You can multiply those ideas: a dog x a desert = a dog on a desert
  • So any number (like 120) can be factorized to find the basic ideas (the prime numbers) that is composed of.
  • Some ideas are magnified (2^n)
  • We can invert ideas - inverse of a desert is a jungle for example.
  • We can shift ideas.
    • Like Room movie, 2015. It’s a deep, hard topic, but shown mostly from the eyes of the child.
  • There are also some irrationals numbers: glitch rendering, goat simulator.
  • There are also transcendental numbers: universal ideas like love, sadness, happiness.
  • We can use operators on those numbers and create some new ideas. The question is will they be fun? Maybe we can create AI that would run through all the combinations and just generate games? We could just wait and verify results: if the game is fun or not.
  • Michał believes there might be a new kind of audience for games: game developers itself. It’s not a little audience, there are for example 5 mln registered Unity developers (even if only 2% of them are the real people that makes still a decent number).


I am looking forward to the next day!

Digitial Dragons 2016 day 2

$
0
0

Digitial Dragons 2016 Conference Logo
Official logo from digitaldragons.pl

Please read about the first day here. Let’s see what happened in the second, final day. For sure, the whether was worse than on Monday.

Sessions

Unity: AAA Graphics and VR

Valentin Simonov, Unity

The presentation mostly discussed the changes in Unity since version 5.0.

Enhanced visuals, better performance, and more: the Unity 5.4 public beta is ready for you to download

  • Last month there were something like 1.1 mln active users of Unity. This is a huuuge number! BTW: as Michał Staniszewski mentioned in his presentation on the first day - game-developers seems to be now a interesting target audience for games.
  • Unity 5.4 gets a set of cinematic effects. You can see some of the effects in the Blacksmith movie or Adam movie.
    • One of the effect is SSRR (Screen Space Raytraced Reflections), or Temporal AA.
  • Enlighten usage is optimized so your lightmaps will be recomputed smarter and faster.
  • For AAA games you get even more control over the native renderer: with Command Buffers, Multithreaded engine, there will be a support for terrain.
  • For VR: most of the games for VR are created in Unity! They are working on VR editor so you can quickly jump into your game.

Lightsaber Escape - Technology

Maciej Zasada (@MaciejZasada), UNIT9

See the experiment’s main page: unit9.com/project/star-wars-lightsaber-escape

There is a whole article about this demo: Creating a Lightsaber with Polymer

Creating Diablo: 20th anniversary

David Brevik (@davidbrevik), Graybeard Games

David is a legend in Gamedev. For many years he had had an idea about creating a RPG game called Diablo (name from Mount Diablo, near SF). Unfortunately at that time no one played RPG as he was told… so his drafts were disapproved.

Here’s the original draft:

And a reddit discussion: https://www.reddit.com/r/gamedev/comments/4b73n1/pdf_for_the_original_diablo_pitch_document/

Some crazy stories:

  • They were all bad businessmen: if I understand correctly, they got a contract of 3k$ for 10+ people for year to create Diablo. But they was able to create a dream game… right?
  • David used inspirations from other games and improved the feeling of RPG: you could immediately start the game (without heavy and long process of creating a character), menu was simple,
  • When Microsoft released DirectX they quickly reimplemented the game engine with this. Thanks to the massive marketing campaign for DX they were able to ship a demo with DirectX Demo CD. 1mln discs were distributed!
  • David mentioned that for the last 9months he was crunching all the time, 16h per day. Crazy! And he’s child was born just after the game release!

WebGL and Unity - Best Practices

Vincent Vergonjeanne (@vvergon), EVERYDAYiPLAY

It was a talk not about rendering techniques behind WebGL and Unity, but actually what are the challenges when you use WebGL target from Unity - in terms of all the systems: gfx, sound, system, experience.

It seems that you can now create a fully featured game with WebGL and it should work! The technology is still it’s early days, but looks promising and there is probably no other choice.

Coding and culture - panel discussion

  • Host: Leszek Godlewski
  • Łukasz Hacura - Anshar Studio
  • Diego de Palacio - Playsoft
  • Michał Staniszewski - Plastic
  • Bartłomiej Waszak - Ubisoft Quebec

Discussion about creating a good team of developers. How to still have fun while coding and deliver best results, best games. There was a long thread about doing code reviews: if they should be simple or detailed (every commit). One of the guests even said that his company has a chance to do a full-detailed review after the game is finished, so they could start from clean start doing a next game (they do smaller reviews during the game development).

Physically based effects

Andrzej Uszakow, Plastic

Short presentation about effects in studio’s new game Bound. They used a custom solution based on position based dynamics. It’s an advanced implementation with several constraints that can be applied: distance, bending, follow the leader, close range attachment. Developers could create all sorts of effects using composition of the core algorithm and selected constraints: ribbons, grass, cloth around main character, etc.

Awards

At the end of the day one, there was Digital Dragons Awards Gala and after-party. Congratulations to the winners:

Digital Dragons 2016 winners:

Best Polish Game: The Witcher 3: Wild Hunt (CD Projekt RED)

Best Mobile Game: This War of Mine (11 bit studios)

Best Game Design: The Witcher 3: Wild Hunt (CD Projekt RED)

Best Game Art: The Witcher 3: Wild Hunt (CD Projekt RED)

Best Game Audio: The Witcher 3: Wild Hunt (CD Projekt RED)

Best Foreign Game: Life is Strange (Dontnod Entertainment)

Małopolska Game Award - Bloober Team

Special Recognition Award – Michał Madej

Summary

Those were really good days for all people interested in game development. The industry is strong and the conference boosts opportunities around Polish Gamedev. Although it’s not a GDC you could learn a lot of interesting topics and be inspired by so many rockstars of games. You could hear John Romero, David Brevik, Chris Avellone and many more industry veterans.

The numbers are also for the conference: last year it was 1100 people, this year that grew into 1300 people. For sure the event is and continues to be an important part of Polish Gamedev.

Still, for me I - a person a bit of outside the gamedev at the moment - it wasn’t that super awesome conference. I’d like to hear more deep programming presentations, especially about the graphics (OpenGL, Vulkan, physics). This is unfortunately not the main aim of the conference. So I might be a bit unsatisfied. Hmmm… maybe I need to finally go to Meeting C++? :)

Coding without Google

$
0
0

Some time ago an intriguing article appeared on reddit: “Do Experienced Programmers Use Google Frequently?”. The author discussed if expert programmers use google more often than novice coders. He mentioned than using google is actually a good thing. It helps to find best solutions, validate ideas, speed the development. Google nowadays seems to be a crucial part of any developer toolbox. That reminded me of old times.

An old computer

I got my first computer when I was around 14 years old (in the year 2000). The specs were pretty decent at that time. If I remember correctly, it was something like Celeron 500mhz, maybe 256mb ram, 20gb HDD, Riva tnt2 Vanta. What’s more important, there was no network connection! Some of my friends got their 56k modems, but it was not that common. Also, it was relatively expensive to surf the net that times… not to mention the great download speed! :)

After playing some games, I started reading some programming books and experimented with C++ language. Instead of playing games I actually wanted to create them… especially the graphics. Without internet connection I couldn’t just google for tutorials or solutions. Was is that horrible?

Old Workflow

How did I survive those days then!? It wasn’t that bad to be honest.

The first compiler that I used was Borland C++ with a nice Windows IDE. I bought C++ in 24h by Jessie Liberty and I got immediately drawn into all of the programming ideas I could find there. I read the book and tried to recreate examples, modify them, write my ideas. Chapter 19 Was about the list data structure. I remember being so proud of myself when I understand the principles behind it!
I could just focus on that book and learning.

Then I started doing graphics. In the first place there was <conio.h> with an amazing color mode for console apps! After that I’ve discovered <graphics.h> header from the library called BGI - Borland Graphics Interface. Now I could really play with individual pixels and using more than 16 colors! I learned what is the update loop pattern and why it’s so powerful technique. Before that my space ship on the screen would freeze if there was a missile animation in the process.
I could just focus on the library.

My workflow updated after a year or two. From time to time I went to an internet cafe and we had also a connection at school. Eventually, I got a modem for my home pc! I won’t lie that I didn’t spend a lot of time being not productive. Just browsing the net. But still, it was a limited experience when we compare it to our current state. And the cost for an hour of browsing was still high. There was no Facebook, Twitter, or other social media. I often downloaded pages with tutorials, so that I could read them offline later.

At some point I stumbled across a great library called Allegro! This was another breakthrough in my development experience! I spent a lot of time coding in the library. After getting a bit comfortable in the API I started to realize my ideas - I had a lot of them at that time. Problems with a bazier path for camera movement? I simply sat down, tried to understand the problem, use a lot of paper, experiment… Finally it was working. Continue until all ideas are in a decent state. You could recently see my updates to Matrix, the demo that was created in those semi offline times.
I could just focus on my ideas. But some distractions started to appear.

After Allegro I learned OpenGL 1.1/1.2. At that time, I had more or less full internet connection. My workflow wasn’t radically changed. I could focus and just play with the code and graphics. Still, there were a lot of online incentives.

What was the main advantage of being partially offline? I could focus better on my tasks.

New Workflow

How would it look like today?

I hope you don’t do it, but sometimes in my spare time I sit in front of my computer with some great idea to implement. Then, instead of doing it I browse the net, I check statuses. Fortunately more often I try to block it and start the task I planned (the task can be still some online activity).

But let’s not be so pessimistic! With the internet I get access to huge amount of good content. A tutorial needed for some strange technology? Just type it in the address window. A solution to a programming problem? Go to stackoverflow. Need some programming/software news: go to reddit.

Depending on the technology you use you might need more net resources or less. For example I don’t imagine going to some offline msdn documentation (I remember installing that back then in the early versions of VS!). Now I simply type “msdn:… ” and visit an online site.

With tons of good stuff out there it’s worth to be a bit resistant to all the distractions. I wonder if I had learnt same things if I would have had today’s net connection.

Lessons

Was I slower in those offline (or semi offline) days? IDE weren’t that advanced so when I wanted to invoke a function I probably needed to look on its file header. I could find it quickly in a pdf/chm with help for the IDE/Library. So it was not that slow….

I believe that offline experience that I had in the past was a good thing. I could focus better on the ideas and on the code. Now, with so many distractions you need to be more resistant and self disciplined.

Lessons: Maybe it’s good from time to time to disconnect and be offline? We could just code and be quicker in the flow?

  • Do you cut-off connection when you want to work?
  • How do you fight with online distractions?

References

11 Debugging Tips That Will Save Your Time

$
0
0

11 Debugging Tips

Programming is not only typing the code and happily see how smoothly it runs. Often it doesn’t run in a way we imagine! Thus, it’s crucial to debug apps effectively. And, it appears that the debugging is an art on its own! Here’s my list of tips that hopefully could help in debugging native code.

Helpers

Everyone should know how to start the debugger, set a breakpoint, continue code execution, step in, step out (using keyboard!). Here are some smaller tips that are just extend the basic actions.

1. Add LinePos to your debug output

No matter how proficient you are, I think, you will still use one of the basic methods: trace some values using printf, TRACE, outputDebugString, etc… and scan the output while debugging. In Visual Studio there’s a nice trick that allows you to quickly move from the debug output window to the particular line of code.

Just use the following syntax for the output format:

"%s(%d): %s", file, line, message

But remember to use file and line from the actual position in the source file, not in some logging function. Thus you should probably have a macro like that:

#define MY_TRACE(msg,...)MyTrace(__LINE__, __FILE__, msg, __VA_ARGS__)

// usage:
MY_TRACE
("hello world %d",5);

Note that __LINE__ and __FILE__ are common, ANSI-Compliant, preprocessor defines that are available to your compiler. See Predefined Macros, MSDN

One more thing: remember to use OutputDebugString so that the message goes into Output Window, not console…

When a particular message goes to the VS output window, you can now double click on the message and VS will move you to that file and line. Same happens for viewing warnings or errors during compilation. I’ve lost a lot of time when I saw a message but I couldn’t know the exact place in the code. In that case I needed to search for the string… that is slow and not effective. With double-click it’s a matter of milisec to be in the proper destination.

BTW: If you use other IDE (other than Visual Studio) do you know if they support similar double-click feature? Let me know, because I am curious.

Here is some simple sample you can play: github.com/fenbf/DebuggingTipsSamples

Update: as jgalowicz mentioned in the comments. If you really like to have only short filenames in the output you can play with his __SHORT_FILE__ technique: see here on his blog..
Still, by default Visual Studio uses /FC compiler option off by default, so you usually have short filenames (probably relative to your solution dir only)

2. Simple static variable to control the feature

// change while debugging if needed
staticbool bEnableMyNewFeature =true;

Edit And Continue in Visual studio is really powerful feature, but here’s a simplified , ‘manual ’ version. Probably not that beautiful, but works. Just make a static variable that can be used to control a feature. Could be just a boolean flag, or an integer. Then, while debugging you can actually change that value. Without the need to restart the program or recompile you can play with your feature.

How to change the value during debugging? Go to the watch window or just hover on top of the variable. You should see an edit box where the value can be changed.

Please remember to disable/remove that ugly variable in the final builds and commits!

3. Conditional breakpoints

I hope you use conditional breakpoints already, but let me just quickly show their basic uses. As name suggests you can set a condition, relatively simple one, upon which a debugger will stop.

Setting a conditional breakpoint in Visual Studio

One hint: write a custom breakpoint if you need more advanced test.

Here the list of expressions you can use in conditions: msdn: Expressions in the Debugger

That’s not all.

As you might notice on the above screen shot, there is also one helpful breakpoint condition: “Hit count”. You can specify after what number of events a breakpoint will really happen. Very handy if you trace some dynamic event or lots of objects.

4. Don’t step into unwanted functions

How many times have you stepped into a constructor for a string type and then needed to quickly step out? Or when you needed to step into lots of small/library functions before the target method? In most cases it’s a waste of time.

See the following example:

voidMyFunc(const string &one,const string &two)
{
auto res = one + two;
std
::cout << res <<"\n";
}
/// ...
MyFunc("Hello ","World");

And then try to press Ctrl+F11 to step into the call of MyFunc(). Where will the debugger go? I see something like this:

Stepping into unwanted function, Visual Studio

What’s more, if you step out of this and then step into again… you’ll go into the second param constructor. Imagine what happens if you have several parameters. You can be easily frustrated before going into your target method!

In most cases it’s better to just filter out those unwanted methods. It’s very rare the problem you’re trying to catch is in the std::string constructor :)

What to do to filter those basic functions out?
Since VS 2012 there is a simple method to create filters: you need to edit default.natstepfilter

Read here about the method of filtering before VS 2012: How to Not Step Into Functions using the Visual C++ Debugger. In older versions you have to play with registry values most of the time.

As a little incentive, the same functionality is greatly simplified in Visual Assist. While Debugging you see VA Step Filter. You can just click on the check box to enable or disable filter for a discovered method. That setting can be global or just for a given project. VA filter setting are custom solution, they don’t merge with default.natstepfilter file.

5. Add helper variables for your objects in debug mode

More data is better then less data! It’s always possible to filter out unwanted messages, but it’s impossible to create data out of nothing. Depending on what you’re doing it might be useful to add some additional variables into your objects. When you’re debugging that variables might bring very important information or just make your life easier.

For example, when you work on Tree structures you’ll probably often need to check pNext, pPrev elements. Often those pointers are placed in some base class like a TreeNode, and if you’re checking MyTreeNode that is three levels of class hierarchy lower it’s a pain to check pNext every time. What if you’ll update MyTreeNode with some additional data from pNext? Then you can easily check that without going through object hierarchies. One disadvantage: how to maintain that additional state? 'pNext might be easily changed, so you would have to make some additional logic to properly sync that. While that’s true in most cases, maybe for debugging you don’t need to have full and perfect solution?

Let me give you an example.

I often work on Tree structures that represents text object. Text object contains lines, and lines contains characters. It was painful to check in what line am I in - what text it contains. Because I had to get the first char from the line, then get the pNext and then I ‘see’ the first two letters of the line so I have a clue what line I’m in. How to make that process a bit easier? I’ve just made strLine and added that to Line. I’m updating that new member from time to time. This might not be a perfect information (it might miss when a letter is added or deleted in one frame, but it would get that info in the next frame). But at least I can quickly get the idea in what text line I am in. Simple and easy! And saves a lot of time.

6. Write custom debugging visualizers

This is a huge topic that I’d just like to introduce:
If you’re unhappy about the view of your objects in the debugger, you might want to write your own visualisers.

Debug Visualizers in Visual C++ 2015

In VS2015 there’s even a new built-in template which can be found under Project->Add New Item->Visual C++->Utility->Debugger visualization file (.natvis)

Techniques

With the basic tools we can compose some more advanced strategies.

7. Lots of objects to investigate?

When you have code that is called for lots of objects it’s hard to go through all the objects and just check them line by line. Think about a unique value that might lead you to the interesting place in the code. Then you can set a conditional break and set condition that catches some range. The smaller the range the better.

For example: often I had to debug code that goes through all the characters in a document. One (special) character was not doing ‘well’. It would be impossible to debug all those character individually. But I knew that this special character has different bounding box size than other letters. So I set a conditional breakpoint and looked for ‘width’ value that might point to my special character (width > usual_char_width). I got only two or three elements to check, so I could quickly investigate what was wrong.

In general, you want to make your available options as narrow as possible so that you have only several (not tens or hundreds) places to debug.

8. Mouse events

Debugging mouse events is especially confusing, because when debugger stops the code, most of the events go away!

Mouse clicks are usually easy: for example if you want to check what code was invoked after mouse clicked on some object. Just break into some OnClick/onMouseDown method.

What about mouse dragging? If the debugger stops then the drag state is lost. In those situations I try to do the following things:

  • Use good old trace/printf output. While dragging I get a lot of messages that leads to better understanding what’s going on. Without breaking the execution. Probably you want to have short drags operations, otherwise you’ll end up with tons of output to filter. Using that output you can isolate the most important place and focus on that part later.
  • Use conditional breakpoints in places that you really want to check. For example you rotate the object, and you’ll interested why it unexpectedly change position. You can set a breakpoint on the position members and you’ll get a chance to see what’s going on there. The state after stopping is lost, but at least you could play with the rotation for a while and you get into the potential place in the code. Another idea is to set the condition when obj_rot > some_meaningful_value.
  • Dragging often happens on a copy of objects. Then after the dragging the real objects are transformed once into the proper state. Maybe you can set breakpoint to look only on the original objects? Maybe there is a separate state in the app that tells this is drag operation happening? Then the debugger will stop at the end of drag operation.

9. Build debug visualizers, tools

This might be an evolution of introducing just a simple variables for debugging. If you’re working with complex object, it’s worthy to have tools that trace the data better. Visual Studio or any other IDE/debugger will help you with general stuff, but since each project is different, it’s useful to have custom solutions.

In games that’s very often situation as I see. You probably have some layer that can be enabled during the game session, it will show game stats, performance data, memory consumption. That can be improved to show more and more stuff - depending on your needs. So I definitely suggest investing in those tools.

Other

10. Debug the Release Build

Release builds are faster because most of the optimizations are enabled. However there is no reason why you couldn’t debug such code. What to do to enable such debugging? It needs the following steps: in VS 2013 and VS 2015:

  • Set Debug Information Format to C7 compatible (/Z7) or Program Database (/Zi).
  • Set Enable Incremental Linking to No
  • Set Generate Debug Info to Yes
  • Set References to /OPT:REF and Enable COMDAT Folding to /OPT:ICF

11. Speed up debug builds!

Summary

In the article I covered 11 tips that will speed up debugging process. What are the most important items for me? Probably that would be conditional breakpoints, debugging lots of objects and improvements in the debug version of the code. But other elements from the list are also important, so it’s not easy to make a real order here. And often you have to exchange one technique into another one, to suit your needs best.
What’s more, the list is definitely not complete, and many more techniques exists. Maybe you have something to add?

  • Do you use any special techniques when you debug your apps?
  • Do you use any custom tools to help debugging?

Resources


C++ (Core) Coding Guidelines

$
0
0

C++  (Core) Coding Guidelines

Since 2011, when C++11 arrived, we all should be changing our coding style into modern C++ and at the same time keep good old tips. There are also general rules for programming and bug-free coding. Here’s a list of guidelines and other resources that might help you.

Core C++ Guidelines

Main site: C++ Core Guidelines

From the abstract:

This document is a set of guidelines for using C++ well. The aim of
this document is to help people to use modern C++ effectively. By
“modern C++” we mean C++11 and C++14 (and soon C++17). In other words,
what would you like your code to look like in 5 years’ time, given
that you can start now? In 10 years’ time?

Elements:

  • In: Introduction
  • P: Philosophy
  • I: Interfaces
  • F: Functions
  • C: Classes and class hierarchies
  • Enum: Enumerations
  • R: Resource management
  • ES: Expressions and statements
  • E: Error handling
  • Con: Constants and immutability
  • T: Templates and generic programming
  • CP: Concurrency
  • STL: The Standard library
  • SF: Source files
  • CPL: C-style programming
  • PRO: Profiles
  • GSL: Guideline support library
  • FAQ: Answers to frequently asked questions
  • NL: Naming and layout
  • PER: Performance
  • N: Non-Rules and myths
  • RF: References
  • Appendix A: Libraries
  • Appendix B: Modernizing code
  • Appendix C: Discussion

The guideline is moderated by Bjarne Stroustrup and Herb Sutter. Here’s Bjarne Stroustrup’s C++ Style and Technique FAQ and Sutter’s GotW section.

Additionally here’s the post from Kate Gregory about coding guidelines: C++ Core Guidelines and Checking Tool And also, she started doing more posts about core guidelines, for example: Using the not_null Template for Pointers That Must Never Be Null

Learn from bugs

Some time ago a company that produces PVS-Studio for C/C++/C# (viva64.com/) posted a very long list of potential bugs and tips that you can use to improve your C++ code. It’s also in the form of a book:

The book covers 42 topics. In spite of the simple titles of the
chapters, the bugs found are really various and non-standard. In
addition to that, the text provides a lot of links to interesting
materials that give more details on topics. To make more use of this
book, please don’t hurry and go to the links provided.

Content:

  1. Don't do the compiler's job
  2. Larger than 0 does not mean 1
  3. Copy once, check twice
  4. Beware of the ?: operator and enclose it in parentheses
  5. Use available tools to analyze your code
  6. Check all the fragments where a pointer is explicitly cast to integer types
  7. Do not call the alloca() function inside loops
  8. Remember that an exception in the destructor is dangerous.
  9. Use the '\0' literal for the terminal null character
  10. Avoid using multiple small #ifdef blocks
  11. Don't try to squeeze as many operations as possible in one line
  12. When using Copy-Paste, be especially careful with the last lines
  13. Table-style formatting
  14. A good compiler and coding style aren't always enough
  15. Start using enum class in your code, if possible
  16. "Look what I can do!" - Unacceptable in programming
  17. Use dedicated functions to clear private data
  18. The knowledge you have, working with one language isn't always applicable to another language
  19. How to properly call one constructor from another
  20. The End-of- file (EOF) check may not be enough
  21. Check that the end-of- file character is reached correctly (EOF)
  22. Do not use #pragma warning(default:X)
  23. Evaluate the string literal length automatically
  24. Override and final identifiers should become your new friends.
  25. Do not compare 'this' to nullptr anymore
  26. Insidious VARIANT_BOOL
  27. Guileful BSTR strings
  28. Avoid using a macro if you can use a simple function
  29. Use a prefix increment operator (++i) in iterators instead of a postfix (i++) operator
  30. Visual C++ and wprintf() function
  31. In C and C++ arrays are not passed by value
  32. Dangerous printf
  33. Never dereference null pointers
  34. Undefined behavior is closer than you think
  35. Adding a new constant to enum don't forget to correct switch operators
  36. If something strange is happening to your PC, check its memory.
  37. Beware of the 'continue' operator inside do {…} while (…)
  38. Use nullptr instead of NULL from now on
  39. Why incorrect code works
  40. Start using static code analysis
  41. Avoid adding a new library to the project.
  42. Don't use function names with "empty"

Here’s the post: http://www.viva64.com/en/b/0391/ that gets regularly updated. I highly encourage you to read about those problems from time to time… maybe something similar could be improved in your apps?

Google Coding Standard

Google C++ Coding Standard is another popular resource that is public and easy to find

Just go here: https://google.github.io/styleguide/cppguide.html

Top level index:

  • Header Files
  • Scoping
  • Classes
  • Functions
  • Google-Specific Magic
  • Other C++ Features
  • Naming
  • Comments
  • Formatting
  • Exceptions to the Rules
  • Existing Non-conformant CodeWindows Code

Since Google is a software giant, we should obey their rules, right? Not that easy! There is a huge discussion going on if the guide is good or not. Read especially this detailed post: Why Google Style Guide for C++ is a deal-breaker. And here is a reddit thread for the article.

What are the main controversies over the guide: banning exceptions, public inheritance, passing reference parameters, probably not using advanced template techniques.

To see reasoning behind the guideline, you can watch this video:

CppCon 2014: Titus Winters “The Philosophy of Google’s C++ Code”

Other guidelines

Bloomberg - BDE

https://github.com/bloomberg/bde/wiki/Introduction-to-BDE-Coding-Standards

OpenOffice

http://www.openoffice.org/tools/coding.html

LLVM

http://llvm.org/docs/CodingStandards.html

Mozilla

https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Coding_Style

Chrominium

https://www.chromium.org/developers/coding-style

Mostly it uses Google Style Guide, but here are also some specific sections: C++11 use in Chromium or C++ Dos and Don’ts

High Integrity C++ Coding Standard Version

http://www.codingstandard.com/

WebKit

https://webkit.org/code-style-guidelines/

ROS (Robot Operating System)

http://wiki.ros.org/CppStyleGuide

They have also invested in auto format tool: roscpp Code Format

SEI CERT C++ Coding Standard

www.securecoding.cert.org

Little Bonus:

Books

Blog posts

Summary

In this post, I brought you a list of c++ guidelines that might add value to your internal guidelines. Please have a look especially at C++ Core Guidelines since it’s created by the community and moderated by Bjarne Stroustrup and Herb Sutter.

What guideline I am missing here? Let me know if you have a useful link to that.

  • What coding guideline do you use? Company internal? Or some open guideline?
  • Do you obey the rules from your guideline?
  • Do you use auto format tools?

Grammarly Review - grammar for coders

$
0
0

Grammarly Logo

Today I’d like to write about a topic that potentially is not much related to the programming: about English Grammar. You may wonder why? There are at least two reasons.

The first: it’s possible that from time to time you have not only to write a code, but also write a spec or documentation. And probably, you’ll write using English - since it’s Lingua franca in coding. Thus, using proper grammar will usually help in producing better docs.

The second: it’s especially useful when blogging! English is not my native language. I do my best to use correct tenses, compose proper sentences, use prepositions, words or articles. But still, it won’t be as good as written by a journalist. So I a second eye is very helpful here.

Let’s meet Grammarly! It’s a tool that can automatically improve your writing.

Intro

Promotional Note I become an affiliate for Grammarly products, so for each registration on pro subscription I’ll get a little commission. Thanks for understanding! I only recommend products I trust and personally find useful.

In short: Grammarly is a robust online writing-enhancement platform that will check English grammar for your input text.

You can use it as an online editor, desktop editor, in Office apps or just as an extension of a browser.

How does it look like?

Grammarly Editor

The picture above shows the basic usage of the tool. Just paste some text into the editor and immediately it’s analysed and errors are displayed. You can click on a tip and understand why it’s reported.

Through extensions it also works in the browser For example in gmail:
Grammarly in Gmail

If you’re not happy with the performance, you can disable the extension for a given site.

Grammarly extension for Firefox

As you can see, the tool goes a bit beyond a basic spell checker!

What can it do?

  • Check your writing across the web
  • Access your personal editor via Grammarly.com
  • Access your documents on multiple devices
  • Integrate with Microsoft® Office (Windows only)
  • Use native desktop apps (Windows and OS X) - still internet connection is required.
  • See definitions and synonyms via double clicks
  • Catch contextual spelling and grammar mistakes
  • Add words to your personal dictionary
  • See explanations of grammar rules
  • Get performance stats via email

Free vs Pro version

Grammarly is using a subscription model. There is a free version that can be used after registering as a new user. Pro version costs 11.66$ per month (paid yearly) or 29.95$ paid monthly. See their offer here: link

Here’s a basic comparison of the two versions:

FeatureFreePro
150 critical grammar and spelling checksYesYes
100+ additional advanced grammar and spelling checksXYes
Vocabulary enhancement suggestionsXYes
Genre-specific writing style testsXYes
Plagiarism detector that scans more than 8 billion web pagesXYes

My experience

I’ve been using Grammarly - the Free version - for several months. Recently I was able to test the Pro version.

Usually, my workflow was simple: write an article, then copy the text into the Grammarly editor and check it, the paste it back to my editor (I am using StackEdit most of the time). I could use browser extensions, but somehow I prefer copy-paste way. Maybe in the future, I’ll play with that, but initially, I got the impression that extensions made my browser a bit slower.

Most of the bugs that I often do: punctuation, tense usage, passive voice, repetitive words, unclear intent, articles, are found in a second. After a while, I’ve noticed that I’ve even stopped making some of the easiest points. So the tool is also an excellent teacher.

And look: there’s even a free Grammar Handbook from Grammarly.

Should you buy the Pro version?

It depends on your needs. Once I’ve tested the paid version, I don’t want to go back to the Free solution! But I’d say that if you write casually from time to time, the free version would be good enough. But for frequent editing, I’d say the Pro saves you a lot of time and effort to make documents better. Note that advanced punctuation checks, sentence structure and style are only available in the Pro version.

Summary

My rating: 5/5

Pros:
* Time saver for any writing.
* Can integrate with almost every editor, Office, or a browser.
* While it corrects your mistakes you also learn something new every time.

Cons:
* Plugins for browser might be relatively slow.
* Desktop app still need an internet connection.

I must admit I am pretty impressed with Grammarly. Previously I relied on my friends or sometimes English teachers to scan my articles. While the tool cannot replace human (not yet) it significantly improves your final text.

I can suggest Grammarly especially to programmers who are not native English users or who write lots of documentation, specifications, articles or blog posts. The tool won’t give you much for your code (hmmm… maybe they create a VS plugin?), but it will improve writing of your docs.

Subscribe to the service through this link or by clicking on the image below:

Grammarly Logo

7 books that taught me how to code

$
0
0

books

As you might read in my recent post - coding without Google - I’ve started learning to program mostly from books. In the beginning, I read them from cover to cover (assuming I could understand them!) and recreated examples. It was an excellent time! In the post, I’d like to share my list of important books that taught me the base of my knowledge. Maybe you’ve shared similar titles along your learning path?

1. C++ in 24h


Sams Teach Yourself C++ in 24 Hours (5th Edition) 5th Edition

I’ve seen that book many times on a shelf of my local bookstore. One day, after a discussion with my friends, we’ve bought the book. One book for three young kids. I didn’t know when I got my turn, but in the end, the book was mine and my friends sold theirs ‘shares’ to me.

It was fascinating! I could type some basic commands (in C++ of course!), create function and classes, and output messages to the console. Similar things probably could be written about any first programming book. Still, C++ in 24h was written in such way that a newbie wasn’t thrown away, could understand most of the things, and in the end progress with the learning.

Each lesson was done very well, and step-by-step one could get most of the basic concepts of C++. In chapter 19 (in the 19th hour to be correct) there was a topic of linked lists. The described problem was not only a C++ thing, but it was also an area of data structures and algorithms. The chapter was hard to get through. In the end, I’ve managed to understand lists, and I was proud of myself!

I’ve seen that the book is a bit outdated at the moment. The 5th edition is from 2011 and doesn’t cover C++11/14. Maybe a new version is being written along the way. If that appears, I would highly recommend it to any C++ beginner.

And you know what?

As it turns out of those three kids, that bought the book, two of them become programmers :)

2. Algorithms in C++

Algorithms in C++, Parts 1-4, by Robert Sedgewick

This time, I bought that book on my own! Typical books cost between 30..40 polish zloty. But Algorithms was priced double - something between 80…90zł. It was a lot of money! I spent a lot of time thinking if that book is really for me, maybe too advanced… in the end, I bought it. And I am glad I did that!

To be honest, I was frightened that I couldn’t understand the first introductory chapter. As an example, they used ‘Connectivity’ and Union-Find algorithm. But since the second chapter things get much better. Slowly I got the idea of how the core algorithms work.

I like that the code was not in pseudo-code as in Cormen, but in C++. You could copy and paste it and see how it executes. It was vital for learning.

Chapter by chapter I got a basic understanding of algorithms and data structures. I often return to that book. I am also thinking about buying the newest edition… but unfortunately, it was rewritten into Java.

3. OpenGL Superbible(s)

Cover From openglsuperbible.com/previous-editions/

You can read the review of the latest (7th) edition of the book on my blog here.

After creating some basic text/console programs, I moved to graphics programming. This choice was heavily inspired by Borland BGI Graphics and Allegro Library. I knew that OpenGL was something big at that time (and still is!) so I thought it would be a good way to improve my graphics knowledge.

Probably, I don’t have to mention that the book is excellent. Even the first version was! As with most books at that time there was a CD attached so you could see all the examples running! I wonder if I still have that CD around. Over the years, with the new editions, the CD was removed from the book, the examples were moved to site download option, and the quality of the book content got better and better.

I followed Superbible series. Today I got three books: 1st, 4th, and the 7th editions. Each version quickly caught the current version of OpenGL. You could see how OpenGL advances: from fixed pipeline to simple vertex/fragment shaders and now to fully ‘GPU side’ programming style. Maybe I wouldn’t lie if I say that the book is the standard book for learning OpenGL. There is OpenGL Programming Guide, but most of the people like Suberbible as I can imagine.

4. Game Programming Gems series

Game Programming Gems on Amazon

Site of the editor - Mark DeLoura - Gems series

Game programming gems was another step for me. I knew something about the graphics, but now I got interested in game engines. I could write a graphics demo, but I was curious how to make a superb graphics engine. Gems were a series of lots of related articles. I liked that. You could pick one topic and just focus on it. They were grouped in logical order, subsystems.

In some of my examples I’ve used “The Parallel Transport Frame” described in the second volume of the series. With that technique I could extrude 2d shapes along splines. That was a cool way of doing any kind of ropes or worms. Maybe that would be a good topic for a separate post.

I own four volumes: 1st, 2nd, 3rd, and 6th.

5. Tricks of the 3D Game Programming Gurus

Tricks of the 3D Game Programming Gurus-Advanced 3D Graphics and Rasterization by Andre LaMothe

This book was highly addictive! I bought it with the vision of creating my software renderer and engine from scratch. Andrea LaMothe created a fantastic book where everything was described from little details. When I wrote code using OpenGL I was a bit like the level above. I could make my triangle have a texture, but I wasn’t aware of the code behind texturing itself. Or even simpler things: how to render wireframe cube? Very easy in OpenGL, but do you know what’s code behind such scene?

To be honest, I wasn’t persistent enough to finish all the book. I followed like half of it. So my ‘amazing’ engine could render wireframe objects only. Then I stopped with the implementation and moved into OpenGL again (there was also many things to learn at that ‘higher’ level). Still, I tried to read the book and understand ideas behind texturing, sampling, clipping and other covered effects.

Some of the screenshots from the samples (found here)

3d animation

z buffer techniques

What’s great about this book is that it’s still valid! Of course our rendering engines are very very powerful, still with the book you can learn how to code triple buffering or mipmaps. That knowledge will be relevant for many years.

6. Code Complete

Code Complete: A Practical Handbook of Software Construction, Second Edition, by Steve McConnell

At the time I bought Code Complete I was in studies/collage (3rd or fourth year), so I needed something valuable that tells me what the software development is. The book is highly respected in the IT world, so I knew I need it.

It covers lots of different aspects of making great software. It describes a good coding style, how to debug apps and even how to apply agile and improve as a programmer. Lots of areas but very well structured.

Over the years I returned to the book many times. There’s always something you can learn or recall.

7. Passionate Programmer

The Passionate Programmer: Creating a Remarkable Career in Software Development by Chad Fowler

Programming was fun; I got my first professional experience. But I’ve noticed there’s another side: how to be a great programmer! It’s not only coding but also your attitude towards tasks and people. The book showed in an inspiring way what are the possibilities to improve yourself.

Ream my review here on my blog.

Other worth mentioning:

I’ve chosen only seven books, but I couldn’t resist mentioning some of other titles.

More OpenGL Programming

More OpenGL Game Programming, David Astle editor.

The book was a step further after reading OpenGL Superbible and other beginner-level books. It gives a lot of interesting articles that are worth implementing and are a must for advanced graphics programming.

ShaderX series

ShaderX6: Advanced Rendering Techniques, edited by Wolfgang Engel

Thanks to probably the 6th volume of the series I was able to finish my master’s thesis! There was a separate section on shadows, so I used it for my work then.

The series is continued, but with a different name: GPU Pro series.

Soft Skills

Soft Skills: The software developer’s life manual by John Sonmez (simpleprogrammer.com)

John Sonmez is doing a great job in the field of inspiring people (especially programmers) how to be better. His Soft Skills book touches a lot of aspects of development career, motivation, learning, blogging. It’s a good addon to Passionate Programmer book. I’ve done a review for the book if you’re interested.

Summary

Sorry for another nostalgic post :) The list was compiled for a lot of weeks, so finally I was able to release it. Of course, I didn’t include all the maths, physics, logic book that were required on studies. I listed only popular IT books.

What are your favorite books? What book was the first that taught you how to code?

Code And Graphics in Mid 2016

$
0
0

Code And Graphics Blog

It’s summer! Hmm… actually it’s summer for almost one month now. This time, I don’t want to write about something heavy and related to programming. Let’s make a little summary of the first part of the year.. and also, I have a question to you - can you help me a bit?

Story

The first part of the year was quite successful for Code And Graphics. First of all, I got new motivation to start blogging! After my little kid was born in April 2015 I found less and less time to do other activities. But near the end of the year, I got some new powers, reorganize things and decided to resurrect my blog. As a result, I wrote C++ Status at the end of 2015. It got a really good reception!

From that time I decided to make an experiment: write something every two weeks. It was quite an ambitious task, to be honest. Previously I wrote when I had some really interesting stuff and motivation. But this experiment was a good choice. There was no problem for me to find new topics to write (and of course I had a lot of ideas in my backlog). What’s more important: the schedule motivated me to really write and create. I highly encourage you to do the same if you struggle with similar problems. Just create a plan and try to stick with it.

Some Stats

1st January 2016 till 1st July 2016:

  • Visits: 233 213
  • Unique visitors: 89 536

Best Posts

Coding without Google

This was a really huge surprise for me. Instead of writing about technical stuff, I just wrote a bit of my story plus some thoughts about workflow… and it got huge attention. In one day it was viewed around 80k times! On my stats, it’s an outlier result.

The Matrix Updated

In the above post, I’ve gone and dug out a very old project, written in Allegro Game Library. The code was a bit updated, so at least I could run in on modern hardware and OS.

C++ Status at the end of 2015

As usually, I try to make a C++ status update at the end of a year. This was my first post after the break so I was really happy to see that people want to read it. Thanks!

Some other posts:

Can you help?

In order to improve this blog, I need some help from your side. I’ve prepared several questions so that I can better understand your preferences and expectations.

Link to the survey

Survey

Summary

Thanks for reading this blog in the first half of the year (+ one month)!

I’ll plan to take some time off, but I promise to return in the beginning of September with more articles, tutorials, and good programming stories.
I am also thinking about changing the time the post appears. Now it’s every second Thursday, but I’ll probably change it to every second Monday.

Please add your answers in my survey so I can serve even better content.

Best regards
Bartek

Wrapping Resource Handles in Smart Pointers

$
0
0

Wrapping resource handles in Smart pointers, C++

Some time ago I covered how to use custom deleters with smart pointers. The basic idea is to specify a dedicated method that will be called when a pointer is released. One logical application of custom deleters might be resource handles like files or the WinApi HANDLE type. Let’s see how can we implement such thing.

Introduction

Modern C++ offers robust methods to minimize resource and memory leaks. You can almost forget about raw pointers and just be using smart pointers. By default, smart pointers work on pointers, but we can reuse their capabilities and apply it to resource management.

BTW: you can even watch the latest talk from Herb Sutter about “Leak-Freedom in C++… By Default” from CppCon 2016.

But let’s discuss some basic things: for example FILE* needs to be opened and closed (via fopen() and fclose() methods). If you forgot about closing the stream, you would risk not only some memory leaks but also locking file or even corrupting it. A similar thing happens for the HANDLE in WinApi, you need to create it and then release by CloseHandle().
BTW: The system will/should release all allocated resources by a process when that process is terminated, still this is no excuse to clean stuff on your own! See here: MSDN: Terminating a Process.

FILE handle

FILE handle is used in CRT api to manage files. We need to wrap the fclose() function in the deleter.

In both of pointer types we can use the following functor object:

// stateless functor object for deleting FILE files
structFILEDeleter
{
voidoperator()(FILE*pFile)
{
if(pFile)
fclose
(pFile);
}
};

Such stateless functor will make unique_ptr just as small as only one pointer (because of optimization, the functor object doesn’t need additional memory…) - see Empty base optimization.

Unique Ptr

For unique_ptr the deleter is part of the type:

using FILE_unique_ptr = unique_ptr<FILE,FILEDeleter>;

For convenience, we can wrap the fopen() function. Moreover, If you work with VisualStudio/Windows SDK then it’s probably better to use secure version: fopen_s():

FILE_unique_ptr make_fopen(constchar* fname,constchar* mode)
{
FILE*fileHandle=nullptr;
auto err = fopen_s(&fileHandle , fname, mode);
if(err !=0)
{
// print info, handle error if needed...
returnnullptr;
}

return FILE_unique_ptr(fileHandle);
}

Shared Ptr

using FILE_shared_ptr = std::shared_ptr<FILE>;

Deleters for shared_pointers are specified during construction:

FILE_shared_ptr make_fopen_shared(constchar* fname,constchar* mode)
{
FILE*fileHandle =nullptr;
auto err = fopen_s(&fileHandle, fname, mode);
if(err !=0)
{
// handle error if needed
returnnullptr;
}

return FILE_shared_ptr(fileHandle,FILEDeleter());
}

Note, that such construction might be not as efficient as using make_shared. We could be more advanced here and create a custom allocator that handles creation and deletion of the resource. That way we could invoke allocate_shared.

Example

FILE_unique_ptr pInputFilePtr = make_fopen("test.txt","rb");
if(!pInputFilePtr)
returnfalse;

FILE_unique_ptr pOutputFilePtr
= make_fopen("out.txt"),"wb");
if(!pOutputFilePtr)
returnfalse;

That’s all! No need to worry about fclose now. This might not be a problem when there’s only one file. When there are more files, then you need to pay attention to call fclose when some operation fails (like opening a new file). As in our example, if the second file cannot be created, then there’s no need to take care of the first (already opened) file. It will be closed automatically on error.

HANDLE from WinApi

Similarly, as for FILE we can define deleter for HANDLE:

structHANDLEDeleter
{
voidoperator()(HANDLE handle)const
{
if(handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
}
};

Unique Ptr

HANDLE is defined as void*, so the unique_ptr type will be specified as:

using HANDLE_unique_ptr = unique_ptr<void,HANDLEDeleter>;

We can wrap the creation procedure inside the function:


HANDLE_unique_ptr make_HANDLE_unique_ptr
(HANDLE handle)
{
if(handle == INVALID_HANDLE_VALUE || handle ==nullptr)
{
// handle error...
returnnullptr;
}

return HANDLE_unique_ptr(handle);
}

The above method is a quite important step to remember! Since Windows functions usually return INVALID_HANDLE_VALUE which is not the same as nullptr we need to make sure we error on such condition. Otherwise, we would happily store INVALID_HANDLE_VALUE as pointer value inside the unique_ptr and the test for validity if (myUniquePtr) would always evaluate to true. See this code for even safer version of such handling

Example

HANDLE type might come from different sources, in the example below we obtain it as a handler to a file:

auto hInputFile = make_HANDLE_unique_ptr(CreateFile(strIn, GENERIC_READ,...));
if(!hInputFile)
returnfalse;

auto hOutputFile = make_HANDLE_unique_ptr(CreateFile(m_strOut, GENERIC_WRITE,...));
if(!hOutputFile)
returnfalse;

Summary

In this article, I’ve quickly covered how to implement smart pointers for resources like file handles or generic Windows kernel handles. You could easily extend this and have a range of smart pointers for other types like texture handles, shaders even library’s custom handles (like SDL_Surface): every time you have a special code that needs to be executed on deletion there’s potential to wrap it as a deleter for a smart pointer.

Do you use such custom deleters in your applications?

What kind of resources have you wrapped?

BTW: If you liked this article, please sign up for my free newsletter.

References

Variadic Templates and a Factory Function

$
0
0

Variadic Templates

Variadic Templates from C++11 is probably not a feature that you use on a daily basis. But recently, I’ve come across one refactoring example where I’ve decided to give a try and apply variadics.

Intro

When I was doing some work in some old UI code I’ve noticed several similar lines of code that looked like that:

switch(ControlID)
{
caseControlA:
if(Message== MSG_CLICK)
{
FirstMenu*pMenu =newFirstMenu("Test");
pMenu
->Show();
// pMenu is stored in manager and then deleted later...
// so no need to delete it here
}
break;
caseOtherControl
{
// code here...
break;
}
caseControlB:
if(Message== MSG_CLICK)
{
SecondMenu*pMenu =newSecondMenu();
pMenu
->Show();
}
break;
}

In the above example code for that shows FirstMenu and SecondMenu is very similar. How can we refactor that into simpler code?

We could go deeper and change the order of this switch statement, but for now, let’s focus only on the menu handling.

I’ve decided that we could wrap the whole test for the click message and a menu invocation into one method. We could use templates or polymorphic types. But then I’ve noticed that one menu has an additional parameter for the constructor. So my simple method wouldn’t work.

So here we need to look at Variadic Templates!

Full code can be found here: http://coliru.stacked-crooked.com/a/d5edfea9c74e7b3c

Improved version

I’ve come up with the following method:

template<typenameTMenu,typename...TArg>
voidShowMenuOnClick(MSG Message,TArg&&...Args)
{
if(Message== MSG::Click)
{
TMenu* pMenu =newTMenu(forward<TArg>(Args)...);
pMenu
->Show();
}
}

To be honest, I’ve just copied code from make_unique’s implementation :)

How does it work then?

The construction allows passing a variable number of arguments into a template function. This is what I needed for the refactored code: one menu requires one attribute, another menu is constructed without anything:

The code can be now changed into:

switch(ControlID)
{
caseControlA:
ShowMenuOnClick<FirstMenu>(Message,"Test");
break;
caseOtherControl:
{
// code here...
break;
}
caseControlB:
ShowMenuOnClick<SecondMenu>(Message);
break;
}

Great! We’ve replaced duplicated code with just one line of helper method call.

What’s more, the code should work as before so it’s a good sign :)

Details

First of all, you might wonder why Variadic Templates were needed. Before C++11 if your template code required several parameters you would manually write each declaration/definition on your own. So for example if a template might use up to 4 parameters, you would declare four different options like

template<class T1>
voidFunc(T1);

template<class T1,class T2>
voidFunc(T1, T2);

template<class T1,class T2,class T3>
voidFunc(T1, T2, T3);

template<class T1,class T2,class T3,class T4>
voidFunc(T1, T2, T3, T4);

With variadics, you can wrap it into one or two declarations (depending if you want some recursion to happen).

Structure

  • typename... Args is called a template parameter pack.
  • Args... args is called a function parameter pack. Each argument might have different type.

The real benefit of having variadics is that we might have functions that take an arbitrary number of parameters and this will be evaluated at compile time! This could be achieved in the past, but with a lot of effort. Now the code can be much more expressive.

Of course, Variadic templates is not the only method you could use: var args from C are well-known technique used especially in printf style functions. They work quite well (by taking benefit from various stack manipulation tricks), but the logic is executed at runtime, so this costs something. With Variadics we might create a type-safe printf, which takes no time at runtime…

Read more about variadics templates in the section 3.10 of te book Discovering Modern C++.

forward what?

In the variadic function, I’ve also used another thing that should be explained. Although all rules are quite confusing about rvalues/lvalues/xvalues… but for our code, it’s worth to remember the following thing:

Since we’ve used rvalue reference (&&) - to be correct, we might even say it’s a universal reference since there’s type deduction happening - we cannot change the type that is passed to internal function calls. Those references have names in the context of ShowMenuOnClick so they now become regular lvalue references. To pass them with unchanged type we have to wrap them into std::forward.

If you’re curious, there’s a whole section in the book Effective Modern C++ - 5th section, items from 23 till 30 about rvalue references and move semantics.

Summary

Variadic Templates offers a way to specify a function or class that accepts a variable number of arguments. All the work happens at compile time, so there’s no cost at runtime. This feature opens a lot of possibilities like type-safe printf, classes similar to touples or “factory” function that was presented in this post.

  • Do you use Variadic Templates?
  • In what areas they are they are especially useful?

BTW: If you liked this article, please sign up for my free newsletter.

References

IIFE for Complex Initialization

$
0
0

IIFE for complex initialization of const variables in C++

How do you initialize your variables, especially the const ones? What do you do when the code for the initialization is complicated? Do you move it to other method or just write inside the current scope?

Intro

I hope you’re initializing most of variables as const (so that the code is more verbose, explicit, and also compiler can reason better about the code and optimize).

It’s easy to write:

constint myParam = inputParam *10+5;

or even:

constint myParam = bCondition ? inputParam*2: inputParam +10;

But what about complex expressions? When we have to use several lines of code, or when the ? operator is not sufficient.

‘It’s easy’ you answer: you can wrap that initialization into a separate function.

While that’s a right answer in most cases, I’ve noticed that in reality a lot of people just writes code in the current scope. That forces you to stop using const and code is a bit uglier. So we have something like:

int myVariable =0;

if(bCondition)
myVariable
= bCond ? computeFunc(inputParam):0;
else
myVariable
= inputParam *2;

// more code of the current function...

I highly suggest wrapping such code into a separate method, but recently I’ve come across a new option.

I’ve got it from a great talk by Jason Turner about “Practical Performance Practices” where among various tips I’ve noticed “IIFE”. This acronym stands for “Immediately-invoked function expression” and thanks to lambdas it’s now available in C++. We can use it for complex initialization of variables. How does it look like?

IIFE

The imaginary code from the previous section could be rewritten to:

constint myVariable =[&]{
if(bCondition)
return bCond ? computeFunc(inputParam):0;
else
return inputParam *2;
}();

// more code of the current function...

We’ve enclosed the original code with a lambda. It takes no parameters but captures the current scope by reference. Also look at the end of the code - there’s () - so we’re invoking the function immediately.

Additionally, since this lambda takes no parameters, we can skip () in the declaration. Only [] is required at the beginning, since it’s the lambda-introducer .

Would you use such thing in your code?

I am a bit skeptical to such expression, but I probably need to get used to it. I wouldn’t use it for a long code. It’s probably better to wrap some long code into a separate method and give it a proper name. But if the code is 2 or three lines long… maybe why not.

One note: regarding generated code you should get the same optimized code no matter if it’s IIFE or static function (or in an anonymous namespace). At least this is what Compiler Explorer is showing me. Basically, the code should be inlined.

Your turn

  • What do you think about such syntax? Have you used it in your projects?
  • Do you have any guidelines about such thing?
  • Is such expression better than having lots of small functions?

BTW: maybe I should ask Java Script guys, since this concept comes from their world mostly :)

If you liked this article, please sign up for my free newsletter.

References

BTW: you can watch the whole Jason’s talk here:

IIFE from ~10:20 (about using const)

And here's another Jason's talk, solely about IIFE


Please declare your variables as const

$
0
0

Please declare your variables as const

I need to confess that for the last few years I’ve been a bit obsessed with the idea of making all variables const. Whenever I declare a variable in a function body, I try to think if I can make it constant. Let me explain why I think you should be doing the same.

What’s wrong?

What’s wrong with the following code?

int myVariable =0;

// some code...

myVariable
=ComputeFactor(params...);

Versus:

// some code...

constint myVariable =ComputeFactor(params...);

In the first sample, we’re just changing the value of some variable, and that’s typical thing in the code… isn’t it?

Let’s go through the list of benefits of the second approach.

Please note that I’ll focus only on variables used in function bodies, not parameters of functions, or class members.

Why it helps

Performance?

Several years ago, my colleague suggested using const for variables. I though the only reason for this was optimization and performance. Later, I understood that’s it’s not that obvious, and there are far more important reasons for using const.

In fact, a good C++ compiler can do the same kind of optimization no matter you use const or not. The compiler will deduce if a variable is changed or just initialized once at the start. So, is there any performance benefit here?

It’s hard to show the real numbers here. Ideally, we could get a C++ project (let say minimum 10k LOC) and then use const whenever possible, and compare it against the same project without const.

In a synthetic, small examples like:

string str;
str
="Hello World";

vs

const string str ="Hello World";

There can be a performance increase of even 30%! Numbers from J. Turner talk “Practical Performance Practices”. As one commenter noticed: the gain comes not from the const itself, but from the fact that we're not reassigning the value.

As we can see, there’s a potential in getting some performance, but I wouldn’t expect much across the whole project. It depends on the context. Maybe something like 1…or 2% max. As usual: measure measure measure! :)

Still, why not making life much easier for the compiler and have better code.

So, it seems that the "performance" is the strongest reason for using const, read below for far more important aspects:

Variables are declared local to their use

If you want to declare a constant variable, you need to have all the required data available. That means you cannot just declare it at the beginning of a function (like in standard old C-way). Thus, it’s a higher chance to have variables quite local to their actual usage.

void foo(int param)
{
constint otherVariable =Compute(param);
// code...

// myVar cannot be declared before 'otherVariable'
const
int myVar = param * otherVariable;
}

Declaring variables local to their use is not only a good practice but can result in less memory usage (since not all variables might be allocated) and even safer code.

Clear Intent

When you declare something as constant you make it clear “I won’t change the value of that variable.”

Such practice is vital when you read the code. For example:

int myVar =0;

// code...

// code...

When you see such a thing, you’re not sure if myVar will change or not. It might not be a problem in small functions, but what about longer, complex methods?

While having:

constint myVar =...;

// code...

You’re at least sure that nothing happens with myVar. You get one parameter less to track.

Clean Code

Sometimes the initialization of a variable won’t be just a simple assignment. Several lines (or more) might be used to give a proper value. In that case making the variable const will force you to move such initialization to a separate place.

As I described in IIFE for Complex Initialization you might enclose the initialization in IIFE or a different method. Anyway, you’ll avoid code looking like that:

int myVariable =0;

// code...

// complex initialization of 'myVariable'
if(bCondition)
myVariable
= bCond ? computeFunc(inputParam):0;
else
myVariable
= inputParam *2;

// more code of the current function...

No matter what you use, you’ll end up with only one place where the variable gets its value.

Fewer bugs

When a variable is const you cannot change it, so some unwanted bugs are less likely to happen.

Accidental problems might easily happen when there’s some long function and variables tend to be reused in some cases. You change the value of a variable, and it works for your case, but the old case where it was used now stops working. Again, declaring a variable as const will at least protect you from such stupid bugs. Not to mention that debugging such errors might be a real pain.

Moving towards functional languages

Functional style is probably a topic worth a separate article, but in general having immutable objects is an essential thing in functional languages.

Immutable objects are thread safe by their nature. When a thread processes that kind of objects, we can be sure that no other threads are changing the objects. Lots of data races can be avoided. That opens many ways to parallelize the algorithm relatively easy.

Because others says so

From C++ Core Guidelines (Con: Constants and Immutability)

Con.1: By default, make objects immutable Reason

Immutable objects are easier to reason about, so make object non-const
only when there is a need to change their value. Prevents accidental
or hard-to-notice change of value.

And

Con.4: Use const to define objects with values that do not change
after construction Reason

Prevent surprises from unexpectedly changed object values.

From Effective C++ by Scott Meyers (chapter 3):

Use const whenever possible.

The wonderful thing about const is that it allows you to specify a semantic constraint - a particular object should not be modified - and
compilers will enforce that constraint. It allows you to communicate
to both compilers and other programmers that a value should remain
invariant. Whenever that is true, you should be sure to say so,
because that way you enlist your compilers’ aid in making sure the
constraint isn’t violated.

Jason Turner:

Exceptions

‘A constant variable’ isn’t that an oxymoron?

Of course, there are situations where a variable need to be a ‘normal.’ In fact, you might argue that most of the cases involve the need to modify a value. So unless you’re trying to write functional code (that likes immutability), you’ll end up with tons of examples when you need to change a value (or just part of an object).

Simple examples: calculating a sum of an array, iterators, small functions, changing health param in GameActor, setting a part of GPU pipeline.

Still, bear in mind that the most of the above examples could be rewritten into an ‘immutable’ version as well. For example, you can use higher-order functions like Fold/Reduce, and recursion to implement many ‘standard’ algorithms. But that’s going into functional languages area.

One remark: while I was writing this article I realized I make a distinction here: variables vs. larger objects. In theory, those are the same, but for practical reasons, it’s easier to use const on smaller, ‘atomic’ types. So, I try to use const for smaller types: like numerics, strings, Vector2d, etc… but when I have some large custom class, I just skip const and allow to mutate its state (if needed). Maybe in my next iteration of my ‘const correctness’ I’ll try to apply that rule also on larger objects… so this would be a more functional style of programming.

Summary

Why you should use const in function bodies, C++

I hope after reading this post; you’ll at least try using const variables more often. It’s not about being 100% const every time, but it’s important to see benefits of such approach.

As I’ve described, the resulting code will be more verbose, explicit, cleaner (with probably smaller functions) and safer. Not to mention that you’ll get additional help from the compiler.

Do you const variables when possible?

Does your project guideline mention const correctness?

Debugging Tips Extra stuff

$
0
0

Async Remote Book Review

$
0
0

Async Remote Book Review

As you might already know, I work remotely. That’s why I try to follow and read guidelines/articles that relate to that style of working. Recently, I got very curious when I saw a new book called “Async Remote” from the Arkency team. Let’s see what it offers.

Warning: the book is not only about remote! :)

Disclaimer: The idea and all the content here is written on my own, but I asked the authors for a free version of the book (and I received it).

The book

Async Remote Book from Arkency

Book’s official site

The authors: Robert Pankowecki (@pankowecki) and Andrzej Krzywda (@andrzejkrzywda)

The structure

  • Story of size 1
  • Forget about sprints
  • Stream Of Work
  • Chronos vs. Kairos

The above chapters present a good flow of work in a project. How to split tasks, how to increase developer morale by using small tasks, how to maintain the backlog and new tasks. “Take the first task rule.”

  • Can developers be project managers?
  • Developer’s attitude
  • Customers Meetings
  • Developers creating tickets
  • Specification as floating ticket
  • Working with tickets
  • Start from the middle
  • Communication as tickets

Management stuff: mixing roles between devs and managers, working with clients, collecting requirements.

  • Over-Communication
  • On async
  • Knowledge sharing
  • Standups and meetings

How to communicate effectively in the async world, when you work remotely.

  • Tooling
  • Friendly workflow environment

Tools, tests, continuous integration,

  • Bonuses

My view

I enjoyed ‘Async Remote‘! It’s written in a good, fresh style. The authors do have the experience; they know what they are writing about. Although the content is sometimes heavy - like project management, the book talks about it in an easy to grasp method.

I only worry that the presented approach is sometimes too idealistic and it’s not easy to apply it in all environments. If you have a small team of people without much of legacy style of management, then there’s some hope of course.

5 Things that I should consider in my current work style:

  • Split tasks into smaller ones. I tend to work on larger, sometimes even not well-specified tickets. Now I understand why it’s important to be able to take smaller steps at a time.
    • People like to tick something off as quickly as possible. If you have some long task, you don’t see much progress then your motivation might drop.
  • Take the first task’ - assuming the tickets are in a proper order it’s vital to focus on the first one from the list. I like to take the least important (but probably easy to fix) and do it! :) I have something to tick off, but did it create any value? probably not…
  • Start from the middle’ approach - an interesting idea that makes you focus on the most important part and skip the other areas. As always it’s easy to work on the basic stuff and defer the real work. If you start doing the core part, there’s a higher chance you’ll progress quicker.
    • BTW: this can be applied even to blog posts! Think about the core part of the article, the real meat.
  • Over-communication and being proactive - another vital part of working remotely. It’s much better to write a bit more mails/comments than not have a clear spec to follow.
  • Experiment with tools and techniques: async standups/meetings, knowledge sharing, Slack.

Summary

Final mark: 4/5

Async Remote is a good book that covers a lot of topics relevant to remote people. Depending on what you prefer: project management, self-management, tools - you’ll find something for yourself. As I mentioned in the intro, it’s not only for remote projects - if you work in a corporate environment you can still benefit from the content. I’d say that everyone that uses some Agile approach will find something for himself.

Pros:

  • Not only about remote
  • Easy to read, small chapters
  • Practical knowledge and proven techniques
  • The authors have a real experience
  • Lots of psychological advice
    • For example “I have so much to do and so little time and the time is just
      passing by…” vs “I have 2 hours of my time, what is the best way I can use it?”

Cons:

  • Minor: Feels like a bunch of selected (but polished) essays so not as ‘professional’ as a larger book. Maybe there will be a second version that would go much deeper?

Have you read the book? What’s your opinion about it?
What’s your favorite book on the similar topic?

2016 Blog Summary

$
0
0

Blog Summary for 2016

Another year of blogging! Was it good or bad? What’s the plan now? What were the most popular posts?

First of all, please answer those quick questions: Bartek’s Coding Blog in 2016

The Story

Keeping things short I’d like to point out four major things that happened this year:

  • Posting schedule - I usually posted a new article every second week! Surprisingly, such routine gives much more ideas and energy to create new things. Much better than writing only when you have huge motivation/good topic.
  • ‘Rebranded’ a bit: I’ve understood that I cannot write about everything… it’s better to be more focused. So during my vacation break I’ve decided to focus on Core C++, Modern C++, some native coding stuff and probably performance optimization. Previously I did also some graphics/gamedev.
  • Newsletter was started - the mailing list is getting bigger and bigger, I am learning how to write better emails. Hopefully, those emails will also add some value to the blog. I plan not only to include recent post info but some bonus content as well. Sign up if you haven’t already! :)
  • Crazy reception of my ‘coding without Google’. In one day I got around 80k views, it’s around 25% of the total page views for the whole year! What’s funny this is relatively easy article, only my thoughts, stories, not a heavy C++ tutorial… but that’s how the internet works :)

Some issues

At the end of July I wanted to take one or two months of break, but it took me three months to return to blogging :) I’ve noticed that having a break makes you quite lazy and it’s hard to go back to the routine again. Ideally, I plan to have some post buffer so I could still get some time off, but not too much.

I know that my domain name is not perfect. But I decided to keep it more personal, and also blog title is now “Bartek’s coding blog” instead of Code and Graphics. I want to be my coding space, and maybe it will be a good starting point for some other future projects (with a better domain).

Some Stats

1st January 2016 till 25th December 2016:

  • Visits: 345 119 (3x more than the previous year!)
  • Unique visitors: 116 194 (2x more than the past year!)

graphics, since 1st June

Stats since June 1st

Coding without Google ~90k views

This post was a huge surprise for me. Instead of writing about technical stuff, I just wrote a bit of my story plus some thoughts about workflow… and it got massive attention. In one day it was viewed around 80k times! On my stats, it’s an outlier result.

The Matrix Updated ~23k views

In the above post, I’ve gone and dug out a very old project, written in Allegro Game Library. The code was a bit updated, so at least I could run in on modern hardware and OS.

C++ Status at the end of 2015 ~22k views

As usually, I try to make a C++ status update at the end of a year. This was my first post after a longer break, so I was very happy to see that people want to read it. Thanks!

Please declare your variables as const - 13k views

Very successful post that appeared in December this year. I knew it might be an intriguing topic, but I didn’t assume it would get so many comments. I plan to write a follow-up for this since it appears there is a conflict between const and move operations.

Custom Deleters for C++ Smart Pointers - 12k views

The text was inspired by one refactoring session where I tried to use unique_ptr in some legacy code. The case was good enough to land as a separate story and a blog post.

  • Notes on C++ SFINAE - a huge amount of work, lots of investigation, lots of learning. But I am glad to finalize this post. It was done in several months!
  • 11 Debugging Tips That Will Save Your Time - it took me a lot of time to prepare that list of tips. A lot of work, but eventually a good reception. An extended version of those tips landed in Visual Studio Magazine, see this post for more info.
  • Micro benchmarking libraries for C++ - reviews of testing libraries that usually should improve your performance tests. They give a lot of flexibility and a good set of reports. Much better than a custom simple approach.
  • Select + Mouse + OpenGL - I haven’t updated this post for a long time… but it still appears in my stats in a relatively good place! Thanks! I know I should update it to modern OpenGL, but I cannot promise anything at the moment.

Summary

As a reminder, if you haven’t already filled it: please give me your feedback through:

Bartek’s Coding Blog in 2016.

This was a good year for the blog I think. The blog didn’t earn 1mln of $$, and I hadn’t become an Internet rockstar… but still it was a lot of fun :) I’ve learned a lot, increased my motivation, creativity, perseverance. And I hope you also got some valuable content.

Some plans

  • Improve the posting pipeline. Keep the quality (or improve it), but reduce the time needed to publish an article.
  • Keep the plan, don’t take too many breaks :)
  • Have the buffer of posts
  • Start with some products? Like little self-contained tutorials? Tips and tricks? bonus content?
  • Improve my writing.
  • Write more guest posts

BTW: maybe it’s a good start thinking about your blog? What topics could you write about? I highly recommend writing down your ideas; other developers might be happy to read it. A blogging course from simpleprogrammer.com might help you with that, check it out here: link. I took that course some time ago, and it was pretty good.

C++ Status at the end of 2016

$
0
0

C++ Status at the end of 2016

Another year and another C++ Status! It’s hard to believe, but it’s my fifth time I am writing this summary. And, as usually, C++ language is very alive. The biggest news for the year?

Of course, it must be: the final draft for C++17!.

What else have happened? See my full report below.

Other reports: 2016, 2015, 2014, 2013, 2012.

Timeline

  • January 14, Alex Stephanov is retiring.
  • February 29 - March 5: ISO meeting in Jacksonville, FL, USA
  • March 9, Clang 3.8 released
  • April 19-23, - ACCU Conference
  • April 1, Intel Compiler v17.0
  • April 27, GCC 6.1 Released
  • June 6-10, NDC Oslo
  • June 20-25, ISO meeting in Oulu, Finland
  • June 27, VS Update 3
  • August 22, GCC 6.2 released
  • Sep 2, Clang 3.9 released
  • September 17 - 23, CppCon 2016
  • November 7 - 12: ISO meeting Issaquah, WA, USA
  • November 16, VS 2017 RC available!
  • November 15 & 16, code::dive conference in Wroclaw, PL
  • November 18 & 19 MeetingCpp, Berlin

C++11/14 compiler status

Before we dive into newest stuff, let’s recall what’s the status of C++11 and C++14 implementation.

Just for the reference Clang (since 3.4 ), GCC (since 5.0) and Intel (version 15.0) already have full support for C++11/14.

Visual Studio (as of current VS 15.0 RC) lacks some features: Expression SFINAE (Partial), Two-phase name lookup, C99 preprocessor (Partial). See this VS blog post for more information. Still, I don’t see that situation as a blocker.

C++17

Let’s now focus on the main topic from this year.

During the ISO meeting in Oulu (June), the Committee accepted the draft for C++17. This is definitely not a minor release since a lot of features were added!

Here’s what we’ll get in the version:

Language features

FeaturePaper
New auto rules for direct-list-initializationN3922
static_assert with no messageN3928
typename in a template template parameterN4051
Removing trigraphsN4086
Nested namespace definitionN4230
Attributes for namespaces and enumeratorsN4266
u8 character literalsN4267
Allow constant evaluation for all non-type template argumentsN4268
Remove Deprecated Use of the register KeywordP0001R1
Remove Deprecated operator++(bool)P0002R1
Removing Deprecated Exception Specifications from C++17P0003R5
Make exception specifications part of the type systemP0012R1
Aggregate initialization of classes with base classesP0017R1
Lambda capture of *thisP0018R3
Using attribute namespaces without repetitionP0028R4
Dynamic memory allocation for over-aligned dataP0035R4
Unary fold expressions and empty parameter packsP0036R0
Template argument deduction for class templatesP0091R3
Non-type template parameters with auto typeP0127R2
Guaranteed copy elisionP0135R1
New specification for inheriting constructors (DR1941 et al)P0136R1
Direct-list-initialization of enumerationsP0138R2
Stricter expression evaluation orderP0145R3
constexpr lambda expressionsP0170R1
Differing begin and end types in range-based forP0184R0
Pack expansions in using-declarationsP0195R2
Structured BindingsP0217R3
Hexadecimal floating-point literalsP0245R1
Ignore unknown attributesP0283R2
init-statements for if and switchP0305R1
Inline variablesP0386R2
DR: Matching of template template-arguments excludes compatible templatesP0522R0
Improving std::pair and std::tupleN4387
constexpr if-statementsP0292R2

Library features

  • Merged: The Parallelism TS, a.k.a. “Parallel STL.”, P0024R2
  • Merged: The Library Fundamentals 1 TS (most parts), P0220R1
  • Merged: File System TS, P0218R1
  • Merged: The Mathematical Special Functions IS, PDF - WG21 P0226R1

Resources:

While the above features look quite ok, we could see during the year that people were not entirely satisfied with the standard. As you might recall, last year I posted a survey on the preferred features for C++17 (number of votes for a feature, multiple selections were accepted), over 650 participants:

preferred features for C++17

And this is what went into the standard:

What went into the standard

Note:array_view, string_view were renamed into span and string_span see this info, and here.

Not much to be honest… Fortunately, most of the really desired features are close to being standardized, so it’s good sign for C++20.

Also, you can see those slides from Michael Wang:
PDF: Michael Wong – C++17, is it great or just OK…

Still, other people aren’t such harsh for C++, Phil Nash: C++17 - Why it’s better than you might think.

I think that C++17 is a decent release. We could always have more, but the committee did a good job this year anyway.

If you’re unhappy about not having concept, ranges, modules in the standard… you can still play with their experimental versions!

What’s your opinion?

ISO C++ meetings

There were three committee meetings this year.

February 29 - March 5: Jacksonville, FL, USA

June 20-25, Oulu, Finland

The most important meeting this year, they voted the C++17 draft!

November 7-12: Issaquah, WA, USA

Compiler Notes

Current versions, updates

The best resource to stay up to date with all the features and compiler releases is probably: cppreference.com/C++ compiler support.

Visual Studio

Current version VS 2015 update 3 (27th June 2016)

But you can check out the recent RC candidate for VS 2017: Visual Studio 2017 Release Candidate.

There are also various libraries available from Microsoft:

GCC

On Windows you can use the latest MinGW distro from nuwen.net (Stephan T. Lavavej): Nuwen MinGW distro updated with GCC 6.3 and Boost 1.63.

Clang

Clang has its implementation of modules; you can play with it here.

Intel compiler

Conferences

As usually, we had two main C++ conferences - CppCon and Meeting C++, but I’d like to mention another one where C++ was the core topic - code::dive from Wroclaw, PL. Plus, there’s NDC Oslo with a good set of talks as well.

CppCon

Trip reports:

Meeting C++

Schedule.

This year Bjarne Stroustrup gave the opening keynote (“What C++ is and what it will become”). The closing keynote was presented by Louis Dionne (“C++ metaprogramming: evolution and future directions”).

Meeting C++ 2016 Playlist

Code::Dive in Wroclaw, PL

November 15th & 16th, code::dive

At last a really awesome C++ conference in Poland! :)

This year the conference took two days, and it included a lot of talks about C++ and native development.

code::dive is non-profit, annual conference for C++ enthusiasts
sponsored by NOKIA. The main idea behind the conference is to share the
knowledge beyond cutting edge technologies and build networking
between the people.

Some of the presenters: Scott Meyers, Venkat Subramaniam, Andrei Alexandrescu, Dan Sacks, Ulrich Drepper, Michael Wong, Chandler Carruth, Sean Parent, and much more!

code::dive 2016 playlist

NDC Oslo 2016

NDC Oslo 2016, 6th-10th June 2016

Videos from the conference

Speakers: Andrei Alexandrescu, Anthony Williams, Dan Saks, Detlef Vollmann, Mark Isaacson, and more.

BTW: The main problem with the conferences: how to watch all of those valuable talks! :) One tip is to watch it at bigger speed (1.5x, 2.0x, …) in the player, but still, it requires a lot of time!

Books

Some of the books released this year worth seeing:

I am still waiting for Large-Scale C++ Volume I, John Lakos, but its release date is moving later and later…

Summary

I think that C++ developers were looking for much more with C++17. In 2015 we got a lot of promises, ideas about the new standard, so people started to dream too much. The reality appeared a bit different, a lot of those impressive features were not accepted or put on hold.

Still, we shouldn’t complain. C++ 17 will be a really decent release, and a lot of teams and companies are not even near accepting/using C++11. The adaptation is also an important factor when looking at the language use. It’s much better to have reliable, verified, checked features than something fragile that might change. I’d like to have concepts, ranges, modules soon… but C++20 is not that far. And yet I can play with those features using their quite stable experimental versions.

I’m glad to see that the committee is working consistently, and also quite transparently. You can feel the enormous community behind the language. So, all in all I think 2017 will be a good year for C++.

  • What do you think about C++ in 2016?
  • What was the most important event/news for you?
  • Did I miss something? Let me know in comments!

The poll below: what's your favourite C++17 feature?

Viewing all 325 articles
Browse latest View live