Sign in to follow this  

Sleep!!

This topic is 4215 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im simply trying to pause the game for a quick second. But before it pauses i want it to put the ball in the right place, but its pausing before the ball is reset to (0,0).
void RestartGame()
{

	Ball.x = 0.0f;
	Ball.y = 0.0f;

	Sleep(1000);
	//Random Ball Angle
}

Share this post


Link to post
Share on other sites
What is Ball? Are you drawing the changes before you Sleep()? "Random Ball Angle"?

You need to give us more information.

Share this post


Link to post
Share on other sites
How are you rendering ball? Chances are it is resetting the position, but you don't see it because Sleep blocks the next frame from being rendered.

Share this post


Link to post
Share on other sites
I recommend aginst using Sleep() at all. Just write your own function to idle for a certein amount of time, like this one (c++):

#include <time.h>

void idle(int seconds)
{
clock_t start = clock();
while ( clock() - start < seconds * CLK_TCK ) {}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Scet
How are you rendering ball? Chances are it is resetting the position, but you don't see it because Sleep blocks the next frame from being rendered.

Quoted for emphasis.

void RestartGame()
{

Ball.x = 0.0f;
Ball.y = 0.0f;

RenderBall(); // or whatever

Sleep(1000);
//Random Ball Angle
}


Share this post


Link to post
Share on other sites
Dammit this is so stupid, i dont get it!!!

this is how im rendering it!?

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();

//Texture
glBindTexture(GL_TEXTURE_2D, texture[filter]);

//Sphere
glTranslatef(Ball.x,Ball.y,-15.0f);
glRotatef(Xrot,0.0f,0.0f,1.0f);
gluSphere(quadratic,0.5,32,32); // Draw A Sphere With A Radius Of 1 And 16 Longitude And 16 Latitude Segments

glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


glTranslatef(PaddleX,PaddleY,-15.0f);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

// Right Face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( -0.7f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( -0.7f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.7f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.7f, -1.0f, 1.0f);

// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.9f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.9f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, 1.0f, -1.0f);

// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);


glEnd();

glLoadIdentity();


glTranslatef(PaddleX2,PaddleY2,-15.0f);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

// Right Face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( -1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( -1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -1.0f, -1.0f, 1.0f);

// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, 1.0f, -1.0f);

// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);


glEnd();


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//EndTexture
glBindTexture(GL_TEXTURE_2D, 0);


//Text
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(-8.1f,5.6f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("SphereX - %f SphereY - %f ", Ball.x ,Ball.y); // Print GL Text To The Screen

glLoadIdentity();

//Text
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(-8.1f,5.3f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("Player1 - %d Player2 - %d ", Player1Score ,Player2Score); // Print GL Text To The Screen


glLoadIdentity();

//Text
glColor3f(1.0f,1.0f,0.0f);
glTranslatef(-8.1f,5.0f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("Distance - %f Distance - %f blnCollide - %d", Distance, Distance2, blnBallY);

glLoadIdentity();

//Text
glColor3f(0.3f,0.3f,1.0f);
glTranslatef(-8.1f,4.7f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("XVelocity - %f YVelocity - %f", Ball.xv, Ball.yv);

glColor3f(1.0f,1.0f,1.0f);
glLoadIdentity();

if (blnCollide == true || blnCollide2 == true)
{
glLoadIdentity();

//Text
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(-8.1f,4.4f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("Collision Detected");

}

EnemyAI();

if (blnMoveBall == true)
{
moveBall();
blnRestart = false;
}

if (blnRestart == true)
{
idle(1);

blnMoveBall = true;
blnRestart = false;
}

Collide(PaddleX - 1.0f,PaddleY,Ball.x,Ball.y);
Collide2(PaddleX2,PaddleY2,Ball.x,Ball.y);

return TRUE; // Everything Went OK
}



Does that make any sense?

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
I recommend aginst using Sleep() at all.

care to provide a reason, or should we just assume you have some secret knowledge as to why Sleep is bad?
Quote:
Original post by Driv3MeFarJust write your own function to idle for a certein amount of time, like this one (c++):
#include <time.h>

void idle(int seconds)
{
clock_t start = clock();
while ( clock() - start < seconds * CLK_TCK ) {}
}


that is not a sleep or an idle function. That is a wasteCpuCyclesInAPointlessTightLoop function.

Do you actually know what Sleep() does? Sleep yields the current thread allowing other processes/threads to use the cpu. At best your "idle" function will be optimised out and at worst it'll just hog the cpu.

to the OP, are you using seperate threads to render the ball?

Share this post


Link to post
Share on other sites
Not how but when. :)

The existing code changes the data about the ball, and then causes everything to pause. The reason you don't see the ball move is because the pause happens after the change in data but *before* the ball is drawn again. After the pause, the ball draws (eventually, depending on what else the code does), now in the updated position.

So you need to reorder some stuff, and likely redesign part or all of the game loop. Do not just hack it. You will regret doing so.

Share this post


Link to post
Share on other sites
exactly i just cant figure out where the hell im gonna put it, like everyone said im gonna have to rearrange some crap!! *sheesh*

The ball does reset to x0,y0 but sleeps right before the render!!!

Share this post


Link to post
Share on other sites
May I suggest not sleeping at all and instead just not updating your physics and game logic. If you are passing a Time Delta to some kind of update function, then you can pass it 0 instead of an actual number and that will in effect pause the game. Or you can simply skip the call to update all together.

Share this post


Link to post
Share on other sites
It looks to me that the compiler has rearranged the instructions to call Sleep before making the assignments. Try putting the assignments in brackets or putting the Sleep call in brackets and see if that makes a difference.


void RestartGame()
{
{
Ball.x = 0.0f;
Ball.y = 0.0f;
}
Sleep(1000);
//Random Ball Angle
}




Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
It looks to me that the compiler has rearranged the instructions to call Sleep before making the assignments. Try putting the assignments in brackets or putting the Sleep call in brackets and see if that makes a difference.

*** Source Snippet Removed ***

I really don't think that would make any difference at all, since he's still not actually rendering anything before invoking "Sleep()". [smile]

A better way would be a state-based approach, which would require some major refactoring (starting with separating rendering and logic code).

Share this post


Link to post
Share on other sites
why not just put sleep after his call to drawglscene? if he is just wanting to pause the game for a bit (and it isn't something that the user is asking to do) then what harm would it be to move it there?

Share this post


Link to post
Share on other sites
Quote:
Original post by darookie
Quote:
Original post by LessBread
It looks to me that the compiler has rearranged the instructions to call Sleep before making the assignments. Try putting the assignments in brackets or putting the Sleep call in brackets and see if that makes a difference.
*** Source Snippet Removed ***

I really don't think that would make any difference at all, since he's still not actually rendering anything before invoking "Sleep()". [smile]


I'm just going on what Bluseed has been saying. In his first snippet the sleep was happening before the assignment. In the second snippet (with the render) the sleep is happening before the render. Both of these examples tell me that the compiler might be placing the call _Sleep instruction before the assignments in the first example and the call _Render instruction in the second example - even though the code places those actions before the call to Sleep. I'm simply looking at that issue and not the larger question of whether the architecture of his program can be improved, refactored or what have you. If my code came up that way, I would look at a disassembly of the function to see what was really going on.

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
Quote:
Original post by darookie
Quote:
Original post by LessBread
It looks to me that the compiler has rearranged the instructions to call Sleep before making the assignments. Try putting the assignments in brackets or putting the Sleep call in brackets and see if that makes a difference.
*** Source Snippet Removed ***

I really don't think that would make any difference at all, since he's still not actually rendering anything before invoking "Sleep()". [smile]


I'm just going on what Bluseed has been saying. In his first snippet the sleep was happening before the assignment. In the second snippet (with the render) the sleep is happening before the render. Both of these examples tell me that the compiler might be placing the call _Sleep instruction before the assignments in the first example and the call _Render instruction in the second example - even though the code places those actions before the call to Sleep. I'm simply looking at that issue and not the larger question of whether the architecture of his program can be improved, refactored or what have you. If my code came up that way, I would look at a disassembly of the function to see what was really going on.

Actually, I see nothing suspicious in both snippets. Same cause, same effect - misplaced "pause" - the actual position inside both functions as well as implementation (e.g. "Sleep()" vs. "idle()") are irrelevant.
Why?
Observation 1)
In the first snippet the state of the ball object is changed, but not promoted to the rendering function. The comment following the actual "Sleep()" call suggests that the state was changed again after the call.
Conclusion -> the OT thinks the assignment didn't take place because the state change wasn't made visible until after the pause.

Observation 2)
In the second snippet there is a pause ("idle(1)") in the rendering code, presumably after the state of the ball object has changed. But, the pause again happens before the state change (in this case the rendered scene) is made visible.
There are two possible reasons for that assumption:
a) The code is using single-buffered immediate mode, in which case a glFlush() is missing at the end of the function.
b) The code is using double- or triple buffering, meaning a "glutSwapBuffers()" or something similar is missing.
Conclusion -> this happens outside of "DrawGLScene()" and hence the pause is misplaced inside this function.

Hope that makes sense,
Pat.

Share this post


Link to post
Share on other sites
Quote:
Original post by ChaosEngine
Quote:
Original post by Driv3MeFar
I recommend aginst using Sleep() at all.

care to provide a reason, or should we just assume you have some secret knowledge as to why Sleep is bad?


Becuase sleep will yield time to other threads/programs for at least the ammount of time you specify, which is not to say it will for exactly the time you specify.

Calling sleep(1000) means that the OS will let other processes run for at least 1000ms, but those proccesses could take any ammount of time, really. Sleep can result in longer delays than is expected/required.

Quote:
Original post by ChaosEngine
Quote:
Original post by Driv3MeFarJust write your own function to idle for a certein amount of time, like this one (c++):
#include <time.h>

void idle(int seconds)
{
clock_t start = clock();
while ( clock() - start < seconds * CLK_TCK ) {}
}


that is not a sleep or an idle function. That is a wasteCpuCyclesInAPointlessTightLoop function.

Do you actually know what Sleep() does? Sleep yields the current thread allowing other processes/threads to use the cpu. At best your "idle" function will be optimised out and at worst it'll just hog the cpu.


Quote:
Original post by Bluseed
Im simply trying to pause the game for a quick second.


That is exactly what my Idle funtion does. It is just a tight loop to eat up CPU time, in order to effectively pause the game for the specified amount of time. I know that sleep yields time to other proccesses, but as I said before, it does so in such a way that you can get unexpected behavior from it, and in a game, there is really no need to yield proccessing time to begin with.

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Quote:
Original post by ChaosEngine
Quote:
Original post by Driv3MeFar
I recommend aginst using Sleep() at all.

care to provide a reason, or should we just assume you have some secret knowledge as to why Sleep is bad?


Becuase sleep will yield time to other threads/programs for at least the ammount of time you specify, which is not to say it will for exactly the time you specify.

Calling sleep(1000) means that the OS will let other processes run for at least 1000ms, but those proccesses could take any ammount of time, really. Sleep can result in longer delays than is expected/required.

Quote:
Original post by ChaosEngine
Quote:
Original post by Driv3MeFarJust write your own function to idle for a certein amount of time, like this one (c++):
#include <time.h>

void idle(int seconds)
{
clock_t start = clock();
while ( clock() - start < seconds * CLK_TCK ) {}
}


that is not a sleep or an idle function. That is a wasteCpuCyclesInAPointlessTightLoop function.

Do you actually know what Sleep() does? Sleep yields the current thread allowing other processes/threads to use the cpu. At best your "idle" function will be optimised out and at worst it'll just hog the cpu.


Quote:
Original post by Bluseed
Im simply trying to pause the game for a quick second.


That is exactly what my Idle funtion does. It is just a tight loop to eat up CPU time, in order to effectively pause the game for the specified amount of time. I know that sleep yields time to other proccesses, but as I said before, it does so in such a way that you can get unexpected behavior from it, and in a game, there is really no need to yield proccessing time to begin with.



I beg to differ on not calling sleep. If your using threads in your engine then your going to need to yeild time in some way. Sleep is the easiest to do. The consequencies of not calling sleep when running multi threaded is that your other threads may not run. I forget to have one of my threads sleep at all and instead it was running a tight loop to dispatch events and it slowed the UI of every program running including the debugger except for iTunes go figure.

Now on the other hand if your not using threads then a tight loop may not be a problem besides you can always just do Sleep(0) to play nice with the OS and get back as quickly as possible.

To the issue at hand is it possible that your not swapping the buffers before sleeping I didn't notice it in your render function or other code.

Share this post


Link to post
Share on other sites
DArookie!! I think your on to something, what exactly would be a solution??

**edit***

It makes perfect sense Darookie, the Swapbuffers is in a loop thats outside of the rendering call right after the GLSceneDraw rendering call in the loop (make sense?) I've even tried putting the sleep(what have you) after the render call(GLScene Draw, and it still doesnt work!!!!

**edit***

check this out, i dont know of any better way to show u fully but heres the source...



#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include <string>

#include <time.h>
#include <stdio.h> // Header File For Standard Input/Output
#include <math.h>

HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application

bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
bool light; // Lighting ON/OFF
bool lp = true; // L Pressed?
bool fp; // F Pressed?
bool blnBallX;
bool blnBallY;
bool blnPaddleUp; //Is Paddle moving up
bool blnPaddleDown; //Is Paddle moving down
bool blnPaddleUp2; //Is Paddle moving up
bool blnPaddleDown2; //Is Paddle moving down
bool blnRestart;
bool blnMoveBall = true;

float PI = 3.14159f;
struct Sphere
{
float x;
float y;
float xv;
float yv;
};
Sphere Ball;

float BallAngle;

float Speed;
float Distance;
float Distance2;

bool blnCollide;
bool blnCollide2;

//Text
GLuint base; // Base Display List For The Font Set


//Sphere
GLUquadricObj *quadratic; // Storage For Our Quadratic Objects ( NEW )

// Absolute value function(comes in handy later).
inline GLfloat ABS(GLfloat A)
{
if (A < 0)
A = -A;
return A;
}

float BallAng = 0.001f;

float PaddleX = -6.2f;
float PaddleY = 0.0f;
float PaddleX2 = 7.9f;
float PaddleY2 = 0.0f;

float MinPaddleY;
float MaxPaddleY;
float MinPaddleY2;
float MaxPaddleY2;

float CollisionDist = 0.0f;

float Dist;
float Dist2;

float Xrot = 1.0f;

int Player1Score = 0;
int Player2Score = 0;
int Score = 1;
int Score2 = 1;

//Light
GLfloat LightAmbient[]= { 2.0f, 2.0f, 2.0f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 0.0f, 1.0f };


//Textures
GLuint filter = 0; // Which Filter To Use
GLuint texture[11]; // Storage For 3 Textures
GLuint loop; // Generic Loop Variable

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc




GLvoid BuildFont(GLvoid) // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
HFONT oldfont; // Used For Good House Keeping

base = glGenLists(96); // Storage For 96 Characters

font = CreateFont( -12, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
0, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Arial"); // Font Name

oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want
wglUseFontBitmaps(hDC, 32, 96, base); // Builds 96 Characters Starting At Character 32
SelectObject(hDC, oldfont); // Selects The Font We Want
DeleteObject(font); // Delete The Font
}

GLvoid KillFont(GLvoid) // Delete The Font List
{
glDeleteLists(base, 96); // Delete All 96 Characters
}

GLvoid glPrint(const char *fmt, ...) // Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments

if (fmt == NULL) // If There's No Text
return; // Do Nothing

va_start(ap, fmt); // Parses The String For Variables
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text

glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(base - 32); // Sets The Base Character to 32
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}


AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}

File=fopen(Filename,"r"); // Check To See If The File Exists

if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}

return NULL; // If Load Failed Return NULL
}

int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[11]; // Create Storage Space For The Textures
memset(TextureImage,0,sizeof(void *)*11); // Set The Pointer To NULL

if ((TextureImage[0]=LoadBMP("Data/Crate.bmp")) && // Logo Texture
(TextureImage[1]=LoadBMP("Data/Number00.bmp")) &&
(TextureImage[2]=LoadBMP("Data/Number01.bmp")) &&
(TextureImage[3]=LoadBMP("Data/Number02.bmp")) &&
(TextureImage[4]=LoadBMP("Data/Number03.bmp")) &&
(TextureImage[5]=LoadBMP("Data/Number04.bmp")) &&
(TextureImage[6]=LoadBMP("Data/Number05.bmp")) &&
(TextureImage[7]=LoadBMP("Data/Number06.bmp")) &&
(TextureImage[8]=LoadBMP("Data/Number07.bmp")) &&
(TextureImage[9]=LoadBMP("Data/Number08.bmp")) &&
(TextureImage[10]=LoadBMP("Data/Number09.bmp")))


{
Status=TRUE; // Set The Status To TRUE
glGenTextures(11, &texture[0]); // Create 2 Textures

for (loop=0; loop<11; loop++) // Loop Through All 5 Textures
{
glBindTexture(GL_TEXTURE_2D, texture[loop]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
}
}
for (loop=0; loop<11; loop++) // Loop Through All 5 Textures
{
if (TextureImage[loop]) // If Texture Exists
{
if (TextureImage[loop]->data) // If Texture Image Exists
{
free(TextureImage[loop]->data); // Free The Texture Image Memory
}
free(TextureImage[loop]); // Free The Image Structure
}
}
return Status; // Return The Status
}



GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}



int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine
{
return FALSE; // If Texture Didn't Load Return FALSE
}

glEnable(GL_TEXTURE_2D);


glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

//Lights
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light
glEnable(GL_LIGHT1); // Enable Light One

//Quadric Sphere
quadratic=gluNewQuadric(); // Create A Pointer To The Quadric Object (Return 0 If No Memory) (NEW)
gluQuadricNormals(quadratic, GLU_SMOOTH); // Create Smooth Normals (NEW)
gluQuadricTexture(quadratic, GL_TRUE); // Create Texture Coords (NEW)

BuildFont();

return TRUE; // Initialization Went OK
}


bool Collide(float Obj1X, float Obj1Y, float Obj2X, float Obj2Y)
{

Distance = pow(Obj2X - Obj1X,2) + pow(Obj2Y - Obj1Y,2);
float sqrtf(Distance);

if (Distance < 1.0f)
{
blnCollide = true;
}
else
{
blnCollide = false;
}

return blnCollide;
}

bool Collide2(float Obj1X, float Obj1Y, float Obj2X, float Obj2Y)
{

Distance2 = pow(Obj2X - Obj1X,2) + pow(Obj2Y - Obj1Y,2);
float sqrtf(Distance2);

if (Distance2 < 2.0f)
{
blnCollide2 = true;
}
else
{
blnCollide2 = false;
}

return blnCollide2;
}


void RestartGame()
{
{
Ball.x = 0.0f;
Ball.y = 0.0f;
}

Sleep(1000);
//blnMoveBall = false;

//blnRestart = true;
//Random Ball Angle
}

int moveBall()
{
if (BallAngle > 360) { BallAngle = 0; }
if (BallAngle < 0) { BallAngle = 360; }



if (Ball.y < -5.8f) { blnBallY = true; }
if (Ball.y > 5.8f) { blnBallY = false; }


if (blnBallX == true)
{
++Xrot;
Ball.x += cosf(BallAngle * PI / 180) * Ball.xv / Speed;
}
if (blnBallX == false)
{
--Xrot;
Ball.x -= cosf(BallAngle * PI / 180) * Ball.xv / Speed;
}

if (blnBallY == true)
{
Ball.y += sinf(BallAngle * PI / 180.0f) * Ball.yv / Speed;
//Ball.y += Ball.yv;
}
if (blnBallY == false)
{
Ball.y -= sinf(BallAngle * PI / 180.0f) * Ball.yv / Speed;
//Ball.y -= Ball.yv;
}


MinPaddleY = PaddleY - 1.5f;
MaxPaddleY = PaddleY + 1.5f;

MinPaddleY2 = PaddleY2 - 1.5f;
MaxPaddleY2 = PaddleY2 + 1.5f;

Dist = (Ball.x - PaddleX) - 1.0f;
Dist2 = (Ball.x - PaddleX2);


if (blnCollide == true)
{

//Paddle1 Physx(LOL)
if (blnPaddleUp == true && blnBallY == true)
{
Ball.xv += 1.5f;
Ball.yv = +10.0f;
blnBallX = true;
}
else
{
blnBallX = true;
}
if (blnPaddleUp == true && blnBallY == false)
{
Ball.yv = +2.5f;
blnBallX = true;
}
else
{
blnBallX = true;
}
if (blnPaddleDown == true && blnBallY == true)
{
Ball.yv = +2.5f;
blnBallX = true;
}
else
{
blnBallX = true;
}
if (blnPaddleDown == true && blnBallY == false)
{
Ball.xv += 1.5f;
Ball.yv = +10.0f;
blnBallX = true;
}
else
{
blnBallX = true;
}

}

if (blnCollide2 == true)
{
//Paddle2 Physx(LOL)
if (blnPaddleUp2 == true && blnBallY == true)
{
Ball.xv += 1.5f;
Ball.yv = +10.0f;
blnBallX = false;
}
else
{
blnBallX = false;
}
if (blnPaddleUp2 == true && blnBallY == false)
{
Ball.yv = -0.5f;
blnBallX = false;
}
else
{
blnBallX = false;
}
if (blnPaddleDown2 == true && blnBallY == true)
{
Ball.yv = -0.5f;
blnBallX = false;
}
else
{
blnBallX = false;
}
if (blnPaddleDown2 == true && blnBallY == false)
{
Ball.xv += 1.5f;
Ball.yv = +10.0f;
blnBallX = false;
}
else
{
blnBallX = false;
}

}

if (Ball.xv > 2.5f) { Ball.xv -= 0.001f; }
if (Ball.xv < 1.0f) { Ball.xv += 0.001f; }
if (Ball.xv > 6.5f) { Ball.xv = 6.5f; }

if (Ball.yv > 2.5f) { Ball.yv -= 0.001f; }
if (Ball.yv < 1.0f) { Ball.yv += 0.001f; }
if (Ball.yv > 10.5f) { Ball.yv = 10.5f; }


if (Ball.x < -9.0f)
{
Player2Score += 1;
Score += 1;

if (Score2 > 9) { Score2 = 1; }
//Ball.x = 0.0f;
//Ball.y = 0.0f;

//blnMoveBall = false;

//blnRestart = true;

RestartGame();
blnBallX = true;
}

if (Ball.x > 9.0f)
{
Player1Score += 1;

Score2 += 1;
if (Score2 > 9) { Score2 = 1; }

//RestartGame();
blnBallX = false;
}

return 0;
}


int EnemyAI()
{
if (Ball.x > 1.0)
{
if (PaddleY2 < Ball.y)// && PaddleY2 != BallY)
{
PaddleY2 += 0.009f ;
}

if (PaddleY2 > Ball.y)// && PaddleY2 != BallY)
{
PaddleY2 -= 0.009f ;
}
}
return 0;
}

void NewGame()
{
//Reset Game-New Game

Ball.x = 0.0f;
Ball.y = 0.0f;

PaddleY = 0.0f;
PaddleY2 = 0.0f;

Player1Score = 0;
Player2Score = 0;
}


int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

glLoadIdentity();

//Texture
glBindTexture(GL_TEXTURE_2D, texture[filter]);

//Sphere
glTranslatef(Ball.x,Ball.y,-15.0f);
glRotatef(Xrot,0.0f,0.0f,1.0f);
gluSphere(quadratic,0.5,32,32); // Draw A Sphere With A Radius Of 1 And 16 Longitude And 16 Latitude Segments

glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


glTranslatef(PaddleX,PaddleY,-15.0f);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

// Right Face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( -0.7f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( -0.7f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.7f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.7f, -1.0f, 1.0f);

// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.9f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.9f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, 1.0f, -1.0f);

// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);


glEnd();

glLoadIdentity();


glTranslatef(PaddleX2,PaddleY2,-15.0f);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

// Right Face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( -1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( -1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -1.0f, -1.0f, 1.0f);

// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, 1.0f, -1.0f);

// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);


glEnd();


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//EndTexture
glBindTexture(GL_TEXTURE_2D, 0);


//Texture2
glBindTexture(GL_TEXTURE_2D, texture[Score]);
glTranslatef(-4.0f,-2.5f,-10.0f);

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, 0);

//Texture3
glBindTexture(GL_TEXTURE_2D, texture[Score2]);
glTranslatef(4.0f,-2.5f,-10.0f);

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, 0);

//Text
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(-8.1f,5.6f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("SphereX - %f SphereY - %f ", Ball.x ,Ball.y); // Print GL Text To The Screen

glLoadIdentity();

//Text
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(-8.1f,5.3f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("Player1 - %d Player2 - %d ", Player1Score ,Player2Score); // Print GL Text To The Screen


glLoadIdentity();

//Text
glColor3f(1.0f,1.0f,0.0f);
glTranslatef(-8.1f,5.0f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("Distance - %f Distance - %f blnCollide - %d", Distance, Distance2, blnBallY);

glLoadIdentity();

//Text
glColor3f(0.3f,0.3f,1.0f);
glTranslatef(-8.1f,4.7f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("XVelocity - %f YVelocity - %f", Ball.xv, Ball.yv);

glColor3f(1.0f,1.0f,1.0f);
glLoadIdentity();

if (blnCollide == true || blnCollide2 == true)
{
glLoadIdentity();

//Text
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(-8.1f,4.4f,-15.0f);
glRasterPos2f(0.0f, 0.0f);


glPrint("Collision Detected");

}

EnemyAI();

if (blnMoveBall == true)
{
moveBall();
blnRestart = false;
}

if (blnRestart == true)
{

blnMoveBall = true;
blnRestart = false;
}
Collide(PaddleX - 1.0f,PaddleY,Ball.x,Ball.y);
Collide2(PaddleX2,PaddleY2,Ball.x,Ball.y);

return TRUE; // Everything Went OK
}


GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
{
gluDeleteQuadric(quadratic);

if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer

}

if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL,"Release Of DC And RC Failed.","Exit",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL
}

if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}

if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}

if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
}


BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height

fullscreen=fullscreenflag; // Set The Global Fullscreen Flag

hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = "OpenGL"; // Set The Class Name

if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (fullscreen) // Attempt Fullscreen Mode?
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE
}
}
}

if (fullscreen) // Are We Still In Fullscreen Mode?
{
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
dwStyle=WS_POPUP; // Windows Style
ShowCursor(false); // Hide Mouse Pointer
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
}

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size

// Create The Window
if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
"OpenGL", // Class Name
title, // Window Title
dwStyle | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Window Width
WindowRect.bottom-WindowRect.top, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Dont Pass Anything To WM_CREATE
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

ShowWindow(hWnd,SW_SHOW); // Show The Window
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd); // Sets Keyboard Focus To The Window
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen

if (!InitGL()) // Initialize Our Newly Created GL Window
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

return TRUE; // Success
}

LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{
switch (uMsg) // Check For Windows Messages
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
if (!HIWORD(wParam)) // Check Minimization State
{
active=TRUE; // Program Is Active
}
else
{
active=FALSE; // Program Is No Longer Active
}

return 0; // Return To The Message Loop
}

case WM_SYSCOMMAND: // Intercept System Commands
{
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0; // Prevent From Happening
}
break; // Exit
}

case WM_CLOSE: // Did We Receive A Close Message?
{

PostQuitMessage(0); // Send A Quit Message

return 0; // Jump Back
}

case WM_KEYDOWN: // Is A Key Being Held Down?
{
keys[wParam] = TRUE; // If So, Mark It As TRUE
return 0; // Jump Back
}

case WM_KEYUP: // Has A Key Been Released?
{
keys[wParam] = FALSE; // If So, Mark It As FALSE
return 0; // Jump Back
}

case WM_SIZE: // Resize The OpenGL Window
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
return 0; // Jump Back
}
}

// Pass All Unhandled Messages To DefWindowProc
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{
MSG msg; // Windows Message Structure
BOOL done=FALSE; // Bool Variable To Exit Loop

// Ask The User Which Screen Mode They Prefer
/*if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE; // Windowed Mode
}*/


fullscreen=FALSE;

// Create Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}

//initialize some variables
Speed = 60.0f;
Ball.xv = 2.5f;
Ball.yv = 2.5f;
BallAngle = 20.0f;

while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (active) // Program Active?
{
if (keys[VK_ESCAPE]) // Was ESC Pressed?
{
done=TRUE; // ESC Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
// Draw The Scene



DrawGLScene();
SwapBuffers(hDC); // Swap Buffers (Double Buffering)


}
}

if (keys['L'] && !lp)
{
lp=TRUE;
light=!light;
if (!light)
{
glDisable(GL_LIGHTING);
}
else
{
glEnable(GL_LIGHTING);
}
}
if (!keys['L'])
{
lp=FALSE;
}

if (keys[VK_UP])
{
blnPaddleUp = true;
PaddleY += 0.2f;

if (PaddleY > 5.2f)
{
PaddleY = 5.19f;
}
}

if (!keys[VK_UP])
{
blnPaddleUp = false;
}

if (keys[VK_DOWN])
{
blnPaddleDown = true;
PaddleY -= 0.2f;

if (PaddleY < -5.2f)
{
PaddleY = -5.19f;
}
}

if (!keys[VK_DOWN])
{
blnPaddleDown = false;
}

if (keys[VK_LEFT])
{


}

if (keys[VK_RIGHT])
{


}

if (keys['F'] && !fp)
{
fp=TRUE;
filter+=1;
if (filter>11)
{
filter=0;
}
}
if (!keys['F'])
{
fp=FALSE;
}

if (keys['A'])
{
//Ball.yv += 0.001f;
BallAngle += 0.01f;
}

if (keys['D'])
{
//Ball.x += 0.03f;
BallAngle -= 0.01f;
}

if (keys['W'])
{
blnPaddleUp2 = true;
PaddleY2 += 0.1f;

if (PaddleY2 > 5.2f)
{
PaddleY2 = 5.19f;
}
}
if (!keys['W'])
{
blnPaddleUp2 = false;
}

if (keys['S'])
{
blnPaddleDown2 = true;
PaddleY2 -= 0.1f;

if (PaddleY2 < -5.2f)
{
PaddleY2 = -5.19f;
}
}
if (!keys['S'])
{
blnPaddleDown2 = false;
}


if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
}
}

// Shutdown

KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}



Share this post


Link to post
Share on other sites
Quote:
Original post by dormlock
I beg to differ on not calling sleep. If your using threads in your engine then your going to need to yeild time in some way. Sleep is the easiest to do. The consequencies of not calling sleep when running multi threaded is that your other threads may not run. I forget to have one of my threads sleep at all and instead it was running a tight loop to dispatch events and it slowed the UI of every program running including the debugger except for iTunes go figure.


You're absolutely correct, when threading I use sleep to make sure all my threads get the time they need. However, I was in my earlier posts assuming that this is a non-threaded app, in which case, I would almost never acutaly call sleep(). If I need to pause a game, I like to know exactly how long it will pause, not at least how long it will pause.

Share this post


Link to post
Share on other sites
Blueseed,

if I understand your code correctly, your sleep() is called within RestartGame(), which is called by moveBall(), which is called in DrawGLScene(), which is called BEFORE SwapBuffers.

This means that your new frame is presented AFTER the sleep, and this is why you see the Ball at 0.0/0.0 only after the sleep.

You somehow need to call the sleep function after SwapBuffers, or have a call to SwapBuffers in your RestartGame function right BEFORE the sleep call.


Hope that makes sense.

Share this post


Link to post
Share on other sites
Blueseed,

if I understand your code correctly, your sleep() is called within RestartGame(), which is called by moveBall(), which is called in DrawGLScene(), which is called BEFORE SwapBuffers.

This means that your new frame is presented AFTER the sleep, and this is why you see the Ball at 0.0/0.0 only after the sleep.

You somehow need to call the sleep function after SwapBuffers, or have a call to SwapBuffers in your RestartGame function right BEFORE the sleep call.


Hope that makes sense.

Share this post


Link to post
Share on other sites

This topic is 4215 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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