Archived

This topic is now archived and is closed to further replies.

Woody FX

Z-Order, a better way ? (J2ME)

Recommended Posts

I have been working on the way my graphics code displays objects and the order it displays them so it gives the perception of dept and handles objects moving around on the z-axis. I have coded this the only way i know how so i'd welcome any critique or suggestions.


buffer.drawImage(background, 0, 0, buffer.TOP | buffer.LEFT);	
		
buffer.drawImage(paddel2, Ax, Ay, buffer.HCENTER | buffer.VCENTER);	


if (rocketFired)
{
if (ballposZ >= rocketZ)
{
if (ballposZ >= 90)  // 90 being the Z position the target always holds

{
buffer.setClip(ScreenX-8,ScreenY-8,16,16) ;
buffer.drawImage(ball,ScreenX-8-(15*ballFrameNo),ScreenY-8, buffer.TOP | buffer.LEFT);                  
buffer.setClip(0,0,101,81);
					
if ( rocketZ >= 90)
{
buffer.setClip(RocketScreenX-6,RocketScreenY-6,12,12);
buffer.drawImage(fireball,RocketScreenX-6-fireballFrame ,RocketScreenY-6, buffer.TOP | buffer.LEFT);
buffer.setClip(0,0,101,81);
				
						buffer.drawImage(target,targetScreenPosX,targetScreenPosY, buffer.HCENTER | buffer.VCENTER);
						}

					else 
						{
						buffer.drawImage(target,targetScreenPosX,targetScreenPosY, buffer.HCENTER | buffer.VCENTER);
						
						buffer.setClip(RocketScreenX-6,RocketScreenY-6,12,12);
						buffer.drawImage(fireball,RocketScreenX-6-fireballFrame ,RocketScreenY-6, buffer.TOP | buffer.LEFT);
						buffer.setClip(0,0,101,81);
						}
					}
				
				
				else
					{
					buffer.drawImage(target,targetScreenPosX,targetScreenPosY, buffer.HCENTER | buffer.VCENTER);
			
					buffer.setClip(ScreenX-8,ScreenY-8,16,16);
					buffer.drawImage(ball,ScreenX-8-(15*ballFrameNo),ScreenY-8, buffer.TOP | buffer.LEFT);                  
					buffer.setClip(0,0,101,81);

					buffer.setClip(RocketScreenX-6,RocketScreenY-6,12,12);
					buffer.drawImage(fireball,RocketScreenX-6-fireballFrame ,RocketScreenY-6, buffer.TOP | buffer.LEFT);
					buffer.setClip(0,0,101,81);
					}
				}

			
			else
				{
				if (rocketZ >= 90)
					{
					buffer.setClip(RocketScreenX-6,RocketScreenY-6,12,12);
					buffer.drawImage(fireball,RocketScreenX-6-fireballFrame ,RocketScreenY-6, buffer.TOP | buffer.LEFT);
					buffer.setClip(0,0,101,81);

					if ( ballposZ >= 90)
						{
						buffer.setClip(ScreenX-8,ScreenY-8,16,16);
						buffer.drawImage(ball,ScreenX-8-(15*ballFrameNo),ScreenY-8, buffer.TOP | buffer.LEFT);                  
						buffer.setClip(0,0,101,81);

						buffer.drawImage(target,targetScreenPosX,targetScreenPosY, buffer.HCENTER | buffer.VCENTER);
						}

					else
						{
						buffer.drawImage(target,targetScreenPosX,targetScreenPosY, buffer.HCENTER | buffer.VCENTER);

						buffer.setClip(RocketScreenX-6,RocketScreenY-6,12,12);
						buffer.drawImage(fireball,RocketScreenX-6-fireballFrame ,RocketScreenY-6, buffer.TOP | buffer.LEFT);
						buffer.setClip(0,0,101,81);
						}

					}
				else
					{
					buffer.drawImage(target,targetScreenPosX,targetScreenPosY, buffer.HCENTER | buffer.VCENTER);

					buffer.setClip(RocketScreenX-6,RocketScreenY-6,12,12);
					buffer.drawImage(fireball,RocketScreenX-6-fireballFrame ,RocketScreenY-6, buffer.TOP | buffer.LEFT);
					buffer.setClip(0,0,101,81);

					buffer.setClip(ScreenX-8,ScreenY-8,16,16);
					buffer.drawImage(ball,ScreenX-8-(15*ballFrameNo),ScreenY-8, buffer.TOP | buffer.LEFT);                  
					buffer.setClip(0,0,101,81);
					}
				}
			}

Sorry about the formating being inconsistance ! Also my code for displaying the score has hit the speed drastically what do you to do this... i could just print an int to the screen if there was a way to do this! String cal = Long.toString(score); buffer.drawString("Score: " + cal, 80, 80, Graphics.BOTTOM | Graphics.RIGHT); [edited by - Woody FX on October 5, 2003 5:59:15 PM][/source] [edited by - Woody FX on October 5, 2003 6:02:55 PM]

Share this post


Link to post
Share on other sites
Unfortunately, J2ME has no in-built sorting functions (unless I''ve missed them in the docs), so you''re going to have to implement something yourself.
Your code as it stands is subject to maintenance hell - if you change a graphic, you''ve got to find and change all the instances.
What I''ve created for my projects is a set of routines that abstract the source graphic data into a unique identifier. What I have is:

DrawGraphic (Graphics dc, int graphic_id, int group, int x, int y);

where graphic_id is the unique identifier (the group parameter allows fine control over memory usage as I can bunch related graphics together and load / free them as required).
What I''m trying to get at is: make the graphics data driven. At startup, load the images, the position and clipping information and then write routines to use this data.
Your drawing routine then becomes:

Sort objects into Z order - use a Vector to store object IDs with an insertion sort
Loop through the Vector and draw the graphic for each object

One other thing, this:

String cal = Long.toString(score);
buffer.drawString("Score: " + cal, 80, 80, Graphics.BOTTOM | Graphics.RIGHT);

can be done as:

buffer.drawString("Score: " + score, 80, 80, Graphics.BOTTOM | Graphics.RIGHT);

but the drawString function shouldn''t hurt the rendering performance.

I do seem to have rambled a bit, but I hope it helps.

Skizz

Share this post


Link to post
Share on other sites
Thanks Skizz..

that gives me food for thought!

Yeah the way it''s done is not great for maintainance but i''m not to worry about that... aslong as its a fast way of doing this!

Are loops not really slow i can think of a really tight way to do all of this using a for loop.

Would save on code bloat.. which is not something that is really a concern i suppose with obfuscators.

Thanks
Brian

Share this post


Link to post
Share on other sites
Code bloat is a problem for current mobile phones (and will be for some time) and unfortunately, obfuscators won''t help a great deal. Obfuscators, as the name suggests, just mangles the symbols to, usually, two characters for members and a single character for classes. They can also remove unused code, variables and classes. They don''t, generally, perform optimisations, although some do mangle the byte code to make understanding the decompiled code harder (you can''t stop decompilation).

Skizz

Share this post


Link to post
Share on other sites