Game engine, C++,SDL (I'm new.)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

I wasn't quite sure where to put this on here...
I think I've become decent with C++, or at least enough so, so I decided I should learn some SDL, so I'm trying to make a little game where the character can move up,down,left, and right around the screen, and stop when touching walls, blocks, and stuff.

Anyhow, this all seemed to be going quite well until I decided I wanted him to be able to collide with more than one object. Wouldn't that take a lot of coding to do for a lot of different objects without some sort of a loop? I did some searching about it, but wasn't quite sure that I understood exactly how to do it...
Now, maybe this is something I should already know, but if so I don't see why I can't learn it now, is there some way to make objects of a class into an array? I guess I didn't feel I had a need to do something like that until it came to this.

Maybe this isn't going to be worth the time of coding. Should I be using a game engine? I'm not exactly sure how a game engine would work with my code. Is there just some library I can use just for collisions?

Maybe this seems like an easy question, but I just really don't understand how I could make a multiple collision detection with a loop, unless I could make an array out of a class.

(If you think this is in the wrong section and you can move it, please do.)

So, any recommendations for engines, or libraries with multiple collision detection?
Oh, and was it a bad idea to start using classes? It seemed like a good idea at first, but now I'm not so sure.


Thank you,

-Benjamin
grrwood
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 1
Joined: Mon Jun 20, 2011 12:57 pm

Re: Game engine, C++,SDL (I'm new.)

Post by grrwood »

in no order this is what i used to help figure out collision detection.

Lazy foo Collision Detection tut
http://lazyfoo.net/SDL_tutorials/lesson17/index.php

SDL tutorial 11 - (bounding box) collision detection
http://www.youtube.com/watch?v=lCc5pWXU ... re=related

Let's make a game in C++ (SDL,OpenGL) - Episode 6 - Movement and collision
http://www.youtube.com/watch?v=h_hYx3swLW4

Beginner's Guide to Game Programming Ep1 Part 9 Bounding Box Collision Detection
http://www.youtube.com/watch?v=evpAErQu ... ideo_title

GyroVorbis's channel on youtube has an explination on separating axis theorem but i cant find it now.
http://www.youtube.com/user/GyroVorbis

here is a text explination of it
http://www.sevenson.com.au/actionscript/sat/
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

Thanks for the response!
I think I saw "Lusikka"'s already, as I think that is where I got my method. But I think it only worked for two objects at a time, (without lots of coding).
I think I will look more into some of those others though.

Thank you,

-Benjamin
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

I think I just figured out what I want to do.

I think instead of creating arrays with the objects, I should just create arrays with the rects.

Thanks for the help anyways.

-Benjamin
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

Okay, now another thing...
Can I create an array of objects for a certain class?
I think I must be able to, how else can you create a game engine where you can put in multiple objects that are the same but in different positions at the same time? I mean, should I type out a new "rock" object 50 times, and apply the settings of the first one to the other 49? Surely there is a way.
Am I missing something obvious here?

-Benjamin
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: Game engine, C++,SDL (I'm new.)

Post by superLED »

Can't you just make a normal array with the objects?

Code: Select all

const int numOfMonsters = 10;
Monster monsters[numOfMonsters];
monsters[0].setPosition(32, 32);
monsters[1].setPosition(64, 8);
...

for(int i = 0; i < numOfMonsters; i++) {
// If there is a collision between the player and monsters[i] {
// Do something about it
// }
}
PS: I tried to make it as simple as possible
Last edited by superLED on Wed Sep 21, 2011 5:31 pm, edited 1 time in total.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

I think I've tried doing that but I think it came up with an error. I'll check again.
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: Game engine, C++,SDL (I'm new.)

Post by superLED »

I'm not totally sure, but I think this "Monster" class needs to have a default constructor (with no parameters).
Because you can't do something like this, as far as I am aware of: Monster monsters[10]("NameOfMonster", positionX, positionY);
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

But wouldn't I want to put the rocks in different locations? Should I have a draw function outside of the class?
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

Like, can I still do

Code: Select all

 rock[i].placement.x=x
?
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: Game engine, C++,SDL (I'm new.)

Post by superLED »

Where is "placement" coming from? A struct?

Anyway, this is perfectly legal to do, if this is what you are aiming for:

Code: Select all

class Rock {
public:
	Rock();
	~Rock();

	int x, y;
};

Rock::Rock() {
	x = 0;
	y = 0;
}
Rock::~Rock() {}

int main() {
	const int numOfRocks = 5;
	Rock rock[numOfRocks];

	rock[0].x = 0;
	rock[0].y = 0;
	rock[1].x = 1;
	rock[1].y = 1;
	rock[2].x = 2;
	rock[2].y = 2;
	rock[3].x = 3;
	rock[3].y = 3;
	rock[4].x = 4;
	rock[4].y = 4;

	return 0;
}
(I would recommend you to set x and y to private. But that's not important right now.)
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

But what is the point of the second function of the class? Do I really need it?
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Game engine, C++,SDL (I'm new.)

Post by ibly31 »

The second one that starts with ~ is a destructor. It's not 100% necessary, but it is called when the object's memory is going to be freed. An example of its use would be in a 3D model object - when you delete that object, who gets rid of all the memory you used to store the data? No one - that is your job, and you can do this easily with a destructor.
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

Of course, a destructor.
I remember reading about those once. I just forgot.
Thanks,

-Benjamin
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Game engine, C++,SDL (I'm new.)

Post by Benjamin100 »

Okay, hopefully this is my final question on the topic;

What is a good way to set up a tile engine? Are there any online lessons about this that are good?

Thank you,

-Benjamin
Post Reply