Jump to content

  • Log In with Google      Sign In   
  • Create Account


FlyingDutchman

Member Since 01 Aug 2011
Offline Last Active Jan 13 2013 02:16 PM

#5013431 Using a pointer to point to a new object every frame

Posted by FlyingDutchman on 22 December 2012 - 09:59 AM

There's much much more wrong in that piece of code than just a plain pointer issue.

 

Besides the allocations you do every frame, there are also a lot more copy-constructor calls than you might expect.

Get rid of that; it may be just a Vec2 class but clean code doesnt hurt anyone.  use const references, or just pass the position as 2 parameters.

 

Next point is ... why do you want to use a pointer anyway? Sprite classes usually store their position for collision detection or rendering or whatever.

And you do realize that pointers take up memory as well, right?  Depending on the OS, the size is different. On Windows it's 4 byte if i remember correctly.

So basically you win nothing using your method.

 

best regards

 

true. And talking about efficiency. Malloc/new are quite expensive in terms of computation cost (if you compare it to just decreasing the stack pointer) and also lead to heap fragmentation. 




#4992776 2D RPG Example Games

Posted by FlyingDutchman on 22 October 2012 - 08:23 AM

Check my homepage . I developed a 2d RPG Engine (inspired by JRPEGS of the SNES area) incl. level editor and small dialog editor + turn based combat system (random encounter). You can find the source on my blog. (link is in signature)


#4968129 c++ initiliazation what do you call it

Posted by FlyingDutchman on 10 August 2012 - 10:30 AM

struct Some_Event** events = NULL, *event;

is a good example for one importat issue to remember: the * doesnt jump over to the next variable


so if u do

int *a, b, *c

you create an int pointer a, a regular int b and an another int pointer c

can be quite confusing.


#4963453 Are open pvp + full loot SANDBOX mmorpg's still possible?

Posted by FlyingDutchman on 26 July 2012 - 04:37 PM

i play UO sometimes on the freeshard the second age and it exactly how it was before they invented trammel and felucca. PvP everywhere. It is kinda frustrating in the beginning but I have to say that the majority of players are not PK'ers and that most of the newbie training spots are actually pretty well guarded. The cool thing about it is , that you really thrill when you go deep into the wilderness and you see someone with a red name (PK). Its like, shit i am dead and i will loose all my stuff. Posted Image But mostly PKs leave you alone if they see you are weak. On the other hand, you very soon learn not go out alone but in groups. That was one of the first lessons i learned in UO (10 years ago). STICK TOGETHER!

btw.. UO was even so cool , that even richard garriot was PKed while playing as lord british Posted Image Posted Image Posted Image

nowadays you dont need it anymore, and thats quite a shame. I can understand why. UO's prime (financially) came with the introduction of trammel.


#4943425 Facilitating communication between classes C++

Posted by FlyingDutchman on 26 May 2012 - 02:17 AM

Hey,

have you thought about using something like a MessageQueue ? This is an awesome concept as it allows you to communicate between classes without them knowing about each other and it works very nicely in multithreaded environment if you get your locks right.

I use it mostly like this:

class Message {
// i dont do anything :-)
};

class MessageQueue {
public:
MessageQueue(std::function<void(const Message &msg)> cb) : callback_(cb) {}
void PostMessage(Message *msg) { queue_.push_back (shared_ptr(msg)) };
void Update() { for (Message msg : queue_) { callback_(*msg) } queue_.clear()};
private:
std::vector<shared_ptr<Message>> queue_;
std::function<void (const Message& msg)> callback_;
};

Thats a bit how the interface looks like.
Now , every runloop call you call Update. and the queue will evaluate messages in the vector.

A custom message you can make if you derive from the Message class. For example like this:

class LayBombMessage : public Message {
public:
LayBombMessage(Player* p) : p_(p) {}
inline const Player& Player() { return *p_ } const;
private:
Player *p_;
};

Now when a player hits space you do this

MainQueue.PostMessage(new LayBombMessage(playerWhoDidIt));

and than in the update callback_ you can for example do s.th. like this.

void EntityManager::HandleMessages(const Message& msg) {
LayBombMessage *lbm = dynamic_cast<LayBombMessage>(msg);
if (lbm) {
LayBomb(lbm->Player());
}
}

Hope that helped a bit :-)

The cool thing is, that you can post Messages from every thread you want and it will evaluate them in the thread you call MyQueue.Update(). I use that a lot in Server environments. The example above needs only one additional mutex_ and two Lock/Unlock calls to work in a multithreading environment.


#4933726 Networking engine design trouble

Posted by FlyingDutchman on 22 April 2012 - 01:19 AM

If you really really want to learn how to do network programming, I would suggest you install Unix first and than read the book Unix Network Programming by W. Richard Stevens. This is an awesome book which covers most of the BASIC aspects of Unix server side coding like Polling, Asynchronous I/O (crucial) etc.. If you than have a thorough understanding of how the basic stuff works you can use to high level libraries like Boost::ASIO , which leverage a lot of work for you and provide a simple but efficient interface to handle at least a couple of hundreds clients.
Of course you could use a dedicated library straight from the beginning but especially in a field like networking it is often necessary to have an understanding about whats going on under the hood. Especially when you need to customize stuff. The cool thing about studying unix sockets is that it will also tell you alot about how unix works in general. It is really an eye opener to get an understanding of file descriptors etc..


#4877559 Array Out Of Bounds Allowed?

Posted by FlyingDutchman on 27 October 2011 - 08:22 AM

Xcode would tell u that the array is out of bound as a compiler warning!


#4870184 2D Zelda clone

Posted by FlyingDutchman on 07 October 2011 - 11:09 AM

I am currently writing a 2D TowDown SNES Engine for iOS and Android incl. Level Editor..

I would be interested .. i loved the old Zeldas...

Who will make the Grapics and Spritesets? Thats mostly a big problem


#4868419 Closed Thread

Posted by FlyingDutchman on 02 October 2011 - 05:16 PM

Well, you should start at the very beginning of such a Programm. The Interface. Look for Java APIS which are helping you to create Windows in Windows and/or Mac..

Than create some Windows, write Code to load pictures, write Code to pick Pictures, write Code to draw a map, write Code to draw different Layers, write Code....

A lot of working in front of you :-) But first, u should start with the Interface.. so google .. i bet Java has some cool Apis for Windows drawing..

peace


#4852562 calculating normal

Posted by FlyingDutchman on 22 August 2011 - 04:55 PM

good work! like straight out of the book :-)

For a plane normal you need a third dimension. Then you take two edges of the plane that share the same corners, make vectors of them, take the cross product of those vectors and then normalize the cross product. So let's give your plane a z axis... and put them in a vertex (pseudo) class so the data can be manipulated by us...

vertice plane_a(700.0f, 500.0f,0.0f);
vertice plane_b(720.0f, 500.0f,0.0f);
vertice plane_c(720.0f, 300.0f,0.0f);
vertice plane_d(700.0f, 300.0f,0.0f);

vector a(a,c);
vector b(b,c);//these constructors are defined as head - tail so the vector here is going to be c.x - b.x, c.y - b.y, c.z - b.z

vector crossvec = crossprod(a,b);
//crossprod is:
// Cx = AyBz - AzBy
//Cy = AzBx - AxBz
//Cz = AxBy - AyBx

//now you want to normalize the crossproduct you do that via sqrt(x^2 + y^2 + z^2) and then divide each of its coordinates by the value obtained. Last but not least you add the resultant vector (which is going to be 0,0,1 or ideally close to it) to one of the planes four vertices. Yeah, the normal here is one along the z but the method I described works for any plane orientation.






#4849101 2D ZombieShooter + Source CODE! [C++]

Posted by FlyingDutchman on 14 August 2011 - 03:18 PM

Hey everybody..

i just uploaded my new Game + Source on my Blog.. Check it out and leave some Comments!!!
http://howtomakeitinamsterdam.wordpress.com/

In two or three weeks I will also upload my 2D RPG TILE MAP EDITOR Source Code... I think its quite interesting for a lot of people here ...

Peace
Markus




#4843251 player look at the mouse, not as easy as it sounds

Posted by FlyingDutchman on 01 August 2011 - 02:20 PM

you can also do the following.

you get the x and y coordinate from the mouse..

than u divide the screen into 4 areas and let the player be 0/0 as the midpoint


               	|
               	|
	       |
               	|
--------------P--------------
               	|
               	|
               	|
               	|


you look now in which area the mouse is and than u create a right-angled triangle.

the vector from the player to the mouse is the hypotenuse. from now you can calculate the alpha or beta radiant and use this to rotate your caracter.

I used this technic 2 weeks ago in a game i wrote which will soon be available on my blog (with source code).. http://howtomakeitin....wordpress.com/

heres the source code.. ( i know, most of the variables are useless and it could be written shorter, .. i dont know what i did, it was 5am s.th. :-)


void CObject::rotate(unsigned int pointx, unsigned int pointy, int w, int h)
{
	unsigned int midx = this->x + w/2;
	unsigned int midy = this->y + h/2;

	double squarea;
	double squareb;
	double root;
	double angleplus;

	// create triangle
	double b;			// ankathete
	double a;			// gegenkathete
	double c;			// hypothenuse			c = sqrt(a² + b²)

	// in which area is pointx, pointy;
	if (pointx > midx && pointy < midy)			// viereck oben rechts
	{
		b = pointx - midx;
		a = midy - pointy;
		this->angle = 270;						// stellt object auf 0 grad
		
		squarea = a*a;
		squareb = b*b;

		root = squarea+squareb;

		c = sqrt(root);			// hypothenuse berechnet

		angleplus = asin(b/c)* 180.0 / M_PI;

		this->angle=this->angle+angleplus;		// addiert den winkel
	}
	else if (pointx > midx && pointy > midy)	// viereck unten rechts		// gut
	{
		b = pointx - midx;
		a = pointy - midy;
		this->angle = 0;
		squarea = a*a;
		squareb = b*b;

		root = squarea+squareb;

		c = sqrt(root);			// hypothenuse berechnet

		angleplus = acos(b/c)* 180.0 / M_PI;

		this->angle=this->angle+angleplus;		// addiert den winkel
	}
	else if (pointx < midx && pointy < midy)	// viereck oben links		// gut
	{
		b = midx - pointx;
		a = midy - pointy;
		this->angle = 180;
		squarea = a*a;
		squareb = b*b;

		root = squarea+squareb;

		c = sqrt(root);			// hypothenuse berechnet

		angleplus = acos(b/c)* 180.0 / M_PI;

		this->angle=this->angle+angleplus;		// addiert den winkel
	
	}
	else if (pointx < midx && pointy > midy)	// viereck unten links
	{
		b = midx - pointx;
		a = pointy - midy;
		this->angle = 90;
		squarea = a*a;
		squareb = b*b;

		root = squarea+squareb;

		c = sqrt(root);			// hypothenuse berechnet

		angleplus = asin(b/c)* 180.0 / M_PI;

		this->angle=this->angle+angleplus;		// addiert den winkel
	}
}



PARTNERS