C++ Progress ish

The Partridge Family were neither partridges nor a family. Discuss.
ceofil
Posts: 39
Joined: April 13th, 2017, 8:35 pm

Re: C++ Progress ish

Post by ceofil » May 7th, 2017, 11:11 am

So I've watched tutorial 21 and apparently using 2d arrays like this ( a[][] ) it's bad and superevil and ugly. It has to do with passing into function and pointers and stuff. So I hope it won't be hard to make the transition.

When I first started watching Chili's videos I had kind of the same problem. The way I understood the framework it was like a matrix but I would always fuck up and switch x and y because in the matrix the first parameter the vertical coordonate and the second is horizontal.

Anyway... why the fuck do we learn [][] arrays in school if we'll have to change anyway. Can't they just tell us about them and then move on using it in the correct way? Or is it just me? Did you guys learn directly using 2d arrays as 1d arrays in the first place?

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: C++ Progress ish

Post by chili » May 7th, 2017, 2:00 pm

Well here's the deal.

I don't super hate [][] arrays in and of themselves. They make the compiler calculate the 2d->1d mapping itself which is a good thing. The problem is: when you get into dynamically allocated arrays, you cannot use that notation, because in order to use it the compiler has to know the dimensions (at least n-1 one of the dimensions) of the array at compile time. So that's why I teach the 1d format; it is applicable in all situations. The other reason I prefer it is because people tend to get the order mixed up ([row][column] vs. [column][row]), whereas with [x+y*width], there is no mixing up.

However, if you are using a static array in 2D, I will not shit all over you for using the [][] style ;)
Chili

ceofil
Posts: 39
Joined: April 13th, 2017, 8:35 pm

Re: C++ Progress ish

Post by ceofil » May 7th, 2017, 2:25 pm

Thank you for explaining the concept better. I really apreciate what you're doing...with the tutorials and the community and all.
I hope I did not sound cocky.
It's just...idk, watching trough the tutorials I think I was able to easier understend some concepts because I did them in school but as I advance in the tutorials there is more and more stuff that I don't know and it's getting harder and harder. And I kinda like it but I kinda don't? I mean I like it when I first see you applying the new concept I'm like "how did I live without knowing about this" and then I get frustrated because things don't work, I don't know why, but in the end, after I figure it out I start liking it again. That stage is nice. Knowing that you have something new to work with. I love that feeling. And you start getting all these ideas of things you could do with it. Awesome.
Anyway, idk what I'm babbling about. Thank you, man.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: C++ Progress ish

Post by chili » May 7th, 2017, 2:41 pm

Np. It definitely shows that you've had some experience coding before starting the tutorials. You're doing really well bro.

I looked at your 2048 code, and it looks good. I feel like you've improved in readability compared to the earlier code you've written. Overall better naming choices. I don't know the rules of the game, but your implementation seems well done. Keep it up!

And if you like the feeling of discovering new shit, you'll love Intermediate ;)
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ Progress ish

Post by albinopapa » May 7th, 2017, 9:04 pm

With the dynamic allocation statement, this isn't entirely true...entirely. Classes that have the operator[] overloaded like std::vector and std::unique_ptr[] can be made to use [][] indexing. The reason I say "not entirely true" is because unlike the C style static multi-dimensional array which is allocated as a 1D array, to get the same indexing with the C++ containers, you'd be allocating an array of arrays, but still a 2D array.

Code: Select all

	constexpr size_t numIntVecs = 25u;
	std::vector<std::vector<int>> myIntVecs( numIntVecs );
	for( auto &myIntVec : myIntVecs )
	{
		myIntVec = std::vector<int>( numIntVecs );
	}

	for( int y = 0; y < numIntVecs; ++y )
	{
		for( int x = 0; x < numIntVecs; ++x )
		{
			myIntVecs[ y ][ x ] = x + ( y * numIntVecs );
		}
	}

// And just for fun
	size_t count = 0;
	for( auto rows : myIntVecs )
	{
		for( auto cols : rows )
		{
			cols = count++;
		}
	}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: C++ Progress ish

Post by chili » May 8th, 2017, 1:29 am

Yeah I have never considered vector of vectors to be a true 2D array. It's fine I guess when performance doesn't matter, but when it is an issue, this solution can be terrible (you lose memory contiguity). You also have a lot more thrashing of alloc.

If you wanna get picky with semantics, such a thing isn't really a multi-dimensional array in the first place. A multidimensional array is an array of arrays, and the elements of an array in C++ are contiguous in memory.

The only true advantage I see in vector of vectors is that each row can have an independent rank.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ Progress ish

Post by albinopapa » May 8th, 2017, 2:10 am

I'm a little confused there chili.
chili wrote:A multidimensional array is an array of arrays
Well, a vector is still technically an array, so a vector of vectors is an array of arrays. To me it wouldn't be any different than std::array<std::array<int>> or std::unique_ptr<std::unique_ptr<int[]>[]> with the latter being the most confusing to work reason about.

I do believe there might be some performance loss in jumping to each container of vectors though, but for each vector in a container, there wouldn't be a performance hit, would there?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: C++ Progress ish

Post by chili » May 8th, 2017, 3:39 am

Nah, technically an array is an array and a vector is a vector. That's why they have different names and different implementations. A single vector could be treated as an array, because you are just accessing a block of memory via a pointer, but when you add multiple dimensions, you have to add a pointer follow operation for each additional dimension, so the data structure you have is effectively a hybrid of a linked list and an array.

For sufficiently large rank sizes of element vectors, the performance hit would be accordingly amortized. You would still require N times more heap allocations to create the object, but whether this would be an issue or not depends on use case.
Chili

ceofil
Posts: 39
Joined: April 13th, 2017, 8:35 pm

Re: C++ Progress ish

Post by ceofil » May 19th, 2017, 6:00 am

Hi there!
So I wanted to make this game for a while but didn't know how to manage the rotation of the tank. Last night I had this idea of using circles so it will look like it's rotating. It took me a long time to figure out why the angle was the negative value of what I wanted it to be. I was working with sin and cos having in mind the graph we use in math functions and stuff but in the framework the y ascending from top to bottom.
Anyways, I'm pretty happy that it works. Left some test code in there. Will post updaates soon.
I'm open to suggestions and all that good stuff.
Have a nice day!

GitHub Link

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: C++ Progress ish

Post by chili » May 20th, 2017, 3:27 am

There are a few ways you can do it. The easiest is to just have n-sprites for n-directions of orientation of the tank, and chose the sprite that is closest to the current angle of the tanks orientation.

The others will require that you learn at least the trigonometry of rotation of a point (check out my old Advanced series for that). You can make the tank as thin lines (poly lines) and then you rotate the points and render the lines. If you figure out how to draw filled in triangles, you can make the tank as solid colors, again by rotating the points of the triangles. Or, if you can get textured 2D triangles working (again, in my advanced series, also in the 3D Fundamentals series) you can rotate a textured quad (basically, rotating a sprite). These techniques will also allow you to scale.
Chili

Post Reply