Show Posts
|
|
Pages: [1] 2
|
|
2
|
Game Development / Newbie & Debugging Questions / Re: 2D Timing Mechanism
|
on: 2008-09-24 03:40:29
|
|
That's exactly what I was looking for. It appears it hasn't changed much. With Java 1.5+ is System.nanoTime a better choice for timer resolution than System.currentTimeMillis? Or does currentTimeMillis try to use the highest resolution timer available in more modern VMs?
Michael
|
|
|
|
|
3
|
Game Development / Newbie & Debugging Questions / 2D Timing Mechanism
|
on: 2008-09-23 05:13:43
|
|
I've seen a lot of tutorials on how to do 2D games. I've done a couple myself, but really haven't dug into it since Java 1.4 was in beta. Back then, the easiest thing to do to control a smooth framerate was a hack that involved an auto-adjusting thread that would sleep 1ms and "tick" every X milliseconds (where was 1000/frame rate).
If I was to start creating a 2D game with the core libraries, what would be the "latest and greatest" strategy to maintain a frame rate and draw to the screen? BufferStrategy and fullscreen? Another sleep/notify thread? I've seen a few, but I'm looking for the best to start with (and possibly an example) with the 1.6 library.
Thanks for any guidance or links you can propose!
Michael
|
|
|
|
|
5
|
Java Game APIs & Engines / Java 2D / Still having trouble with 90-degree turns...
|
on: 2004-01-29 23:40:17
|
|
I've managed to do a 180 degree flip using drawImage() but I can't seem to get 90 degrees. If I draw a single Pacman with his mouth pointing, say left, can't I use drawImage() to render that image with him pointing up, down, and right? Given that the sprite is 32x32:
drawImage(img, 0, 0, 32, 32, 32, 32, 0, 0, null) should turn a left-facing Pacman to a right-facing Pacman. I can't figure out how to get an up/down facing Pacman, or is this not possible and the image is only flipped?
Michael Bishop
|
|
|
|
|
6
|
Java Game APIs & Engines / Java 2D / Re: Mirroring an image
|
on: 2004-01-23 14:08:34
|
|
OK, so to sum up for best performance and efficiency:
Let's start with Pacman (since I'm working on it and it's a situation everyone should be familiar with). Pacman himself has nine frames of animation. I have all 9 of these frames in a PNG file that are loaded as compatible images. That should be all I need because with the drawImage() function, I can "flip" him 90 degrees in any direction and still retain hardware-acceleration.
Then we move to maze tiles. All the unique ones are stored on the hard drive as PNG files and again are loaded as compatible images. Again, they can be turned 90 degrees with drawImage(). The difference here is that some mazes are different colors. So in the "pause" between loading levels, I can use the Java2D API to do a color-swap and store the new images in memory.
For ghosts, it's trickier because they never flip 90 degrees; the only thing that moves are their eyes. Regardless, I have one strip of ghost animations. At load-time for the game, I take that strip, convert it into 4 different colors and associate each set of images to each ghost. It would be too expensive to swap colors and draw as necessary each frame, but that saves me from having 4 times as many ghosts stored on the hard drive.
Does this cover it, or am I missing something else? The gist of it is that color-swapping is done at load time; I will always have images of different color in memory. Rotation, or more specific, 90-degree turns do NOT need to be stored in memory because the particular flavor of drawImage retains hardware-acceleration unlike something like an AffineTransform. Correct?
Michael Bishop
|
|
|
|
|
7
|
Java Game APIs & Engines / Java 2D / Re: Mirroring an image
|
on: 2004-01-21 22:24:41
|
As far as changing a single color, here's what I came up with: 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
| public BufferedImage changeColor(BufferedImage inImage, Color oldColor, Color newColor) { int imageHeight = inImage.getHeight(); int imageWidth = inImage.getWidth(); int targetColor = oldColor.getRGB(); int[] imagePixels = new int[imageWidth * imageHeight];
PixelGrabber pixelGrabber = new PixelGrabber(inImage, 0, 0, imageWidth, imageHeight, imagePixels, 0, imageWidth); try { pixelGrabber.grabPixels(); } catch (InterruptedException iE) { iE.printStackTrace(); return inImage; } if ((pixelGrabber.getStatus() & ImageObserver.ERROR) != 0) { System.err.println("Pixel-grabbing aborted or errored."); return inImage; } int[] newPixels = new int[imageHeight * imageWidth]; Image newImage = thisToolkit.createImage(new MemoryImageSource( imageWidth, imageHeight, ColorModel.getRGBdefault(), newPixels, 0, imageWidth)); for (int y = 0; y < imageHeight; y++) { for (int x = 0; x < imageWidth; x++) { int currentPixel = imagePixels[x + y * imageWidth]; if (currentPixel != targetColor) { newPixels[x + y * imageWidth] = currentPixel; } else { newPixels[x + y * imageWidth] = newColor.getRGB(); } } } if (newImage != null) { newImage.flush(); } BufferedImage returnImage = createImage(imageWidth, imageHeight, inImage.getColorModel().getTransparency()); Graphics2D returnGraphics = returnImage.createGraphics(); returnGraphics.setComposite(AlphaComposite.Src); returnGraphics.drawImage(newImage, 0, 0, imageWidth, imageHeight, 0, 0, imageWidth, imageHeight, null); returnGraphics.dispose(); return returnImage; } |
Pretty self-explanatory; the createImage() call is a call to GraphicsConfiguration.createImage() and thisToolkit is a Toolkit. The only problem is that when you render your original image with anti-aliasing, you get different shades of a particular color. I need to modify that function to determine whether or not a color is a "shade" of the given color and apply the same shade of the new color. Aside from that, it works well. Thanks Onyx! Michael Bishop
|
|
|
|
|
8
|
Java Game APIs & Engines / Java 2D / Re: Mirroring an image
|
on: 2004-01-20 23:37:23
|
|
That's interesting, but the concept of working with MemoryImageSource and PixelGrabber is new to me. I get the idea, but what happens if you only want to manipulate a certain color? When ghosts are "energized" because Pacman ate a power pill, they go to blue ghosts with white faces. When they start flashing, they alternate between the above and WHITE ghosts with BLUE faces. Is there a way to say "Take this particular color and make it this other color"? Your example appears to be images with different shades of the SAME color; you don't have multiple colors.
I also assume that since you're painting it to a compatible Image, the result is hardware-accelerated or at least a candidate for it.
Thanks a bunch, this is pretty helpful.
Michael Bishop
|
|
|
|
|
9
|
Java Game APIs & Engines / Java 2D / Re: Mirroring an image
|
on: 2004-01-20 01:15:07
|
|
Well can we take this a step further? If this guy can face left and right with drawImage(), can he also face up and down from the same original image? The JDK docs say "scaled and flipped", but doesn't say anything about rotation. If you want your sprite to go 4 directions, it looks like you need two source images. One for up/down and the other for left/right.
A different question also begs palette-swapping. In my Pacman case, a ghost is blue when you eat a power pill. When he's getting ready to turn back to normal he flashes white. Do I need new images for that, or can I say "Turn all the blue pixels white"? And will it be hardware-accelerated?
Didn't mean to hijack a topic, but I figure my questions are in the same vein.
Michael Bishop
|
|
|
|
|
10
|
Java Game APIs & Engines / Java 2D / Re: Best way to load images.
|
on: 2003-12-18 01:42:18
|
|
Yes, so many questions, but so many good answers. Oddly enough, my current ImageManager class gave the user the ability to create a compatible Image and load an Image from a File. I was on the right track. Thanks a lot for the concise and solid answers to my many questions. That pretty much nailed it for me.
Michael Bishop
|
|
|
|
|
11
|
Java Game APIs & Engines / Java 2D / Best way to load images.
|
on: 2003-12-16 18:31:23
|
|
Hello all. If I want to store and load images for a game, what is the best way to go about it? I'm interested in the following:
Hardware-acceleration? (If I remember correctly, this rules out Toolkit.getImage()).
Format? I hear PNG is the best. I'm interested in basic, small sprites (32x32 or 64x64) with single transparency (BITMASK?)
Transparency? Last time I tested, for some reason, TRANSLUCENT was actually better and faster than BITMASK. This was around the release of 1.4.0.
Classpath? Using ImageIO and Toolkit, they want a URL or a filename. If you JAR up your entire package including images, it makes more sense to use getResourceAsStream(). I'd rather my images be found on the classpath than through absolute or relative file names.
So in essence, I'm looking for the best format to write an image in for gaming. Afterwards, I'd like to load that image and maintain transparency, have hardware-acceleration, and be found on the classpath.
Too much to ask? I'm hoping there's some kind of best practice. No third-party libraries either. I'm aware of GAGE and LWJGL, but this is a learning exercise for me and I'd like to do it using core libraries and extensions. Some of you might remember my Pacman project over a year ago, and I'm trying to improve on that again. The images were rendered at runtime using Arc2D and Rectangles, so I never worried about loading them before.
Thanks for any help!
Michael Bishop
|
|
|
|
|
13
|
Discussions / Miscellaneous Topics / Re: Anyone know where I can get a Sun T-shirt?
|
on: 2003-04-04 20:31:02
|
|
lol, I'm a large. I'll see what I can do about pictures. Working at a C# shop is going to be tough getting a partner in crime to take a picture. It's not until the end of May. 29th to be exact. If you'll seriously send me one, great, let me know Space Ghost, otherwise, I'll get one ordered off one of these other sites. Thanks for the posts people and yes, I'll try to get a decent picture.
|
|
|
|
|
19
|
Discussions / Miscellaneous Topics / Re: A Difficult One!
|
on: 2003-03-14 18:01:51
|
|
Hi Toby, Sent you an email. And yeah, I agree, doesn't necessarily have to be FFT, but it would be nice to model some kind of tactical RPG. Seems to be a successful genre lately.
Michael Bishop
|
|
|
|
|
20
|
Discussions / Miscellaneous Topics / Re: A Difficult One!
|
on: 2003-03-13 19:47:26
|
|
Wow, sign me up for the FFT team. I'm having my first playthrough (spread out over several months) and it's pretty fun. Just started Chapter 4.
Favorite console game: Judging solely by time spent, I'd have to go with the Hot Shots Golf series for PS/PS2.
Favorite PC game: I'd say the old Sierra adventure game series. Played a lot of King's Quest and Space Quest games to single one out.
I love RPGs, but I rarely play them more than once.
Michael Bishop
|
|
|
|
|
25
|
Games Center / Archived Projects / Re: New World Order: The Fortress
|
on: 2002-12-10 12:59:47
|
|
It ran pretty good under Windows XP for me. Really didn't look like there was any gravity or clipping, but the guy animated smoothly and the screen scrolled just fine. I was getting around 55FPS. I have a P3-733 with 512MB RAM, onboard video, and an ATI XPert 128 (Dual monitors). It's a work computer, so nothing's gaming material. I did notice that when it ran in fullscreen on my primary monitor, it moved all my other windows to the secondary monitor. Strange.
Michael Bishop
|
|
|
|
|
27
|
Java Game APIs & Engines / Java 2D / Re: Tricks to get smooth animation
|
on: 2002-11-04 14:43:12
|
|
Actually, in the Pacman example, I found that redrawing the screen was necessary. setClip() and dirty rectangles breaks a more important commandment; creating objects. setClip() creates a new rectangle object every time you call it and causes the code to slow down.
To avoid recreating images as much as I could, I used a couple layers:
- The maze (never changes) - The pills (same size as the maze, like an overlay. Obviously, this one changes when you eat a pill) - The sprites (Don't change, but they move)
Of course, the possibility exists that I could've used setClip() wrong and maybe there's better performance to be had, but with what I've done, it didn't turn out well and I discovered the Rectangles being created in a profiler.
Michael Bishop
|
|
|
|
|
28
|
Game Development / Shared Code / Re: Sleep based timer hack
|
on: 2002-11-01 12:57:02
|
|
This is all the code I used to get it running at "fps" fps.
frameTimer = new SleepTimer(); frameTimer.setDelay(1000 / fps); frameTimer.setAutoCorrection(true, fps); frameTimer.startTimer();
I've never had it throw an error before. Then again, I haven't run it over 60 frames-per-second.
Michael Bishop
|
|
|
|
|
30
|
Java Game APIs & Engines / Java 2D / Re: Simple game framework?
|
on: 2002-10-24 14:11:08
|
|
1. The history of that particular Pacman application is that it was simply my attempt at learning Java Game Programming. I'm the author, although the input from this community has been tremendous. In the archives, there are an excessive number of posts. If you're asking about the author of the class that handles timing, that would be "thelorax", another user on this board.
2. Frames and ticks are supposed to be one and the same but it doesn't always happen. I'll explain.
- Frames are the number of frames the application has actually drawn.
- Ticks are the number of frames the application is supposed to have drawn. When it's time to draw a frame. the timer class "ticks" and increments its tick count. The application waits for the next tick to maintain the given frame rate.
- When the application is ready to draw the next frame, it examines the frame count versus the tick count. Due to garbage collection or otherwise, sometimes a frame or two is skipped. To compensate for that, I'll do the logic loop until it's caught up again.
Take a good look at the run() method in the GameBoard class. It does something like this:
- Run the game logic (tickCount - frameCount) times. - Draw the offscreen buffer. - Wait for the next tick. - Draw the screen.
Michael Bishop
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|