Hah, C++ is nothing.
Wait till you apply something like OpenGL to it.
It's really not that compilcated once you practice, but this is just an example of what to look for. Really makes me appreciate DarkBasic sometimes.
This is just to set up your program for OpenGL.
(Don't forget to link your headers first)
#define WIN32_LEAN_AND_MEAN // This trims down the libraries.
#define VC_LEANMEAN
// All the necessary includes...
#include <windows.h> // The windows header file.
#include <gl/gl.h> // Standard opengl include.
#include <gl/glu.h> // Opengl utilities.
#include <gl/glaux.h> // auxiliary functions.
// function Prototypes
void RenderScene();
bool InitializeGL();
void SetupPixelFormat(HDC hDC);
// This is the global device context. It is used to swap frames
// every time our program renders a scene.
HDC g_HDC; // Global device context.
void SetupPixelFormat(HDC hDC)
{
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of structure.
1, // always 1.
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGl
PFD_DOUBLEBUFFER, // support double buffering
PFD_TYPE_RGBA, // support RGBA
16, // 32 bit color mode
0, 0, 0, 0, 0, 0, // ignore color bits
0, // no alpha buffer
0, // ignore shift bit
0, // no accumulation buffer
0, 0, 0, 0, // ignore accumulation bits.
16, // number of depth buffer bits.
0, // number of stencil buffer bits.
0, // 0 means no auxiliary buffer
PFD_MAIN_PLANE, // The main drawing plane
0, // this is reserved
0, 0, 0 }; // layer masks ignored.
// this chooses the best pixel format and returns index.
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
// This set pixel format to device context.
SetPixelFormat(hDC, nPixelFormat, &pfd);
// Remember that its not important to fully understand the pixel format,
// just remember to include in all of your applications and you'll be
// good to go.
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC; // Rendering context.
static HDC hDC; // Device context.
int width, height; // The window width and height.
switch(message)
{
case WM_CREATE: // Windows creation.
hDC = GetDC(hwnd); // This gets the device context for our window.
g_HDC = hDC; // Assigns the global device context to this one.
SetupPixelFormat(hDC); // Call the pixel format function.
hRC = wglCreateContext(hDC); // Creates the rendering context.
wglMakeCurrent(hDC, hRC); // Makes the rendering context.
return 0;
break;
case WM_CLOSE: // Close message.
case WM_DESTROY:
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC); // Deletes the rendering context.
PostQuitMessage(0); // Says close the program.
return 0;
break;
case WM_SIZE: // re-size message.
height = HIWORD(lParam); // This gets the height of the window.
width = LOWORD(lParam); // This gets the width of the window.
if(height==0) // we don't want it to be possible for a
{ // height of 0. If it is 0 me make it 1.
height = 1;
}
glViewport(0, 0, width, height);// resets the viewport to new dimensions.
glMatrixMode(GL_PROJECTION); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.
// calculate the aspect ratio of the window.
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.
return 0;
break;
default: // Always have a default in case.
break;
}
// What this does is pass all of the unhandled messages to DefWindowProc
return (DefWindowProc(hwnd, message, wParam, lParam));
}
// This is the WinMain class. Remember that every C and C++ program required
// a main() function, well every Windows program require a WinMain().
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg; // A message variable.
WNDCLASSEX windowClass; // Your Window class.
HWND hwnd; // The Window handle.
bool isFinished; // This will be used to check if the program is done or not.
// This is the Window class. Each attribute is defined for the creation of the
// window.
windowClass.cbSize = sizeof(WNDCLASSEX); // size of the WNDCLASSEX structure.
windowClass.style = CS_HREDRAW | CS_VREDRAW; // style of the window.
windowClass.lpfnWndProc = WndProc; // Address to the windows procedure.
windowClass.cbClsExtra = 0; // Extra class information.
windowClass.cbWndExtra = 0; // Extra window information.
windowClass.hInstance = hInstance; // Handle of application Instance.
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);// Handle of application Icon.
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);// mouse cursor
windowClass.hbrBackground = NULL; // background color.
windowClass.lpszMenuName = NULL; // name of the main menu.
windowClass.lpszClassName = "UltimateGameProgrammingClass";// window class name.
windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);// icon when minimized.
// You must register you class with Windows. What this does is if the class is
// not registered, then the program will close right away.
if(!RegisterClassEx(&windowClass))
return 0;
hwnd = CreateWindowEx(NULL,// The extended window style.
"UltimateGameProgrammingClass",// window Class name.
"A Triangle by the Programming Ace.",// window name.
WS_OVERLAPPEDWINDOW | WS_VISIBLE |// The window style.
WS_SYSMENU | WS_CLIPCHILDREN |// window style.
WS_CLIPSIBLINGS,// window style.
100, 100,// window x, y coordinate.
400, 400,// window width and height.
NULL,// handle to parent window.
NULL,// menu.
hInstance,// handle to app instance.
NULL); // pointer to window creation data.
if(!hwnd)
return 0;
ShowWindow(hwnd, SW_SHOW); // This shows the window.
UpdateWindow(hwnd); // This forces a paint message.
isFinished = false; // False = running, True = not running.
// Since our program is running we will of cource
// set this to false. For this demos it has
// no real purpose but in future demos we will
// use this for when the user presses the esc key
// the program will stop.
// Can't run if any initialization fails.
if(!InitializeGL())
isFinished = true;
while(!isFinished)// While the program is running...
{
PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);
if(msg.message == WM_QUIT)// If the application gets a quit message...
{
isFinished = true; // Then quit the program.
}
else // else render the scene.
{
RenderScene(); // Render and display the next frame.
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;// End of the program.
}
bool InitializeGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Clear the screen to black.
// If all went well we return true.
return true;
}
// This will render every frame of our program.
void RenderScene()
{
// Always clear the screen and depth buffer then reset modleview matrix.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen.
glLoadIdentity(); //Reset modelview matrix for new frame.
glTranslatef(0.0f, 0.0f, -5.0f); // This says move back 5 units before drawing.
glBegin(GL_TRIANGLES); // This starts drawing the triangle.
glVertex3f(0.0f, 1.0f, 0.0f); // The first point.
glColor3f(0.0f, 0.0f, 1.0f); // Second point color.
glVertex3f(1.0f, 0.0f, 0.0f); // Second point.
glColor3f(0.0f, 0.0f, 1.0f); // Third point color.
glVertex3f(-1.0f, 0.0f, 0.0f); // Third point.
glEnd(); // Stop drawing triangle.
// You use glVertex3f to define the x, y, and z points of a vertex.
// For our triangle it will look like this...
//
// . (Point 1 (0.0, 1.0, 0.0)
//
// (Point 2 (1.0, 0.0, 0.0) . . (Point 3 (-1.0, 0.0, 0.0)
SwapBuffers(g_HDC); // Display the new frame by swapping the
// old frame with the new one.
}
Note: This is from a tutorial I followed at Ultimate Games Programming.
A book? I hate book. Book is stupid.
(Formerly known as Yellow)