Code:
/**************************
* Includes
*
**************************/
#include <windows.h>
#include <gl/gl.h>
bool keys[256] = {false};
float xme = 0.0;
float xcom = 0.0;
float xball, yball;
class pong {
public:
pong();
~pong();
float move(int i, float v);
float coords[8];
float xmove, ymove;
void gldisplay(float c[8], float x, float y);
};
/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
pong player, cpu, ball;
player.xmove = 0.0;
cpu.xmove = 0.0;
ball.xmove = 0.0;
ball.ymove = 0.0;
glVertex2f (0.05, -0.05);
glVertex2f (0.05, 0.05);
glVertex2f (-0.05, 0.05);
glVertex2f (-0.05, -0.05);
ball.coords[0] = 0.05;
ball.coords[1] = -0.05;
ball.coords[2] = 0.05;
ball.coords[3] = 0.05;
ball.coords[4] = -0.05;
ball.coords[5] = 0.05;
ball.coords[6] = -0.05;
ball.coords[7] = -0.05;
player.coords[0] = -0.25;
player.coords[1] = -0.9;
player.coords[2] = -0.25;
player.coords[3] = -0.8;
player.coords[4] = 0.25;
player.coords[5] = -0.8;
player.coords[6] = 0.25;
player.coords[7] = -0.9;
cpu.coords[0] = 0.25;
cpu.coords[1] = 0.9;
cpu.coords[2] = 0.25;
cpu.coords[3] = 0.8;
cpu.coords[4] = -0.25;
cpu.coords[5] = 0.8;
cpu.coords[6] = -0.25;
cpu.coords[7] = 0.9;
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 256, 256,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
/* OpenGL animation code goes here */
glPushMatrix();
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0f, 1.0f, 1.0f);
//glOrtho (0.0, 1.0, 0.0, 1.0, -1.0, 1.0); // could someone explain how to
// set this code correctly? every time i use it, i can't see anything
player.gldisplay(player.coords, player.xmove, 0.0);
cpu.gldisplay(cpu.coords, 0.0, 0.0);
ball.gldisplay(ball.coords, 0.0,0.0);
if (keys[VK_RIGHT] == true && player.xmove < 0.7) {
player.xmove = 0.015;
}
if (keys[VK_LEFT] == true && player.xmove > -0.7) {
player.xmove = -0.015;
//^^^^^^^^^^^^^^^^^^
// why does this work? shouldn't
// this number have to get bigger for the gltranslate() to work?
}
if (keys[VK_RIGHT] == false && keys[VK_LEFT] == false){
player.xmove = 0.0;
}
SwapBuffers (hDC);
theta += 1.0f;
Sleep (5);
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
keys[wParam] = true;
return 0;
case WM_KEYUP:
keys[wParam] = false;
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
float pong::move(int i, float v) {
if (i == 2) {
return v + 0.0002;
}
else if (i == 1) {
return v - 0.0002;
}
}
void pong::gldisplay(float c[9], float x, float y)
{
glTranslatef(x , y, 0.0);
glBegin (GL_QUADS);
glVertex2f (c[0], c[1]);
glVertex2f (c[2], c[3]);
glVertex2f (c[4], c[5]);
glVertex2f (c[6], c[7]);
glEnd ();
}
pong::pong(){}
pong::~pong(){}