Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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  
  Using BufferStrategy But Still Got Flicker  (Read 5397 times)
0 Members and 1 Guest are viewing this topic.
Offline maxking1234

Senior Newbie





« Posted 2012-05-10 18:32:14 »

Hi,

I am using the game loop in the tutorial basic game, which uses BufferStrategy,  but when I render my world I still get flicker.
If any one could help I would appreciate it. Loop source code:
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  
public class Planetoids implements Runnable {

   public static int xOffs = 0, yOffs = 0;

   public final int WIDTH = 800;
   public final int HEIGHT = 600;

   private PlanetEarth earth;

   private JFrame frame;
   private Canvas canvas;
   private BufferStrategy bufferStrategy;

   public Planetoids() {
      frame = new JFrame("Planetoids Pre-Alpha 0.0.1");

      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(null);

      canvas = new Canvas();
      canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);

      panel.add(canvas);

      canvas.addKeyListener(new InputManager());

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);

      canvas.createBufferStrategy(2);
      bufferStrategy = canvas.getBufferStrategy();

      canvas.requestFocus();
      init();
   }

   private void init() {
      earth = new PlanetEarth(30,30);
      earth.map = PlanetGenerator.loadMap(30, 30);
   }

   long desiredFPS = 60;
   long desiredDeltaLoop = (1000 * 1000 * 1000) / desiredFPS;

   boolean running = true;

   public void run() {

      long beginLoopTime;
      long endLoopTime;
      long currentUpdateTime = System.nanoTime();
      long lastUpdateTime;
      long deltaLoop;

      while (running) {
         beginLoopTime = System.nanoTime();

         render();

         lastUpdateTime = currentUpdateTime;
         currentUpdateTime = System.nanoTime();
         update((int) ((currentUpdateTime - lastUpdateTime) / (1000 * 1000)));

         endLoopTime = System.nanoTime();
         deltaLoop = endLoopTime - beginLoopTime;

         if (deltaLoop > desiredDeltaLoop) {
            // Do nothing. We are already late.
        } else {
            try {
               Thread.sleep((desiredDeltaLoop - deltaLoop) / (1000 * 1000));
            } catch (InterruptedException e) {
               // Do nothing
           }
         }
      }
   }

   private void render() {
      Graphics g =  bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      render(g);
      g.dispose();
      bufferStrategy.show();
   }

   protected void update(int deltaTime) {

   }

   protected void render(Graphics g) {
      earth.render(g);
   }

   public static void main(String[] args) {
      Planetoids ex = new Planetoids();
      new Thread(ex).start();
   }

}

The world render code:
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  
public void render(Graphics g) {
      super.render(g);
      int x = 0, y = 0;
      for (int i = 0; i < map.length; i++) {
         if (x == width) {
            x = 0;
            y += 1;
         }
         switch (map[i]) {
         case Tile.TILE_EARTH_DIRT:
            g.drawImage(Tile.TILE_EARTH_DIRT_IMG, x * 32 + Planetoids.xOffs, y * 32 + Planetoids.yOffs, 32, 32, null);
            break;
         case Tile.TILE_EARTH_GRASS:
            g.drawImage(Tile.TILE_EARTH_GRASS_IMG, x * 32 + Planetoids.xOffs, y * 32 + Planetoids.yOffs, 32, 32, null);
            break;
         case Tile.TILE_EARTH_STONE:
            g.drawImage(Tile.TILE_EARTH_STONE_IMG, x * 32 + Planetoids.xOffs, y * 32 + Planetoids.yOffs, 32, 32, null);
            break;
         case Tile.TILE_EARTH_SAND:
            g.drawImage(Tile.TILE_EARTH_SAND_IMG, x * 32 + Planetoids.xOffs, y * 32 + Planetoids.yOffs, 32, 32, null);
            break;
         }
         x++;
      }
   }


Max
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-05-10 20:23:21 »

I see nothing wrong with the code. What kind of flickering do you see?

Offline maxking1234

Senior Newbie





« Reply #2 - Posted 2012-05-10 20:25:51 »

When I move about, up and left it flickers
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline maxking1234

Senior Newbie





« Reply #3 - Posted 2012-05-10 20:57:07 »

Strangely now I only get flicker when moving up?
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-05-10 21:03:59 »

What kind of flickering are you talking about? Flickering means that it disappears for a frame or two then reappears. Maybe you mean it stutters, where it doesn't move smoothly?

Offline ReBirth
« Reply #5 - Posted 2012-05-11 04:35:31 »

It's unique that you use int for delta. Show us your class code that moving.

Offline nsigma
« Reply #6 - Posted 2012-05-11 09:21:12 »

What kind of flickering are you talking about? Flickering means that it disappears for a frame or two then reappears. Maybe you mean it stutters, where it doesn't move smoothly?

Or do you mean that the image isn't VSynced (ie. you're seeing a bit of the current frame and a bit of the previous one)?  BufferStrategy isn't guaranteed to be VSynced so this can result in flicker, depending on the system (which is?).

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline maxking1234

Senior Newbie





« Reply #7 - Posted 2012-05-11 18:30:06 »

It's unique that you use int for delta. Show us your class code that moving.
This is the movement code
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public void keyPressed(KeyEvent key) {
      switch (key.getKeyCode()) {
      case KeyEvent.VK_ESCAPE:
         System.exit(0);
         break;
      case KeyEvent.VK_W:
         Planetoids.yOffs += Player.moveSpeed;
         break;
      case KeyEvent.VK_S:
         Planetoids.yOffs -= Player.moveSpeed;
         break;
      case KeyEvent.VK_A:
         Planetoids.xOffs += Player.moveSpeed;
         break;
      case KeyEvent.VK_D:
         Planetoids.xOffs -= Player.moveSpeed;
         break;
      }
Offline maxking1234

Senior Newbie





« Reply #8 - Posted 2012-05-11 18:31:17 »

What kind of flickering are you talking about? Flickering means that it disappears for a frame or two then reappears. Maybe you mean it stutters, where it doesn't move smoothly?
When I move quite often white rectangles appear instead of the tiles.
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-05-11 22:03:04 »

Shocked Please tell me you load images once in a constructor/init method somewhere and don't reload them every time you render!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline maxking1234

Senior Newbie





« Reply #10 - Posted 2012-05-12 01:48:16 »

I load them statically in a Tile class?
1  
private static Image TILE_DIRT_IMG = new ImageIcon("res/grass.png").getImage();

Does this load them every time I call it??
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-05-12 01:54:34 »

Oh ok never mind that's fine. Side note: It's best to use javax.imageio.ImageIO by calling its read(URL) method. You get a URL using ClassName.class.getClassLoader().getResource("res/grass.png");

Hmm white rectangles popping randomly or predictably?

Are there any other rendering codes?

Offline ReBirth
« Reply #12 - Posted 2012-05-12 06:09:27 »

Looking on your moving code inside KeyAdapter class, I guess you move hardly, one tile per keypress. If you want a smooth one, you should use delta to calculate the movement little by little each frame.

Offline maxking1234

Senior Newbie





« Reply #13 - Posted 2012-05-12 06:29:45 »

Looking on your moving code inside KeyAdapter class, I guess you move hardly, one tile per keypress. If you want a smooth one, you should use delta to calculate the movement little by little each frame.
Like ?
1  
Planetoids.xOffs += Player.moveSpeed * delta
Offline maxking1234

Senior Newbie





« Reply #14 - Posted 2012-05-12 06:35:08 »

Thanks, This seems to have stopped the flicker"! Smiley
Offline ReBirth
« Reply #15 - Posted 2012-05-12 06:39:49 »

Yes inside your entity class, that's normally what we do for continuous movement. If, in your case you want "blocky" move then yours is right. Just I can't imagine if it'll flicking.

Offline maxking1234

Senior Newbie





« Reply #16 - Posted 2012-05-12 06:47:57 »

Yes inside your entity class, that's normally what we do for continuous movement. If, in your case you want "blocky" move then yours is right. Just I can't imagine if it'll flicking.
Well I think it must of been that as it is now perfect Smiley
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #17 - Posted 2012-05-12 18:11:58 »

I still don't understand how blocky movement == flicker Tongue

Offline theagentd
« Reply #18 - Posted 2012-05-12 19:44:19 »

I still don't understand how blocky movement == flicker Tongue
Someone needs to make a FAQ for this. How are we supposed to help people when they can't describe the problem? =S

Myomyomyo.
Offline ReBirth
« Reply #19 - Posted 2012-05-13 03:21:53 »

I still don't understand how blocky movement == flicker Tongue
Someone needs to make a FAQ for this. How are we supposed to help people when they can't describe the problem? =S
+1, let's see how many threads that asking same problem again and again.

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #20 - Posted 2012-05-13 05:02:48 »

Yeah I agree, I hate constantly seeing threads that ask the same questions over and over and over again. I feel compelled to write that FAQ right now.......

@Riven, you should create an FAQ page which a certain group people can edit so we can all contribute stuff to it. Then make a big bold banner on the front page Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 802
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #21 - Posted 2012-05-13 06:32:09 »

@Riven, you should create an FAQ page which a certain group people can edit so we can all contribute stuff to it. Then make a big bold banner on the front page Smiley
Yeah, it's called a wiki, and was a huge failure on JGO. Not going to bother with it anymore.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ReBirth
« Reply #22 - Posted 2012-05-13 07:30:32 »

Let's make simpler, like a sticky post.

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #23 - Posted 2012-05-13 07:30:43 »

It was a huge failure because I wasn't here! *looks up majestically*

But seriously, an FAQ page would drastically reduce the number of repeat noob threads who don't Google properly.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #24 - Posted 2012-05-13 16:11:27 »

There is a FAQ post, but I'm not even sure what the FAQ is here.  Interpolated movement?
Offline Gjallar

JGO Coder


Medals: 13
Projects: 1


Follower of Nurgle


« Reply #25 - Posted 2012-05-13 16:22:23 »

Interpolated movement?

Yeah... that's the first thing I'd google for if I experience flickering  Grin
Offline theagentd
« Reply #26 - Posted 2012-05-14 09:13:30 »

Interpolated movement?

Yeah... that's the first thing I'd google for if I experience flickering  Grin
Maybe it's the new "Something's wrong!"?

Myomyomyo.
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (46 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (36 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

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

BurntPizza (54 views)
2014-09-19 03:14:18
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!