newbie question (sort of)

hi there,

this is my first post here, so hi there :).

i’m working with java for some years (server & normal gui apps), did some c++ gaming stuff (mostly 3d, using opengl, irrlicht).

since i want to learn java2d and game programming (2d) in java in general, i started with a very simple experiment: a scrolling background. the render loop looks like this:


Graphics g;
for (int i = 0; i < 1000; i++) {                  
    g = (Graphics) strategy.getDrawGraphics();    
    g.drawImage(img, 0, 0, 640, 480, i, 0, i + 640, 480, null);    
    g.dispose();                              
    strategy.show();
}


it runs very slow. i use a java.awt.Window with a size of 640x480. what am i wrong about?

the second thing i want to ask:
what is (in general) a good way or technique to do these image stuff (scrolling and drawing sprites)? i do not want to use a third-party api like gage cos i want to learn how it works :).

ok, cu,
verence

Hi there, and welcome!

(to those who’ve been wondering: recent turmoil in my personal life, including moving to a new state and such, prevented my posting till recently.)

One thing of note is that you’re using an AWT window, and that will tend to cause some slowdown. There are some game loops you should look at in the ‘Shared Code’ forum that may be of use. See:

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=share;action=display;num=1036252001

This is a good resource too:

Its probably the same thing actually

Welcome back Gerghis!

[quote]This is a good resource too:
Its probably the same thing actually
[/quote]
actually i’m doing nearly the same in my little experiment (i took a look around here before i did post ;)). i was just wondering about scrolling a big background image around, which seemed a bit slow to me. hm, maybe i should use tiles for the background also. i was maybe expecting too much concerning speed :). i did a small j’n’r game ages ago in pascal on a 386dx40 in 320x200 and the background scrolling was very fast, but that was done by directly modifying the linear vga memory (mode13, $0a000 and y*320+x, these things i won’t forget for a lifetime :)).

as a start i wanted to create a similar game using my native tongue, because j’n’r games are so damn addictive :).

greets

Whats the size of your background image (kb)? Reducing the size of the image file or tweaking the Image loading could help.

[quote]Whats the size of your background image (kb)? Reducing the size of the image file or tweaking the Image loading could help.
[/quote]
it’s a 30kb jpeg. but doesn’t java convert it into it’s own data format when it’s loaded? and what do you mean by “tweaking the image loading”? i use getResourceAsStream() and a MediaTracker, but the loading time does not matter (display a fancy progress bar and the player is content :)).

greets

Try http://wiki.java.net/bin/view/Games/LoadingSpritesWithImageIO for better image loading. Also see if you can make the image file size smaller. There was a difference for me displaying a few 20 kb tiles and a few 10 kb tiles 85 frames a second. Also g.darwImage() is the way to draw it but the type of image you have (volatile, automatic, buffered) and weather your app is windowed or fullscreen makes all the difference.
Other things to think about: if your game is in a window then you cannot do page flipping or blitting, so your game will have performance problems.

Well if you are running with the Sun VM, try it with -Xcompile and see if that helps. if so then what you are seeing is the initial uncompiled code and aren’t running it long enough to see the compiled execution. (-Xcompile forces it to compile right away)

Also, I see you are using absolute sizes, not your image size, in your BLT. If you are trying to scale each time you BLT that will slow you down a lot. Pre-scale the image into a new image thats the right size and do an unscaled BLT on each update.

[quote]Other things to think about: if your game is in a window then you cannot do page flipping or blitting, so your game will have performance problems.
[/quote]
Sure about that?
If u make a BufferStrategy with BufferCapabilities like this for pageblitting:


BufferCaps = new BufferCapabilities(new ImageCapabilities(true),
                                                                                                                                                                                                              new ImageCapabilities(true),
                                                                                                                                                                                                              null);

flipContents - the contents of the back buffer after page-flipping, null if page flipping is not used

this works with windowed frames or what else u use, exclusiv fullscreen is just needed if u use a pageflipping strategy for flipContents.

Frame flipping is almost the same for windows or full screen in Java and you can do it with either.

If you flip frames for a window, then flipping is done with an (unsynchronized on Win32) BLT to the screen. If you flip frames in full screen then its done with video page flipping and may be scan synchronized depending on video driver settings and behavior.

Btw. the word “you” is spelled with 3 letters.

In general “kewl-dewd-speak” just annoys people here as its childish and hard to read.

Meow :slight_smile:

u doodz need 2 lern 2 spk dood. lol.

Kev

PS. Obviously I do too.

K, I will stop writing as if I am chat with friends in a MMORPG, will be more simply to read for the normal ones :slight_smile:

[quote]If you flip frames for a window, then flipping is done with an (unsynchronized on Win32) BLT to the screen. If you flip frames in full screen then its done with video page flipping […]
[/quote]
if you make your bufferstrategy with
createBufferStrategy(2)
then it will be done like that, but furthermore you can create the bufferstrategy with
createBufferStrategy(2, BufferCapabilities)
then you can select between page flipping and page blitting.

If we are already at Hardwarefeatures, which methods from Graphics2D (got with createGraphics from a BufferedImage which was got with createCompatibleImage) are Hardwareaccelerated?
All of them or only drawImage(). And if only drawImage is accelerated, what happens if I call (e.g.) drawLine()?
Is the line drawn on the BufferedImage in the VRAM, or is the (BufferedImage in) VRAM copied to SystemRAM, the line drawn and the result copied back to VRAM?

And what happens if I call setRGB() from the BufferedImage?

I hope my grammar is understandably … to read some english text and to write some is a a larger difference than I thought **

[quote]Meow :slight_smile:

u doodz need 2 lern 2 spk dood. lol.
.
[/quote]
D3wd, u rok! U r 2 l33t!

I can speak it too. I can also speak baby-talk. Doesn’t mean its appropriate here.

[quote]K, I will stop writing as if I am chat with friends in a MMORPG, will be more simply to read for the normal ones :slight_smile:

if you make your bufferstrategy with
createBufferStrategy(2)
then it will be done like that, but furthermore you can create the bufferstrategy with
createBufferStrategy(2, BufferCapabilities)
then you can select between page flipping and page blitting.
[/quote]
You can force fullscreen to BLT, but I don’t know why you would want to.

You cannot make windowed do anything but BLT because thats the nature of windows on a Win32 box, they are BLTd. There is no hardware windowing support on WIntel graphics cards.

In general the code picks the best method for your situation, so let it unless you have a really strong reason to want to defeat it.

I believe this depends on the abilities of your graphics card and driver. If you post questions like this to the Java 2D category you can get answers from people like Dmitri who actually work on that code inside of the JDK.

And your english grammer is just fine :slight_smile:

[quote]You can force fullscreen to BLT, but I don’t know why you would want to
[/quote]
mh … good question, I think the only reason would be to test the approximately FPS the programm has if it’s started under linux, without reboot to it and test.

For the ones which are interessted in my questions about the acceleration of Graphics2D methods I copied it to the Java2D forum, topic is named:
Which methods from Graphics2D are HW-accelerated?