Sign in to follow this  
AlysiumX

Trying to program a game.

Recommended Posts

AlysiumX    152
So am at the early stages of developing a game in java. I wanted to attempt to build a game based off of knowledge of how an engine is suppose to work vs actually copying code from some one else. Now I just put an image to my canvas and I was attempting to move it "smoothly" using keybindings from it. However when I try to move the image, it doesn't seems to show movement for a few seconds after I hit the button. I am sure it has to do with my main game loop, can you please take a look and let me know if I am doing something wrong?

Other notes:
- I am setting booleans for movement on my keybindings(using pressed and released)
- I am updating the image position in my update() functions using above boolean.
- I have a JPanel added to the main JApplet which I am drawing to by overridding the paintComponent method on(which i hate btw, wish i could add it to the main game loop and always call it instead along with getting key inputs)



[source lang="java"]public void run() {
int frameCount = 0;
//Used for time
Calendar date;
date = Calendar.getInstance(); //Get the latest instance of date
int oldSecond = date.get(Calendar.MILLISECOND); //Get the current second.
int timeLeft = 0;//We delay this time after we've updated to the framerate target.

while(running){
date = Calendar.getInstance(); //Update Second

if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed.
{
if(frameCount != TARGET_FRAMERATE) //Limit target frame rate.
{
frameCount++;
Update();
}else{
//Get the time left in milliseconds
timeLeft = (oldSecond + 1000) - date.get(Calendar.MILLISECOND);

try {
fps = frameCount; //Get frames per second.

Thread.sleep(timeLeft); //and sleep the game thread for that amount of time.

frameCount = 0; //Reset frame count.

oldSecond = date.get(Calendar.SECOND); //Reset old second.

} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}[/source] Edited by AlysiumX

Share this post


Link to post
Share on other sites
You could try putting the code that updates the keyboard polling, into a separate thread, and it would likely stop happening. I cant see to much wrong with this setup. I am curious about this part though:
[CODE]if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed.[/CODE]
Why do you wait a second before updating the game? If your trying to limit frame rates just use a sleep command when it goes over your desired frame rate. Edited by DevLiquidKnight

Share this post


Link to post
Share on other sites
AlysiumX    152
Alright after further review I can see a few things I am doing wrong with the code, I was trying to work off the process that I sleep a thread to allow drawing time. However now I realize I am doubling up my efforts by manually capping the framerate and also sleeping which is stopping draw cycles. I am now getting closer to what I am looking for, here is the current code.

[source lang="java"]public void run() {
int frameCount = 0;
//Used for time
Calendar date;
date = Calendar.getInstance(); //Get the latest instance of date
int oldSecond = date.get(Calendar.MILLISECOND); //Get the current second.
int timeLeft = 0;//We delay this time after we've updated to the framerate target.

while(running){
date = Calendar.getInstance(); //Update Second

if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed.
{
if(frameCount != TARGET_FRAMERATE) //Limit target frame rate.
{
frameCount++;
Update();
}
}else{
//Get the time left in milliseconds
//timeLeft = (oldSecond + 1000) - date.get(Calendar.MILLISECOND);

fps = frameCount; //Get frames per second.

frameCount = 0; //Reset frame count.

oldSecond = date.get(Calendar.SECOND); //Reset old second.
}
//Draw the canvas
gameCanvas.paintComponent(gameCanvas.getGraphics());
}
}[/source]

The goal I am really trying to accomplish is to build a game loop that follow the convention I have seen with the explanation of how it works. So :

[source lang="java"]private void Init(){
//Initialize game objects here.

running = true;
GameLoop();
}

private void GameLoop(){
while(running){
if (within frames per second){
Update();
}
Draw();
}
}

private void Update(){
GetInput();
//Update game objects here
}

private void Draw(){
//Draw game objects here
}

private void GetInput(){
//Get key input
}[/source]

Two questions :
Can you get the key inputs in java the same way you getGraphics?

Why is a JPanel/Canvas perferred vs just doing getGraphics on the main JFrame/JApplet?

Thanks.

Share this post


Link to post
Share on other sites
AlysiumX    152
[quote name='DevLiquidKnight' timestamp='1341273152' post='4955110']
You could try putting the code that updates the keyboard polling, into a separate thread, and it would likely stop happening. I cant see to much wrong with this setup. I am curious about this part though:
[CODE]if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed.[/CODE]
Why do you wait a second before updating the game? If your trying to limit frame rates just use a sleep command when it goes over your desired frame rate.
[/quote]

Sorry that comment is kind of misleading, It should really read, //if a second has not passed.

Share this post


Link to post
Share on other sites
[CODE]
private void GameLoop(){
while(running){
if (within frames per second){
Update();
}
Draw();
}
}
[/CODE]
This isn't an accurate representation of frames per second. The code should be changed to the following:
[CODE]
private void GameLoop(){
while(running){
if (within frames per second){
Draw();
}
Update();
}
}
[/CODE]

Share this post


Link to post
Share on other sites
AlysiumX    152
[quote name='DevLiquidKnight' timestamp='1341274373' post='4955114']
[CODE]
private void GameLoop(){
while(running){
if (within frames per second){
Update();
}
Draw();
}
}
[/CODE]
This isn't an accurate representation of frames per second. The code should be changed to the following:
[CODE]
private void GameLoop(){
while(running){
if (within frames per second){
Draw();
}
Update();
}
}
[/CODE]
[/quote]

ive been confused on this for quite some time, so you limit the draw rate by frame per second not the update?

I don't understand this concept.

Wouldn't this happen?

[url="http://imageshack.us/photo/my-images/802/limitdrawexample.png/"]http://imageshack.us...rawexample.png/[/url]

Which would make the draw part of the program drop frames? Edited by AlysiumX

Share this post


Link to post
Share on other sites
[quote name='AlysiumX' timestamp='1341277235' post='4955125']
ive been confused on this for quite some time, so you limit the draw rate by frame per second not the update?
I don't understand this concept.
[/quote]
Frame rate is the frequency or rate at which unique consecutive images are drawn to the screen. So delaying the updating of say the keyboard polling, movement and the physics, does not correlate to frame rate. The human eye cannot even really detect changes in high frame rates.

Check out this [url="http://www.koonsolo.com/news/dewitters-gameloop/"]link[/url] on different FPS handling methods. Edited by DevLiquidKnight

Share this post


Link to post
Share on other sites
L. Spiro    25638
The way in which commercial game engines work is that the logic and rendering are separate, and the logic—[i]not the rendering[/i]—is limited.

Render every cycle, update only once for a given fixed amount of time.
The implementation is a bit tricky to get right, but I have documented it [url="http://lspiroengine.com/?p=378"]here[/url].

Your original post is disheartening because you asked how an [i]engine[/i] should handle movements [of your character] etc., which means you have not yet really made a proper distinction between engine tasks and game tasks.
But that should come with age.

For now, read [url="http://www.gamedev.net/blog/355/entry-2250186-designing-a-robust-input-handling-system-for-games/"]this[/url] to understand how a robust input system works on the engine side.
And [url="http://www.gamedev.net/topic/615796-input-manager-mapping-functionality/"]this[/url] to understand how to map functionality to in-game actions.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites
AlysiumX    152
Thanks for the clarification and those links.

I think I may just need to go back to basics.

Any one know where I can find some really good java game programming tutorial?

I have found some tutorials like :
[url="http://www3.ntu.edu.sg/home/ehchua/programming/java/J8d_Game_Framework.html"]http://www3.ntu.edu...._Framework.html[/url]
and
[url="http://www.youtube.com/thejavahub"]http://www.youtube.com/thejavahub[/url]

But I am not sure of the quality of these tutorials. Any one have any links to tutorials that they would stand by as a "Good" game creation tutorial? Edited by AlysiumX

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this