Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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 / Performance Tuning / Re: Accelerating images through BufferStrategy on: 2011-12-06 10:04:32
I still need a fix to the flickering resize...

Should I drop Canvas completely? And what for?
2  Game Development / Performance Tuning / Re: Accelerating images through BufferStrategy on: 2011-12-05 23:25:40
Agree, Swing sucks, but still, it's the standard library, so I'm just trying to learn the basics before moving on.

I finished my example, I modded the tutorial I posted, and another one too.

Here are my 2 classes. Do you know of a (quick) way to fix the flickering when resizing the window?
Because it look really awful, and I don't want to force a fixed size.
Here's my whole Eclipse project.

BTW I guess I also need to interrupt the thread when the frame closes, but fixing the resize takes priority now

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  
package sandbox;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

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

public class MyGame {
   private static final int FRAME_DELAY = 20; // 20ms. implies 50fps (1000/20) = 50

   public MyGame() {
      System.out.println(this.getClass().getClassLoader().getResource("resources/smile.gif"));
     
      JFrame frame = new JFrame();
      JPanel myPanel = new JPanel();
      Canvas myCanvas = new Canvas();
//      myCanvas.setIgnoreRepaint(true); // Doesn't seem to work at all
     
      BorderLayout myBLayout = new BorderLayout();
      myPanel.setLayout(myBLayout);
      myPanel.add(myCanvas, BorderLayout.CENTER);
      myPanel.add(new JButton("Clicky"), BorderLayout.EAST);
     
      frame.setContentPane(myPanel);
      frame.setSize(500, 500);
      frame.setVisible(true); // start AWT painting.
     
      Thread gameThread = new Thread(new GameLoop(myCanvas));
      gameThread.setPriority(Thread.MIN_PRIORITY);
      gameThread.start(); // start Game processing.
  }
   
   public static void main(String[] args) {
      MyGame myGame = new MyGame();
   }
 
   private static class GameLoop implements Runnable {
 
      boolean isRunning;
      Canvas myCanvas;
      long cycleTime;
      int moveX = 0;
      int moveY = 0;
     
      ImageLoader myImageLoader = new ImageLoader(null);
      BufferedImage myImage = myImageLoader.loadImage("resources/smile.gif", Transparency.TRANSLUCENT);
 
      public GameLoop(Canvas canvas) {
         myCanvas = canvas;
         isRunning = true;
      }
 
      public void run() {
         cycleTime = System.currentTimeMillis();
         myCanvas.createBufferStrategy(2);
         BufferStrategy strategy = myCanvas.getBufferStrategy();
 
         // Game Loop
        while (isRunning) {
 
            updateGameState();
 
            updatemyCanvas(strategy);
 
            synchFramerate();
         }
      }
 
      private void updateGameState() {
         // Nothing to do right now
     }
 
      private void updatemyCanvas(BufferStrategy strategy) {
         Graphics g = strategy.getDrawGraphics();
 
         g.setColor(Color.WHITE);
         g.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
         g.setColor(Color.BLACK);
 
         moveX = (moveX + 8)%myCanvas.getWidth();
         moveY = (moveY + 8)%myCanvas.getHeight();
         for(int i = 0; i < 5; ++i) {
            g.drawImage(myImage, (int) (i*5 + moveX), (int) (i*5 + moveY), null);
         }
         g.dispose();
         strategy.show();
      }
 
      private void synchFramerate() {
         cycleTime = cycleTime + FRAME_DELAY;
         long difference = cycleTime - System.currentTimeMillis();
         try {
            Thread.sleep(Math.max(0, difference));
         }
         catch(InterruptedException e) {
            e.printStackTrace();
         }
      }
 
   }
}


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  
package sandbox;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Hashtable;

import javax.imageio.ImageIO;

public class ImageLoader {
   final GraphicsConfiguration gc;
   private Hashtable<String, BufferedImage> chache = new Hashtable<String, BufferedImage>();

   public ImageLoader(GraphicsConfiguration gc) {
      if (gc == null) {
         gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
               .getDefaultScreenDevice().getDefaultConfiguration();
      }
      this.gc = gc;
   }

   public BufferedImage loadImage(String resource, int transparencyType) {
      try {
         if (chache.get(resource) != null) {
            return chache.get(resource);
         } else {
            URL imageURL = this.getClass().getClassLoader().getResource(resource);
            BufferedImage src = ImageIO.read(imageURL);
            // Images returned from ImageIO are NOT managedImages
           // Therefore, we copy it into a ManagedImage
           BufferedImage dst = gc.createCompatibleImage(src.getWidth(),
                  src.getHeight(), transparencyType);
            Graphics2D g2d = dst.createGraphics();
            g2d.setComposite(AlphaComposite.Src);
            g2d.drawImage(src, 0, 0, null);
            g2d.dispose();
            chache.put(resource, dst);
            return dst;
         }
      } catch (java.io.IOException e) {
         System.err.println("Cannot find resource " + resource);
         return null;
      }
   }
}
3  Game Development / Performance Tuning / Re: Accelerating images through BufferStrategy on: 2011-12-05 00:38:23
Thanks, if a BufferedImage can be accelerated as well, I'll gladly avoid using VolatileImage(s).

I'll try to make an example and post here for review.
4  Game Development / Performance Tuning / Accelerating images through BufferStrategy on: 2011-12-04 22:40:07
I need to draw on screen the same images many times, so I'm trying to accelerate it.

Every tutorial on acceleration I've seen so far uses a BufferedStrategy, inside a Canvas, inside a Frame.

But I can't find a simple tutorial that draws an accelerated image onto that Canvas.

I'd just draw a VolatileImage on the canvas, but I guess that would defeat the purpose of the BufferedStrategy.

Also, I can't figure out how to validate both the VolatileImage and the BufferedStrategy.
I guess they would interfere as they both call dispose() on the Graphics object.

I've seen this tutorials so far:

- this one never seems to validate the images
http://www.cokeandcode.com/info/tut2d.html

- this one doesn't load images, but is much clearer
http://www.javalobby.org/forums/thread.jspa?threadID=16867&tstart=0

EDIT: this one seems interesting too, but I think there's a little odd on the comment "This should not happen"
http://www.exampledepot.com/egs/java.awt.image/VolImage.html

Also, only the first tutorial use setIgnoreRepaint(true);

I'd like to edit the second tutorial making it draw a .gif image loaded from outside,
what's the best way to do that?

BTW I'd like to allow the user to resize the window, if that's possible
5  Game Development / Game Play & Game Design / Re: Executing Threads in a queue on: 2011-11-24 23:02:21
Ok, I need an example that can do the print the kind of things that I wrote above (worker - driver).

*Somebody* wants me to do this stupid kind of printing/bouncing with threads. I hope you get it.

Anyway, I'm sure that there is some way this could turn out to be useful. I'm learning something new, after all.

Now I just have to print things in an ordered fashion. I know it's simple, but I have to do it with threads.

EDIT
: I've read some more about the Executor. My Workers are Runnables, but I have loop in them (4 days of work), and so I'm worried that submitting them and then waiting for the completion of the loop, could lead to results like

"hello I'm worker #1 and I worked a day"
"hello I'm worker #1 and I worked a day"
"hello I'm worker #1 and I worked a day"
"hello I'm worker #2 and I worked a day"

Maybe there's some way to monitor (and pause) the progress of the submitted runnables, but I don't get how.
6  Game Development / Game Play & Game Design / Re: Executing Threads in a queue on: 2011-11-24 22:19:30
EDIT: sorry, quoted my own post, instead of fixing a typo
7  Game Development / Game Play & Game Design / Re: Executing Threads in a queue on: 2011-11-24 22:17:30
I mean simulated (not real hours), just print 1 time its "hello I'm worker #1 worked a day"

So, I have a Driver, and, say, 3 workers. I want this output

//day 1
"hello I'm the driver and I'm starting my turn" //some settings
"hello I'm worker #1 and I worked a day"
"hello I'm worker #2 and I worked a day"
"hello I'm worker #3 and I worked a day"
"hello I'm the driver and I'm finishing my turn"

//day 2
"hello I'm the driver and I'm starting my turn" //some settings
"hello I'm worker #1 and I worked a day"
"hello I'm worker #2 and I worked a day"
"hello I'm worker #3 and I worked a day. I RESIGN!"
"hello I'm the driver and I'm finishing my turn"

//day 3
"hello I'm the driver and I'm starting my turn" //some settings
"hello I'm worker #1 and I worked a day. I RESIGN!"
"hello I'm worker #2 and I worked a day"
"hello I'm the driver and I'm finishing my turn"

//day 4
"hello I'm the driver and I'm starting my turn" //some settings
"hello I'm worker #2 and I worked a day"
"hello I'm the driver and I'm finishing my turn. I'M FIRING YOU ALL!"

//day 5
*no worker shall work* //could I use some interrupts?

This is an example that came up to my mind.
I could make it worse so that the driver fires everybody on day 2 and every worker has to resign before working another day.

I don't know what a Future is and I'm afraid I don't know exactly what a Task is either. Lots of new classes here.

There are a lot of examples using Executor for a server, I hope you can point me to a simple non-distributed one.
8  Game Development / Game Play & Game Design / Re: Executing Threads in a queue on: 2011-11-24 20:04:01
It's not in my intentions to be such a masochist, but I have to deal with some "requests".

I don't know, the Executor looks complicated. Please forget the balls and look at this:

Driver-Worker example of the Java doc:
- I need those Workers to work once (and only once) a day for 4 days,
- Some Workers can resign before the 4th day is over.
- I need the rest of them to keep working on, once a day till the 4th day

Would it be possible to destroy a CyclicBarrier, recreate it (and update the Workers' references) in its Runnable() ?
Or is it better to use an Executor?

9  Game Development / Game Play & Game Design / Re: Executing Threads in a queue on: 2011-11-24 18:15:45
Ok, balls or not, now I'm curious about the Driver-Workers problem.

I'd like to learn some new option for concurrency, probably it's not the best option this time, but could turn out useful next time.
10  Game Development / Game Play & Game Design / Re: Executing Threads in a queue on: 2011-11-24 17:57:22
Thank you, this CyclicBarrier could be useful.

Now I have a little problem. What if one of the balls dies?

Then I need to change the N of the CyclicBarrier, but so far I can't see a way of doing this.

I'm looking at the CountDownLatch class, but after literally hours I can't figure out how to make it work.

Let's take the Driver-Worker example of the Java doc:
- I need those Workers to work once a day for 4 days,
- Some Workers can resign before the 4th day is over.
- I need the rest of them to keep working on, once a day till the 4th day

I put the CountDownLatches in the Driver. The Workers read them through the Driver.
I need to recreate them at the start of every day.
If I recreate them too soon, some Worker will not be able to read the old value and awake. (big problem)

I tried to modify the code of the example, but I can't get it to work.
11  Game Development / Game Play & Game Design / Executing Threads in a queue on: 2011-11-24 14:01:28
Hello everybody, I enjoyed the Game Loops tutorial on this board, but I have a slightly different problem, so I decided to post.

I have 10 balls-Threads, and I need every one of them to move once, then wait for the others to move before it can
move again. Sounds awful, it is awful, but please bear with me. I'll design it better next time.

If the 10 balls were not Threads, moving them one by one would be as simple as selecting then in a for loop.
Since I screwed it up, now it's a synchronization nightmare, and I'd like the simplest of the solutions, if you have one, to make
those threads run the way I want.

The balls can move like this
turn 1) 1, 2, 3, 4, 5, ..., 10
turn 2) 10, ..., 5, 4, 3, 2, 1
turn 3) 2, 1, 3, 5, 4, ..., 10

But not like this
turn 1) 2, 2, 1, 3, 3, 3, ..., 10


BTW I tried searching round-robin on Google, and I found some simulations of a real scheduler, so let me make this clear
I do not need a quantized time (some thread can take more time than the others to execute)
I do not need absolute timing (maybe it can help, but if your solution is simpler without, then it's better without)

EDIT: see this post for further informations (and a different example)
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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

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

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
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!