Sign in to follow this  

opengl help

This topic is 3588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how to make an opengl window with a menu bar i am using dev c++ where in the examples , there is an example of opengl window and also an example of windows menu program . i dont know how to mix the two programs that is , create a single program in which opengl window comes with an menu too on it thanks

Share this post


Link to post
Share on other sites
Well make your OpenGL window and when you create the Window itself in the class regisration is when you would add the Menu Bar.

Here is an example of creating a menu bar.

First you need to make the menu bar as a resource file like so.


ID_MENU MENU DISCARDABLE
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&New", ID_FILE_NEW
MENUITEM "&Open", ID_FILE_OPEN
MENUITEM "&Save", ID_FILE_SAVE
MENUITEM SEPARATOR
MENUITEM "E&xit", ID_FILE_EXIT
END
POPUP "&Do"
BEGIN
MENUITEM "&blaaaa", ID_DO_SOMETHING
MENUITEM "lalalalallal", ID_DO_SOMETHING_ELSE
END
POPUP "&Help"
BEGIN
MENUITEM "&About db", ID_HELP_ABOUT
END
END



Then in the actualy sorce file for your window define your control ID's


//This should be in the header file for your main code or at the top

enum int ID_FILE_NEW = 100;
enum int ID_FILE_OPEN = 101;
enum int ID_FILE_SAVE = 102;
enum int ID_FILE_EXIT = 103;
enum int ID_DO_SOMETHING = 104;
enum int ID_DO_SOMETHING_ELSE = 105;
enum int ID_HELP_ABOUT = 106;




No in the class registration for the window witch looks like this



WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = ghInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

//This line here is for loading your menu to the window
WndClass.lpszMenuName = "ID_MENU";
WndClass.lpszClassName = gszClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);




Ok and in the WinProc for the window it self.



LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

//This is where all the code really gets to work with what happens when your
//menu item gets clicked on.

case WM_COMMAND:
switch(LOWORD(wParam)) {
case ID_FILE_NEW:
MessageBox(hwnd, "You Clicked New File", "Menu", 0);
break;
case ID_FILE_OPEN:
MessageBox(hwnd, "Blaaa ... Open File", "Menu", 0);
break;
case ID_FILE_SAVE:
MessageBox(hwnd, "Save Save Save", "Menu", 0);
break;
case ID_FILE_EXIT:
PostQuitMessage(0);
case ID_DO_SOMETHING:
MessageBox(hwnd, "blaaaaaa!", "Menu", 0);
break;
case ID_DO_SOMETHING_ELSE:
MessageBox(hwnd, "lalalalllaa", "Menu", 0);
break;
case ID_HELP_ABOUT:
MessageBox(hwnd, "db 0wnZ j00!!", "About", 0);
break;
}
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}




Ok well i hope that helps.

Regards Jouei.

Share this post


Link to post
Share on other sites
Hmm ok i supose my next question is it in windows because i hade assumed so.
Ps im not sure what that error is about since i don't use Dev C++ i use VC++ Express 2005.

Do you have all the files needed to do windows if you are im assuming since you hade made a openGl window that you do. If you post your code then mabye i can help you out better.

ps When you post your code remmber to use source tags.

Regads Jouei.

Share this post


Link to post
Share on other sites
ok
here goes my code
there are two files in it
one resource file (which is exactly the same which u posted)
other cpp file (which i am posting)


//This should be in the header file for your main code or at the top

enum int ID_FILE_NEW = 100;
enum int ID_FILE_OPEN = 101;
enum int ID_FILE_SAVE = 102;
enum int ID_FILE_EXIT = 103;
enum int ID_DO_SOMETHING = 104;
enum int ID_DO_SOMETHING_ELSE = 105;
enum int ID_HELP_ABOUT = 106;



#include <windows.h>
#include <gl/gl.h>

// 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)
{
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 = "ID_MENU";
wc.lpszClassName = "GLSample";
// WndClass.lpszClassName = gszClassName;
// WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);


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

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glRotatef(theta, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
glEnd();
glPopMatrix();
SwapBuffers( hDC );

theta += 1.0f;

}
}
// 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:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage( 0 );
return 0;
}
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 );
}

Share this post


Link to post
Share on other sites
According to this thread you might be having a problem with your makefile.

I am assuming that you are compiling and linking your resource script (*.rc) file?

Also, insure you are building this as a Win32 application.

Share this post


Link to post
Share on other sites

This topic is 3588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this