Grand Poeba
|
 |
«
Posted
2006-01-11 02:33:59 » |
|
Hey I am trying to convert a game to java. But i cant find the right components and layout to use. I trie with a jframe and internalframes but i cant use that any longer becaus i now use GraphicsDevice and displaymode to set it fullscreen and change the screenresolution Here's a screen: http://www.openttd.org/images/screens/1.pngCan anyone help? thx
|
|
|
|
|
Jeff
|
 |
«
Reply #1 - Posted
2006-01-11 05:04:44 » |
|
Looks like a tile based game.
I wouldnt use Swing at all (or any AWT widgets either). Id go Full Screen Exclusive AWT with active rendering r OGL and in either case just draw the dialogs as graphics each frame.
|
|
|
|
Grand Poeba
|
 |
«
Reply #2 - Posted
2006-01-11 10:36:15 » |
|
Looks like a tile based game.
I wouldnt use Swing at all (or any AWT widgets either). Id go Full Screen Exclusive AWT with active rendering r OGL and in either case just draw the dialogs as graphics each frame.
i had first made a version with xith3d as well but my tilemap looks al screwy and bad compared to my java2d version I made a post about it here http://www.java-gaming.org/forums/index.php?topic=11992.0Or do you meen something else with Full Screen Exclusive AWT with active rendering r OGL? Also which type of images should i use? image, bufferedimage or volatileimage?
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Grand Poeba
|
 |
«
Reply #3 - Posted
2006-01-11 13:32:13 » |
|
i have made somehing with full screen excluseive awt mode but it runs so slow :s This is my code so far 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
| package ttgj;
import java.awt.*; import java.awt.image.VolatileImage; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import javax.swing.*; import javax.imageio.ImageIO;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
public class Test { private JFrame mainFrame; private BufferStrategy bufferStrategy; private BufferedImage bi; private int ScreenWidth, ScreenHeight; private boolean Running = true; private int i = 0; private Tile[][] tiles = new Tile[25][25]; private Bus b; public static void main( String[] args ) { Test test = new Test( true ); System.exit( 0 ); } private static DisplayMode[] PreferredModes = new DisplayMode[] { new DisplayMode( 1024, 768, 32, 0 ), new DisplayMode( 1024, 768, 16, 0 ), new DisplayMode( 1024, 768, 8, 0 ) }; private DisplayMode bestmode( GraphicsDevice gd ) { DisplayMode[] modes = gd.getDisplayModes(); for( int j = 0; j < PreferredModes.length; j++ ) for( int i = 0; i < modes.length; i++ ) if( modes[i].getWidth() == PreferredModes[j].getWidth() && modes[i].getHeight() == PreferredModes[j].getHeight() && modes[i].getBitDepth() == PreferredModes[j].getBitDepth() ) return PreferredModes[j]; return null; } public Test( boolean fs ) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); DisplayMode dm = bestmode( gd ); if( dm == null || gd.isFullScreenSupported() == false ) fs = false; mainFrame = new JFrame( gc ); mainFrame.setLayout( null ); mainFrame.setIgnoreRepaint( true ); if( fs ) { mainFrame.setUndecorated( true ); gd.setFullScreenWindow( mainFrame ); gd.setDisplayMode( dm ); ScreenWidth = dm.getWidth(); ScreenHeight = dm.getHeight(); } else { ScreenWidth = 1024; ScreenHeight = 768; mainFrame.setSize( new Dimension( ScreenWidth, ScreenHeight ) ); mainFrame.setVisible(true); } mainFrame.createBufferStrategy( 3 ); bufferStrategy = mainFrame.getBufferStrategy(); bi = null; try { bi = ImageIO.read( new java.io.File("Images\\tile.png")); } catch (Exception e) { e.printStackTrace(); System.exit(0); } b = new Bus ( new Point (32,16), new Point (20*65,20*16)); ExecutorService threadExecutor = Executors.newCachedThreadPool(); threadExecutor.execute (b); threadExecutor.shutdown(); makeTiles (); while( Running ) { render((Graphics2D) bufferStrategy.getDrawGraphics()); try { Thread.sleep( 100 ); } catch( InterruptedException e ) {} } } private void render(Graphics2D g2d ) { mainFrame.repaint(); g2d.setColor( Color.black ); g2d.fillRect( 0, 0, ScreenWidth, ScreenHeight );
for (int i = 0; i < tiles.length; i++){ for (int j = 0; j < tiles[i].length; j++) { Point pos = new Point(tiles[i][j].getX(), tiles[i][j].getY()); g2d.drawImage(bi, pos.x, pos.y,mainFrame); } } g2d.drawImage(b.getAppearance(), b.getPosition().x, b.getPosition().y, mainFrame); i++; if( i > 4000 ) Running = false; bufferStrategy.show(); g2d.dispose(); } private void makeTiles () { int maxTileW = 70, mapW = maxTileW * 25; int x = (mapW - maxTileW) /2; int y = 0; for (int i = 0; i < tiles.length; i++) { for (int j = 0; j < tiles[i].length; j++) tiles[i][j] = new Tile (x + j * 32, y + j * 16); x -= 32; y += 16; } } } |
|
|
|
|
|
|
|
Ask_Hjorth_Larsen
Junior Member  
Java games rock!
|
 |
«
Reply #5 - Posted
2006-01-11 19:53:49 » |
|
Why are you repainting the frame when you setIgnoreRepaint(true) ? That should have no effect. Don't create new Point objects every time. You don't use them for anything. As for the performance, what are you using Thread pools for? Doing something with a bus!? Anyway, Transport Tycoon is a really nice game. Be sure to fix the money overflow bug when building bridges more expensive than 32 bits 
|
|
|
|
|
Grand Poeba
|
 |
«
Reply #6 - Posted
2006-01-11 20:23:19 » |
|
Why are you repainting the frame when you setIgnoreRepaint(true) ? That should have no effect. Don't create new Point objects every time. You don't use them for anything. As for the performance, what are you using Thread pools for? Doing something with a bus!? Anyway, Transport Tycoon is a really nice game. Be sure to fix the money overflow bug when building bridges more expensive than 32 bits  the repaint is gone my bus moves with the help of a thread (the code of bus isnt pasted I now drew the bi first on a volatileImage which speed things up but now the iamges arent tranparent anymore you can test it with this http://users.skynet.be/fa006997/Test.jari use tranparency.translucent option
|
|
|
|
|
Ask_Hjorth_Larsen
Junior Member  
Java games rock!
|
 |
«
Reply #7 - Posted
2006-01-12 17:48:43 » |
|
You shouldn't use a Thread to manage a bus. It's extremely much more demanding for the system, and the synchronization will cause trouble. Most games have a loop which updates the sprites (such as the bus) and graphics in turn. Multiple threads are useful for when you want to listen for network input, for example, and when you want heavy calculations to not stop your GUI from working. They are not efficient when you have to update everything continuously anyway.
|
|
|
|
|
Grand Poeba
|
 |
«
Reply #8 - Posted
2006-01-12 18:11:06 » |
|
You shouldn't use a Thread to manage a bus. It's extremely much more demanding for the system, and the synchronization will cause trouble. Most games have a loop which updates the sprites (such as the bus) and graphics in turn. Multiple threads are useful for when you want to listen for network input, for example, and when you want heavy calculations to not stop your GUI from working. They are not efficient when you have to update everything continuously anyway.
but if i use them i can set different sleeptimes to simulate the different speeds of the vehicles
|
|
|
|
|
CaptainJester
|
 |
«
Reply #9 - Posted
2006-01-13 13:42:58 » |
|
You shouldn't use a Thread to manage a bus. It's extremely much more demanding for the system, and the synchronization will cause trouble. Most games have a loop which updates the sprites (such as the bus) and graphics in turn. Multiple threads are useful for when you want to listen for network input, for example, and when you want heavy calculations to not stop your GUI from working. They are not efficient when you have to update everything continuously anyway.
but if i use them i can set different sleeptimes to simulate the different speeds of the vehicles That is definitly a good way to do it. Make all your calculations time based. Then you can just set a velocity for the vehicles and it will be more accurate. hread.sleep() has no real guaruntees about how it works except that it will sleep for at least the amount of time specified. The more vehicles you get the more screwed up it will become.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Grand Poeba
|
 |
«
Reply #10 - Posted
2006-01-13 14:04:25 » |
|
You shouldn't use a Thread to manage a bus. It's extremely much more demanding for the system, and the synchronization will cause trouble. Most games have a loop which updates the sprites (such as the bus) and graphics in turn. Multiple threads are useful for when you want to listen for network input, for example, and when you want heavy calculations to not stop your GUI from working. They are not efficient when you have to update everything continuously anyway.
but if i use them i can set different sleeptimes to simulate the different speeds of the vehicles That is definitly a good way to do it. Make all your calculations time based. Then you can just set a velocity for the vehicles and it will be more accurate. hread.sleep() has no real guaruntees about how it works except that it will sleep for at least the amount of time specified. The more vehicles you get the more screwed up it will become. It aint the vehicles who are the problem its the map I display around 900 tiles on 1 screen so he has to repaint trhem every frame which makes it very slow :s
|
|
|
|
|
Ask_Hjorth_Larsen
Junior Member  
Java games rock!
|
 |
«
Reply #11 - Posted
2006-01-16 19:39:13 » |
|
Stop abusing Threads! And you can minimize the drawing of tiles if you make a validation/invalidation system so tiles are only rendered once unless something is moving exactly at that location.
|
|
|
|
|
Grand Poeba
|
 |
«
Reply #12 - Posted
2006-01-16 22:50:51 » |
|
Stop abusing Threads! And you can minimize the drawing of tiles if you make a validation/invalidation system so tiles are only rendered once unless something is moving exactly at that location.
I have to redraw my screen every frame because i usefullscreen mode
|
|
|
|
|
Jeff
|
 |
«
Reply #13 - Posted
2006-01-17 00:22:25 » |
|
You don't *have* to redraw every frame.
But it shouldnt be hurting you either if you are doing it right. I did 50 animated characters on a free-scrolling tiled map at something better then 24fps ona laptop without any tuning when FullScreen and active rednering first became available. if anything its improved since then.
I'd profile and find out where your time is going.
|
|
|
|
Grand Poeba
|
 |
«
Reply #14 - Posted
2006-01-17 00:36:50 » |
|
You don't *have* to redraw every frame.
But it shouldnt be hurting you either if you are doing it right. I did 50 animated characters on a free-scrolling tiled map at something better then 24fps ona laptop without any tuning when FullScreen and active rednering first became available. if anything its improved since then.
I'd profile and find out where your time is going.
but i use double buffer so every thime i show it my frame gets cleared so i have to draw it all over again no?
|
|
|
|
|
|