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  
  Starting to make a game  (Read 1120 times)
0 Members and 1 Guest are viewing this topic.
Offline Quillion

Senior Newbie





« Posted 2010-04-14 19:15:23 »

Ok I know I have been pretty much spamming all the forums asking for help lately and I am very sorry for that.
The problem is that my younger bros birthday is on may 7th and for the last 2 months I have been hell bent into making him a game (he had some health implications so I wanna cheer him up). If you want proof of what I did for the last 2 months I will gladly provide it. Anyways the problem is that I have hit a brick wall. My engine works, and all the mechanics are there. But I can not find a single tutorial anywhere that will teach me how to draw png(I already made a huge library of them) images to the screen without them lagging crazily.
Now I do not want to use pre build engines or anything like that. I want this to be my own work made by me from ground up, otherwise the present will not feel as good.
Anyways if someone could please redirect me or show me some kind of tutorial where I can draw images onto the screen efficiently I will be forever grateful to you.

Currently in the last 2 months I came up with these 4 skeletons on where everything is located

Skeleton 1 http://pastebin.com/RjRDfU9V
Skeleton 2 http://pastebin.com/kpxGZA0z
Skeleton 3 http://pastebin.com/V6Jy9470
Skeleton 4 http://pastebin.com/6eWf0fzG

I tried using g.drawImage but the game starts lagging, if I replace everyimage with fillRect it works super fast and smooth, so I know the issue is drawing.

Thanks to anyone who will help ^^
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 6
Exp: 8 years



« Reply #1 - Posted 2010-04-14 19:52:35 »

The skeleton 1 is fine.
But you should also paste the way you are loading and drawing your pngs to see what's wrong .

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #2 - Posted 2010-04-14 19:57:13 »

It's great that you're starting a game, I'm happy for you. We also don't mind answering questions here at JGO. In fact, many of us enjoy doing it!

However, duplicate questions are frowned on, and posting something 3 times (as far as I've counted so far) is definitely a no-no. Now I'm annoyed with reading the same thing again.

This question is, more or less a duplicate of:
http://www.java-gaming.org/topics/how-to-draw-png-images-to-the-screen-efficiently/22250/view.html

Which was already a duplicate post (the other post was deleted). I am locking the thread mentioned above.

So in the future post each question once and then be patient.
Thanks!

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Quillion

Senior Newbie





« Reply #3 - Posted 2010-04-14 20:07:06 »

Ok I am very sorry about that  Undecided  Cry
Promise I won't do it again ><

As to answer your question teletubo I load png into ImageIcon and then load it into Image like so

1  
2  
ImageIcon ii = new ImageIcon(this.getClass().getResource("images/objects/"+sprite+".PNG"));
image = ii.getImage();


And everything including platforms, main character and monsters is loaded same way

For example this is my platform class

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  
package MediumFullScreen;
import java.awt.Image;

import javax.swing.ImageIcon;


public class Wall
{
   private Image image;
   private double x, y;
   private double leftOffset, rightOffset, topOffset, bottomOffset;
   private int width, height;
   private static int WIDTH, HEIGHT;
   
   // set the screen  parameters, if object goes out of screen it becomes null
  public static void setScreen(int width, int height)
   {
      WIDTH = width;
      HEIGHT = height;
   }
   
   public Wall(String sprite)
   {
      ImageIcon ii = new ImageIcon(this.getClass().getResource("images/objects/"+sprite+".PNG"));
      image = ii.getImage();

      width = image.getWidth(null);
      height = image.getHeight(null);
     
      x = 0;
      y = 0;
     
      leftOffset = 0;
      rightOffset = 0;
      topOffset = 0;
      bottomOffset = 0;
   }
   
   public Wall()
   {
      image = null;
     
      width = 0;
      height = 0;
     
      x = 0;
      y = 0;
     
      leftOffset = 0;
      rightOffset = 0;
      topOffset = 0;
      bottomOffset = 0;
   }
   
   public Wall(Image img)
   {
      image = img;
     
      width = image.getWidth(null);
      height = image.getHeight(null);
     
      x = 0;
      y = 0;
     
      leftOffset = 0;
      rightOffset = 0;
      topOffset = 0;
      bottomOffset = 0;
   }
   
   public Image getImage()
   {
      if(x<=WIDTH && x+width>=0 && y+height>=0 && y<=HEIGHT)
         return image;
      return null;
   }
   
   public void setOffset(int topOffset, int bottomOffset, int leftOffset, int rightOffset)
   {
      this.topOffset = topOffset;
      this.bottomOffset = bottomOffset;
      this.leftOffset = leftOffset;
      this.rightOffset = rightOffset;
   }
   
   public int getBottomY()
   {
      return (int)(y+height-bottomOffset);
   }

   public int getLeftX()
   {
      return (int)(x+leftOffset);
   }

   public int getRightX()
   {
      return (int)(x+width-rightOffset);
   }

   public int getTopY()
   {
      return (int)(y+topOffset);
   }

   public int getHeight()
   {
      return height;
   }

   public int getWidth()
   {
      return width;
   }

   public int getX()
   {
      return (int)x;
   }

   public int getY()
   {
      return (int)y;
   }

   public void setHeight(int height)
   {
      this.height = height;
   }

   public void setWidth(int width)
   {
      this.width = width;
   }

   public void setX(double x)
   {
      this.x = x;
   }

   public void setY(double y)
   {
      this.y = y;
   }
}


And the problem is that once I reach 30 images, then the whole game lags out. So is there a better way to load them? Maybe using something else?
Offline DzzD
« Reply #4 - Posted 2010-04-14 20:11:00 »

1  
2  
ImageIcon ii = new ImageIcon(this.getClass().getResource("images/objects/"+sprite+".PNG"));
image = ii.getImage();

you dont have to use ImageIcon you can load Image directly


could you give a little more code ? maybe the drawing /loop part it would be more interresting to found the botleneck

Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 6
Exp: 8 years



« Reply #5 - Posted 2010-04-14 20:15:15 »

first, you shouldn't load one image for each wall . You can have a static class with just one copy of the image and then just draw the same image in different places .

You should also use BufferedImage instead of Image .
ex:
1  
2  
3  
4  
5  
6  
7  
8  
public static BufferedImage[] images;

public static final int IMAGE_WALL=0, TOTAL_IMAGES=1;
...

images = new BufferedImage[TOTAL_IMAGES];

images[IMAGE_WALL] = loadImage(.....);


and then when you can just refer to the index on this array in your sprite class, ex:
1  
2  
3  
4  
5  
6  
7  
int imageIdx=0;


public void drawMe(Graphics2D g)
{
g.drawImage(x,y, ImagesLib.images[imageIdx], null);
}



by the way you didn't post the method you are using to draw the images .

Offline Quillion

Senior Newbie





« Reply #6 - Posted 2010-04-14 20:21:13 »

Well in the skeleton where I have *start drawing
right in between there I put this in

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
g.drawImage(sky.getImage(), 0, 0, this);
       
        for(int i=0; i<background.length; i++)
        {
           g.drawImage(background[i].getImage(), background[i].getX(), background[i].getY(), this);
        }
       
        for(int i=0; i<wall.length; i++)
        {
           g.drawImage(wall[i].getImage(), wall[i].getX(), wall[i].getY(), this);
        }
       
        for(int i=0; i<platform.length; i++)
        {
           g.drawImage(platform[i].getImage(), platform[i].getX(), platform[i].getY(), this);
        }
       
        g.drawImage(hero.getImage(), hero.getX(), hero.getY(), this);


Thanks to everyone for helping ^^
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 6
Exp: 8 years



« Reply #7 - Posted 2010-04-14 20:32:41 »

remove the image observer (the last parameter from the drawImage calls) and pass "null".

that is,

1  
    g.drawImage(background[i].getImage(), background[i].getX(), background[i].getY(), null);

Offline Quillion

Senior Newbie





« Reply #8 - Posted 2010-04-14 21:00:29 »

Everyone I thank you from the bottom of my heart, you guys are awesome.
I used buffered image and changed the observer to null, and now it runs smoothly.
My brother sure will be happy ^^
THANK YOU ONCE AGAIN
Of course first I will give him the present once I finish designing the levels and then I will post a copy of the game here later on
THANK YOU ^^  Smiley
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 (62 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (215 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!