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   
Pages: 1 ... 6 7 [8] 9 10
 71 
 on: 2014-08-29 11:24:35 
Started by lcass - Last post by lcass
Basics of construction have now been implemented , just need to fix up resource consumption and camera position.

 72 
 on: 2014-08-29 11:13:02 
Started by xtre - Last post by basil_
aah i see. yes, you have the canvas set right (probably).

if you profile your game-loops i guess updateScreen() sticks out. basically you paint on a image (should be speedy), then paint the image on the canvas (slow). as lcass said, it's not possible to do this with good performance.

see if you can achieve similar results by just using "g" without any extra images.

 73 
 on: 2014-08-29 10:26:00 
Started by xtre - Last post by xtre
Yeah I use a canvas. I'm saying it adds to the pixel per cycle count. If I added an animated background that would get updated so thats like 800*600 screen + a 50*50 ball thats 24002500 things that needs changing from previous pixel colour to new. If I then add transparent things like a pause menu I would just about double that. Which I've tested and it gives me something like 30fps from 200+

Edit: I might not be doing what you said. Take a look at the GameWindow.java , its pretty straight forward. Is that what you mean?

And this is the Canvas
1  
2  
3  
4  
5  
6  
7  
8  
public class GameCanvas extends Canvas{
    public void addListeners(MouseInput mouseInput, MouseMotion mouseMotion, KeyboardInput keyboardInput){
        addMouseListener(mouseInput);
        addMouseWheelListener(mouseInput);
        addMouseMotionListener(mouseMotion);
        addKeyListener(keyboardInput);
    }
}

 74 
 on: 2014-08-29 10:18:17 
Started by xtre - Last post by basil_
so you paint using a buffered-image or a canvas.

on a canvas you should achieve superb performance for just a tiny bouncing ball.  Clueless

 75 
 on: 2014-08-29 10:16:19 
Started by xtre - Last post by xtre
you can get reasonable performance with javas Graphics2D and enough willpower.

dont use the default swing paint methods tho'. create something like this :

http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html

buffer-strategy is key.

BufferedStrategy is what I've been using. I can't seem to get very far with it since everything is processed via the CPU. I don't have a bad CPU either. It seems as if I'm pretty restricted and have to down scale everything to reduce the pixel per cycle. :/ If I have something like a button, I have to keep it updated for the highlight when hovering over it. I've tried updating when necessary but it just gets so messy and harder to work with. Say if I had an animated ball bouncing around the screen. I would have to update the balls pixels and the background pixels behind the ball. Which adds pixels per cycle. The problem is when animating. You have to remove the previous pixels before adding the next.

Thanks for the help!

 76 
 on: 2014-08-29 10:13:55 
Started by TehJavaDev - Last post by basil_
using Swing or other "timers" is fine. all of them use System.sleep(1) anyway.

just coming along with more stuff that you dont need for a game-loop. so it's not "clean" to use them but also totally fine.

on the other hand ... imo they're all wrong. a proper time/clock in java is a pain in the butt. my current approach comes more from measuring and profiling :

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  
public class clock
{
  private double hz; // desired fps
 private double interval; // nanosec
 
  private double delta; // in millsec  
 private double start;
  private double last;
  public double currentHZ; // current fps

  public clock(float desiredHZ)
  {
    this.hz = desiredHZ;
    this.inverval = 1.0e9/hz;
  }

  public void begin()
  {
    start = (double)System.nanoTime();
    delta     = ( start - last )*1.0e-6;
    currentHZ = 1000f/delta;
    last = start;
  }  

  // usually you would use this which yields the same result as using any java Timer class.
 public void end()
  {
    double now = (double)System.nanoTime();
    while(now - start < interval)
    {
      Thread.sleep(1);
      now = (double)System.nanoTime();
    }
  }

  // but to get it more precise we can use Thread.yield()
 public void endYielding()
  {
    double now = (double)System.nanoTime();
    double t;
    while(( t = now - start ) < interval)
    {
      // switch to expensive yield when remaining sleep time is less then a threshold.
     // ideally this would be 1 ms when sleep(1) would actually be a 1 ms sleep, what it never is.
     if(( interval - t ) < 1.4e7) Thread.yield();  // magic number 1.4e7, works for me tho. higher = more precise + higher cpu usage
     else Thread.sleep(1);
      now = System.nanoTime();
    }
  }
}


in a game loop :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void init()
{
  clock = new clock(45.33f); // 45.33
}

public void update()
{
  clock.begin();

  // paint things
 // swapBuffers

  clock.endYielding(); // sleep
 System.out.println("current FPS : " + clock.currentHZ);
}


i'm not able to program a more precise timer in java Sad

 77 
 on: 2014-08-29 10:00:52 
Started by pixelprime - Last post by Riven
Line 12 and 13 shouldn't be there, right? As that was the cause of the original problem.

Also, the 'on' field can be removed, as this can be implicitly determined with
wireMap[x][y].generatorID != 0
if you clear the IDs each tick, like you clear the 'on' property currently.

Last but not least: it's preferable to pass the generatorID as a parameter, removing the presumably static CURRENT_GENERATOR field.


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  
public void turnOffAllWires() {
   for(...)
      wireMap[x][y].lastGenID = 0;
}

public boolean isWireOn(int x, int y) {
   return wireMap[x][y].lastGenID != 0;
}

public void tick() {
   this.turnOffAllWires();

   for(Generator gen: generators)
      this.calcNextWireNode(gen.x, gen.y, gen.reach, gen.id);

   for(...)
      if(wireMap[x][y].type != NONE)
         wireMap[x][y].type = isWireOn(x,y) ? ON : OFF;
   
}

private void calcNextWireNode(int x, int y, int remaining, int genID)
   {
      // if we've hit our maximum recursion (distance) depth, finish here

     if (remaining == 0) return;
     
      // if we're not in bounds, finish here
     if (!tileInBounds(x, y)) return;
     
      // if this tile isn't a valid wire type, finish here
     if (wireMap[x][y].type == WireType.NONE) return;
     
      // don't process this if we're on the same generator ID
     if (wireMap[x][y].lastGenID == genID) return;      
      wireMap[x][y].lastGenID = genID;

      CURRENT_GENERATOR_OPS++;
     
      // test the four spaces around this tile
     calcNextWireNode(x-1, y, remaining-1, genID);
      calcNextWireNode(x+1, y, remaining-1, genID);
      calcNextWireNode(x, y-1, remaining-1, genID);
      calcNextWireNode(x, y+1, remaining-1, genID);
   }

 78 
 on: 2014-08-29 09:36:09 
Started by TehJavaDev - Last post by basil_
I had a few questions to ask, before trying to implement it into my game.

just add -Dsun.java2d.opengl=true and -Dsun.java2d.opengl=false or the other way around and see what it does to your game if you use Graphics2D. thats what it is about. either you use this API or you choose another, or what do you mean by "implement" ?

Will these, when set by 'System.setProperty()' truly enable hardware acceleration?
Can I enable/disable these property's on the fly (Example JComboBox for high/low quality rendering) and have them take effect instantly?

afaik, switching them at runtime is not supported, also using System.setProperty() is not always working, just use the jvm args and your set.

you can check what implementation is behind Graphics2D at runtime tho by looking into :

1  
2  
3  
4  
graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
primaryDisplay = graphicsEnvironment.getDefaultScreenDevice();
graphicsConfiguration = primaryDisplay.getDefaultConfiguration();
bufferCapabilities = graphicsConfiguration.getBufferCapabilities();


buffer-caps can be a D3DGraphicsConfig or WGLGraphicsConfig or some proxy implementation.

you can pass the caps directly into createBufferStrategy. at least now you know what's going on and can ask java the right stuff.

*edit*

just to complete this : after you create the bufferstrategy, you can grab the Graphics2D reference by :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
// an overwritten Canvas class around the constructor :

public class Canvas extends java.awt.Canvas
{
  [...]

  public Canvas()
  {
    BufferCapabilities caps = primaryDisplay.getDefaultConfiguration().getBufferCapabilities();
    createBufferStrategy(2,caps);
    buffer = getBufferStrategy();
    g = getDefaultGraphics((Graphics2D)buffer.getDrawGraphics()); // use this to paint your stuff
 }

  [...]
}


the downside of using Graphics2D like that, it's not possible to access the "data-buffer" of a "buffered-image" cos' there is simply none. so reading pixels and setting one by one is not gonna fly (too well).

reading pixels works, many to fail tho' :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
private void readBackBuffer()
{
  if(buffer instanceof BltBufferStrategy) // the buffer from above
 {
    BltBufferStrategy _buffer = (BltBufferStrategy)buffer;
    try
    {
      Method m = _buffer.getClass().getSuperclass().getDeclaredMethod("getBackBuffer");
      m.setAccessible(true);
      Object v = m.invoke(_buffer);

      if(v instanceof VolatileImage)
      {
          backBuffer = (VolatileImage)v;  // at least a Image, we get width height etc.
     }
    }
    catch(NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
    {
      // ...
   }
  }
}


"showing" such buffer should be something like :
1  
2  
3  
4  
public void swapBuffers()
{
  if(!buffer.contentsLost()) buffer.show();
}


finally you when you resize/reshape things you might want check if the buffers graphics2D instance changed and update yours in case.

 79 
 on: 2014-08-29 09:22:44 
Started by xtre - Last post by basil_
you can get reasonable performance with javas Graphics2D and enough willpower.

dont use the default swing paint methods tho'. create something like this :

http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html

buffer-strategy is key.

 80 
 on: 2014-08-29 08:44:33 
Started by xtre - Last post by Gibbo3771
Use the libraries however you like. I'm sure there's quite a few paid Android and iOS apps made with libGDX

Probably go as far as saying most of them will be coded using LibGDX, on Android anyway.

Pages: 1 ... 6 7 [8] 9 10
 

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!