Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  [Solved][Slick2D] Bit Background  (Read 864 times)
0 Members and 1 Guest are viewing this topic.
Offline Vladiedoo
« Posted 2012-09-30 21:46:21 »

Hi, for my main menu I'm trying to create an animated background similar to the Matrix green code but for binary. I currently draw a bunch of UnicodeFont Strings onto the screen but I recently found out that it's an expensive operation. Here is a screenshot.


Here is the code I use to draw it. Sorry for the code being so messy, essentially it randomizes every aspect of the binary numbers and draws it, setting the x position back to the start of the screen once it has flown off.
1  
2  
3  
4  
5  
6  
7  
8  
   // Number of lines to draw, currently 300 for stress testing.
   private int number_Of_Lines = 300;

   private StringBuffer background[] = new StringBuffer[number_Of_Lines];
   private int xArray[] = new int[number_Of_Lines];
   private int xSpeed[] = new int[number_Of_Lines];
   private int alpha[] = new int[number_Of_Lines];
   private float randomSize[] = new float[number_Of_Lines];

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void init() {
      Random dice = new Random();
      for (int i = 0; i < xArray.length; i++) {
         background[i] = new StringBuffer();
         if (i == 0) {
            xArray[i] = 800;
         } else {
            xArray[i] = dice.nextInt(900) + 800;
         }
         xSpeed[i] = dice.nextInt(20) + 1;
         alpha[i] = dice.nextInt(200) + 56;
         randomSize[i] = dice.nextFloat() + .3f;
         for (int j = 0; j < 120; j++) {
            if (dice.nextInt(2) == 0) {
               background[i].append('0');
            } else {
               background[i].append('1');
            }
         }
      }
   }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   public void render(GameContainer gc, StateBasedGame sb, Graphics g) {
      // Using regular g.drawString works much faster
      // If I set the font though, it also slows down
     
//      g.setFont(FontManager.smallDefaultFont);
      for (int i = 0; i < xArray.length; i++) {
         g.scale(randomSize[i], randomSize[i]);
         FontManager.smallDefaultFont.drawString(xArray[i], FontManager.smallDefaultFont.getHeight("1") * i, background[i].toString(), new Color(76, 256, 0,
               alpha[i]));
//         g.drawString( background[i].toString(),xArray[i], FontManager.smallDefaultFont.getHeight("1") * i);
         g.resetTransform();
      }
   }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void update(GameContainer gc, StateBasedGame sb, int delta) {
      for (int i = 0; i < xArray.length; i++) {
         if (i == 0) {
            xArray[i] -= 5;
         } else {
            xArray[i] -= xSpeed[i];
         }
         if (xArray[i] < -xArray.length * 9) {
            xArray[i] = 800 + xArray.length * 9;
         }
      }
   }


I'm wondering why using UnicodeFont is so expensive. Should I just use images for the background or should I create a good algorithm?
Offline davedes
« Reply #1 - Posted 2012-09-30 23:02:02 »

The main reason it's slow is because each line/string is drawn using separate glBegin/glEnd calls to OpenGL. Basically this means each string of text is being pushed to the GPU individually. In order to improve performance, you would send all of the lines of text in a single batch. In Slick, the general way to resolve this is by using startUse/drawEmbedded/endUse -- but this is only available for images (and by extension AngelCodeFont, which has the glyph class exposed).

Also note that there is a strange bug/limitation/quirk on certain Macs which can greatly increase unicode font loading time.

There is no need to use Strings or UnicodeFonts to achieve this effect, though. The best way to do this is to use a sprite sheet image containing the various glyphs (in this case "0" and "1"), and render each glyph using drawEmbedded.

1  
2  
3  
4  
5  
6  
7  
glyphSheet.startUse();

... for each glyph in your entire background ...
    Image glyph = getRandomGlyph();
    glyph.drawEmbedded(...);

glyphSheet.endUse();

Offline Vladiedoo
« Reply #2 - Posted 2012-09-30 23:15:01 »

I'm still learning the basics of OpenGL but I would of thought glBegin/glEnd to be fast since it's a "low level" operation.

.startUse() on a SpriteSheet is also new to me Shocked, I'm assuming it's going to speed up all my SpriteSheet drawings (I have a good amount). Edit -> Yeah it's going to speed it up by a lot.

Thank you so much, this really helps.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-10-01 07:16:31 »

For more details, code samples, etc:
http://slick.cokeandcode.com/wiki/doku.php?id=performance_memory_tips

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.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (35 views)
2014-12-09 22:41:13

BurntPizza (70 views)
2014-12-08 04:46:31

JscottyBieshaar (32 views)
2014-12-05 12:39:02

SHC (44 views)
2014-12-03 16:27:13

CopyableCougar4 (40 views)
2014-11-29 21:32:03

toopeicgaming1999 (108 views)
2014-11-26 15:22:04

toopeicgaming1999 (94 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!