OpenGL and Win98 Console box

Do I need to create a win98 console box object to have it work with an Opengl window?

I used NeHe's OpenGL framework as a starting point for my prog. (http://nehe.gamedev.net/lesson01.asp)

I'm trying to avoid using MFC for providing a UI for inputting a user-defined clipping polygon for my graphics class. I want to use the cout/cin commands. I also read something in MSDN that mentioned "QuickWin" library.... is this what I should be looking for instead of using (i haven't been able to find anything on QuickWin)? or should I just give up, and write a second message handler for a petzold style dialog box? I wasn't sure how to get this second message handler to work in conjunction with the OpenGL window's message handler, or will Win98 handle this for me?

Here is my current code:
[b]cse418hw3.h[/b]
[code]
#include
#include
#include

int Sign( int number );
void DrawLineBres( CPoint P1, CPoint P2, bool FrameBuffer[33][33]);
void DrawClip( CPoint PolyPoint[7], bool FrameBuffer[33][33] );
[/code]


[b]cse418hw3.cpp[/b]
[code]
#define STRICT

//OpenGL window creation routines from the OpenGL Tutorials on
// NeHe Productions website. http://nehe.gamedev.net/lesson01.asp

#include "stdafx.h"
#include "cse418hw3.h"

HGLRC hRC = NULL;
HDC hDC = NULL;
HWND hWnd = NULL;
HINSTANCE hInstance;

bool keys[256];
bool active = true;
bool fullscreen = true;

CWinApp theApp;
using namespace std;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int _tmain(CPoint PolyPoint[7])
{
int iSides = 0;
int i = 0;
char * input = " ";
bool cancelled = false;

int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
//-- HN Pre-scene generation User input code
iSides = -1;
input = " ";
while( (int)input < 0 ||
( iSides < 0 || iSides > 7 ) )
{
cout << "How many sides for your Clipping polygon (3-7)? (0 to return to Windows)" << endl;
cin >> input;
iSides = (int)input;
}

if(iSides)
{
i=0;
cout << "
";
cout << "Clipping %d-sided Polygon Definition:
" << iSides;
cout << "------------------------------------------
";
cout << "
";
cout << " Polygons must be defined for a 32x32 grid.
";
cout << " The lower left corner of the display grid is (1,1), so the upper
";
cout << " left corner of the 'A' polygon is (4,4).
";
cout << "
";
cout << " Enter 999 to cancel polygon and return to Windows.
";
cout << "
";
cout << "-------------------------------------------
";
cout << "
";
while(i<=iSides)
{
i++;
cout << "Enter Polygon Vertex #" << i << " (1-32)" << endl;
input = " ";
while( (int)input<0 || input!="999" )
{
cout << "X-coordinate: ";
cin >> input ;
}
if(input=="999")
{
i=iSides;
cancelled = true;
MessageBox(NULL, "Polygon has been canceled.", "(void)Polygon",MB_OK|MB_ICONINFORMATION);
}
else
{
PolyPoint[i].x = (int)input;
input = " ";
}
while( (int)input<0 || input!="999" )
{
cout << "Y-coordinate: ";
cin >> input;
}
if(input=="999")
{
i=iSides;
if(!cancelled)
{
MessageBox NULL, "Polygon has been canceled.", "(void)Polygon",MB_OK|MB_ICONINFORMATION);
}
}
else
{
PolyPoint[i].y = (int)input;
}
}
}
}
return nRetCode;
}

int Sign( int number )
{
if(number > 0)
{return(1);}
else
{ if(number = 0)
{return(0);}
else
{return(-1);}
}
}

void DrawLineBres( CPoint P1, CPoint P2, bool FrameBuffer[33][33])
{
int x = P1.x;
int y = P1.y;
int dx = abs(P2.x - P1.x);
int dy = abs(P2.y - P1.y);
int Signx = Sign(dx);
int Signy = Sign(dy);
int interchange;

if(dy > dx)
{
int temp = dx;
dx = dy;
dy = temp;
interchange = 1;
}
else
{interchange = 0;}

int error = 2*dy - dx;

for(int i=1;i<=dx;i++)
{
FrameBuffer[x][y] = true;
while(error > 0)
{
if(interchange == 1)
{x =+ Signx;}
else
{y =+ Signy;}
error = error - 2*dx;
}

if(interchange==1)
{y =+ Signy;}
else
{x =+ Signx;}
error = error + 2*dy;
}
}

void DrawClip( CPoint PolyPoint[7], bool FrameBuffer[33][33] )
{MessageBox(NULL, "more to do
", "DrawClip", MB_OK);}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{height=1;}
glViewport(0, 0, width, height);
//set up perspective
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Calculate aspect ration of the window
gluPerspective(32.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return true;
}

int DrawGLScene( bool FrameBuffer[33][33] )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
int i;
int j;
//Draw A
CPoint A1;A1.x = 4;A1.y = 4;
CPoint A2;A2.x = 4;A2.y = 26;
CPoint A3;A3.x = 20;A3.y = 26;
CPoint A4;A4.x = 28;A4.y = 18;
CPoint A5;A5.x = 28;A5.y = 4;
CPoint A6;A6.x = 21;A6.y = 4;
CPoint A7;A7.x = 21;A7.y = 8;
CPoint A8;A8.x = 10;A8.y = 8;
CPoint A9;A9.x = 10;A9.y = 4;
CPoint H1;H1.x = 10;H1.y = 12;
CPoint H2;H2.x = 10;H2.y = 20;
CPoint H3;H3.x = 17;H3.y = 20;
CPoint H4;H4.x = 21;H4.y = 16;
CPoint H5;H5.x = 21;H5.y = 12;

DrawLineBres(A1, A2, FrameBuffer);
DrawLineBres(A2, A3, FrameBuffer);
DrawLineBres(A3, A4, FrameBuffer);
DrawLineBres(A4, A5, FrameBuffer);
DrawLineBres(A5, A6, FrameBuffer);
DrawLineBres(A6, A7, FrameBuffer);
DrawLineBres(A7, A8, FrameBuffer);
DrawLineBres(A8, A9, FrameBuffer);
DrawLineBres(A9, A1, FrameBuffer);
DrawLineBres(H1, H2, FrameBuffer);
DrawLineBres(H2, H3, FrameBuffer);
DrawLineBres(H3, H4, FrameBuffer);
DrawLineBres(H4, H5, FrameBuffer);
DrawLineBres(H5, H1, FrameBuffer);

for(i=0;i<=32;i++)
{
for(j=0;j<=32;j++)
{
if(FrameBuffer[i][j])
{
glColor3f( 1.0f, 1.0f, 1.0f );
glTranslatef((GLfloat)i, (GLfloat)j, 0.0f);
glBegin(GL_QUADS);
glVertex3f( -1.0f, 1.0f, 0.0f );
glVertex3f( 1.0f, 1.0f, 0.0f );
glVertex3f( 1.0f, -1.0f, 0.0f );
glVertex3f( -1.0f, -1.0f, 0.0f );
glEnd();
}
}
}
glFlush();
return true;
}

GLvoid KillGLWindow(GLvoid)
{
if(fullscreen)
{
ChangeDisplaySettings(NULL,0);
ShowCursor(true);
}

if (hRC)
{
if( !wglMakeCurrent( NULL, NULL ) )
{
MessageBox( NULL, "Release of DC and RC failed.","SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
}

if( !wglDeleteContext( hRC ) )
{
MessageBox( NULL, "Release Rendering Context failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION );
}
hRC = NULL;
}

if ( hDC && !ReleaseDC(hWnd,hDC) )
{
MessageBox( NULL, "Release Device Context Failed.", "SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION );
hDC = NULL;
}

if ( hDC && !DestroyWindow(hWnd) )
{
MessageBox( NULL, "Could not Release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
hWnd = NULL;
}

if ( !UnregisterClass("OpenGL", hInstance))
{
MessageBox( NULL, "Could not Unregister class.", "SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION );
hInstance = NULL;
}
}


bool CreateGLWindow( char* title, int width, int height, int bits, bool fullscreenflag )
{
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExStyle;
DWORD dwStyle;

RECT WindowRect;
WindowRect.left = (long)0;
WindowRect.right = (long)width;
WindowRect.top = (long)0;
WindowRect.bottom= (long)height;

fullscreen = fullscreenflag;

hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground= NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName= "OpenGL";

if (!RegisterClass(&wc))
{
MessageBox(NULL, "Failed to register the window class.", "ERROR", MB_OK | MB_ICONEXCLAMATION );
return FALSE;
}

/*[full screen code is here... but later I disable fullscreen mode, so this stuff never gets executed. I will clip from my posting to save space(refer to nehe's website if you need to see it.] -- HN*/

if (fullscreen)
{
dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_POPUP;
ShowCursor(false);
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle=WS_OVERLAPPEDWINDOW;
}

AdjustWindowRectEx( &WindowRect, dwStyle, FALSE, dwExStyle);

if (!(hWnd=CreateWindowEx( dwExStyle, "OpenGL", title, WS_CLIPSIBLINGS |WS_CLIPCHILDREN |dwStyle, 0, 0, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, NULL, NULL, hInstance, NULL)))
{
KillGLWindow();
MessageBox(NULL, "Window Creation Error.","ERROR",MB_OK | MB_ICONEXCLAMATION);
return false;
}

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,bits,0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 16,0,0,PFD_MAIN_PLANE,0, 0, 0, 0};

if (!(hDC=GetDC(hWnd))) {
KillGLWindow(); MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; }

if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))
{
KillGLWindow(); MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; }

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) {
KillGLWindow(); MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; }

if (!(hRC=wglCreateContext(hDC))) {
KillGLWindow(); MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; }

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

ShowWindow(hWnd,SW_SHOW); SetForegroundWindow(hWnd); SetFocus(hWnd); ReSizeGLScene(width, height);
if (!InitGL()) {
KillGLWindow(); MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; }
return TRUE;
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_ACTIVATE: {
if (!HIWORD(wParam)) // Check Minimization State
{
active=TRUE;
}
else
{
active=FALSE;
}

return 0;
}

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

MessageBox(NULL, "Fullscreen mode has been disabled.", "Sorry!", MB_OK|MB_ICONINFORMATION);
fullscreen = FALSE;

//if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
//{
// fullscreen=FALSE; // Windowed Mode
//}

// Create Our OpenGL Window
char MyTitle[] = "NeHe's OpenGL Framework -- Press ESC: quit, SPACE: Clipping Polygon";
CPoint WindowMax;
WindowMax.x = 512;
WindowMax.y = 512;
if (!CreateGLWindow(MyTitle,WindowMax.x,WindowMax.y,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}

//--HN
int iSides = 0;
char * input = " ";
CPoint PolyPoint[7];
bool FrameBuffer[33][33];
int i;
int j;

for(i=0;i<=32;i++)
{
//initialize framebuffer
for(j=0;j<=32;j++)
{
FrameBuffer[i][j] = false;
}
}



//--HN


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
{
if( keys[VK_SPACE] )// Not Time To Quit, Update Screen
{
keys[VK_SPACE] = FALSE;

_tmain( PolyPoint );

DrawGLScene( FrameBuffer ); // Draw The Scene
DrawClip( PolyPoint, FrameBuffer );
SwapBuffers(hDC);
}//end-if-SPACE
else
{
//clear the screen and redraw A polygon
// if(MessageBox(NULL,"Reset screen to original polygon?","Redraw Main Window",MB_YESNO|MB_ICONQUESTION)==IDYES)
// {
DrawGLScene( FrameBuffer ); // Draw The Scene
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
// }
}
}//end-if-ESC
}//end-if(active)

/* Full screen mode disabled:
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(MyTitle,WindowMax.x,WindowMax.y,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
*/
}
}

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

[/code]

Comments

  • Start a new Windows32 Application Project, then paste that code into it, and remember to add the nessesary libraries to it. That code will NOT work in a console window, as it their is no publicly known way to drawn in a windows console window.



    ----
    A figure divine
    Her radiance shadows sun
    Like wind is her song


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion