Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Game Play & Game Design / I have a problem with VolatileImages?
|
on: 2010-05-11 23:42:02
|
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
| package MyGame;
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics2D; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.VolatileImage;
public class VImage {
private BufferedImage sourceImage; private VolatileImage volatileImage; private static GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); private static GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
public VImage(BufferedImage sourceImage) { this.sourceImage = sourceImage; }
public Image getVolatileImage() { if(Program.useVolatileImages){ if (volatileImage == null || volatileImage.validate(gc) != VolatileImage.IMAGE_OK) reload();
return volatileImage; } return sourceImage; }
public void setSourceImage(BufferedImage sourceImage) { this.sourceImage = sourceImage; }
private void reload() { do volatileImage = getVolatileImageFromImage(sourceImage); while (volatileImage.contentsLost()); }
public static VolatileImage getVolatileImageFromImage(BufferedImage image) { VolatileImage volatileImage = gc.createCompatibleVolatileImage(image.getWidth(null), image.getHeight(null), image.getTransparency()); Graphics2D g = (Graphics2D) volatileImage.getGraphics(); g.setComposite(AlphaComposite.Src); g.setBackground(new Color(0, 0, 0, 0)); g.clearRect(0, 0, volatileImage.getWidth(), volatileImage.getHeight()); g.drawImage(image, 0, 0, null); g.dispose(); return volatileImage; }
} |
This is what i wrote for handle volatile images like explained here: http://gpwiki.org/index.php/Java:Tutorials:VolatileImageTo test this VImage class i made a program that has 2 balls which can controllable form keyboard and added a 1920x1200 background image.It worked well with volatile images on window mode but when i toggle full screen mode fps dropped down immediately from 60 to 40 and sometimes i got heap space exception.After set useVolatileImages as false fps increased again and working perfect.Now i'm wondering am i doing something wrong?
|
|
|
|
|
2
|
Game Development / Game Play & Game Design / Re: Hi all,i have a question about my game engine?
|
on: 2010-05-07 21:52:10
|
hehhe  My bad. I was so sleepy when i was writing it.Thanks for catching that leaking,it was stacking me. In the meantime I did some changes too and i'm wondering is it ok now or am i doing somethings wrong again. here, new engine 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| package MyGame;
import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Toolkit; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.image.BufferStrategy; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; import MyGame.Input.Keyboard; import java.awt.GraphicsConfiguration; import java.awt.image.VolatileImage; import java.util.Timer; import java.util.TimerTask; import net.phys2d.math.Vector2f; import net.phys2d.raw.World; import net.phys2d.raw.strategies.QuadSpaceStrategy;
public class Engine extends JFrame {
protected float frameCounter = 0;
long StartTime = System.currentTimeMillis(); long EndTime; public long TotalGameRuningTime = 0; long elapsedTime = 0;
JPanel panel; Canvas Canvas; BufferStrategy BackBuffer;
ArrayList<IComponent> Components;
public int designX = 800,designY = 600;
protected static ResourceManager ResourceManager;
protected World world;
public static Keyboard keyboard;
private int FPS = 60;
protected boolean DebugMode = true; Toolkit toolkit; public int WorldStepCount = 5;
private static GraphicsConfiguration gc;
private boolean isFullScreen = false;
public Engine(){ world = new World(new Vector2f(0f,9.8f), 100, new QuadSpaceStrategy(100, 100)); ResourceManager = new ResourceManager(); panel = (JPanel) this.getContentPane(); panel.setLayout(null); Components = new ArrayList<IComponent>(); keyboard = new Keyboard(); Canvas = new Canvas(); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(designX,designY); this.setLocationRelativeTo(null); Canvas.setBounds(0, 0, this.getWidth(), this.getHeight()); Canvas.setIgnoreRepaint(true); panel.add(Canvas); Canvas.createBufferStrategy(2); BackBuffer = Canvas.getBufferStrategy(); toolkit = Toolkit.getDefaultToolkit(); gc = getGraphicsConfiguration(); }
public static VolatileImage createVolatileImage(int widht,int height,int transparancy){ return gc.createCompatibleVolatileImage(widht, height, transparancy); }
public void setFPS(int FPS) { this.FPS = FPS; }
public void SyncFPStoScreenRefreshRate(){ this.FPS = gc.getDevice().getDisplayMode().getRefreshRate(); }
public void setFullScreen(boolean fullScreen){ isFullScreen = fullScreen; gc.getDevice().setFullScreenWindow(isFullScreen ? this : null); }
public void toogleFullScreen(){ isFullScreen = !isFullScreen; gc.getDevice().setFullScreenWindow(isFullScreen ? this : null); }
protected void StartLoop(){ final Timer t = new Timer(); TimerTask tt = new TimerTask(){ @Override public void run() { GameLoop(); } }; t.scheduleAtFixedRate(tt, 0, FPS == 0 ? 1 : 1000 / FPS); } protected void GameLoop(){ EndTime = System.currentTimeMillis(); elapsedTime = EndTime - StartTime; StartTime = EndTime;
Graphics2D g2 = (Graphics2D)BackBuffer.getDrawGraphics(); Clear(g2); Update(elapsedTime); Draw(g2); if(DebugMode)DebugDraw(g2); Render(); CheckCanvasSize();
g2.dispose(); }
protected void Clear(Graphics2D g2){ g2.setColor(Color.black); g2.fillRect(0, 0, this.getWidth(), this.getHeight()); }
protected void Update(long elapsedTime){ TotalGameRuningTime += elapsedTime; for(int i = 0; i < WorldStepCount; i++) world.step(); for(IComponent c : Components) c.Update(elapsedTime); }
protected void Draw(Graphics2D g2){ frameCounter++; if(this.getWidth() != designX && this.getHeight() != designY){ double zoomX = this.getWidth() / 800.0; double zoomY = this.getHeight() / 600.0; g2.scale(zoomX, zoomY); } for(IComponent c : Components){ c.Draw(g2); } }
protected void DebugDraw(Graphics2D g2){ g2.setColor(Color.white); g2.drawString("Fps: " + CalculateFPS(), 30, 40); g2.drawString("Total Energy: " + world.getTotalEnergy(), 30, 60); g2.drawString("Delta Time: " + elapsedTime, 30, 80); g2.drawString("Total Frame Count: " + frameCounter + "s", 30, 100); g2.drawString("Total Game Time: " + TotalGameRuningTime / 1000 + "s", 30, 120); }
protected void Render(){ if(!BackBuffer.contentsLost()) BackBuffer.show(); toolkit.sync(); }
protected void CheckCanvasSize(){ if(this.getSize()!= Canvas.getSize())Canvas.setSize(this.getSize()); }
protected float CalculateFPS(){ return frameCounter * 1000 / TotalGameRuningTime; }
private int GetLostFrameCount(){ float fps = CalculateFPS(); return (int) (fps > 1000 / FPS ? 0 : 1000 / FPS - fps); } } |
And how can i catch when window bounds or size change except checking it on every loop to resize canvas?
|
|
|
|
|
4
|
Game Development / Game Play & Game Design / Hi all,i have a question about my game engine?
|
on: 2010-05-05 16:16:54
|
Hello, i just started to game programing with java, before that i was using c# Xna.I wrote my engine,and it is working fine.But i didn't use a canvas.You can see my game engine below.I'm wondering should i use canvas to draw (if so why?) or not? And i saw nobody preferred using timers to a while loop and my fps should be fixed 60 but it is more than 60 so am i doing something wrong ? 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
| package kutuyatopat;
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Transparency; import java.awt.image.BufferedImage; import javax.swing.JFrame; import java.util.Timer; import java.util.TimerTask; import java.util.Vector; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel;
public class Engine extends JFrame { int frameRate = 0; int frameCounter = 0;
long StartTime = System.currentTimeMillis(); long EndTime; long ElapsedTime = 0;
JPanel panel; JLabel label;
Vector<IComponent> Components;
GraphicsConfiguration gc;
public Engine(){ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(800,600); this.setLocationRelativeTo(null); panel = new JPanel(); this.add(panel); label = new JLabel(); panel.add(label); Components = new Vector<IComponent>(); gc = GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice().getDefaultConfiguration(); }
protected void Initialize(){ Timer t = new Timer(); TimerTask tt = new TimerTask(){ @Override public void run() { EndTime = System.currentTimeMillis(); long elapsedTime = EndTime - StartTime; GameLoop(elapsedTime); StartTime = System.currentTimeMillis(); } }; t.scheduleAtFixedRate(tt, 10, 1000 / 60); } void GameLoop(long elapsedTime){ Update(elapsedTime); Draw(); }
public void Update(long elapsedTime){ ElapsedTime += elapsedTime; if(ElapsedTime > 1000){ ElapsedTime -= 1000; frameRate = frameCounter; frameCounter = 0; } for(IComponent c : Components){ c.Update(ElapsedTime); } }
public void Draw(){ frameCounter++; String fps = "Fps: " + frameRate; BufferedImage i = gc.createCompatibleImage(this.getWidth(), this.getHeight(),Transparency.BITMASK); Graphics2D g2 = i.createGraphics(); for(IComponent c : Components){ c.Draw(g2); } g2.setColor(Color.white); g2.drawString(fps, 30, 40); g2.dispose(); ImageIcon ic = new ImageIcon(i); label.setIcon(ic); }
} |
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|