irrConfigController - controls to be configured by players

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

sio2 wrote: This has contiguous indices so of course it can be reduced to a jump table. But key codes are not necessarily contiguous indices. [I don't know if the compiler can boil the list down to sets of contiguous indices though].
Halifax wrote:On another note about optimization, most compilers should optimized non-sequential switch-cases by adding padding to the jump table if warranted.
I know TIGCC had optimizations to do this, I'm almost sure that GCC has them.

EDIT:

I got a chance to test it, and I was correct. It either pads the jump table or predicts every value prior with a byte table (for indicies [0,255]) or a word table (for indicies [0,65536]), etc.
Example #1:

Code: Select all

int func2(int a)
{
	switch( a )
	{
	case 8: return 1;
	case 3: return 2;
	case 5: return 3;
	case 1: return 4;
	case 9: return 5;
	}
	return 0;
}

?func2@@YAHH@Z PROC
	dec	eax
	cmp	eax, 8
	ja	SHORT $LN6@func2
	jmp	DWORD PTR $LN10@func2[eax*4]
$LN5@func2:
	mov	eax, 1
	ret	0
$LN4@func2:
	mov	eax, 2
	ret	0
$LN3@func2:
	mov	eax, 3
	ret	0
$LN2@func2:
	mov	eax, 4
	ret	0
$LN1@func2:
	mov	eax, 5
	ret	0
$LN6@func2:
	xor	eax, eax
	ret	0
	npad	2
$LN10@func2:
	DD	$LN2@func2
	DD	$LN6@func2
	DD	$LN4@func2
	DD	$LN6@func2
	DD	$LN3@func2
	DD	$LN6@func2
	DD	$LN6@func2
	DD	$LN5@func2
	DD	$LN1@func2
?func2@@YAHH@Z ENDP					; func2
Example #2:

Code: Select all

int func2(int a)
{
	switch( a )
	{
	case 8: return 1;
	case 3: return 2;
	case 5: return 3;
	case 11: return 4;
	case 19: return 5;
	}
	return 0;
}

?func2@@YAHH@Z PROC
	add	eax, -3
	cmp	eax, 16
	ja	SHORT $LN6@func2
	movzx	eax, BYTE PTR $LN10@func2[eax]
	jmp	DWORD PTR $LN11@func2[eax*4]
$LN5@func2:
	mov	eax, 1
	ret	0
$LN4@func2:
	mov	eax, 2
	ret	0
$LN3@func2:
	mov	eax, 3
	ret	0
$LN2@func2:
	mov	eax, 4
	ret	0
$LN1@func2:
	mov	eax, 5
	ret	0
$LN6@func2:
	xor	eax, eax
	ret	0
	npad	1
$LN11@func2:
	DD	$LN4@func2
	DD	$LN3@func2
	DD	$LN5@func2
	DD	$LN2@func2
	DD	$LN1@func2
	DD	$LN6@func2
$LN10@func2:
	DB	0
	DB	5
	DB	1
	DB	5
	DB	5
	DB	2
	DB	5
	DB	5
	DB	3
	DB	5
	DB	5
	DB	5
	DB	5
	DB	5
	DB	5
	DB	5
	DB	4
?func2@@YAHH@Z ENDP
Notice the cost function used to evaulate when it's beneficially to pad with 32-bit pointers, and when to change to 8-bit indicies into the the jump table. So as I said before, switch was made for a reason, let the compiler decide what to do these days. As long as you're using MSVC or GCC they are going to optimize it 99.9% of the time.

Secondly, I'm almost sure the indicies (for the function he's implementing) aren't even that sparsely scattered. They're almost perfectly sequential.

At any rate, back on the topic of irrConfigController!
TheQuestion = 2B || !2B
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

OK, back to topic:

I did some changes yesterday:

- improved joystick axis detection
- fix a bug that kept the items marked as "in conflict" (red) although the conflict was fixed

Updated the initial post and uploaded the new version to my homepage.
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
teto
Posts: 159
Joined: Thu Dec 03, 2009 9:37 pm
Location: /home
Contact:

Post by teto »

thx for the code and the binary in yoru archive.I tested it and it worked great even with my dual joystick.

I will try to implement it in my game
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

Nice to head someone's using it ;). I have written it for my current project, and it works fine there as well. I think I should check whether the online version is still up to date and maybe do an update.
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
wowdevver24
Posts: 26
Joined: Mon Jan 04, 2010 8:02 pm

Post by wowdevver24 »

if the numbers are perfectly sequential why not

Code: Select all

int offset = 32;
int func2(int a)
{
   if( (a != "33") && (a != "57") && (a != 0) )
      return 0;
   return a+offset;
} 
OR

Code: Select all

int offset = 32;
int func2(int a)
{
   switch(a)
   {
      case 33:
         return 0;
      case 57:
         return 0;
      case 0:
         return 0;
   }
   return a+offset;
} 
either way everyone is nitpicking but this should not be at the OP to change code as we can for the code to our own uses and just give credit
Remember all information is contextual and if I do not understand the context I cannot gras the information
kburkhart84
Posts: 277
Joined: Thu Dec 15, 2005 6:11 pm

Post by kburkhart84 »

I'm trying to implement this awesome code snippet. I'm using the latest Irrlicht 1.7 from svn, and that may have something to do with my problems. I'm only using the IrrCC, without the little state machines that come with the download, as I'd rather use my own systems for managing state. The code compiles almost OK, with at this point only one error. I'm using VC++ 2005/8.0 Standard, and the error I get is this.

Code: Select all

>c:\programming stuff\test\irrcc.cpp(171) : error C2440: 'initializing' : cannot convert from 'irr::core::dimension2d<T>' to 'irr::core::dimension2d<T>'
1>        with
1>        [
1>            T=irr::u32
1>        ]
1>        and
1>        [
1>            T=irr::s32
1>        ]
1>        Constructor for class 'irr::core::dimension2d<T>' is declared 'explicit'
1>        with
1>        [
1>            T=irr::s32
1>        ]
It is indicating the line causing the error is this one, in irrCC.cpp.

Code: Select all

  dimension2di dim=pFont->getDimension(m_sName.c_str());
It is in this function, posted in its entirety.

Code: Select all

IGUITab *CIrrCC::CIrrCCItem::getGuiElement(IGUIElement *pParent, position2di cPos, position2di cEditPos) {
  IGUIFont *pFont=m_pGuiEnv->getSkin()->getFont();
  dimension2di dim=pFont->getDimension(m_sName.c_str());

  position2di ulc(cPos.X,cPos.Y),lrc(cPos.Y+dim.Width+cEditPos.X+250,cPos.Y+dim.Height+10);

  m_pGuiElement=m_pGuiEnv->addTab(rect<s32>(ulc.X,ulc.Y,lrc.X,lrc.Y),pParent,-1);
  m_pGuiEnv->addStaticText(m_sName.c_str(),rect<s32>(0,0,dim.Width+10,dim.Height+10),false,true,m_pGuiElement);
  m_pCtrlText=m_pGuiEnv->addStaticText(L"Not set",rect<s32>(cEditPos.X,cEditPos.Y,cEditPos.X+200,cEditPos.Y+dim.Height+2),true,true,m_pGuiElement,65536+m_iIdx);
  this->updateCtrlText();
  return m_pGuiElement;
}
I know it has something to do with the use of templated dimension2d, but I don't know enough about templates and how dimension2d is using them to see what the mistake it. It appears to be working for other people, so it may have to do with my using the newer version of Irrlicht.

Any ideas?? Thanks in advance.
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

I can't say for sure, because I still use 1.6, but maybe another dimension2d<s32> has been turned to dimension2d<u32>? In that case you'd have to update that part of the irrCC code.
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
kburkhart84
Posts: 277
Joined: Thu Dec 15, 2005 6:11 pm

Post by kburkhart84 »

Brainsaw wrote:I can't say for sure, because I still use 1.6, but maybe another dimension2d<s32> has been turned to dimension2d<u32>? In that case you'd have to update that part of the irrCC code.
I don't know if it works due to not having tested yet. The IGUIFont function return that is creating the error has the dimension2d as the "u32", like you mentioned maybe changed, and so I made the line dimension2di into dimension2du, and it now compiles no errors, though until I actually use the code, I can't confirm it works. I will post as soon as I test it to let everybody know.
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

Feedback is always welcome ;). I use IrrCC (slightly modified) in my current project (Marbles2) with Irrlicht 1.6, and it works just fine (imho).
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
kburkhart84
Posts: 277
Joined: Thu Dec 15, 2005 6:11 pm

Post by kburkhart84 »

I finally got a little time. With my change mentioned above, my code compiles. I've added 3 different controls, and so far, they are working. I still haven't tested the GUI part because I don't have anything but a quick test app, but I'm pretty sure it should work, considering the change I made. I will continue to post, giving feedback. If it works right, I will probably use it for all of my future games assuming I continue with Irrlicht. I was going to have to code this anyway, so I appreciate the work.
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

I am planning to use this snippet for all future Irrlicht projects as well. You could (as sort of demo) download my "marbles2 - the race" game that uses it for 2 players (splitscreen). The only thing I might change in the future is the GUI. It just uses standard Irrlicht GUI, and there are some GUI systems floating around in this forum that give better options and look better. But that change is definetely far away, still a lot of other things to complete.
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

I update this code snippet today. Updated the initial as well, or download the package from http://www.bulletbyte.de/products.php?s ... show=irrcc.

Changes:

- it is now possible to define an axis of two control items. If you query on of the items a value between -1 and 1 will be returned. This way it's easier to create such controls, e.g. for an airplane. This does also affect setting the controls. If you have defined an axis and set one direction to e.g. Joystick 1 axis 1 up the second direction will automatically be set to Joystick 1 axis 1 down.
- there is a new item called "Fader". If you configure it with a joystick axis the value will be taken directly from the joystick, but if you attach a key to this item the value will be slowly (can be configured) increased or decreased. It is useful e.g. for thrust control of an airplane.

These new features have already been added to the IrrOdeCar demo of my IrrOde wrapper. The demo that comes with the download package of this package is not the best place to see the news.
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
d3jake
Posts: 198
Joined: Sat Mar 22, 2008 7:49 pm
Location: United States of America

Post by d3jake »

I can't quite figure it out from the code posted in this thread, but how is the mouse handled? Can you only retrieve the positional change between update calls? I am trying to be able to use mouse input as a non-joystick-like input, meaning that if the mouse is moved slightly to the right, the player rotates slightly to the right, etc. I've been trying to figure out how this can be done as the mouse only returns an x\y position, and if allowed to move it would eventually leave the window.
The Open Descent Foundation is always looking for programmers! http://www.odf-online.org
"I'll find out if what I deleted was vital here shortly..." -d3jake
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

I don't know if this can be done in another way (I know there was one under DOS ;) ), but I set the mouse cursor back to a defined position after a movement was detected. Don't know how good this works, I don't use the mouse in my projects.
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
netpipe
Posts: 669
Joined: Fri Jun 06, 2008 12:50 pm
Location: Edmonton, Alberta, Canada
Contact:

Re: irrConfigController - controls to be configured by players

Post by netpipe »

https://github.com/netpipe/IrrlichtDemo ... Controller it compiles detects joystick but does not seem to move when joystick is used.
Live long and phosphor!
-- https://github.com/netpipe/Luna Game Engine Status 95%
Post Reply