Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Best system for drawing ints to screen?  (Read 1456 times)
0 Members and 1 Guest are viewing this topic.
Offline bh94704

Senior Newbie




AxAy(x=y)


« Posted 2004-01-30 19:30:38 »

I have a lot of data stored as ints that needs to be drawn once per second or so. (As in "you have 8 widgets available", where the 8 can reasonably be expected to change every couple of seconds - and then 20 kinds of widgets) Graphics objects can draw byte arrays, char arrays, and Strings. What is the most efficient way to convert? I guess I should say that I don't expect the numbers to ever be larger than 32,000 (although they will be larger than 255).


Thanks for any help!
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2004-01-30 21:10:20 »

Totally implementation dependant, sorry.  The answer can change depending on the VM.

An intelligent VM maker will likely try ot optimize the expected most-used path. My guess would be that woudl be strings.

BUT I have to wonder if you aren't worrying about the wrong things.  Have you profiled your app yet? Is this *really* a bottleneck??

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #2 - Posted 2004-01-30 21:28:36 »

I just put it into Strings... then I call my drawText method with it. It uses toCharArray() in order to create a char array were I can loop through... then I just use that char number to determine wich Image I should draw and that's it.

I can't imagine how that could be a bottleneck. This way I can draw more than 20000 Strings per second on a half decent PC.

There isn't much room for optimisations. A method wich is twice as fast would mean I would get 0.00001 fps more (dramatisation Smiley)... but even if it's 0.1fps - who would notice that?

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bh94704

Senior Newbie




AxAy(x=y)


« Reply #3 - Posted 2004-01-30 23:36:43 »

Ah I think I understand. I think I phrased my question poorly - I'm not worried about the drawing, I'm worried about the allocation of hundreds of strings per second (is that something to worry about?). But if I understand oNyx correctly, I should just have a static final array of Chars, and draw those.

Please reply if I missed something. Thanks for the help!
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #4 - Posted 2004-01-31 00:16:05 »

>I should just have a static final array of Chars, and draw those.

Hm... well I draw (managed) Images...



I load it, place each char into a seperate Image (52 or so) and just draw the letters I need.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public void drawText(Graphics2D g, String text, Color col, int scale, int xOff, int yOff)
{
      char[] t=text.toCharArray();

      g.setColor(col);
      g.fillRect(xOff,yOff,6*scale*t.length,5*scale);

      int c=43;
      for(int i=0;i<t.length;i++)
      {
            switch(t[i])
            {
                  case 'a':  c= 0;break;
                  case 'b':  c= 1;break;
                  case 'c':  c= 2;break;
                  case 'd':  c= 3;break;
                  case 'e':  c= 4;break;
                  case 'f':  c= 5;break;
                  case 'g':  c= 6;break;
                  [...etc...]
            }
            if(c!=-1)
                  g.drawImage(chars[c],xOff+i*6*scale,yOff,6*scale,5*scale,null);
      }


You get the idea? Obviously it's easier if the chars match their natural order (e.g. no switch at all and just g.drawImage(chars[c&0xff]....)).

>I'm worried about the allocation of hundreds of strings per second

Well that wasn't a problem for me or even noticable (on a 4+ year old PC). However it's "relativly" slow if you use drawString... it also produces a fair amout of garbage. Switch to images and it's about as fast as it can get... oh and it's cuter Smiley

If you are still worried about Strings... well just write a simiar method wich takes an int as argument, tear it apart to get the digits and... well you see? It's about as expensive as using Strings - after all an Array is also just an object Wink

Well just profile your code and find the real bottlenecks and spend time on speeding them up.

弾幕 ☆ @mahonnaiseblog
Offline bh94704

Senior Newbie




AxAy(x=y)


« Reply #5 - Posted 2004-01-31 18:58:20 »

Images...yes that does make the most sense in terms of cost/benefit. Thank you very much for your help. As a followup, is there a good profiler that plugs into NetBeans? Or do I have to write my own? Thanks,

Bret
Offline Jeff

JGO Coder




Got any cats?


« Reply #6 - Posted 2004-01-31 22:05:07 »

I dotn know if there is an integrated profiler.

There are some wonderful commecial profiling tools that you can use with netbeans  on the same VM.  (eg OptimizeIt, True Time, etc)

There is also a basic post-mortem profiler built into the VM itself (see -hprof).


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline ChrisRijk

Senior Newbie




Optimise or Die


« Reply #7 - Posted 2004-02-04 16:12:58 »

Another tool to help with optimisation is Jfluid:
http://research.sun.com/projects/jfluid/

The most recent version has stuff to track object liveliness and can be used to find memory leaks. I used it for just such a thing last week!


If you'd like to see this become a standard feature of Java 1.5 then  vote for this RFE:
http://developer.java.sun.com/developer/bugParade/bugs/4879835.html
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!