To use tiles or pre rendered images for game graphics

Started by
16 comments, last by Ravyne 8 years, 2 months ago

I never got the point of structs. I mean, it's literally just like a class, except it can't do anything.

It's exactly like a class. When you say, "it can't do anything", what particularly can structs not do? it can do everything a class can do (constructors, destructors, operators, functions, inheritance, etc...). It's exactly the same thing - two different keywords for identical purposes.

(Note: because C++ likes to repurpose keywords for unrelated purposes, 'class' is also an unrelated keyword for template arguments, and is interchangeable with 'typename' - but that's unrelated to the regular usage of the 'class' keyword)

'C' had 'struct'. C++ wanted the idea of object-orientedness, and added the 'class' keyword for basically no purpose except to underline the OOP-ness. It could've just as easily kept using the 'struct' keyword and C++ would be 100% the same, or could've easily gotten rid of the 'struct' keyword and only used 'class' for everything. The only reason it didn't get rid of the 'struct' keyword was for backwards compatability, and the only reason they added the 'class' keyword was... to be hip? tongue.png

I'm trying to underline that they are the same and so am being slightly facetious, but you can read Bjarne's own reasoning (at least somewhat). Essentially, though, it's just a quirk in C++'s design that there are two separate keywords.

Here's some example code showing that they have the same features:


#include <iostream>

struct MyStructBase
{
	int var = 0;
	
	//Structs can have member functions.
	void MemberFunction()
	{
		std::cout << "var = " << var << std::endl;
	}
	
	//Structs can have virtual functions (even pure virtual).
	virtual void SetSomething(int x) = 0;
};

//Classes can inherit from structs (because structs are classes - they're the same).
class InheritsFromStruct : public MyStructBase
{
	public:
	void SetSomething(int x) override { this->var = x; }
};

//Structs can inherit from classes (because classes are structs - they're the same)
struct InheritsFromClass : public InheritsFromStruct
{
	//Structs can have constructors and destructors.
	InheritsFromClass(int value)
	{
		//Structs can do polymorphism just like classes (because it's two keywords for same feature)
		InheritsFromStruct &polymorphism = *this;
		polymorphism.SetSomething(value);
	}
	
	//...stuff...
};

int main()
{
	InheritsFromClass myStruct(357);
	myStruct.MemberFunction();
	
	return 0;
}

What's the point of that? What can it be used for?

Well, since structs and classes are exactly the same (except for the aforementioned defaulting to public vs private, and the default access for structs/classes you inherit from - which is arguably an inconsequential and unnecessary difference), they can be used for exactly the same purposes. Two reserved keywords for the exact same functionality.

So, since they are the same thing, C++ programmers have generally decided to give them conceptual differences: structs for collections of data without logic, and classes for logic bundled with data, or just logic (or just interfaces without logic, or etc...). It's a "how we code" difference, rather than a difference in C++ code.

When you want an integer, do you do "int" or "signed int"? They both mean the same. You can also say, "signed long int", "long int", or just "long". rolleyes.gif

When you want an unsigned integer, do you do "unsigned" or "unsigned int"? They both mean the same.

(alright, so on some architectures "int" and "long int" may be different sizes, but on many systems they'd be the same)

Different people come up with their own coding styles and guidelines, and the C++ community in general has come to some consensus on some of it.

For bonus points:


auto signed long int myInt; //Usage of 'auto' prior to C++11. This usage of auto has now been either removed or deprecated.

Alot of older C++ features are so underused, some of them could be removed. Personally, I never liked the whole signed/unsigned/long system, and think C++ should standardize around the int32_t and friends, though I use 'int' and 'unsigned' when I don't care about the size.

Advertisement

I never got the point of structs. I mean, it's literally just like a class, except it can't do anything.

It's exactly like a class. I don't understand what you mean that "it can't do anything"... it can do everything a class can do (constructors, destructors, operators, functions, inheritance, etc...). It's exactly the same thing - two different keywords for identical purposes.

'C' had 'struct'. C++ wanted the idea of object-orientedness, and added the 'class' keyword for basically no purpose except to underline the OOP-ness. It could've just as easily kept using the 'struct' keyword and C++ would be 100% the same, or could've easily gotten rid of the 'struct' keyword and only used 'class' for everything. The only reason it didn't get rid of the 'struct' keyword was for backwards compatability, and the only reason they added the 'class' keyword was... to be hip? tongue.png

(I'm trying to underline that they are the same and so am being slightly facetious, but you can read Bjarne's own reasoning (at least somewhat))

Here's some example code showing that they have the same features:


#include <iostream>

struct MyStructBase
{
	int var = 0;
	
	//Structs can have member functions.
	void MemberFunction()
	{
		std::cout << "var = " << var << std::endl;
	}
	
	//Structs can have virtual functions (even pure virtual).
	virtual void SetSomething(int x) = 0;
};

//Classes can inherit from structs (because structs are classes - they're the same).
class InheritsFromStruct : public MyStructBase
{
	public:
	void SetSomething(int x) override { this->var = x; }
};

//Structs can inherit from classes (because classes are structs - they're the same)
struct InheritsFromClass : public InheritsFromStruct
{
	//Structs can have constructors and destructors.
	InheritsFromClass(int value)
	{
		//Structs can do polymorphism just like classes (because it's two keywords for same feature)
		InheritsFromStruct &polymorphism = *this;
		polymorphism.SetSomething(value);
	}
	
	//...stuff...
};

int main()
{
	InheritsFromClass myStruct(357);
	myStruct.MemberFunction();
	
	return 0;
}

What's the point of that? What can it be used for?

Well, since structs and classes are exactly the same (except for the aforementioned defaulting to public vs private, and the default access for structs/classes you inherit from - which is arguably an inconsequential and unnecessary difference), they can be used for exactly the same purposes. Two reserved keywords for the exact same functionality.

So, since they are the same thing, C++ programmers have generally decided to give them conceptual differences: structs for collections of data without logic, and classes for logic bundled with data, or just logic (or just interfaces without logic, or etc...). It's a "how we code" difference, rather than a difference in C++ code.

When you want an integer, do you do "int" or "signed int"? They both mean the same. You can also say, "signed long int", "long int", or just "long". rolleyes.gif

When you want an unsigned integer, do you do "unsigned" or "unsigned int"? They both mean the same.

(alright, so on some architectures "int" and "long int" may be different sizes, but on many systems they'd be the same)

Different people come up with their own coding styles and guidelines, and the C++ community in general has come to some consensus on some of it.

By saying they can't do anything, I meant that they couldn't use methods. Seems I was wrong.

What will you make?

Yea, it's just by habit and from coding style, programmers have mostly standardized on not giving structs functions to communicate intent.

I still occasionally give them functions, most often just constructors, but occasionally something more.

So much great information I have learned from this thread I am going to take the various advice and put it to good use. Hopefully in the next month my company will have our first demo of the game ready to show to potential investors and to the world in general.

Darcmagik

Of all the things I've lost I miss my mind the most.

Hey another question related to this same topic I'm trying to decide how I want to store the data related to my levels do I simply use text files, do I use XML or do I use lua? One big thing that is making it tough to decide is I am not sure what is supported on the XBOX One and with plans to release the game on there I was wondering if anybody has any insight into what is supported? I only ask because I can't find any documentation because I'm not a registered developer yet with Microsoft. So any insight into the topic would be appreciated.

I'm so far leaning towards either using text files to initialize the levels and maps of the game, or XML or I guess the other option is do hard code the texture file names and tile atlas/ sprite sheet data into the game? I realize this is something I need to decide but again just looking for people to help me talk this out lol.

Darcmagik

Of all the things I've lost I miss my mind the most.

Hey another question related to this same topic I'm trying to decide how I want to store the data related to my levels do I simply use text files, do I use XML or do I use lua?


Whichever makes the most sense for your project.

Personally, I'd use human-readable text files (which includes XML and Lua, and other things like JSON and YAML) and, if necessary, have your editor produce both the human-readable files, and produce optimized binary files for your project's actual releases. However, this most likely isn't needed for your current project.

One big thing that is making it tough to decide is I am not sure what is supported on the XBOX One

Doesn't matter. Focus on PC first (focus on whichever OS you use), and you'll be able to adapt it to XBox One once you do get that information. Your effort won't be wasted. If this is your first project, you should really focus on getting your project finished and running on your development machine before you worry about the "what if's" of console development.

I'm so far leaning towards either using text files to initialize the levels and maps of the game, or XML

XML is text files, as is JSON and YAML and many others. Lua also, unless you pre-compile the scripts, is text.

I think what you mean is, "I'm leaning towards making my own format, or using a pre-existing format".

I personally don't like XML, because I think it gets overused for situations that don't make sense and where XML is weak at, but many developers will tell you that they happily use XML.

Personally, I'd look at JSON first, and I'd suggest that to you (despite not using it myself! tongue.png) because it seems like it'd fit your map data better. However, look at examples of several different human-readable

I guess the other option is do hard code the texture file names and tile atlas/ sprite sheet data into the game?

Unless you have less than, say, 50 sprites, this is not a good idea.

Hmm OK I see what you are saying and yes I do mean making my own format, I was just trying to decide if that was really a path I should follow or not. As far as focusing on primary that is good advice. I just always keep the fact that I want to release on console in the back of my mind when I'm planning on various systems for my game. I just don't want to get to the end of something and find out that in order to make it work on a console that I have to completely rework a large chunk of code. To be honest I was mainly leaning towards just putting everything into .txt files, texture names, the map data for parts where I use it and other stuff like that. But when I started looking into the various tile map editors out there I keep running into the option to export to CSV, XML or lua. That got me to thinking well which one was a better option. I know how to work with CSV files and already have code for handling them and pulling the data out but I started to question which way is faster. But after reading your thoughts on my question I seem to be heading in the right direction. Now to actually get a map displaying on the screen....

Darcmagik

Of all the things I've lost I miss my mind the most.


By saying they can't do anything, I meant that they couldn't use methods. Seems I was wrong.

As an aside:

It's always (at least since proper C++, not sure if pre-standard C-with-classes was different) been the case that structs and classes were identical except for their default access specifier and their default inheritance (when you derive from a struct and don't specify public inheritance is assumed, but for classes private inheritance is assumed). Looking at it from this light, its plain to see that 'class' as a keyword was invented strictly as a way to promote default private accessibility and inheritence since the default behavior of the 'struct' keyword could not be modified without breaking backwards compatibility with C; the same could have been achieved simply by saying "you need to start a private specifier block to keep things private" or by not having blocks but requiring the private keyword to appear on each declaration as other languages have done. Bjarne, probably rightly, rejected those options as putting excess keystrokes between the programmer and best-practices, programmers being lazy and all (and also, computers were small back then, and the size of source code files was a legitimate concern).

Of course, the irony for myself and many people is that 'public:' is almost always the very first thing I type into my class declaration since I prefer public members to appear first, starting with constructors -- I usually only put private typedefs and the like in that initial private scope.

[addendum] And if that's news, you might also be surprised that unions probably aren't as limited as you thought either -- they can have constructors and member functions, and even have class types as members since C++11. They can't inherit or be inherited, can't have static member variables (but can have static member functions as long as the union has a name), and can't hold references (I'm not actually sure why this needs to be prohibited, and I think it should be allowed with the additional requirement that this would require the union to have a constructor that sets them). I find that not a lot of people know that.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement