Crosshair explanation

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
screwgh
Posts: 19
Joined: Tue Oct 14, 2008 12:27 am

Crosshair explanation

Post by screwgh »

Here a bit off code snippet to make a crosshair:

put this following code in you`re while loop but remember to put this after drawAll otherwise it won`t work.

Code: Select all

	while(device->run())
	{


		driver->beginScene(true, true, SColor(255,100,101,140));	//Must draw between begin and end. Begin drawing
		//////////////////////////////////////////////////////////////Begin drawing


		smgr->drawAll();											//Scenemanager drawing
		

		//Draw crosshair
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW,cenH-8,cenW+2,cenH-3) ); //above 
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW+5,cenH,cenW+10,cenH+2) ); //right 
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW,cenH+5,cenW+2,cenH+10) ); //down 
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW-8,cenH,cenW-3,cenH+2) ); //left 
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW,cenH,cenW+2,cenH+2) ); //center of screen
	
		guienv->drawAll();											//Gui environment drawing
	
		

		//////////////////////////////////////////////////////////////End drawing
		driver->endScene();											//Stop drawing
	}
to get the position and the middle of the screen I created the folowing variables.

Code: Select all

int screenW = 800;
int screenH = 600;
int cenW = screenW / 2;
int cenH = screenH / 2;
Thats all you now you got a nice crosshair to start with.
Mazza
Posts: 1
Joined: Sun Oct 19, 2008 12:13 pm

Post by Mazza »

Simple and works great, thanks a lot :)
Pakje
Posts: 11
Joined: Sun Sep 07, 2008 11:42 am

Post by Pakje »

im wondering isnt it better to use the irrlicht variables?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Pakje wrote:im wondering isnt it better to use the irrlicht variables?
what are Irrlicht variables !?!?! :lol:
you mean Irrlicht types, I think... ;)

well, the code is nice, even it was covered a few times in the beginner forum... ;)

but I, for myself, prefere to use an image for the cross hair though, this way you can use any shape for the CH and switch between different CHs easily... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

There u have texture support

Code: Select all

class CCrossHair : public irr::gui::IGUIElement
{
public:
    CCrossHair(irr::gui::IGUIEnvironment *  environment, irr::u32 id = -1) : irr::gui::IGUIElement  (  irr::gui::EGUIET_ELEMENT, environment, environment->getRootGUIElement(), id, irr::core::rect< irr::s32 >(0,0,0,0))
    {
        Texture = 0;
        Color = irr::video::SColor(255,255,255,0);
        Size = 10;
    }

    virtual ~CCrossHair(void)
    {
        if (Texture)
            Texture->drop();
    }

    void draw(void)
    {
        irr::video::IVideoDriver* driver = Environment->getVideoDriver();

        irr::u32 screenW = driver->getScreenSize().Width;
        irr::u32 screenH = driver->getScreenSize().Height;
        irr::u32 cenW = screenW / 2;
        irr::u32 cenH = screenH / 2;

        if (!Texture)
        {
            //Draw crosshair
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW-1,cenH-Size,cenW+1,cenH-4) ); //above
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW+4,cenH-1,cenW+Size,cenH+1) ); //right
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW-1,cenH+4,cenW+1,cenH+Size) ); //down
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW-Size,cenH-1,cenW-4,cenH+1) ); //left
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW-1,cenH-1,cenW+1,cenH+1) ); //center of screen
        }
        else
        {
            irr::video::SColor Colors [] = {Color, Color, Color, Color};
            driver->draw2DImage(Texture, irr::core::rect< irr::s32 >(cenW-Size, cenH-Size, cenW+Size, cenH+Size), irr::core::rect< irr::s32 >(0, 0, Texture->getOriginalSize().Width, Texture->getOriginalSize().Height), 0, Colors, true);
        }

    }

    void setTexture(irr::video::ITexture* texture)
    {
        if (Texture)
            Texture->drop();

        Texture = texture;

        if (Texture)
            Texture->grab();
    }

    void setSize(irr::u32 size)
    {
        Size = size;
    }

    void setColor(irr::video::SColor color)
    {
        Color = color;
    }
protected:
    irr::video::ITexture* Texture;
    irr::u32 Size;
    irr::video::SColor Color;
};
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
kiranmaya
Posts: 31
Joined: Tue Jan 30, 2007 9:10 am

Post by kiranmaya »

nice code ,im goona use it..

thankq
Post Reply