Code:
// Include headers
#include <windows.h>
#include <gl/gl.h>
#include <math.h>
#define PI 3.14159265
// Declare functions
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, // Windows procedure
WPARAM wParam, LPARAM lParam); // Windows procedure contd..
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC); // Initialize OpenGL
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC); // DisableOpenGL
void draw_scene(); // Draw OpenGL Scene
// Declare global variables
char class_name[] = "OpenGL 2D Pong"; // Windows class name
char window_name[] = "Pong"; // Window title name
int window_width = 500; // Window width
int window_height = 500; // Window height
WNDCLASS wc; // Windows class handle
HWND hWnd; // Window Handle
HDC hDC; // Device context handle
HGLRC hRC; // Rendering context handle
MSG msg; // Message handle
bool bQuit = FALSE; // Quit the loop
bool keys[256]; // Array for the keypress routine
// Windows Main function
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
// register window class
wc.style = CS_OWNDC; // Window style - own decive context
wc.lpfnWndProc = WndProc; // Which windows procedure to use
wc.cbClsExtra = 0; // Extra parameters, 0
wc.cbWndExtra = 0; // Extra parameters, 0
wc.hInstance = hInstance; // Program instance handle
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); // Program icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW); // Mouse cursor
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // Background color of our screen
wc.lpszMenuName = NULL; // Menu Name
wc.lpszClassName = class_name; // Name of our class
RegisterClass (&wc); // Register the class
// Create the window
hWnd = CreateWindow (
class_name, window_name,
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, window_width, window_height,
NULL, NULL, hInstance, NULL);
// Enable OpenGL
EnableOpenGL(hWnd, &hDC, &hRC);
// Main Program Loop
while (!bQuit)
{
// Check for messages in the queue
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
{
// Draw the OpenGL Scene
draw_scene();
}
}
// Shutdown OpenGL
DisableOpenGL(hWnd, hDC, hRC);
// Destroy the window
DestroyWindow(hWnd);
return msg.wParam;
}
// Windows Procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
break;
case WM_DESTROY:
return 0;
break;
case WM_KEYDOWN:
keys[wParam] = true;
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
break;
}
return 0;
break;
case WM_KEYUP:
keys[wParam] = false;
break;
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 for the window
*hDC = GetDC(hWnd);
// Set PFD
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 the RC and make it current
*hRC = wglCreateContext(*hDC);
wglMakeCurrent(*hDC, *hRC);
}
// Disable OpenGL
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd, hDC);
}
// Draw OpenGL Scene
void draw_scene()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f(0.2f, 1.0f, 0.2f);
glBegin(GL_LINES);
//for (int i = 0; i < 360; i++)
//{
glVertex2f(0.0f, 0.0f);
glVertex2f(cos((270 * (PI / 180)) * 0.9f), sin((270 * (PI / 180)) * 0.9f));
//}
glEnd();
glPopMatrix();
SwapBuffers(hDC);
}