Help me with C++ for my game


Note: I wrote almost all of this last week, and I still didn’t upload your version to my Arduboy to confirm that everything works the same

You’ve explained the const thing very well.

I’m assuming one thing, a constant member functions cannot use a non constant member variable to calculate its stuff and return a value? Meaning, based on the the same arguments, a constant member function needs to do the exact same thing everytime?

In simpler terms, does it need to be a pure function?
Other question… is a member function considered pure if it uses non constant member variables to come up with its return value/what it does?

I think a reason people might not like having multiple files is because they don’t know how to name their files properly/decide which things to put where? (although… you still have that problem if you keep everything in one file - what to put where)

Or they’re a front-end web dev and they’re used to having long files?

I’m trying to implement slope tiles on my platformer (in the GameMaker version first, and I don’t it I’ll add it to the Arduboy version). It’s kind of breaking my brain trying to implement it. I don’t need help with that yet, I have the right resources, just still trying to implement it.

Stuff from today…

Hey @Pharap I just wanted to wish you merry Christmas, happy holidays, vacations, whatever it is you do at the end of December and whether you do anything at all, that you have a wonderful time doing it.

Thanks again for all the help from these past two months. A few people helped but you really did a ton. I was hesitant to buy the Arduboy when I did it but if I knew there was gonna be this “Pharap” feature I would have bought it instantly.

This part probably should have been a private message but I don’t know how to do that here, so it’ll have to be public. I’m moving this week so I’ll be back in a week or two

By the way I’ll make a UWP (Universal Windows Platform) version of my platformer, and it will be playable on Xbox One.

1 Like
(Pharap) #324

Precisely, const-qualified functions can’t call non-const-qualified functions.

In fact, when you’re in a const-qualified function,
the this pointer is no longer T *, it’s const T *.

Not necessarily. It depends on why there are two different versions of the function.

That said, I can’t think of a good reason for there to be a const-qualified function and a non-const-qualified function apart from wanting to return a const-reference and a non-const-reference depending on whether the object is const.

So in 99% cases, yes they should be the same.

No, they don’t have to be ‘pure’.

Technically they are allowed to modify their arguments*,
and techically all (non-static) member functions have state (the implicit this**).

* I recommend against modifying arguments in 90% of cases because I think reassigning arguments makes the code harder to follow. (There are exceptions to this of course.)

** If a function doesn’t make use of any of the object’s state (i.e. it doesn’t use this, implicitly or explicitly) then it’s often worth considering whether it should be a static member function, or even a free function.

For the duration of a const-qualified functions, all member variables are effectively const (because this is const)*.

If a const-qualified function doesn’t modify any state or any of its arguments then arguably it could be classed as ‘pure’, but it depends whether you count the implicit this as an argument or not.

From a language point of view, it’s not an argument.
From an implementation point of view, it is usually implemented as a function argument.

But remember, even if you class the object’s state as an argument, you still have to get the exact same return value for the same set of arguments.

So ultimately it depends on your outlook.

* That’s actually a bit of a lie. There’s one case where they aren’t, but that case is a very dangerous edge case that I don’t like teaching people about because it’s one of the few parts of the non-C parts of C++ I consider to be an outright mistake and I can count on one hand the number of times I’ve seen it used in production code.

Usually you just name it after the most important thing that lives in it.

Most of the time I’m a one-class-per-file (or one-struct-per-file, or one-enum-class-per-file) kind of person, so I find that pretty easy.
(Where did I put GameplayState? In GameplayState.h of course.)

It gets a bit more difficult when I’ve got a file that has multiple free functions, but really I ought to put those in a namespace and name the file after the namespace.

If I have a lot of related classes I’ll make a file that includes all of them and then include that file.
(And often I’ll put them all inside a folder with the same name as that file, and then have an outer file that includes the file that includes all the other files.)

Do any front-end web developers actually read and write pure HTML anymore?

I got the impression most used HTML generators and provided page layouts in some kind of template language (like Mustache) or server-side language (like PHP).

(If I ever make a website I always write the HTML myself, but I almost never make websites.)

That’s something I’ve never actually implemented.

I like the look of this solution:

It needs floating point (or fixed point) calculations, but the equation itself looks quite simple and the diagram makes is relatively easy to understand.

The thought is much appreciated and the same to you.

(Truthfully I’m a bit of a grinch though.)

As far as I’m aware nobody says ‘happy holidays’ in Britain.
It’s an American (and aparently Canadian) thing.

It doesn’t seem that long.

Don’t say it too loudly or everyone will want one. :P

Fair enough.

I’m trying to take a break from programming for a week or two anyway,
but I’m not doing a very good job so far.

Go onto a person’s profile and press the ‘message’ button.

(Alternatively if you click on someone’s name on one of their comments you get a box with the same button, but doing it that way starts the message with a reference to the comment. You can delete the comment reference, but it takes longer.)

I haven’t bought any new consoles since I got my dekstop.
Steam has converted me. :P

Just to confuse you, I hope you enjoy Boxing Day :P.

1 Like

Sounds like this would make it easier to change a single file and do a pull request specifically for it?

I was gonna ask about how to handle related classes

I’m not that knowledgable in that field, but people use Bootstraps or their own setup with just what they need (Bootstrap or not). With CSS files being kept seperately, as long as you use the same structure everywhere, there’s only one file you need to change to modify the looks and layout of your website

I figured out how to make it work for a 45 degrees slope (not on the Arduboy), it’s not finished yet. I did it seperately to test it better and debug it faster, now what I’ve got is collision with the cursor - if the cursor is in the slope, the slope turns green. Here’s the GML (GameMaker Language) code:

var _result = 0;
var _color = c_red;
var _x0 = x;
var _y0 = y;
var _x1 = x + TILE_WIDTH-1;
var _y1 = y + TILE_HEIGHT-1;

var _in_tile_x = (_x0 <= mouse_x && mouse_x <= _x1);
var _in_tile_y = (_y0 <= mouse_y && mouse_y <= _y1);
var _in_tile = (_in_tile_x && _in_tile_y);

var _local_x = mouse_x - _x0;
var _local_y = mouse_y - _y0;
var _top = _y1 - _y0 - (_local_x - floor(_local_x / TILE_WIDTH) * TILE_WIDTH);
if (_local_y >= _top) && (_in_tile) {
	_color = c_green;
	_result = 1;

if keyboard_check_pressed(vk_enter) {
	show_message("localY: " + string(_local_y) + "\ntop: " + string(_top));

draw_triangle(bbox_left-1, bbox_bottom, bbox_right, bbox_top-1, bbox_right, bbox_bottom, false);

draw_text(x-16, y-16, _local_x % 8);
draw_text(x-8, y-16, _top % 8);
draw_text(x, y-16, _result);
draw_text(x+8, y-16, _local_y);

draw_rectangle(mouse_x, mouse_y, mouse_x, mouse_y, false);


They were productive months with few headaches. I get actual headaches if I focus hard on something and can’t figure it out after a while. You have literally saved me a ton of headaches

I know what Boxing Day is! You couldn’t confuse me. Your evil plan did not work.

Where I’m from we only say “Joyeux Noel”, but we do say “Joyeuses fêtes”, that last one is when you’re talking about Christmas + vacation time (no school/no work) and new year’s

(Pharap) #326

Good point, that’s also true.

If you had one massive file then pull requests would always affect just a small part of the same file.

I wouldn’t be surprised if even the CSS is machine generated these days.

The problem is then how to resolve that collision.

I get a bit stressed sometimes if I’m doing something difficult,
but the more I learn the less difficult things I encounter.

Usually the most difficult thing I end up doing is library design or making sense of certain people’s code.
Occaisionally I’ll pick a very difficult project.

Often if I can’t figure something out I usually just write a few notes and let it simmer for a few days while I’m doing other things and I’ll often have a sudden epiphany when I get back to it later.

I find I concentrate a lot better when it’s quiet, but I rarely get quiet.

Often the end result is worth the frustration though.

I’m sure we used to have an ornament that said that.
(As well as one that said ‘feliz navidad’.)

Do they celebrate it in Canada?

Bambozled again.

‘En vacances’. (Merci M. Poirot.)

In Britain we don’t say ‘vacation’, we say ‘holiday’.
(“I’m going on holiday.”, “Holiday time” “Enjoy your Holiday(s).”)

We say that even if people aren’t actually leaving the country or it’s just taking paid time off and there isn’t an actual holiday as such.

1 Like

I’ve been bamboozled by my Xbox One. I keep playing, it’s addictive (I’m in love with Halo 5’s “Forge”).
I’d like to finish what I started so I’ll be coming back this week. My Arduboy platformer was almost finished, it’d be great to finally release it this month.

1 Like
(Pharap) #328

I decided to take a programming break from late December onwards so I could finally take some time to play some games (mainly Driftmoon on Steam).
I’ve already started breaking my break though.


I’m finally back Edit: maybe I’m not, I just realized I’m on my old laptop and I don’t have all my tools. But the Arduino IDE is installed and I can compile things, so I’m probably back.

I’ve been looking into older NES games to see how they designed things back then.

I really like how Super Mario Bros 1 on the NES handled changing levels. There’s a transition screen, you see the current stats, it fades away and then you focus on beating the level. If you run out of lives, you start from the beginning*. I think that’d be a good approach for my platformer.

As for the levels, I think I’ll try two different things since I don’t know which will end up being better for what I need. I’ll do tiles on an array like I’ve been doing, then I’ll try using a type of “object” (a block with a width and height, and what type it is. For example:

type = brick;
widthInMeters = 8;
heightInMeters = 1;
x = 7;
y = 0;

In practice I can probably squeeze some of those values together so that they take less space. The advantage of this is that one long floor would cost 5 bytes instead of the 16 for the whole width. Then I could load it as tiles if I want to keep using my tile collision system. This way of doing it would allow me to use all my beautiful sprites.

*I prefer Super Mario Bros 3’s method of starting at the beginning of the current world instead of at the beginning of the game, but I see advantages to SMB1’s design - mainly that it lets you see how much you improved when you try the first levels again)

1 Like
(Pharap) #330

I think the original games used two layers of tiles,
or just one tile sheet and had different kinds of tiles (with some being solid).

Blocks with specific states (e.g. breakable blocks and ? blocks) were probably entities though.

You might want to look at @filmote’s LodeRunner since that’s the closest I can think of to the original mario games on Arduboy.
(I know there’s a maruino or something but I’ve never tried it.)

If you keep all blocks the same size then you don’t need to store width and height.

Lastly, some code for you to mull over:

enum class BlockType : uint8_t

struct BlockInfo
	// May be compressed
	uint8_t width;
	uint8_t height;
	bool solid;

const BlockInfo blockTypeInfo[] PROGMEM =
	// None
	{ 0, 0, false },
	// Brick
	{ 8, 8, true },
	// MysteryBox
	{ 8, 8, true },

// memcpy_P is from <avr/pgmspace.h>
// See here:
BlockInfo getInfo(BlockType type)
	const uint8_t index = static_cast<uint8_t>(type);
	BlockInfo result;
	memcpy_P(&result, &blockTypeInfo[index], sizeof(BlockInfo));
	return result;

struct Block
	// May be compressed
	BlockType type;
	uint8_t x;
	uint8_t y;

(The two structs should ideally be classes with getters,
but I couldn’t be bothered to write out all the getters for the sake of an example.)


A few centuries later…

I’m back but not directly to the Arduboy, I’ll make a version of my Adventure/RPG/Hack 'n Slash/Zelda-like thingy (am I using enough labels yet!?) for PC first with the same art style but more sprites and probably a bigger world.

Then I’ll take whatever’s best about it, cut out anything unnecessary and remake that as a version for the Arduboy as was originally plan. Either make a small world that’s great enough or continue with the ideas I had to have a big world proceduraly generated to save storage space.

The good thing is, since the two games wil have different worlds entirely, playing the game on one platform won’t ruin your experience for the other version.

The Arduboy version shouldn’t be at a disadvantage when it comes to dungeons, because I have no doubt amazing dungeons are possible with procedural generation. I mentioned a few options for that higher in the thread, which is great, because I’ll be able to read what I thought about if I forget anything.

Either way for now I’m doing sprites:


For the Arduboy I could get rid of the wall sprites entirely and leave it as a black background instead but for PC I don’t have sprite limits

The chairs were my attempts at a smaller table, but I don’t think a decent single tile table is possible at an 8x8 sprite size.