My goal was to make a simple scene that displays nothing but a red background. Instead, for some reason, it’s showing me a black scene. What am I doing wrong here? I’ll post the entire code since I don’t know what sections to look for.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include "winmain.h"
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
// Macros
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define MsgError(msg) { MessageBox(NULL, msg, "Error", MB_OK | MB_ICONERROR); }
#define ALIGN32 __declspec(align(32))
// Globals
const char *g_szClassName = "WNDCLASS1";
const char *g_szWinTitle = "OpenGL Application";
const int WIDTH = 800;
const int HEIGHT = 600;
HWND g_hWnd = NULL;
HINSTANCE g_hInstance;
HGLRC hRC = NULL;
bool active = TRUE, fullscreen = TRUE;
// Prototypes
void Game_Main();
void Game_Destroy();
void GetInput();
// Resize & Initialize the OpenGL Scene
GLvoid ReSizeGLScene(GLsizei w, GLsizei h) {
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)w/(GLfloat)h, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Init OpenGL Scene Here
int InitGL(GLvoid) {
glShadeModel(GL_SMOOTH); // enable smooth shading
glClearColor(1.0f, 0.0f, 0.0f, 0.5f); // black background
glClearDepth(1.0f); // depth buffer setup
glEnable(GL_DEPTH_TEST); // enable depth testing
glDepthFunc(GL_LEQUAL); // what kind of testing to do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // really nice perspective calculations
return TRUE;
}
int DrawGLScene(GLvoid) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
HWND hWnd = NULL;
MSG msg;
// Build the parent window class
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH) (GetStockObject(BLACK_BRUSH));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = g_szClassName;
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS | CS_OWNDC;
// Register the parent window class
if (!RegisterClassEx(&wc)) {
MsgError("Registering the parent window class failed!");
return 0;
}
// Create the parent window
hWnd = CreateWindowEx(NULL, g_szClassName, g_szWinTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);
if (hWnd == NULL) {
MsgError("Creating the parent window failed.");
return 0;
}
// Save globals
g_hWnd = hWnd;
g_hInstance = hInstance;
// Center the parent window
int xPos = (GetSystemMetrics(SM_CXSCREEN) - WIDTH) / 2;
int yPos = (GetSystemMetrics(SM_CYSCREEN) - HEIGHT) / 2;
SetWindowPos(g_hWnd, NULL, xPos, yPos, WIDTH, HEIGHT, SWP_NOZORDER | SWP_NOSIZE);
// Show & Update Window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
InitGL();
ReSizeGLScene(WIDTH, HEIGHT);
// Message Loop
while (TRUE) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Game logic
Game_Main();
}
Game_Destroy();
return (int) msg.lParam;
}
// Main game loop
void Game_Main() {
GetInput();
DrawGLScene();
}
// Release objects and resources here
void Game_Destroy() {
}
// Get user input
void GetInput() {
if (KEYDOWN(VK_ESCAPE))
SendMessage(g_hWnd, WM_CLOSE, 0, 0);
}