Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / OpenGL Development / Re: Why god why... More game loop nonsense on: 2013-01-10 23:56:27
Oh, sorry, I ran this:
That one synched to 59 FPS and didn't use VSync, which gave screen tearing.

Concerning your latest paste: First of all,
glColor3f(255, 255, 255);
is wrong. Float values go from 0.0 to 1.0. It's getting clamped to 1.0 right now anyway, so it works by chance. A gray color would be
glColor3f(0.5f, 0.5f, 0.5f);


I changed it to use nanoTime() instead and to use longs instead of doubles for measuring time. See if it's any better.

http://www.java-gaming.org/?action=pastebin&id=383

Also, setting the update rate to 30 seems to improve things slightly unless I'm imagining things.

I tried your code and something strange is happening for sure. If I leave the line setting Vsync on it makes the square hiccup horrible.
But if I turn it off it moves smoothly but hiccups every once and a while.

Any ideas?

I have also tried a complete removal of my graphics drivers by
1. Running the ATI Catalyst uninstaller
2. Using driver sweeper AKA Driver Fusion to pick up any remaining files
3. Reinstalled ATI Drivers and Catalysis Control Center
2  Java Game APIs & Engines / OpenGL Development / Re: Why god why... More game loop nonsense on: 2013-01-10 04:44:13
I just ran it and you're syncing to 59 FPS. Sigh. -_-'

Is that a bad thing, cause I never set any sync options. Other than the Display.setVSyncEnabled(true) up at the top

I have added a FPS counter and I get

Roughly 2000 FPS without vsync, and then I get 59 - 61? FPS with it on (using the Display.setVSyncEnabled(true) command)

Here is the code I am currently using
http://pastebin.java-gaming.org/0b421288735
3  Java Game APIs & Engines / OpenGL Development / Re: Why god why... More game loop nonsense on: 2013-01-09 05:37:28
Have you checked in the nvidia control panel if you're forcing vsync off ? This can screw with some things, not sure if it applies to your specific problem, but worth checking.

Both machines have the setting set to vsync off unless the application says other wise. I tried using force off anyway but I still stutter.

Also can someone check out
https://sites.google.com/site/starflighttlc/

And tell me if they get this really weird horizontal line (its like the refresh line that distorts everything) going down the screen every second or so when it is in full screen mode or a steady ticking (once again every second or so) in windowed mode?
Also, if you do get the moving line, what would you call this? I'm not sure how to research it at the moment.
4  Java Game APIs & Engines / OpenGL Development / Re: Why god why... More game loop nonsense on: 2013-01-09 03:13:03
Wait, you're getting stuttering with VSync on? -_-'

Yep Sad

Is it stuttering because you have a low FPS or is it something less consistent?

According to the Direct X Samples from the June 2010 SDK I run in the range of 200 - 700 FPS.
Where samples with multiple lighting effects and etc trigger the low end of my FPS range.
5  Java Game APIs & Engines / OpenGL Development / Re: Why god why... More game loop nonsense on: 2013-01-09 02:13:53
Butter smooth here at 7000 FPS.

EDIT:
Try changing your initing code to this:
1  
2  
3  
         Display.setDisplayMode(new DisplayMode(800, 600));
         Display.create(new PixelFormat(24, 0, 0, 0, 8));
         Display.setVSyncEnabled(true);


I tried this and still same results Sad

This also happens on the other computer I test on which was built for gaming and much more powerful than the dev computer. It has a fresh install of Windows 7 and updated drivers directly from the nvidia site.

Now this may sound crazy but is there some "openGL driver" I have to install? I mean this should have already been with my drivers but at this point I don't know. DirectX games and etc run just fine, but anything dealing with openGL stutters or has some crazy line distortion going down the screen when I go full screen (Same thing happens on the gaming pc).
6  Java Game APIs & Engines / OpenGL Development / Why god why... More game loop nonsense on: 2013-01-08 08:56:00
For one reason or another my game loop has been acting strange. Its seems like I have started to stutter again... and again, no matter if I used a fixed time step or variable time step!

If possible could any one test this code and/or tell me where I went wrong? Its based on a fixed timestep decoupled from FPS. You should just be able to just cut and paste it
Thanks!

http://pastebin.java-gaming.org/4440b947231

{Edit : I always forget this, specs on my dev pc}

Dell Studio XPS 1640 Laptop -
Windows 7 64 bit
4.0 GB ram DDR2
Core Duo T955 2.66 Ghz
ATI Mobility Radeon HD 4670 [Drivers up to date]
7  Java Game APIs & Engines / OpenGL Development / Re: Questions about Shaders and 2D sprites on: 2013-01-06 22:54:28
@theagentd :
You can also do things that don't have to be done per pixel like calculating atlas texture coordinates from a tile index or something.

This is extremely interesting, but how is this possible?
Do you just give the sprite's x and y origin along with its width and height to the vertex shader and it automatically does it?

I know you said it could be done quickly on the CPU, but is that better than just passing in all that information to a function and having it calculate it all on the fly?
8  Java Game APIs & Engines / OpenGL Development / 2D world camera best approach? on: 2013-01-06 22:41:27
What is the best approach for creating a movable 2D camera and applying it to my game in openGL?

I have seen people use

1  
glTranslatef(-focusCam.x + (SCREENX / 2), -focusCam.y + (SCREENY / 2));


before sending there data off to the graphics card for processing.
But is that way to do it for best performance?

Assuming my game loop is like so:

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  
   while(runGame)
   {
                //Add onto frame count and get the current time
     frames++;
      gameClockNow = timeGetTime();

                //The game update loop
               //Reset the loop count and execute the loop
     loops = 0;
      while( (timeGetTime() >= gameClock) && (loops < MAXUPDATES) )
      {

         //Poll for event inputs
        pollForInput();

         //Update game objects
        //updateGame();

         //Update the game clock and loop count
        gameClock += updateTime;
         loops++;
      }

      //Calculate the interpolation
     alpha = ((timeGetTime() - gameClock) + updateTime) / updateTime;
   
      //Calculate the FPS
     calculateFPS(gameClockNow, startTime);      

      //Calculate interpolation for the debug player sprite
               interpolateAllObjects(alpha);

      //Draw the game
     drawGame();



And my drawGame Function looks like this and uses vertex arrays:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
void drawGame()
{

   batcher.begin();
         
        //Draw the sprites based on COUNTSPRITE using the atlas draw method
       //Where it takes in the x and y position of the sprite, the sprites width and height, its x and y origin on the atals in pixels, and the width and height of the atlas (128.0 x 128.0 in or case)
  for(int i = 0; i < COUNTSPRITE; i++)
      batcher.drawAtlasSprite((float)spriteList[i].x, (float)spriteList[i].y, 64, 64, spriteList[i].OriginX, spriteList[i].OriginY, 128.0f, 128.0f);

   batcher.end();
        batcher.swapBuffer();
}


Also Im not sure if you need this but here is my init funciton for openGL :

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  
void initOpenGl()
{
   //Clear openGl items
  glDisable(GL_DEPTH_TEST);
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   //Set the view port
  glViewport(0, 0, SCREENX, SCREENY);

   //Set / load the projection matrix
  glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   //Set the ortho
  glOrtho(0.0f, SCREENX, SCREENY, 0.0f, -1.0f, 1.0f);

   //Set / load the model view
  glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   //Enable use of textures
  glEnable(GL_TEXTURE_2D);

   //Enable blending of alpha layer
  glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
9  Java Game APIs & Engines / OpenGL Development / Re: Questions about Shaders and 2D sprites on: 2013-01-06 19:57:55
So many questions

About shaders-

@theagentd :
Why are vertex shaders exactly useless for 2D games? Cause don't sprites have there vertex positions always move and if I use a texture atlas cant I use the vertex shader to manipulate those point too? Or am I just not really understanding what a vertex shader is really for?

@davedes :
I looked over your tutorials and I just have few questions

Once the shaders are loaded in and you link them to the openGL context, how does openGL know when you want to use them? Once they are linked are they just automatically used on everything you draw? Or is there something else that says only use the shaders on these group of sprites?

@phibedy
Thanks for the links I will look into them for sure!



10  Java Game APIs & Engines / OpenGL Development / Questions about Shaders and 2D sprites on: 2013-01-05 19:07:01
I have a few questions about shaders and there use with a 2d game.

If I have a basic basic basic sprite batcher.
I mean it only uses vertex arrays and texture coord arrays, data in the array is used by calling glDrawArrays based on GL_QUADS,
and the draw function loads the vertex position and texture coords into the array.

drawGame function
1  
2  
3  
4  
5  
6  
7  
8  
9  
//Clear the GL_COLOR_BUFFER_BIT and set up for the use of vertex and texture coord arrays
batcher.begin();

//Place a sprites data into the vertex and texture coord array
// By passing in : x position, y position, sprite Width, sprite Height, and the texture coords
batcher.draw(10, 10, 64, 64, 0, 0, 1, 0, 1, 1, 0, 1);

//call the render function which holds the glDrawArrays call then disable the use of  vertex and texture coord arrays
batcher.end()


What is the benefit of using a shader program or is there even any benefit if I'm only using 2D?

And honestly

What is exactly is a shader program?
What is its purpose?
Why should I care to use them?
What can I even do with it I I am to implement one?

Thanks! Also please thorough explanations would be great as I'm new to openGL / graphics programming and have no idea what any of this is Sad
11  Java Game APIs & Engines / OpenGL Development / Re: Best 2D sprite animation performance on: 2013-01-04 16:40:23
These days a texture atlas is only one small part of the equation. You also need to look into VBOs, mapped byte buffers, state change minimisation, and asynchronicity.

Cas Smiley

Could you explain what you mean Cheesy ?

I only know what VBOs and possibly asynchronicity are if you talking about making a multi-threaded program Sad
12  Java Game APIs & Engines / OpenGL Development / Best 2D sprite animation performance on: 2013-01-04 01:13:50
Whats the best way to do or make 2D animations using sprites?

I am thinking about having a sprite sheet that has all the frames needed for it's animation.
Then just changing what frame to look at when I'm actually running the animation after X amount of time.

The only other way I can think of doing it is to load in all the frames as textures and the bind them,
but that's like shooting my self in the foot when it comes to performance

Whats the best way to do this for the best performance in openGL?
 
13  Java Game APIs & Engines / OpenGL Development / Re: Texture atlas for 2D game on: 2013-01-04 01:04:48
We seem to be the same, I just started from the top left. Also thanks for the heads up on the atlas programs!

But is that the way I should be going about it? Using / changing glTexCoord2d's parameters?
14  Java Game APIs & Engines / OpenGL Development / Texture atlas for 2D game on: 2013-01-03 16:47:22
What is the proper way to do a texture atlas for a 2D game?

I have read that you can just load in your atlas, calculate a ratio or map position based on the width / height of the atlas, and then in your render function use
glTexCoord2d(x, y)

Example
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
//Map caulation (upper left corner calculation)
// atlas width / atlus height : the size of the atlas in pixels
// imageColCountX / imageRowCountY : The number of sprites in the atlas based on row / columns
// spriteMaxWidthLocal / spriteMaxHeightLocal : the end pixel location of the sprite you wish to use

double spriteLoationX =  (atlasWidth / imageColCountX) / spriteMaxWidthLocal;
double spriteLoationY =  (atlasHeight / imageRowCountY) / spriteMaxHeightLocal;

//Upper left
glTexCoord2d(spriteLoationX, spriteLoationY);
glVertex2d(player.viewX, player.viewY);

/*
     Other positions of the quad and etc ...
*/


Is there a better way?

edit: fixed inconstant variable names in example code
15  Java Game APIs & Engines / OpenGL Development / Re: deWitters 4th game loop HELP! on: 2013-01-03 16:17:20
When I do this I seem to jump forward whenever my player stops moving.
That's because you're still extrapolating. You need to change
player.viewX = player.x + interpolation * (player.x - player.prevX)

into
player.viewX = player.prevX + interpolation * (player.x - player.prevX)


With that change you'll be INTERpolating between the previous position and the current position.

It seems that the inclusion of updateTime in the interpolation calculation greatly effects this. If I did at it in the first part of the equation I started jumping!

but...

After playing around with the code and doing your suggestions I seem to have got it to work! My movement is now smooth! Agent D thank you so much for all the help!

The final game loop product
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  
//The max amount of time the game logic loop can be updated befoire forcing the render call
const int MAXUPDATES = 5;

//Time we have updated the game logic loop
int loops = 0;

//The amount of time (in milliseconds) spent updating the game loop in this case 33.33333 milliseconds
double updateTime = 1000.0 / 30.0;

//Used for the interpolation calculation
double interpolation;

/*
   Get the current time before we start the main game loop
   timeGetTime() returns the amount of time since the "system" has started in milliseconds
   In this case the system is Windows
*/

double gameClock = timeGetTime();

while(runGame)
{


   //Reset the loop count
  loops = 0;

   /*
      The GAME LOOP!
      Continue to update the game until the game Clock is greater than the current time ( timeGetTime() )
      OR
      Until we have hit the max amount of time we are allowed to update before we a re focring the game to render
      ( the loops < MAXUPDATES part)
   */

   while( (timeGetTime() >= gameClock) && (loops < MAXUPDATES))
   {

     
      //Store the previous player position
     player.prevX = player.x;
      player.prevY = player.y;

   
      //Movement right
     if(currentKeystate[SDKL_d])
         player.x += player.speed;

      //Movement left
     if(currentKeystate[SDKL_a])
         player.x -= player.speed;

      //Movement up
     if(currentKeystate[SDKL_w])
         player.y -= player.speed;

      //Movement down
     if(currentKeystate[SDKL_s])
         player.y += player.speed;

      //Add onto the game clock by the update time; Add onto the number of loops
     gameClock += updateTime;
      loops++;
   }

   
   //Calculate for interpolation
  interpolation = ((timeGetTime() - gameClock) + updateTime) / updateTime;

   //Apply the interpolation to the player's render position before actually rendering the game
  player.ViewX = player.prevX + interpolation * (player.x - player.prevX);
   player.ViewY = player.prevY + interpolation * (player.y - player.prevY);

   //Render the game sprites and etc using on the render positions ( player.viewX / player.viewY )
  drawGame();

   //Flip / swap the buffers to draw everything to the screen
  SDL_GL_SwapBuffers();
}
16  Java Game APIs & Engines / OpenGL Development / Re: deWitters 4th game loop HELP! on: 2013-01-02 03:19:56
...
Should give me perfect smoothness and no stutter? Correct?
Yes, but running the game logic at 60Hz might not be possible if you have heavy physics or simply lots of objects, etc. That's why you may want to run the logic at a slower rate and use interpolation. Also keep in mind that not all screens update at 60Hz. Some refresh at 59.<something> Hz, others at 120Hz. Since we can't really assume anything about the screen's refresh rate, it's better to just pick an update rate that's as high as possible but still has good performance. The interpolation then allows you to actually render the game at any FPS. Vsync in turn can provide stutter-free synchronization (well, in theory I guess since it's not helping here...?).

Okay, here's my last card: For now, keep the update rate at 60Hz and enable VSync. Now, for each frame, output the interpolation value to the console. If the game really is stutter-free the interpolation value should be relatively constant. It will undoubtedly drift around a bit, and if something disturbs it (other programs hogging CPU time or so) it might jump randomly, but when it's not stuttering it should be constant. If your interpolation value is jumping around a lot, it could indicate a problem with the timing method. If it however actually is smooth even though it visually stutters (and you're not imagining it =P) I'd just conclude that it's unfixable and that it's probably a problem with your graphics drivers or something like that.


I did this and I am pretty good on the interpolation values! I really only fluctuate between .034 and .036
Where my interpolation calculation (tried basing it on player.prevX instead of player.x, but I seem to jump forward) is

1  
2  
3  
4  
//updateTime : 1000.0 / 60.0 or 16.66667 
//gameClock : the value added on to during the loop by updateTime; originally this value is set to GetTicks() (which returns a clock value in milliseconds) before the start of the main game loop
double interpolation = (GetTicks - gameClock) / updateTime;
player.viewX = player.x + interpolation * (player.x - player.prevX)


Now since this is based on deWitters 4th game loop like the title implies

He has his interpolation calculation as (copied directly from the article I was basing this post off of) :
1  
2  
3  
4  
5  
6  
7  
/*
SKIP_TICKS (time spent updating the game loop) : 1000.0 / 60.0

next_game_tick : the value added on to during the loop by next_game_tick; originally this value is set to GetTicks() (which returns a clock value in milliseconds) before the start of the main game loop

*/

double interpolation = (double)(GetTicks() + SKIP_TICKS - next_game_tick) / (double)SKIP_TICKS;


Now my question is what is the point or why should I add SKIP_TICKS (or time spent updating the game loop)? When I do this I seem to jump forward whenever my player stops moving.
17  Java Game APIs & Engines / OpenGL Development / Re: deWitters 4th game loop HELP! on: 2013-01-02 00:53:18
[edit3]
This is a JAVA forum. http://lmgtfy.com/?q=c%2B%2B+forum

What this is a java forum? I thought this was a c++ forum all this time... That would explain all of the java threads and the name of the site...
I know this is not a c++ forum! I'm using openGL and coding is coding! Do I really need to explain more?

I'm not sure why you need to divide 1000 by 60.  VSync should do that for you. Unless it is different in c++?

Vsync is Vsync no matter what. I say its on because I mention that there is no interpolation going on. With out vsync on I stutter horribly.

Yes I know I can not get perfect smoothness, but I don't believe my player sprite should hicup / stutter every other second or so (no matter if vsync is on or off)
18  Java Game APIs & Engines / OpenGL Development / Re: deWitters 4th game loop HELP! on: 2013-01-01 23:43:48
I'm not a mind reader. Did you try any of my suggestions?

I have tried changing form extrapolating to interpolating the character position, but like you said it seems to do nothing for me.
I am still working on finding a more accurate timer to use.

But in the meantime

In order for my game loop to give me perfect smoothness I need to be "stuck" in the update loop for 16.666667 milliseconds (because my monitor is 60 hz and vsync is on)
Lets also assume I never want to skip frames, there is no interpolation going on, and my computer can always keep up

So the following 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  
//Set up the updates per tick; or the amount of time to spend on each game logic update
double updates = 1000.0 / 60.0; //16.66667 milliseconds (perfect update rate for 60hz monitor)

//Set up the start time of the game
double gameClock = GetTicks(); //returns milliseconds

//enter the game loop
while(runningGame)
{

      //Get the Current time in milliseconds
     double nowTime = GetTicks();

      //Update the game
     while(nowTime >= gameClock)
     {
          player.x += 3;
          gameClock += updates;
     }

     //Render the game
    drawGame();

}


Should give me perfect smoothness and no stutter? Correct?

19  Java Game APIs & Engines / OpenGL Development / Re: deWitters 4th game loop HELP! on: 2013-01-01 15:12:33
If I am using this type of game loop should I be converting my times into seconds? I don't think that would matter as long as everything is matching times (milliseconds or seconds), but at this point I'm trying to think of anything
20  Java Game APIs & Engines / OpenGL Development / Re: deWitters 4th game loop HELP! on: 2012-12-31 16:41:00
Well, at least the interpolation needs millisecond precision to be smooth. The reason why I mentioned the timing problem is because the standard way of measuring time had a very bad precision in some OSes, more to the tune of over 10ms or so. Since many important functions like sleep() and just basic FPS calculation rely on being able to accurately measure time you could get very bad stuttering. If that's not the problem, it's most likely some logic problem, but be sure to measure the FPS to see that you're actually getting exactly 60 FPS (assuming you have a 60Hz screen). You also didn't post your draw function?

I edited my original post to show the draw function

My monitor is at 60hz and my fps calculator with vsync on fluctuates between 59.98XX and 60.03XX FPS
Is that to much fluctuation?
21  Java Game APIs & Engines / OpenGL Development / Re: deWitters 4th game loop HELP! on: 2012-12-31 15:57:27
This is a Java forum...

Anyway, could it be a problem with SDL_GetTicks()? A similar function in Java had very bad precision on Windows, which could lead to heavy stuttering. Other than that I have no idea...

I know this is a java forum but code is still code. There is one fatal thing I forgot though and that was to post my draw function!

Also what type of timing precision should I be aiming for? Nano seconds (Is that even needed really)?
22  Java Game APIs & Engines / OpenGL Development / deWitters 4th game loop HELP! on: 2012-12-31 07:27:49
Hello everyone!

I am attempting to implement the deWitters game loop (The one where the game rate is consistent but the FPS is independent), but I have no luck!
For one reason or a other I have a tiny stutter in my image as it moves across the screen even if vsync is enable.

If you guys could take a look at it and tell me what Im doing wrong that would be great.

The code below is in C++ but still is openGL based!
Thanks!

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  
//Set up the ticks and such
double ticksPerSec = 25.0;
double skipTicks = 1000.0 / ticksPerSec;
double maxFrameSkip = 10;

//SDL_GetTicks returns the number of ticks since the init of the game window in milliseconds

//Set the clock and the loop count
Uint32 gameClock = SDL_GetTicks();
int loops = 0;

while(gameRunning)
   {
                 //Poll for events such as window close
     while(SDL_PollEvent(&event))
      {
            if(event.type == SDL_QUIT)
               gameRunning = false;
      }

                //reset the loopcount
     loops = 0;

      while(SDL_GetTicks() > gameClock && loops < maxFrameSkip)
      {
         player.prevX = player.x;
         player.prevY = player.y;

         player.x += 1;

         gameClock += skipTicks;
         loops++;
      }

      double alpha = (double)(SDL_GetTicks() + skipTicks - gameClock) / skipTicks;

      player.viewX = player.x + alpha * (player.x - player.prevX);
      player.viewY = player.y;

      drawGame();
     
      SDL_GL_SwapBuffers();
   }


The draw function

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  
        //Open GL code to render the things that need to be drawn

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        //Bind the texture to the quad to draw
  glBindTexture(GL_TEXTURE_2D, playerImage);

        //Begin the simple render
       //All of the player values (player.x, player.y, player.viewX, etc) are in double
       //player.width / player.height is the size in pixels of the image (in this case 32 x 32 image)
  glPushMatrix();
   glBegin(GL_QUADS);
 
        //Upper left
  glTexCoord2d(0, 0);
   glVertex2d(player.viewX, player.viewY);

         //Upper right
  glTexCoord2d(1, 0);
   glVertex2d((player.viewX + player.width), player.viewY);
   
        //Bottom right
  glTexCoord2d(1, 1);
   glVertex2d((player.viewX + player.width), (player.viewY + player.height));
   
        //Bottom left
  glTexCoord2d(0, 1);
   glVertex2d(player.viewX, (player.viewY + player.height));

   glEnd();
   glPopMatrix();


EDIT : Posted draw function code
23  Java Game APIs & Engines / OpenGL Development / Re: New to OpenGL, need some guidence on 2D Game / sprites and VBO on: 2012-11-07 09:10:23
Awesome! One part does part concern me a little, you said I would call my render
function if I changed textures, but wouldn't that have the potential to slow me down?

What I mean is that if sprite 1 is texture A and sprite 2 is texture B then code below

1  
2  
3  
4  
5  
6  
myBatcher.Begin();

myBatcher.draw(sprite1, 50, 50);
myBatcher.draw(sprite2, 12, 9);

myBatcher.End();


is really like doing

1  
2  
3  
4  
5  
6  
7  
myBatcher.Begin();
myBatcher.draw(sprite1, 50, 50);
myBatcher.End();

myBatcher.Begin();
myBatcher.draw(sprite2, 50, 50);
myBatcher.End();


Or am I completely wrong?
24  Java Game APIs & Engines / OpenGL Development / Re: New to OpenGL, need some guidence on 2D Game / sprites and VBO on: 2012-11-07 08:37:48
I guess I really didn't understand the concept of a sprite batcher then.

Correct me if I'm wrong. Ok so when we make a sprite batcher, we are really just calculating the vertex, texture, and color data for an X amount of quads (sprites) and then
placing them in giant arrays.

Then when we reach our X amount of quads to handle at one time we render them using a single bind call

So basically this  :

1  
2  
3  
4  
5  
6  
myBatcher.Begin();

myBatcher.draw(sprite1, 50, 50);
myBatcher.draw(sprite2, 12, 9);

myBatcher.End();


Would start up the batcher Then add sprite 1 to the batcher and plan to draw it at x cord 50 and y cord 50.
Then add sprite 2 to the batcher and set up its position. Finally the batcher's end function is called which
actually renders everything and does any clean up




25  Java Game APIs & Engines / OpenGL Development / Re: New to OpenGL, need some guidence on 2D Game / sprites and VBO on: 2012-11-07 07:37:38
Haha I just posted a tutorial on how to make a basic spritebatcher...follow davedes as his is more robust but if you want to stay away from shaders look at what I just posted.

drawRegion I think is for drawing only a region of the texture...basically texture atlases and stuff.

Idk what your system specs are but you should be at way higher then 10k sprites in 180-215 milliseconds.

I recommend getting a batcher working using glBegin/glEnd first so you understand what type of abstraction you want to use. Then try out vertex arrays/VBO.


I should have probably posted my system specs! I would like to use VBOs and the above code is my attempt at them,
but are saying that I should have gotten a way less execution time?

Specs [Dell Laptop] -
OS : Win 7 Home Premium Service Pack 1
Processor : Intel Core Duo t9550 2.66 GHz
Ram : 4 Gb DDR3
Video Card : ATI mobility Radeon HD 4670

I will look into your example as well!
26  Java Game APIs & Engines / OpenGL Development / Re: New to OpenGL, need some guidence on 2D Game / sprites and VBO on: 2012-11-07 04:04:14
Hey its GiraffeTap22 I forgot my pass and the email I used... anyway...

Thank you both very much for the replies! But if I can I would like to make my game from scratch.

Davedes I looked at the link to your classes. It gave me a few ideas, but I have a few questions about the whole thing

Could you explain the shaders program to me? I am completely confused about them, because when I hear the word shader I think about 3D models and lighting. Something not used in a 2D game.

Also, this maybe stupid of me asking, but what is the difference between drawRegion and draw?

Is drawRegion just drawing sprites that fall in a certain area?

Also I went back and changed my code up a bit. Its seems I can render 10000 sprites in about 180 - 215 milliseconds according to a simple execution run time check. I get the execution time by rendering all the of the sprites once and then closing down the program.

Is there anything else I could do?

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  
public class Sprite
   {
      public float x;
      public float y;
      public Texture texture;
   }
   
   public GameWindow()
   {
         
      //init the game window and OpenGl
     initGameWindow();
      initOpenGL();
     
      //Load some textures
     square = loadTexture("square.png");
      smile = loadTexture("smile.png");
         
      gamepad = new GamepadsController();
      //lastFrame = getTime();
  }
   
   public void run()
   {
      //Create an array of sprite objects
     Sprite [] sprite = new Sprite[10000];  
     
      //Create a new random gen
     Random gen = new Random();      
     
      //create sprites and place them randomly based on the screen size
     for(int i = 0; i < sprite.length; i++)
      {
         sprite[i] = new Sprite();
         sprite[i].x = gen.nextInt(SCREENWIDTH) + 1;
         sprite[i].y = gen.nextInt(SCREENHEIGHT) + 1;
         
         //Set the texture
        if(i % 2 == 0)
            sprite[i].texture = square;
         else
            sprite[i].texture = smile;
      }
   
      //set a place holder for the current texture to be rendered
     currentTexture = sprite[0].texture;
     
      //Set up the vertex buffer
     final int amountOfVertex = 4;
      final int vertexSize = 2;

      FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertex * vertexSize);

      FloatBuffer texCoordData = BufferUtils.createFloatBuffer(amountOfVertex * vertexSize);
      texCoordData.put(new float[]{
            0.0f, 0.0f,
            1.0f, 0.0f,
            1.0f, 1.0f,
            0.0f, 1.0f});
      texCoordData.flip();
     
      int vboVertexHandle = glGenBuffers();
      glBindTexture(GL_TEXTURE_2D, currentTexture.getTextureID());
      glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
      glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
      int texCoordHandle = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, texCoordHandle);
      glBufferData(GL_ARRAY_BUFFER, texCoordData, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
      float [] quadPosition = new float[amountOfVertex * vertexSize];
     
      for(int i = 0; i < (amountOfVertex * vertexSize); i++)
         quadPosition[i] = 0.0f;
     
      //Init OpenGL      
     while(!Display.isCloseRequested())
      {
         //poll data
        gamepad.pollInput();
         
         //Clear
        glClear(GL_COLOR_BUFFER_BIT);
         
         //Draw all the sprites
        for(int i = 0; i < sprite.length; i++)
         {
           
            //set the texture for the quad and bind it
           currentTexture = sprite[i].texture;
            currentTexture.bind();
           
            //Test movement
           //sprite[i].x += 1;
           
            //clear the vertex data
           vertexData.clear();
           
            //Set up the position of the quad
           quadPosition[0] = sprite[i].x;
            quadPosition[1] = sprite[i].y;
            quadPosition[2] = sprite[i].x + sprite[i].texture.getImageWidth();
            quadPosition[3] = sprite[i].y;
            quadPosition[4] = sprite[i].x + sprite[i].texture.getImageWidth();
            quadPosition[5] = sprite[i].y + sprite[i].texture.getImageHeight();
            quadPosition[6] = sprite[i].x;
            quadPosition[7] = sprite[i].y + sprite[i].texture.getImageHeight();
           
            vertexData.put(quadPosition);
            vertexData.flip();

            //Bind and draw the Quad
           glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
            glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
               
         
            glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
            glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
           
            glBindBuffer(GL_ARRAY_BUFFER, texCoordHandle);
            glTexCoordPointer(vertexSize, GL_FLOAT, 0, 0L);
                     
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glDrawArrays(GL_QUADS, 0, amountOfVertex);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
         }
         
         Display.update();
         Display.sync(60);
         
      }
     
      Display.destroy();

   }


Thanks again!


The specs on the computer I'm working with

Specs [Dell Laptop] -
OS : Win 7 Home Premium Service Pack 1
Processor : Intel Core Duo t9550 2.66 GHz
Ram : 4 Gb DDR3
Video Card : ATI mobility Radeon HD 4670
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!