Writing

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Showing posts with label 2d engine. Show all posts
Showing posts with label 2d engine. Show all posts

OpenGL and D3D Pipeline

OpenGl Pipe versus D3D Pipe:

On my desktop, OpenGL showed no performance increase. However, turning off D3D gave a huge speed hit, dropping FPS by 30%.

-Dsun.java2d.d3d=false

Unlike "-Dsun.java2d.opengl=True" where the capital "T" in true lets us know if it is initialized in the IDE, -Dsun.java2d.d3d=True doesn't let us know if it is connected. I have Direct X 9 on both of my test machines. Oddly only my desktop takes the speed hit when I turn off D3d. If I turn it off on my laptop, frame rate remains the same.

So I did an interesting test on my desktop, turning off D3D and turned on OpenGL and found no difference in FPS.

-Dsun.java2d.d3d=false -Dsun.java2d.opengl=True

On my laptop, OpenGL failed to load completely. Researching the issue I found that Intel chip set and OpenGL rarely work together.

I think the only problem with my testing is I'm not using a game that uses heavy graphics yet, only a few 100 sprites. I will have to revisit the test once I start pushing the graphics card more.

Optimizing Java 2D for a game

I like Java 2D. 3D engines seem overwhelming for me at the moment despite the fact that the pros say OpenGL or an OpenGL wrapper is the way to go. I'm a firm believer that 2D art still rocks hard with the right artist. The "Mona Lisa" was not tossed out of the museum when "The Thinker" statue was added to the collection. The question is can I do a large screen implementation that is fast enough for most computers. I have my doubts that it will be fast enough but I'll let the numbers decide.

Goal:

  • 100 FPS with a reasonable animation smoothing delay of 10 ms.
  • Screen size 1024 x 768 and greater, hopefully 1900x1200.
  • 32x32 tiles with multiple layers.
  • One background
  • 2 Parallax backgrounds.
  • Mode: Windowed. If Linux supports fullscreen, I'll test that as well.
  • Do a little bit of alpha blending

Ideas for improvement:
  • Force OpenGL and D3d Pipelines
  • Test Timers
  • Test VolatileImage
  • Optimize Code
  • Try different blitting techniques.
  • Try different art formats, png, jpg, bmp, gif with different depths.

Test Environments


a) Desktop Radeon 3800 with 512mb of ram. 2 gigs memory. Windows xp pro.
b) Laptop Intel Chip set. 1 gig of ram Windows xp pro.








Another




I need to sleep my game loop for time than the processing time of the game loop.


Changing of my original goal of 100 FPS was quite surprising to me. Over the years, several gaming buddies and I have had silly competitions about who gets the most FPS out the game we play. Basically, FPS is not a true
representation of how smooth the graphics really are.

Problems

Jitter
System.currentTime() & nanoTime not accurate on Windows
Thread.sleep() not accurate on Windows, probably related to above.
Third Party timers inaccurte.
Bugs in Java

Jitter: Jitter is basically the varying time lag between paints. It is caused by several things, bugs, poor resolution of timers, Windows issues, and implementation.





I was System.currentTimeMillis() for my timer, thinking it really doesn't matter that much. I began playing with it and noticed if I set my delay between 5-13 milliseconds I saw no change. Weird. I then moved the delay to 14 and presto my FPS went from 65 to 70. I assume what this means is the inaccuracy of the timer was causing a delay in display. To test this I'm going to add a third party timer.

I tried using the GAGETimer a free timer. With the same resolutions of delay, I was getting about 1% improvement. Ok that is a bit strange, maybe a math difference.

Then I plugged in the number 10 a few times. I got totally random results.

I'm using the standard Kevin Glass setup with a delta with non-recommended System.currentTimeMillis

try { Thread.sleep(loopTime+10-System.currentTimeMillis()); } catch (Exception e) {}

Results

1) Framerate changes drastically everytime I run the test even though all I'm doing is drawing tiles and backgrounds. Sometimes it is 65 fps and others 94.
2) For some reason setting the delay between 9-12 seems to have little effect on the frame rates.

Question: Why does the frame rate vary everytime I run it?

Okay so now I throw out the System.currentTimeMillis and go to the Gage timer.

try { Thread.sleep(loopTime+10-SystemTimer.getTime()); } catch (Exception e) {}

I still get random results but I got a slight improvement by not using 10. 9 gave me an awesome frame rate. 105! woot this was the number i wanted. Even 11 gave me frame rates in the high 80s. 10 gave me 65.

Lets see if I can do better with the nanotimer.



Test number 1:














import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
*
* @author Darrin Adams
*/
public class Speed extends Canvas{

private BufferStrategy bsStrategy; // fast flips
private boolean bRunning = true; // main loop
private static final int CANVAS_X = 1024;
private static final int CANVAS_Y = 768;

public Speed (){
//  Frame Setup
JFrame frame = new JFrame("Speed Test");
frame.setBounds(0, 0, CANVAS_X, CANVAS_Y);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {  // a new way to close
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

// Panel added
JPanel pane = new JPanel();
pane.setBounds(0, 0, CANVAS_X, CANVAS_Y);
pane.setLayout(null);
frame.add(pane);

// this Canvas added
setBounds(pane.getBounds());
pane.add(this);
setIgnoreRepaint(true); // active painting only on canvas, does this apply to swing?
requestFocus();  // get focus for keys

// make it fast with this strat
createBufferStrategy(2);
bsStrategy = getBufferStrategy();
}

public void loopGame(){
// Timer and FPS
long loopTime = System.currentTimeMillis();

int fps=0;
int frames=0;
long startTime = System.currentTimeMillis();

// TODO remove this external timer for weapons
long shotTime = System.currentTimeMillis();

while (bRunning) {
long timeLapse = System.currentTimeMillis() - loopTime;  // used to move objects smoothly
loopTime = System.currentTimeMillis();

Graphics2D g2d = (Graphics2D) bsStrategy.getDrawGraphics();

// Wipe
g2d.setColor(Color.black);
g2d.fillRect(0, 0, CANVAS_X, CANVAS_Y);

// fps counter
if ((System.currentTimeMillis() - startTime) > 1000){
   startTime = System.currentTimeMillis();
   fps = frames;
   frames = 0;
}
++frames;
g2d.setColor(Color.green);
g2d.drawString("fps: " + fps, 5, 30);


// finally, we've completed drawing so clear up the graphics
// and flip the buffer over
g2d.dispose();
bsStrategy.show();

// Attempt to sleep for a consistant time. Smooths out bumps in processing.
try { Thread.sleep(loopTime+10-System.currentTimeMillis()); } catch (Exception e) {}
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Speed game = new Speed();
game.loopGame();
}

}







Reference:

2d tests using pixel draws not quite what I'm doing but a good start
http://www.yov408.com/javagraphics/javagraphics.html

How to open the Opengl Pipe and logs. Also include D3d.
http://java.sun.com/j2se/1.5.0/docs/guide/2d/flags.html


http://weblogs.java.net/blog/campbell/archive/2004/11/behind_the_grap.html

Explanation of VolitileImages
http://www.javalobby.org/forums/thread.jspa?threadID=16840&tstart=0

Ibm's example of frame double buffered
http://www.ibm.com/developerworks/java/library/j-mer04293.html

Finding the right 2D Engine

I spent alot of time getting Java 2D to work for my next game. There were some amazing tutorials out there, especially those done by Kevin Glass at http://www.cokeandcode.com/. However, when I did a test rendering a large screen (1024 x 768) with four parallax layers my FPS dropped to about 20. With map display optimization I could increase it to 42 but still I wanted to do a 2d engine 1900 x 1200 with a good frame rate of lets say 100. My home computer runs a radeon 3800 and my old work lap top a couple of years old couldn't even achieve 10 fps. So 40 on a pretty robust machine was just not enough.




What are the options?

A. Use a third party Game Maker.

B. Use a third party Engine.

C. Write my own Engine.

D. Optimize Java2d Engine.

Before looking into all three of these options, I should probably decide what I want. Lets start with the dream list.


Dream 2D Engine

Fast Frame Rate at high resolutions 100+
Full screen, windowed or browser embedded
Multiple OS Windows, Mac, Linux.
Sprite Engine including a sprite manager database
Graphic alpha blending, rotation, gamma controls
Map Manager
Map Editor
Flexbile maps: Baldur's gate style, Zelda style 2d, Mario style sidescrolling, Diablo isometric.
Parallax Scrolling
Scripting Extensible for adding maps, levels, art.
Widgets for the GUI
Sound and Music
Database reader/writer
Logger
Networking
Package maker for art and sound
Pathfinding
Collision Detection
Math
Physics
Timer

Not too much to ask for is it? :P

Other Requirements
Low cost, prefer free.
Great tutorials.
Published games
Maintained and active.
High quality community.

Coding Language

There are only a few choices for coding games and each has its advantage.

  • C++
  • C#
  • Flash (Actionscript)
  • Java
  • Others

C++ is the language of game makers. It is an old language and is quite difficult for the non-professional programmer. The memory management is a real pain for myself. I've done a bit of coding in it--half a game--and just ended up getting a bit frustrated. I also do not like Microsoft's implementation of web launch. That being said, it is still king of the game languages and most coders can make it sing and dance. It can be ported to other platforms but generally speaking there are issues. Decision: Rejected.

C# looks really good. It solved MFC mess and handles the memory management much better. However it is tied strongly to windows and is not as portable as others. The IDE is reasonable but still costs. Decision: Rejected mostly because of IDE and portability.

Flash is a winner. It is on every system with an Internet connection. It is popular with great tutorials. Publishing looks very easy. There are third party agents that automatically find you place to generate advertising revenue. The downside is that the IDE is expensive and proprietary. Another downside is that Flash is not suited for those ginormous, pipe-dream games that require major networking and 3D. This can be a pretty important decision depending on the games you are building. I've seen some very successful indie game makers build one game over and over again improving it with each version. The same can be done with Flash of course but it will never be a triple AAA title (see Fantasy Tactics and Sword and Sandal). Decision: I'm going to hire someone to port a game to test the market. Revenue seems almost exclusively ad based although some do have license locks.

Java should be a winner. Why it is not an instant winner seems to mostly come from a false rumor about it being slow. Yes it is a bit slower than C++ but the reality is any game you love to play can be written in it. The web launch is slick. The IDEs Netbeans and Eclipse are both free. I'm partial to Netbeans. It has applets. How cool is that? However I did notice on a recent visit to the Yahoo game's portal that it is almost all Flash whereas 3-4 years ago there was a healthy mix of Java. David Brackeen wrote a slick applet game called Milpa as well as a good java book called Developing Games in Java. The game is similar to some popular Popcap games but he still made a few thousand on it. Decision: For better or worse, this is the language of choice for now mostly because of price, great community, web integration, portability, and ok tutorials.

Others include Delphi and VBasic. I programmed a Simon Says game in Delphi, selling one copy! It is quite usable but at the time I used it the graphic libraries were terrible. Both can work with OpenGL and DirectX. I'm not sure about the portability issues. They are quite old languages. IDEs are free which is nice. Decision: Rejected for portability and lack of web launch.


Before I pare down the dream list alot, let me quote some forum wisdom.

You cannot create a generic network engine/library. There's no shortcut. You have to understand the problem and write the solution. Anyway, stop putzing around and dive right in. You'll never figure this out if you keep waiting for a magical library to fix your problems for you. Backov writes http://forums.indiegamer.com/showthread.php?t=14146





I took a look at some cool scripting engines like RPGVX which is extremely enticing. It comes with an editor, Ruby, artwork, sound, just about everything you need to actually make an old schoool RPG. The two major issues I had with it was the screen size is limited to 640x480 (note the older version RPGXP which has better artwork is even smaller screensize) and the second was that I would have to master Ruby, the scripting language.

Game Creators

http://creators.xna.com/en-US/


BlitzMax

TGE

TGB

RPG Maker





C++ Engines/Graphics

These have been eliminated because they use C++ which is an older language and mostly focused on the Widnows platform.

Allegro

SDL


http://developer.popcap.com/

http://hge.relishgames.com/

http://www.jenkinssoftware.com/