Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  My StarGenerator Program, and Optomizations...  (Read 1710 times)
0 Members and 1 Guest are viewing this topic.
Offline DarkMortar

Junior Member




Java Padawan


« Posted 2006-09-28 04:30:44 »

Hi, I made this little star program in two days, well it woulda been one but I ran out of time.

What this program does, is that it generates how ever many stars u want from the int final STARS, and it then makes that many tiny ovals with various variables in the arrays using many for loops. This program seems to work fine, but I'm looking at optomizing it, and I'm a little confused about my commented out code in main() which controls the renderer, as obvious i did soemthing wrong and all of them seem to make no effect, and they do nothing. Also I want to make this program get higher fps, as it runs at 21 fps for me(2.6Ghz HT, 1.5GB,radeon9800(not sure if this prog even uses the video card...)) with 3200 stars and 40 fps with 1600 stars and 65fps with 800 stars, but the weird thing is that anything below 800 is still 65fps, when the cap is 100fps.

I ran this prog on an old pentium 3 machine (600Mhz, 128MB, onboard gfx) and the loading of the stars was not a few seconds like on my machine, it was MINUTES it was so slow at making the stars, well the comp at school uses java 1.3 i beleive so maybe that is part of the cause, but  geeze it is WAY slower at rendering too like 14fps with 800 stars. So i want to optomize the prog, I think it scans too much and it causes major slow down.

My prog:

EDIT: updated code a bit, removed an extra unnecssary for-loop, seemed to increase .5fps lol.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
/********************
 * StarField Program
 * Star Generator
 * v1.0
 *******************/


import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class StarField extends Canvas implements Runnable {
   
   Random r = new Random();
   private int rand,rand2;

   Thread a;

   private Image image;
   private Graphics gfx;

   private final int DELAY = 10; // loop delay, target 100fps
 
   private boolean firstRun=true;
   
   private final int STARS = 3200;
   
   private int[] starXpos,starYpos,starSize,starColor,starTwinklePos,starSpeed;
   
   private boolean failPositionTest=false;
   
   private long startTime=0;
   private int Global_I,yMove,fps=0,frameCount=0;
   
   /*StarField Constructor*/
   public StarField() {
      // create a window to display StarField
     JFrame container = new JFrame("StarField Generator v1.0");

      // Set dimentions of the newly created window
     JPanel panel = (JPanel) container.getContentPane();
      panel.setPreferredSize(new Dimension(800, 600));
      panel.setLayout(null);

      // canvas size within the new frame
     setBounds(0, 0, 800, 600);
      panel.add(this);

      // Tells AWT not to bother repainting the canvas since
     // accelerated mode will do it
     setIgnoreRepaint(true);

      // make the window visible
     container.pack();
      container.setResizable(false);
      container.setVisible(true);

      // listens if user closes the window, it will then exit the game
     container.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }

   public void paint(Graphics g) {
     
      //calculate fps
     frameCount++;
      if (System.currentTimeMillis() > startTime + 1000) {
         startTime = System.currentTimeMillis();
         fps = frameCount;
         frameCount=0;
      }
     
      g.setColor(Color.cyan);
       
      if (firstRun==true) { g.drawString("L o a d i n g . . .", 10, 20); }
     
      g.drawString("Fps: " + fps, 10, 590); //display calculated fps
     
      for (int i = 0; i < STARS; i++)
      {        
         //scan for color reference
        if (starColor[i]==0) { g.setColor(Color.yellow); }
         if (starColor[i]==1) { g.setColor(Color.orange); }
         if (starColor[i]==2) { g.setColor(Color.red);    }
         if (starColor[i]==3) { g.setColor(Color.cyan);   }
         if (starColor[i]>=4) {
               //star twinkle effector
              if (starTwinklePos[i]>500)   { g.setColor(Color.darkGray); }
               if (starTwinklePos[i]>=500)  { g.setColor(Color.lightGray);}
               if (starTwinklePos[i]>=5000) {  g.setColor(Color.white);   }
               if (starTwinklePos[i]>=10000){ starTwinklePos[i]=0; g.setColor(Color.darkGray); }
            }
         
         //position maintainer
        if (starXpos[i] >= 800) { starXpos[i]=0;   }
         if (starYpos[i] >= 600) { starYpos[i]=0;   }
         if (starYpos[i] <= 0)   { starYpos[i]=600; }
         
         //minor vertical effector
        rand2 = r.nextInt(101);
         if (rand2==0)
         {
            rand = r.nextInt(5);
            if (rand==0) { yMove=1; }
            if (rand==1) { yMove=-1;}
            if (rand>=2) { yMove=0;}
         }
         else { yMove=0; }
         
         //draw the planet of i from the array
        g.fillOval(starXpos[i]+=starSpeed[i], starYpos[i]+=yMove, starSize[i], starSize[i]);
         
         starTwinklePos[i]++;
      }
   }

   public void run() {
     
      setBackground( Color.black );
     
      if (firstRun==true) { generateStarField(); }
     
      while (true)
      {                  
         fpsController();
         repaint();
      }
   }

   public void fpsController() {
      // Thread sleep timer for 10milis, should be around 100fps cap
     try { a.sleep(DELAY); } catch (Exception e) {}
   }
   
   public void generateStarField() {
     
      //System.out.println("generateStarField Initilized.");
     
      //create the star arrays
     starXpos  = new int[STARS];
      starYpos  = new int[STARS];
      starSize  = new int[STARS];
      starColor = new int[STARS];
      starSpeed = new int[STARS];
      starTwinklePos = new int [STARS];
     
      for (int i = 0; i < STARS; i++)
      {
         //generate star x position
        rand = r.nextInt(799)+1;
         starXpos[i] = rand;            //System.out.println("Star" + i + " X POS: " + rand);
        //generate star y position
        rand = r.nextInt(599)+1;
         starYpos[i] = rand;            //System.out.println("Star" + i + " Y POS: " + rand);
       
         //check to insure no star overlaps
        Global_I=i;
         positionCheck(starXpos[i], starYpos[i]);
         if (positionCheck(starXpos[i], starYpos[i])==false) { i--; System.out.println("Star" + i + " Failed Position Test.");}
         
         //generate star size
        rand = r.nextInt(5); // create high percentage of distant stars
        if (rand!=0) { rand = 1; }
         if (rand==0) { rand = r.nextInt(4)+1; }
         starSize[i] = rand;            //System.out.println("Star" + i + " SIZE: " + rand);
        //generate star color reference
        rand = r.nextInt(21);
         starColor[i] = rand;         //System.out.println("Star" + i + " COLOR REF: " + rand);
       
         rand = r.nextInt(3)+1;
         starSpeed[i] = rand;         //System.out.println("Star" + i + " SPEED: " + rand);
       
         rand = r.nextInt(10001);
         starTwinklePos[i] = rand;      //System.out.println("Star" + i + " TWINKLE PHASE: " + rand);
       
         repaint(); // load effects :)
     }
     
      firstRun=false;
   }
   
   public boolean positionCheck(int x, int y)
   {
      for (int k = STARS-1; k > Global_I; k--)
      {
         if (x==starXpos[k] && y==starYpos[k]) { failPositionTest=true; break; }
      }
      if (failPositionTest==true) { failPositionTest=false; return false; }
       else return true;
   }

   /***************************************************************************
    * Creates double buffering double buffering smoothens the animation
    **************************************************************************/

   public void update(Graphics g) {
      // initialize buffer
     if (image == null) {
         image = createImage(this.getSize().width, this.getSize().height);
         gfx = image.getGraphics();
      }

      // clear screen in background
     gfx.setColor(getBackground());
      gfx.fillRect(0, 0, this.getSize().width, this.getSize().height);

      // draw elements in background
     gfx.setColor(getForeground());
      paint(gfx);

      // draw image on the screen
     g.drawImage(image, 0, 0, this);
   }

   public static void main(String argv[]) {

      StarField prog = new StarField();

      // Begin the StarField program
     //using the run() method.
     
      //System.setProperty("sun.java2d.opengl","True");
     //System.setProperty("sun.java2d.noddraw","");
     //System.setProperty("sun.java2d.accthreshold", "0");
     //System.setProperty("sun.java2d.ddforcevram", "true");
     
      prog.run();
   }
}

Offline chrm

Junior Newbie





« Reply #1 - Posted 2006-09-28 13:06:16 »

hi,
there are a lot of
1  
integerArray = integer;


which should be
1  
integerArray[i] = integer;


for example:
1  
2  
3  
4  
5  
6  
7  
      for (int i = 0; i < STARS; i++)
      {        
         //scan for color reference
        if (starColor==0) { g.setColor(Color.yellow); }
         if (starColor==1) { g.setColor(Color.orange); }
         if (starColor==2) { g.setColor(Color.red);    }
         if (starColor==3) { g.setColor(Color.cyan);   }


which editor do you use? maybe you should try eclipse.org

Offline Mr_Light

Senior Member




shiny.


« Reply #2 - Posted 2006-09-28 18:10:09 »

this topic seems out of place, -> shared code?

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline chrm

Junior Newbie





« Reply #3 - Posted 2006-09-28 21:23:52 »

on second thought, if you have just four different speeds at which the stars move, you could draw all stars with the same speed to an image, and then just move/draw the four images. aka http://en.wikipedia.org/wiki/Parallax_scrolling

this would save a hell of a lot callculations ;-)

Offline DarkMortar

Junior Member




Java Padawan


« Reply #4 - Posted 2006-09-29 02:47:11 »

hi,
there are a lot of
1  
integerArray = integer;


which should be
1  
integerArray[i] = integer;


for example:
1  
2  
3  
4  
5  
6  
7  
      for (int i = 0; i < STARS; i++)
      {        
         //scan for color reference
        if (starColor==0) { g.setColor(Color.yellow); }
         if (starColor==1) { g.setColor(Color.orange); }
         if (starColor==2) { g.setColor(Color.red);    }
         if (starColor==3) { g.setColor(Color.cyan);   }


which editor do you use? maybe you should try eclipse.org

Actually it is like that, but its odd, for some reason when I pasted the code, it did not put the brackets [.i], i beleive its my mistake that i did not wrap this in [.code][./code] and then it may like have accidently made them ittalic! opps, here it is again... edits post... check it again...

I use eclipse 3.2 btw, and i dont want to draw images, because the white stars twinkle and the user (in the code) can set how many stars to render, and the y axis of each star moves slightly at times.

also i need help on the "nodraw" and other stuff in main.

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #5 - Posted 2006-09-30 03:49:58 »

Take a look at http://www.java-gaming.org/forums/index.php?topic=14988.0.

I think it will give you some ideas.

Offline DarkMortar

Junior Member




Java Padawan


« Reply #6 - Posted 2006-09-30 09:08:38 »

Ah, you did it in a way that is much more advanced in style. Hmm, your prog generates the stars quickly and the fps is high. Good job!

EDIT: actually i found out that my prog can load up and generate the stars on instant, i removed the debugging print statements, and the program generates the stars immedtiatly now.

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.

pw (12 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!