Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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: Screen Tearing on: 2013-02-04 17:27:12
Alright, fair enough. However: do you have words of advice on how I go about fixing this?
2  Game Development / Newbie & Debugging Questions / Re: Screen Tearing on: 2013-02-04 07:25:36
Maybe the attempt to get a Hardware Double Buffer failed.  It doesn't always succeed, particularly when running windowed (or if there isn't any spare graphics memory).  Check BufferCapabilities.isFlipping().  You can use BufferStrategy.GetCapabilities() to obtain the capabilities structure.  Also check whether your memory card has enough memory to double buffer at your chosen screen resolution.

If it's double buffering by blitting (spell checker keeps changing that to blotting - bad spell check, down boy, down boy), you will get tearing when the window gets above a certain size.  It's most noticeable if you have sideways scrolling that scrolls in big increments, or objects that move very fast.

On the whole, I've been most successful with BufferStrategy when going fullscreen.  Otherwise, particularly for applets, I often just draw onto a bufferedImage and then draw that to the screen with one call.  It still tears if the image is big enough.  I rely on not clearing the background every frame and a high frame rate, so that the image doesn't change much between frames.  Admittedly I got into that habit for writing 4k games.  The BufferStrategy method ought to be better as in some circumstances one might get a hardware double buffer.


Sounds good; however, how does one debug efficiently whilst working in full screen mode? I generally enjoy the convenience of being able to see the console and write various messages to it.
3  Game Development / Newbie & Debugging Questions / Re: Screen Tearing on: 2013-02-04 05:56:44
You know, I may as well attempt a dive into libgdx. Thanks for the helpful pointers.
4  Game Development / Newbie & Debugging Questions / Re: Screen Tearing on: 2013-02-04 05:43:01
I just checked out the java docs for BufferedCapabilities, and there is no vsync option there, the only way to get vsync in Java2D that I know of is to use a GraphicsDevice and a GraphicsEnvironment, then set fullscreen and it is enabled by default (I think)

Aside from that the first thing you should do is cast your Graphics g into a Graphics2D object

1  
Graphics2D g = (Graphics2D) bufferStategy.getDrawGraphics();


What about Toolkit.getDefaultToolkit().sync()?
5  Game Development / Newbie & Debugging Questions / Re: Screen Tearing on: 2013-02-04 05:14:54
A few things:

1. Java2D is the current library.
2. I am using a triple buffer.

Oh, and a bit more code to brighten your day.  Grin

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  
package com.mizzath.sandbox;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import com.mizzath.sandbox.input.Keyboard;
import com.mizzath.sandbox.level.Level;
import com.mizzath.sandbox.screen.Screen;

public class Sandbox implements Runnable {
   final int WIDTH = 300;
   final int HEIGHT = WIDTH * 9 / 16;
   final int SCALE = 3;
   
   private String title = "Sandbox";
   
   private JFrame frame;
   private Canvas canvas;
   private BufferStrategy bufferStrategy;
   private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
   private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
   
   private Screen screen;
   private Level level;
   private Keyboard keyboard;
   
   boolean running = true;
   
   public Sandbox() {
      Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
     
      frame = new JFrame(title);
      frame.setPreferredSize(size);
     
      canvas = new Canvas();
     
      frame.add(canvas);

      canvas.setPreferredSize(size);
     
      keyboard = new Keyboard();
      canvas.addKeyListener(keyboard);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.setIgnoreRepaint(true);
      frame.pack();
      frame.setVisible(true);
     
      canvas.requestFocus();
     
      canvas.createBufferStrategy(3);
      bufferStrategy = canvas.getBufferStrategy();
     
      screen = new Screen(WIDTH, HEIGHT);
      level = new Level(64, 64, screen);
   }
   
   public void run() {
      long lastTime = System.nanoTime();
      double timer = System.currentTimeMillis();
      double ns = 1000000000.0 / 60;
      double delta = 0;
      int frames = 0;
      int updates = 0;
     
      while (running) {
         long now = System.nanoTime();
         delta += (now - lastTime) / ns;
         
         lastTime = now;
         
         while (delta >= 1) {
            delta -= 1;
            update();
            updates++;
         }
         
         frames++;
         render();
         
         if (System.currentTimeMillis() - timer >= 1000) {
            timer = System.currentTimeMillis();
            frame.setTitle(title + " || Updates: " + updates + ", FPS: " + frames);
            updates = 0;
            frames = 0;
         }
      }
   }
   
   private void render() {
      Graphics g = null;
     
      try {
         g = bufferStrategy.getDrawGraphics();
         render(g);
      } finally {
         g.dispose();
      }

      bufferStrategy.show();  
   }
   
   public void render(Graphics g) {
      screen.wipe();
      level.render(keyboard.x, keyboard.y);
     
      for (int i = 0; i < pixels.length; i++) {
         pixels[i] = screen.pixels[i];
      }
     
      g.drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight(), null);
      g.setColor(Color.CYAN);
      g.drawString("X: " + -keyboard.x + ", Y: " + -keyboard.y, 10, 10);
   }
   
   public void update() {
     
   }
   
   public static void main(String[] args) throws Exception {
      Sandbox sandbox = new Sandbox();
      new Thread(sandbox).start();
   }
}
6  Game Development / Newbie & Debugging Questions / Screen Tearing on: 2013-02-04 04:53:40
Hello all,

I seem to be experiencing a bit of screen tearing -- or, at least, that's what I hope it is.

How does one go about mending this heinous deed?



Level.java:
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  
package com.mizzath.sandbox.level;

import java.util.Random;

import com.mizzath.sandbox.screen.Screen;
import com.mizzath.sandbox.tile.Tile;

public class Level {
   public int width, height;
   public int xOffset, yOffset;
   public int[] tiles;
   public Screen screen;
   
   Random random = new Random();
   
   public Level(int width, int height, Screen screen) {
      this.width = width;
      this.height = height;
      this.screen = screen;
     
      tiles = new int[width * height];
     
      for (int i = 0; i < tiles.length; i++) {
         tiles[i] = random.nextInt(2);
      }
   }
   
   public void render(int xOffset, int yOffset) {  
      int x0 = 0, x1 = 0, y0 = 0, y1 = 0;
     
      int xPixelChange = 0, yPixelChange = 0;
     
      if (yOffset > 0) {
         if (yOffset <= 16) {
            yPixelChange = -yOffset; // ypos + yPixelChange
           y0 = 0;
         } else if (yOffset > 16) {
            yPixelChange = -(yOffset % 16); // ypos + yPixelChange
           y0 = yOffset >> 4;
         }
      } else if (yOffset < 0) {
         if (yOffset >= -16) {
            yPixelChange = 16 + yOffset; // ypos - yPixelChange
           y0 = -1;
         } else if (yOffset < -16) {
            yPixelChange = (16 + (yOffset % 16)); // ypos - yPixelChange
           y0 = (yOffset - yPixelChange) >> 4;
         }
      }
     
      int deltaY = yOffset + screen.height;
      y1 = deltaY + (16 - (deltaY % 16)) >> 4;
     
      if (xOffset > 0) {
         if (xOffset <= 16) {
            xPixelChange = 16 - xOffset; // xpos - xPixelChange
           x0 = -1;
         } else if (xOffset > 16) {
            xPixelChange = 16 - (xOffset % 16); // xpos - xPixelChange
           x0 = (-xOffset - xPixelChange) >> 4;
         }
      } else if (xOffset < 0) {
         if (xOffset >= -16) {
            xPixelChange = xOffset; // xpos + xPixelChange
           x0 = 0;
         } else if (xOffset < -16) {
            xPixelChange = xOffset % 16; // xpos + xPixelChange
           x0 = -xOffset >> 4;
         }
      }
     
      int deltaX = -xOffset + screen.width;
      x1 = deltaX + (16 - (deltaX % 16)) >> 4;
     
      for (int y = y0, yTilePos = 0; y < y1; y++, yTilePos++) {
         int yAbsPos = yTilePos << 4;
         
         if (yOffset > 0) {
            yAbsPos += yPixelChange;
         } else if (yOffset < 0){
            yAbsPos -= yPixelChange;
         }
         
         for (int x = x0, xTilePos = 0; x < x1; x++, xTilePos++) {
            int xAbsPos = xTilePos << 4;
           
            if (xOffset > 0) {
               xAbsPos -= xPixelChange;
            } else if (xOffset < 0) {
               xAbsPos += xPixelChange;
            }
           
            getTile(x, y).render(xAbsPos, yAbsPos, screen);
         }
      }
   }
   
   public Tile getTile(int x, int y) {  
      if (x < 0 || y < 0 || x >= width || y >= height) return Tile.nil;
     
      int tile = tiles[x + y * width];
     
      if (tile == 0) {
         return Tile.grass;
      } else if (tile == 1) {
         return Tile.water;
      } else {
         return Tile.nil;
      }
   }
}


Screen.java:
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  
package com.mizzath.sandbox.screen;

import com.mizzath.sandbox.tile.Tile;

public class Screen {
   public int width, height;
   public int[] pixels;
   public int xOffset, yOffset;
   
   public Screen(int width, int height) {
      this.width = width;
      this.height = height;
      pixels = new int[width * height];
   }
   
   public void wipe() {
      for (int i = 0; i < pixels.length; i++) {
         pixels[i] = 0xffffff;
      }
   }
   
   /* Renders a tile.
    *
    * xp = the starting pixel's x position
    * yp = the starting pixel's y position
    * xa = the x position of the pixel being rendered
    * ya = the y position of the pixel being rendered
    * tile = the tile to be rendered
    */

   
   public void renderTile(int xp, int yp, Tile tile) {      
      for (int y = 0; y < tile.size; y++) {
         int ya = y + yp;
         if (ya < 0 || ya >= height) continue;

         for (int x = 0; x < tile.size; x++) {
            int xa = x + xp;
            if (xa < 0 || xa >= width) continue;
           
            pixels[xa + ya * width] = tile.colour;
         }
      }
   }
}


Sorry for dumping all of that upon you, and in no way are you required to meander through all of it. However, if you have some spare time, why not rummage though my predicament?

Thanks.

-Miles
7  Game Development / Newbie & Debugging Questions / Re: Rendering and Scale Issue on: 2012-11-12 06:10:25
I don't want to be rude, but what is with everyone using all this pixel[] nonsense? Unless you are trying to do some super ridiculous effects in java2D or are working on a java4k game there is really no need to touch an array of pixels.

Look into Graphics2D.scale I think. It may help.

Also, I recommended not over thinking this too much. When I wanted to do some pixel effects it java2D was slow. So I rendered everything like normal to a Volatile Image at full resolution. This keeps things fast. After the rendering was done and I wanted to access the pixels for lighting or w/e, I then drew the volatile image to a bufferedimage at the a reduced resolution. Thing are still fast. Now I access the pixels from the bufferedimage which un-managed it. This is were the slow down happens but because the resolution is low, it is manageable. After doing my black pixel magic, I draw the edited buffered image using the buffer strategies graphics object.

You could use two volatile images instead of bufferedImage and us getSnapShot() but I am not sure if that would be much of a performance boost. The key thing is keeping things managed. It is hard to beat the performance of the simple, g.drawImage() as it keeps everything managed. The moment you touch pixel[], BAM! unmanaged. If you only do this every once in a while it is no problem but if you do it every frame...

Believe me, I'm absolutely new when it comes to Java game development, so input like this is truly appreciated. I'll research a VolatileImage for a while and then attempt to implement it. However, how simple would it be to recreate my tile map?
8  Game Development / Newbie & Debugging Questions / Rendering and Scale Issue on: 2012-11-12 04:50:52
Good day, me hearties!

Let's get straight to it. I've made a game that's composed of three class files: Game.java, Screen.java, and Map.java.

Game.java: game loop, etc

Screen.java: renders a map

Map: the map

Alright. Pretty basic, eh? Right. That's what I was hoping for.

Issue starts here: THE SCREEN IS SCALED AFTER RENDERING PROCESS COMPLETES IN ORDER TO SAVE CPU POWER.

Not much of a problem, it seems. Well, read on.

In order to calculate the width/height (in pixels) of my tile-map, I simply multiplied the tile size by the width/height in tiles. This would be accurate, but after the scaling (x3), this data is completely wrong. A coordinate before the scaling doesn't represent the same thing it did after the scaling. I implemented a player & camera, not realizing that my coordinate system was entirely nullified, and I began experiencing many issues, such as not being able to calculate when the player reaches the edge of the map.

Keep in mind that the player was a BufferedImage rendered on top of the Screen/Map (also a BufferedImage).

How can I retain this scale and accurately implement a coordinate system? Should I somehow merge the player image into the map image, resulting in both items being scaled? This seems sensible, yet how would I carry it out?

Game.java:

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  
package com.mizzath.game;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import com.mizzath.graphics.*;

public class Game extends Canvas implements Runnable {
   private static final long serialVersionUID = 1L;
   
   int width = 300;
   int height = width / 12 * 9;
   int scale = 3;
   String title = "Game";
   
   boolean running = false;
   
   BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
   
   JFrame frame;
   Thread thread;
   Screen screen;
   
   Map map;
   
   public Game() {
      frame = new JFrame(title);
     
      map = new Map(64, 64, 16);
      screen = new Screen(width, height, map);
     
      Dimension size = new Dimension(width * scale, height * scale);
     
      setSize(size);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.add(this);
      frame.pack();
      frame.setVisible(true);
     
      start();
   }
   
   public void run() {
      long start = System.nanoTime();
      long timer = System.currentTimeMillis();
      double period = 1000000000D / 60D;
      double delta = 0;
     
      int updates = 0; // Logic
     int frames = 0;  // Rendering
     
      while(running) {
         long now = System.nanoTime();
         
         delta += (now - start) / period;
         
         start = now;
         
         if (delta > 1) {
            updates++;
            update();
            delta--;
         }
         
         frames++;
         render();
         
         if (System.currentTimeMillis() - timer > 1000) {
            frame.setTitle(title + " | Logical updates: " + updates + ", Graphical Updates: " + frames);
           
            timer += 1000;
            updates = 0;
            frames = 0;
         }
      }
   }
   
   public void render() {
      BufferStrategy strategy = getBufferStrategy();
     
      if (strategy == null) {
         createBufferStrategy(3);
         return;
      }
     
      screen.render(0, 0);
     
      for (int i = 0; i < pixels.length; i++) {
         pixels[i] = screen.pixels[i];
      }
     
      Graphics g = strategy.getDrawGraphics();
     
      g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
     
      g.dispose();
      strategy.show();
   }
   
   public void update() {
     
   }
   
   public void start() {
      running = true;
     
      thread = new Thread(this, "Display");
      thread.start();
   }
   
   public void stop() {
      running = false;
     
      try {
         thread.join();
      } catch (InterruptedException e) {
         
      }
   }

   public static void main(String[] args) {
      Game game = new Game();
   }
}


Screen.java:

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  
package com.mizzath.graphics;

public class Screen {
   private int width, height;
   
   private Map map;
   
   public int[] pixels;
   
   public Screen(int width, int height, Map map) {
      this.width = width;
      this.height = height;
      this.map = map;
     
      pixels = new int[width * height];
   }
   
   public void render(int xOffset, int yOffset) {
      for (int y = 0; y < height; y++) {
         int yy = y + yOffset;
         if (yy < 0 || yy >= map.heightInPixels()) break;
         
         for (int x = 0; x < width; x++) {
            int xx = x + xOffset;
            if (xx < 0 || xx >= map.widthInPixels()) break;
           
            int tileIndex = (xx >> (map.tileSize() / 4)) + (yy >> (map.tileSize() / 4)) * map.widthInTiles();
           
            if (tileIndex < map.tiles().length) {
               pixels[x + y * width] = map.tiles()[tileIndex];
            }
         }
      }
   }
}


Map.java:

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  
package com.mizzath.graphics;

import java.util.Random;

public class Map {
   private int widthInTiles, heightInTiles, tileSize;
   private int[] tiles;
   
   private Random random = new Random();
   
   public Map(int widthInTiles, int heightInTiles, int tileSize) {
      this.widthInTiles = widthInTiles;
      this.heightInTiles = heightInTiles;
      this.tileSize = tileSize;
     
      tiles = new int[widthInTiles * heightInTiles];
     
      for (int i = 0; i < tiles.length; i++) {
         tiles[i] = random.nextInt(0xffffff);
      }
   }
   
   public int[] tiles() {
      return tiles;
   }
   
   public int tileSize() {
      return tileSize;
   }
   
   public int widthInTiles() {
      return widthInTiles;
   }
   
   public int heightInTiles() {
      return heightInTiles;
   }
   
   public int widthInPixels() {
      return widthInTiles * tileSize;
   }
   
   public int heightInPixels() {
      return heightInTiles * tileSize;
   }
}


Thanks, mates!
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.

TehJavaDev (15 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (30 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (43 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
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!