2D Addon to Irrlicht

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Listing
Posts: 12
Joined: Wed Jan 28, 2009 9:30 am

2D Addon to Irrlicht

Post by Listing »

I would recommend you to add this code to the next
irrlicht release :)

It comes with the methods draw2DTriangle and draw2DCircle, that should be in every common 2d engine.

To fill the circle just change the width to the max :-)


Irrlichtprimitives.h

Code: Select all

#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

class CIrrlichtPrimitives
{
public: //Public functions
	CIrrlichtPrimitives(IVideoDriver*Driver) : pVideoDriver(Driver)	{}
	~CIrrlichtPrimitives()	{}
	void draw2dCircle(const core::position2d<s32>& center, unsigned int radius, irr::video::SColor &color);
	void draw2dCircle(const core::position2d<s32>& center, unsigned int radius, irr::video::SColor &color, unsigned int width);
	void draw2dTriangle(const core::position2d<s32>& a,const core::position2d<s32>& b,const core::position2d<s32>& c, irr::video::SColor &color);

private: //Private functions
	unsigned int GenerateCirclePointY (bool upperLimit, int x, int a, int b, int radius)
	{ //Point P(a|b) is the center of the circle... x is the relative pos to the radius
		if(upperLimit)
			return(unsigned int)(0.5+(b + sqrt((double) (-x * x + 2 * x * a - a * a + radius * radius))));
		else
			return(unsigned int)(0.5+(b - sqrt((double) (-x * x + 2 * x * a - a * a + radius * radius))));
	}

	unsigned int GenerateCirclePointX  (bool upperLimit, int y, int a, int b, int radius)
	{ //Point P(a|b) is the center of the circle... x is the relative pos to the radius
		if(upperLimit)
			return(unsigned int)(0.5+(a + sqrt((double) (2 * y * b - b * b - y * y + radius * radius))));
		else
			return(unsigned int)(0.5+(a - sqrt((double) (2 * y * b - b * b - y * y + radius * radius))));
	}




private: //Attributes
	IVideoDriver * pVideoDriver;
};
Irrlichtprimitives.cpp

Code: Select all

#include "IrrlichtPrimitives.h"

void CIrrlichtPrimitives::draw2dCircle(const core::position2d<s32>& center, unsigned int radius, irr::video::SColor &color)
{
	for(int vertical = 0; vertical < 2; ++vertical)
	{
		if(vertical)
		{
			for(unsigned int p = 1; p < 2*radius; ++p)
			{
				this->pVideoDriver->drawPixel(center.X-radius+p,this->GenerateCirclePointY(false,center.X-radius+p,center.X,center.Y,radius),color);
				this->pVideoDriver->drawPixel(center.X-radius+p,this->GenerateCirclePointY(true ,center.X-radius+p,center.X,center.Y,radius),color);
			}
		}
		else //horizontal
		{
			for(unsigned int p = 1; p < 2*radius; ++p)
			{
				this->pVideoDriver->drawPixel(this->GenerateCirclePointX(false,center.Y-radius+p,center.X,center.Y,radius),center.Y-radius+p,color);
				this->pVideoDriver->drawPixel(this->GenerateCirclePointX(true ,center.Y-radius+p,center.X,center.Y,radius),center.Y-radius+p,color);
			}
		}		
	}

}

void CIrrlichtPrimitives::draw2dCircle(const core::position2d<s32>& center, unsigned int radius, irr::video::SColor &color, unsigned int width)
{
	for(unsigned int i = 0; i < width; ++i)
	{
		this->draw2dCircle(center,radius-i,color);
	}
}

void CIrrlichtPrimitives::draw2dTriangle(const core::position2d<s32>& a,const core::position2d<s32>& b,const core::position2d<s32>& c, irr::video::SColor &color)
{
	this->pVideoDriver->draw2DLine(a,b,color);
	this->pVideoDriver->draw2DLine(b,c,color);
	this->pVideoDriver->draw2DLine(c,a,color);
}
Recognize it as a present done because of the nice support for irrlicht :-)

Greetings
Listing
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Drawing circles this way is very inefficient. Believe me I tried. I draw 2 circles and my fps drop below 50 (without anything on screen) on a quadcore and geforce 8800
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Filled circles can be efficiently rendered with a triangle fan, and a circle edge with a line loop.

Travis
Listing
Posts: 12
Joined: Wed Jan 28, 2009 9:30 am

Post by Listing »

Yes, you can if you want, post an optimized version.

Didn't know that it is that inefficient :(

Greetings
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

i dont think drawing a circle is efficient at all,cos an image is a quad :P that makes the image of a circle fastest.but i dont actually see any reason to have a draw 2d circle? Maybe im just biased
Post Reply