Autechere
Junior Newbie
|
 |
«
Posted
2011-04-10 20:36:57 » |
|
Hey everyone, im a second year computer science major and im interested in beginning game development. My university offers a concentration in game design to computer science majors and I would like to try and make a game before I decide if its for me or not.
I am pretty fluent in java at this point, so I might as well use java to start out with. What I lack is the knowledge of API's for game development. What I would like to get from you all here is: where do I start? I'd just like some broad advice as to where to concentrate my efforts as a novice game programmer.
|
|
|
|
kappa
|
 |
«
Reply #1 - Posted
2011-04-10 21:09:13 » |
|
Just decide if you'd like to go 2D or 3D then pick one from here.
|
|
|
|
ReBirth
|
 |
«
Reply #2 - Posted
2011-04-11 11:11:30 » |
|
Decide too what library you will use 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Maximus
Junior Newbie
|
 |
«
Reply #3 - Posted
2011-04-14 03:20:02 » |
|
My first post  . For simple 2D games, graphically AWT and Swing is all you're gonna need. Swing to have the main window(jpanel) to draw in, AWT for manipulating transforms and drawing sprites. http://download.oracle.com/javase/6/docs/api/
|
\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020\u0022\u004D\u0061\u0078\u0069\u006d\u0075\u0073\u0022\u0029\u003b
|
|
|
ra4king
|
 |
«
Reply #4 - Posted
2011-04-14 04:15:36 » |
|
There is no point in drawing in JPanel. JComponent is suggested for very simple games, Canvas is otherwise recommended.
|
|
|
|
ReBirth
|
 |
«
Reply #5 - Posted
2011-04-14 12:57:24 » |
|
I prefer canvas if it still on java2d. Double buffered and I feel it (in my old PC) pretty fast.
|
|
|
|
Z-Man
|
 |
«
Reply #6 - Posted
2011-04-17 16:17:17 » |
|
There is no point in drawing in JPanel. JComponent is suggested for very simple games, Canvas is otherwise recommended.
I picked up Killer Game Programming in Java a day ago and have been reading it. KGPJ draws on a JPanel by drawing to an image then drawing the image to the JPanel (Double buffering I think?) from what I have read so far the book seems to be a little old. So is there a better way of drawing for a 2D game?
|
|
|
|
ra4king
|
 |
«
Reply #7 - Posted
2011-04-17 20:50:06 » |
|
Drawing on an image, then on a JPanel?!? Now that is not efficient at all since a JPanel is already double-buffered. However, to get you used to this habit, start using a Canvas. If this is going to be a desktop app, then there is no need to extend anything. All you have to do is create a Canvas object, add it to the JFrame, and to double buffer, do the following: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Canvas canvas; BufferStrategy strategy;
canvas = (Canvas)jframe.add(new Canvas()); canvas.createBufferStrategy(2); strategy = canvas.getBufferStrategy();
do{ do{ Graphics2D g = (Graphics2D)strategy.getDrawGraphics(); g.dispose(); }while(strategy.contentsRestored()); strategy.show(); }while(strategy.contentsLost());
|
|
|
|
|
ReBirth
|
 |
«
Reply #8 - Posted
2011-04-18 00:16:25 » |
|
It's my first time I see this 1
| canvas = (Canvas)jframe.add(new Canvas()); |
any different from 1 2
| canvas = new Canvas(); jframe.add(canvas); |
??
|
|
|
|
ra4king
|
 |
«
Reply #9 - Posted
2011-04-18 04:03:14 » |
|
Both are the same. The add() method returns the same object you added. EDIT: Fixed my code a little: strategy.contentsRestored() is supposed to be inner do while loop 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ReBirth
|
 |
«
Reply #10 - Posted
2011-04-19 00:31:23 » |
|
Both are the same. The add() method returns the same object you added.
I just know that  thanks
|
|
|
|
ra4king
|
 |
«
Reply #11 - Posted
2011-04-19 03:11:58 » |
|
Glad to help 
|
|
|
|
Z-Man
|
 |
«
Reply #12 - Posted
2011-04-24 03:21:42 » |
|
EDIT: I just realized I asked the first question about KGPJ  I guess I didn't understand the answers. I also feel stupid for not realizing that until now  I'm not sure if I should create a new thread for this... but it kind of relates to a question already asked here. I too picked up Killer Game Programming in Java (KGPJ) and used it's "double buffering" strategy. Now I am trying to convert it's double buffering strategy which is: "Have a global Graphics object and a global Image. Have a method called renderGame() that gets a Graphics object from the global image and does all the game drawing on the that Graphics object. Then there is a method called paintGame() which draws the Graphics object from the Image onto the Graphics object of a JPanel (I use a JComponent). So drawing on an image then drawing the image to the screen." My problem is I am having trouble converting this to the suggested Canvas method. Does anyone have tips? One of the problems that comes to my mind is that I handle input through the JComponent with a MouseAdapter and a KeyAdapter, can the same thing be done with a Canvas?
|
|
|
|
ReBirth
|
 |
«
Reply #13 - Posted
2011-04-24 03:55:52 » |
|
I had left canvas long time so see if I can recall... first, you maybe confused how to get the big image (for your draw) from canvas right? 1 2 3 4
| Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.dispose(); strategy.show(); |
you get "strategy" object from getBufferStrategy();. Second, canvas can work with those adapters.
|
|
|
|
Z-Man
|
 |
«
Reply #14 - Posted
2011-04-24 04:08:05 » |
|
Thanks that helped, I was a little confused by the do-while part, your explanation helped. It's also nice to know that I can use the adapters.
|
|
|
|
biro
|
 |
«
Reply #15 - Posted
2011-04-24 18:10:00 » |
|
Hello, I have a question to the above posted code.
}while(strategy.contentsRestored());
For what does this loop wait?
biro
|
|
|
|
|
Z-Man
|
 |
«
Reply #17 - Posted
2011-04-24 19:35:34 » |
|
So for a 2D game in java would you recommend extending Canvas and controlling the looping, drawing and logic there. Then have a main method somewhere create a JFrame and add your custom Canvas to it. Or is there another method you would suggest.
|
|
|
|
ra4king
|
 |
«
Reply #18 - Posted
2011-04-24 21:35:41 » |
|
That's my recommended method 
|
|
|
|
ReBirth
|
 |
«
Reply #19 - Posted
2011-04-25 03:17:09 » |
|
@Z-Man I use that too  you can also have a canvas -> JPanel -> JFrame too so you can set the preferred size.
|
|
|
|
biro
|
 |
«
Reply #20 - Posted
2011-04-25 15:19:27 » |
|
Hello, today I wrote a short programm, to play around with canvas and a little with the MouseListener. For the Graphic part I used ra4kings given code or rather the one from http://ra4king.is-a-geek.net/javadocs/java/awt/image/BufferStrategy.html. My Programm works fine, as long as I have a while(true) loop to refresh my frame contents. Buf if I remove the loop and add render() (rander is the method that redraws my canvas, it's also the one with the while(true) loop from before) tags, in exchange, to the methods that modify my graphicoutput, then I only see my "image" for a split second and then I only see gray until I do something that activates the render method again. Hope you can help me with the short Explanation I gave you, if it's not enough tell me and I'll add the code biro Edit: It's me again. I got my Programm so far, that it shows me the correct screen after I drag my mouse. But before I do that (in between the time after my startup and before the first mouseDragged() method starts) I only see a gray screen, except for a short "blinking" in which I believe to see the correct screen. Here the important parts of the code: 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
| public synchronized void render() { BufferedImage g = null; BufferedImage s = null; try { g = ImageIO.read(new File("green.png")); s = ImageIO.read(new File("schwarz.png")); } catch (IOException ie) { ie.printStackTrace(); System.exit(0); } do { do { Graphics graphics = strategy.getDrawGraphics();
for (int x = 0; x < xMax(); x++) { for (int y = 0; y < yMax(); y++) { BufferedImage draw = null; int drawx = this.activetilex+x; int drawy = this.activetiley+y; if (drawx >= this.tilesx || drawy >= this.tilesy || drawx < 0 || drawy < 0) { draw = s; } else { switch (feld[drawx][drawy].getTyp()) { case 1: draw = g; break; default: draw = s; } } if (x == 0 && y == 0) { BufferedImage newdraw = draw.getSubimage(this.activex, this.activey, this.tilewidth-this.activex, this.tileheigth-this.activey); graphics.drawImage(newdraw, 0, 0, null); } else if (x == 0) { BufferedImage newdraw = draw.getSubimage(this.activex, 0, this.tilewidth-this.activex, this.tileheigth); graphics.drawImage(newdraw, 0, y*this.tileheigth-this.activey, null); } else if (y == 0) { BufferedImage newdraw = draw.getSubimage(0, this.activey, this.tilewidth, this.tileheigth-this.activey); graphics.drawImage(newdraw, x*this.tilewidth-this.activex, 0, null); } else { graphics.drawImage(draw, x*this.tilewidth-this.activex, y*this.tileheigth-this.activey, null); } } } graphics.dispose();
} while (strategy.contentsRestored());
strategy.show();
} while (strategy.contentsLost()); } |
This is my render method. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private Canvas board; private BufferStrategy strategy; private MapMover mouselistener; private GuiListener listener; private int tilewidth = 50; private int tileheigth = 50; private int showwidth = 800; private int showheigth = 600; private int activetilex = 0; private int activetiley = 0; private int activex = 0; private int activey = 0; private int tilesx = 25; private int tilesy = 15; |
Here the class attributes, these also have getter and setter methods! 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public Gui() { super(); initField(); listener = new GuiListener(this); initGUI(); board.createBufferStrategy(2); this.strategy = board.getBufferStrategy(); mouselistener = new MapMover(this); board.addMouseListener(mouselistener); board.addMouseMotionListener(mouselistener); this.setVisible(true); render(); } |
And here my startup routine Like I said the render method link in the startup method fabricates a short correct screenblink and than the screen get's gray agains. Because the MouseListener and MouseMotionListener is doing it's job correct I won't post it now, if you ant it pls tell me. Many thanks for your help biro
|
|
|
|
Z-Man
|
 |
«
Reply #21 - Posted
2011-04-25 21:22:20 » |
|
@ReBirth I think that Canvas has a setPreferredSize() method as well so if you wanted you could probably cut out the JPanel there. Unless of course there are other things you need the JPanel for.
|
|
|
|
ra4king
|
 |
«
Reply #22 - Posted
2011-04-25 21:58:10 » |
|
You need to start a new thread where the run method has the game loop. So change render() to run(), implement Runnable, and start a new thread at the end of the constructor.
|
|
|
|
biro
|
 |
«
Reply #23 - Posted
2011-04-26 07:10:54 » |
|
Hello, thanks for your answer. But how does it help to implement a new Thread for the drawing? because I don't want an endless loop to redraw my graphic content, because it eats to many ressources (with the endlessloop before which is like a new thread, one of my cores was 100% used). Hope you can explain that to me, why I should implement a thread there^^
biro
|
|
|
|
ra4king
|
 |
«
Reply #24 - Posted
2011-04-26 08:18:19 » |
|
You need a constant FPS, which will not use the CPU much. That's what all games use, an endless game loop with a constant refresh rate and it updates the game logic and graphics.
|
|
|
|
Sinuath
|
 |
«
Reply #25 - Posted
2011-04-26 08:29:16 » |
|
you make it sound so simple... been reading a lot of tutorials and that line kind of made everything come together and make sense.. the idea of a video game seems really simple now
|
|
|
|
biro
|
 |
«
Reply #26 - Posted
2011-04-26 09:20:55 » |
|
Hello, thanks for your answers, but in my case I don't need a constants update loop. I only want to update the screen, when it's need, which is after you drag the screen or you resize the frame and after you start the programm. So why should I use a thread, which burns so many many resources, and when I need a thread, how much should I reduce the the framerate (I believe I should do it with the sleep() command, so how many millisecs. should I let the thread sleep?).
Thanks a lot for your answers biro
|
|
|
|
Mads
|
 |
«
Reply #27 - Posted
2011-04-26 13:44:35 » |
|
Hello, thanks for your answers, but in my case I don't need a constants update loop. I only want to update the screen, when it's need, which is after you drag the screen or you resize the frame and after you start the programm. So why should I use a thread, which burns so many many resources, and when I need a thread, how much should I reduce the the framerate (I believe I should do it with the sleep() command, so how many millisecs. should I let the thread sleep?).
Thanks a lot for your answers biro
If you want something that is not a still image, you need an update loop. You're just drawing pictures fast, so it looks like things move. If you don't update you have a picture without any animation.
|
|
|
|
biro
|
 |
«
Reply #28 - Posted
2011-04-26 13:56:28 » |
|
Hello, my problem is not the animation, at the moment I have no real animation, I only need to update the field, when a user gives a command, that changes the world in some aspects. Then should the world be refreshed. And for that I don't need a loop if I'm not wrong, the problem is, that if I don't use a loop and draw the pictures on the screen I see them for a few milliseconds after these few milliseconds I see a gray window again, and I want to know why. I know that when I want to add animations I need a constant loop, that refreshs my screen, but at the moment that is not needed But let's look a bit in the future, to a time when I add animations or some other feature that needs a graphic refresh loop, than how should I drossel it, so that it won't eat 100% of my CPU power? My guess is that I should use sleep() for that, but how do I calculate the right amount of milliseconds my thread should sleep, so that my cpu doesn't need to create such a high output and on the other hand, that my "animation" is still fluid? Please don't forget to answer the question about doing it at first without a loop and why the correct screen is only shown for a split second. Greetings biro
|
|
|
|
Dx4
|
 |
«
Reply #29 - Posted
2011-04-26 14:55:45 » |
|
Hello, my problem is not the animation, at the moment I have no real animation, I only need to update the field, when a user gives a command, that changes the world in some aspects. Then should the world be refreshed. And for that I don't need a loop if I'm not wrong, the problem is, that if I don't use a loop and draw the pictures on the screen I see them for a few milliseconds after these few milliseconds I see a gray window again, and I want to know why. I know that when I want to add animations I need a constant loop, that refreshs my screen, but at the moment that is not needed But let's look a bit in the future, to a time when I add animations or some other feature that needs a graphic refresh loop, than how should I drossel it, so that it won't eat 100% of my CPU power? My guess is that I should use sleep() for that, but how do I calculate the right amount of milliseconds my thread should sleep, so that my cpu doesn't need to create such a high output and on the other hand, that my "animation" is still fluid? Please don't forget to answer the question about doing it at first without a loop and why the correct screen is only shown for a split second. Greetings biro use double buffering.
|
|
|
|
|