Category: C++

Using printf type variable parameters in your function

Using printf type variable parameters in your function

The C programming languagI needed this in a bit of debug code. I wanted it to work like printf where there’s a format string containing one or more % format specifications and then write this into a buffer and dump it where ever.

C has a library stdarg which lets you do this. It’s not the most intuitive but it’s definitely worth understanding.

What I’m wanting to do is a function that does something like this (assume s1,s2 and s3 are char *).

sprintf(buffer,"Some string values %s %s %s",s1,s2,s3);
doSomething(buffer);

But in my own function and with the ability to have 0,1,2 or how ever many parameters without having to write a separate function for each. Kind of what printf does.

Here’s the code:

 

#include <stdarg.h>
void op(char* s, ...) {
	char buffer[50];
	va_list argptr;
	va_start(argptr, s);
	vsprintf_s(buffer,sizeof(buffer),s,argptr);
	OutputDebugStringA(buffer);
	va_end(argptr);
}

The … represent the variable number of parameters. it’s called the variadic operator. To access the actual parameters needs the various va_ macros and types. For instance va_list is a type that manages the list of parameters. The va_start macro takes the list and the parameter before the list. vsprintf_s is the Microsoft secure version of vsprintf. Both are the variable parameter equivalent of sprintf/sprintf_s.

OutputDebugString is the Windows debug string function. Finally the va_end tidies up everything.

So you use this just like printf, except the output goes to the Debug channel and can be picked up in Visual Studio (if debugging) or by running the SysInternals free DebugView utility.

Note, the original version of this used OutputDebugString but I found it was outputting gibberish. I correctly guessed that it was linking to OutputDebugStringW ; the MBCS version and changing it to OutputDebugStringA (the ASCII version) fixed it. Something to watch out for on Windows. 

The Joys of C++

The Joys of C++

Maze
Image by Arek Socha from Pixabay

My progress on the C++ asteroids game took a little detour down a one way street. The problem I hit was until then I thought I was being clever by passing in a reference to another class in the constructor. That worked fine until I started implementing array classes.

The issue I got was using my constructor meant that the default constructor was deleted. This happens in any class where you add your own constructor as all the special functions are deleted. You then have to add your own Move or Copy assignments if you are doing things that invoke them. Like iterating through an array (or vector in my case). Although I use Bullet and Asteroid classes, I manage  collections of them through an Asteroids and a Bullets class.

Plus I’d decided that vector class wasn’t perhaps the best class to use. This thing runs at 60 fps, so adding and deleting elements from a vector seems a bit wasteful. Instead by using a std::array, and constructing all elements in it when the managing class is instantiated, all I have to do is scan for the first element with an active flag set false. (all are set false when constructed), set a few fields for velocity and position and there it is on screen.

Is this premature optimisation? I don’t think so. One of the things that programmers are told NOT to do!

So I have cleaned up my constructors now. They are parameterless and no special functions are deleted. It’s this kind of stuff that can do your head in and one of the reasons why I think C++ is considered a harder language to master.  I’m certainly a long long way from that.

The maze? Just a metaphor for C++ programming!

A small C++ Tip. Return values from Constructors

A small C++ Tip. Return values from Constructors

C++ LogoYou can’t return anything from a constructor.  One way is to use exceptions but those can bring their own issues. Google’s C++ guidelines actually stipulate no exceptions and I’ve heard it from others as well that they prefer not to use them. Some people avoid it by having a mostly empty constructor and then an init() method to do the real initialisation and return a success/fail state.

But there is a simple trick that you can use, just return the state as a reference. Here’s an example. If you want to return more values, use a struct.

#include <iostream>
using namespace std;

class Simple {
public:

    Simple(bool& failure) {
        failure = true;
    }

    ~Simple() {
    }
};

int main() {

    bool fail_flag = false;
    Simple f(fail_flag);
    if (fail_flag)
        cout << "failed " << endl;
    else
        cout << "Success" << endl;
}
C++ enum class vs enum

C++ enum class vs enum

C++ LogoI’d read about enum class in C++. It’s a slight strengthening of the compiler checking compared to plain old enums.  Why you may wonder? Well,

  • Conventional enums can implicitly convert to int, causing errors when someone does not want an enumeration to act as an integer.
  • Conventional enums export their enumerators to the surrounding scope, causing name space pollution.
  • The underlying type of an enum cannot be specified, causing confusion, compatibility problems, and makes forward declaration impossible.

Additionally you can declare the underlying storage type, which lets the compiler catch bugs where a value is too large for the storage. Here’s a made up example to show this:


enum class byteThings : unsigned char {
Thing1 = 0x01,
Thing2 = 0x02,
Thing3 = 0x04,
BigThing = 0x120  // Compiler will complain!
}

The downside is that to get the int value, you now need to do a static cast but it does make your code safer.

byteThings b = Thing2; 
int i= static_cast(thing); // 2

 

C++ Asteroids is progressing

C++ Asteroids is progressing

C++ AsteroidsThis is a screenshot of it as it stands and yes those asteroids are moving! It looks identical to the C version; the only difference is the code, not the appearance.

What makes it interesting is the structure. The Player and Asteroids classes both inherit from a Common base class that has all the position and move data.

It took a bit of time to get the overall architecture right. There’s a Game class which manages everything; the Player, Asteroid and Lib classes.  But Asteroids and Player classes also use the Lib class so  it took a bit of faffing around to make them all play happily together.

This is the biggest difference between C++ and C; the classes and how they all fit together. My biggest source of frustration has been arm wrestling with the C++ compiler; it sometimes seems as if never get easier! Sometimes a wrongly placed semicolon can generate hundreds of compile errors…

It took half a day to get it right and bullets should fit in much easier. Mind you I’ve had to incorporate two static variables in the Common class. One is a copy of the SDL renderer; this is needed to draw things on screen. The other is a pointer to the Game class so that I can access the DebugFlag state.  This is used to show debug information but I may be able to move the flag itself into the Common base class and change it through a call on a Player method. So long as it is static then changing it for the Player also changes it for the Asteroids and Bullets.

 

Slight change in direction – more C++ and Pi

Slight change in direction – more C++ and Pi

Raspberry Pi
Image by planet_fox from Pixabay

As I said recently, writing about C and games probably isn’t enough to sustain this site, but if I extend it to include C++ and Raspberry Pi and still maintain the overall direction of writing about game development then that I think will do it.

So I’ve added a new page for C++. I’m currently working on the C++ version of Asteroids and making good progress.

I’m striving to write it in modern C++. To that end, the first entry in the C++ page is a link to a very long document: C++ Core Guidelines written by two luminaries of the C++ world Herb Sutter and Bjarne Stroustrup (creator of C++). You should definitely give it a read.

Interesting fact about the Raspberry PI. Did you know that in March 2020, they sold 640,000! That’s pretty amazing! And while that article says that AAA game playing isn’t something you can do on a Pi, we now know that you can run simple 2D arcade quality games on a Raspberry Pi 4.

I’ve started on the C++ Windows eBook

I’ve started on the C++ Windows eBook

C++ Code listing photoI made the mistake of starting by trying to convert the final version of Asteroid; all 2,200 lines of C into C++.

It got very messy because I was trying to have all the moving objects (Player ship, asteroids, bullets, aliens ship) all based on a common ancestor class but then was trying to manipulate those instances of the ancestor class and downcast back from the ancestor instances and I don’t think you can in C++. Compiler errors galore!

It was the wrong approach and I wasn’t using virtual functions. So instead I’m doing it step by step, adding on new features. Much like the original C development in 13 different steps.

Here’s the slightly shorter asteroids.cpp:

// Asteroids C++ 2020 Chapter 27
#include "game.h"

int main(int argc,char * args[])
{
	Game g;
	g.InitSetup();
	g.GameLoop();
	g.FinishOff();
    return 0;
} 

There are other classes used from Game. I haven’t put everything in one “God” class!

Why C and not C++?

Why C and not C++?

C++ LogoConfession, I can program in C++ though I’m a bit rusty and my C++ knowledge stops short of creating template classes but I can use them.  But given that probably most games these days are programmed in C++ why is this site about C?

Well it’s been mostly personal preference. I like C; it is a heck of a lot simpler than C++ (understatement!) and compilations are very fast! I know most of C though to my chagrin I admit I got include guards wrong for quite a while. I was using them in .c files rather than .h.

Plus, I haven’t really looked into WebAssembly enough. I know it will work with both C and C++. But it has to be said that there is probably only so much you can write about C, so I will be including C++ in the future. I’d planned to do a C++ conversion of my Ebook(s).

Besides which if you google for something involving C, you’d be amazed how many C++ and even C# websites are in the results!