Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Performance Issues: CPU on: 2008-05-11 01:21:02
You should really read a book about the basics of programming. You'll find yourself severly constrained in making your game when you do not yet master these basic concepts of object oriented programming.

Any suggestions?  I'm more adept at Java than I let on perhaps by my questions.  Any books on basic program layout and programming basics would be greatly appreciated.

2  Game Development / Newbie & Debugging Questions / Re: Performance Issues: CPU on: 2008-05-10 15:32:08
Quote
You should have a class for the unit type and give the class a drawTo method.  Different unit types will implement this method differently, though you can probably just make all the unit types the draw an image be different instances of the same class.

How do I draw images outside of gameRender()?

Do I just pass my Graphics2D variable to the method and use it to draw in the method? 

I'm not sure how that works if that isn't how you do it.


And yes I am using active rendering I'm sure if it, I'm using the same animation loop for FSEM active rendering as is in Killer Game Programming in Java.

I had the dbImage buffering commented out, I'm not sure why all of a sudden it is in now.

The irony of this whole thing is that this is the second time writing this game and this time it was supposed to be more readable and CPU efficient.  So much for that.

EDIT: I've been surfing around some of the other posts on this forum and it seems that swing and active rendering are a problem.  All of this code is in a JFrame is that a problem?  What else do I use if not JFrame?
3  Game Development / Newbie & Debugging Questions / Re: Performance Issues: CPU on: 2008-05-10 06:41:32
Profiler screenshot:


EDIT: If that is unreadable, its telling me my CPU is spending most of its time in gameRender() by a long shot.
4  Game Development / Newbie & Debugging Questions / Re: Performance Issues: CPU on: 2008-05-10 06:25:43
I see, so for good programming conventions in the future I should just use properly named constants for those?


Profiler stats incoming soon.
5  Game Development / Newbie & Debugging Questions / Re: Performance Issues: CPU on: 2008-05-10 06:07:24
a) There are magic numbers.
b) == isn't for string comparison (use equals() instead).
c) Use a profiler to identify the bottlenecks.

a) What?
b) It seems to work, is there a reason to use equals() instead?
c) How do I use a profiler?

EDIT: Okay, wiki'd magic numbers and I'm assuming you're refering to my poorly named, non-documented variables.  This was is mentioned in my post and it makes my code horribly unreadable, I know, I'm trying to fix that habit.
6  Game Development / Newbie & Debugging Questions / Performance Issues: CPU on: 2008-05-10 05:28:44
I'm having a ton of issues with performance and it seems to be a CPU issue.  I think I might be trying to do far too much with my game.

Its a top down RTS style game and you have 20 units.  The problem is that I'm addicted to loops and arraylists.  I dont know any other way to do some of the things I need to do.  I am using active rendering and FSEM.  My FPS is supposed to be 60.  I'm going to post most of the relevant code, but it is quite long so don't feel obligated to sift through my horrible...horrible code.

Be warned, its ugly and probably bad programming.  All of this gets done 60 times a second.  I'm sure that there is a much better way to do this so it doesn't need to be done every 60 seconds.  But the screen needs to move in response to the mouse going to the edge of the screen so things need to be updated constantly.  Also, I have never made a habit out of commenting so seriously, DO NOT FEEL OBLIGATED TO READ MY CODE, but any help would be appreciated. Thanks to anyone who will hold my hand through this =)

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  
private void gameRender(Graphics dbg)
    {
        counter++;
        if(counter == 61)counter = 0;
       if(dbImage == null){
            dbImage = createImage(pWidth, pHeight);
            if(dbImage == null){
                System.out.println("dbImage is null!");
                return;
            }else
                dbg = dbImage.getGraphics();
        }
        g2 = (Graphics2D)dbg;
        dbg.setColor(Color.black);
        dbg.fillRect(0, 0, pWidth, pHeight);
        //draw game elements
       //draw main screen
       screen = new Rectangle2D.Double(-x, -y, 1024, 588);
        for(int i = 0; i < 12; i++){
                for(int p = 0; p < 7; p++){
                  int tile;
                  int xMod;
                  int yMod;
                  int x1Mod;
                  int y1Mod;
                  xMod = 0;
                  yMod = 0;
                  x1Mod = x%100;
                  y1Mod = y%100;
                  if(-x > 99) xMod = -(x/100);
                  if(-y > 99) yMod = -(y/100);
                  tile = mL.getTile(xMod + i, yMod + p);
                  if(tile == 48) g2.drawImage(grass, (i*100)+x1Mod, (p*100)+y1Mod, null);
                  if(tile == 49) g2.drawImage(tree, (i*100)+x1Mod, (p*100)+y1Mod, null);
                }
        }
       
        for(int i = 0; i < 20; i++){
            if(counter == 60)combat(i);
            if(p1Units.get(i).getBox().intersects(screen)){
                if(playerNum == 2){
                    g2.setColor(Color.red);
                    g2.draw(new Ellipse2D.Double(p1Units.get(i).getBox().getX()+x+2.5, p1Units.get(i).getBox().getY()+45+y, p1Units.get(i).getBox().getWidth(),p1Units.get(i).getBox().getWidth()));
                }
                for(int p = 0; p < 5; p++){
                    if(i == selectedUnit[p]){
                        g2.setColor(Color.white);
                        g2.draw(new Ellipse2D.Double(p1Units.get(i).getBox().getX()+x+2.5, p1Units.get(i).getBox().getY()+45+y, p1Units.get(i).getBox().getWidth(),p1Units.get(i).getBox().getWidth()));                    
                    }
                }
                if(p1Units.get(i).getType() == "tank"){
                    g2.drawImage(tank.get(p1Units.get(i).getFacing()-1), (int)p1Units.get(i).getX() + x, (int)p1Units.get(i).getY() + y, null);
                }
                if(p1Units.get(i).getType() == "artillery"){
                    g2.drawImage(artillery.get(p1Units.get(i).getFacing()-1), (int)p1Units.get(i).getX() + x, (int)p1Units.get(i).getY() + y, null);
                }
            }
            if(p2Units.get(i).getBox().intersects(screen)){
                if(playerNum == 1){
                    g2.setColor(Color.red);
                    g2.draw(new Ellipse2D.Double(p2Units.get(i).getBox().getX()+x+2.5, p2Units.get(i).getBox().getY()+45+y, p2Units.get(i).getBox().getWidth(),p2Units.get(i).getBox().getWidth()));
                   
                }
                g2.drawImage(tank.get(p2Units.get(i).getFacing()), (int)p2Units.get(i).getX() + x, (int)p2Units.get(i).getY() + y, null);
                //if(playerNum == 1){
                   //g2.fill(new Rectangle2D.Double(p2Units.get(i).getX()+x+40, p2Units.get(i).getY()+y+100, 100, 10));
                   //g2.setColor(Color.green);
                  // g2.fill(new Rectangle2D.Double(p2Units.get(i).getX()+x+40, p2Units.get(i).getY()+y+100, p2Units.get(i).getHealth(), 10));
              // }
           }
            if(p1Units.get(i).getType() == "artillery" && p1Units.get(i).isFiring()){
                Point2D point = explosionHandler.drawExplosion(i, counter);
                if(point.getX() != 0 && point.getY() != 0){
                    g2.drawImage(explosion, (int)point.getX()-55+x, (int)point.getY()-48+y, null);
                }
            }
        }
        //draw border
       g2.setColor(Color.white);
        g2.drawImage(hud, 0, 588, null);
        g2.drawImage(minimap, 849, 593, null);
        for(int i = 0; i < 20; i++){
            g2.setColor(Color.blue);
            g2.fill(new Rectangle2D.Double((p1Units.get(i).getX()/22.86) + 849, (p1Units.get(i).getY()/22.86)+593, 4.37, 4.37));
            g2.setColor(Color.red);
            g2.fill(new Rectangle2D.Double((p2Units.get(i).getX()/22.86) + 849, (p2Units.get(i).getY()/22.86)+593, 4.37, 4.37));
        }
        if(selectedUnit[0] != 21){
            if(p1Units.get(selectedUnit[0]).getType() == "artillery"){
                Unit u = p1Units.get(selectedUnit[0]);
                if(u.getStance() == "mobile") g2.drawImage(mobile, 775, 598, null);
                else g2.drawImage(fire, 775, 598, null);
            }
        }
        g2.setColor(Color.yellow);
        g2.draw(new Rectangle2D.Double((849+(-x/22.86)), (593 + (-y/22.86)), 44.79, 25.94));
        //g2.fill(new Rectangle2D.Double(0, 568, 1024, 200));
       
    }
    private void combat(int index){
        int i2 = p1Units.get(index).getTheTarget();
        if(p1Units.get(index).isFiring()){
           
            if(i2 != 21){
                if(!p2Units.get(i2).isDead()){
                    if(!p1Units.get(index).isInRange(p2Units.get(p1Units.get(index).getTheTarget()).getX(), p2Units.get(p1Units.get(index).getTheTarget()).getY())){
                        p1Units.get(index).setIsFiring(false);
                        p1Units.get(index).setTarget((int)p2Units.get(i2).getX(), (int)p2Units.get(i2).getY());
                        p1Units.get(index).setIsMoving(true);
                    }
                    p2Units.get(i2).attacked(p1Units.get(index).getExplosionAttack(), p1Units.get(index).getBulletAttack());
                }else{
                    p1Units.get(index).setTarget(21);
                    p1Units.get(index).setIsFiring(false);
                }
            }
           
        }
       
        for(int i = 0; i < 20; i++){
            if(p1Units.get(index).isInRange(p2Units.get(i).getX(), p2Units.get(i).getY())){
                p1Units.get(index).setIsFiring(true);
                p1Units.get(index).setTarget(i);                  
            }
        }
        if(i2 != 21){
            if(p2Units.get(i2).isDead()){
                    p1Units.get(index).setTarget(21);
                    p1Units.get(index).setIsFiring(false);
            }
        }
    }
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

UprightPath (12 views)
2014-09-20 20:14:06

BurntPizza (26 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (95 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!