游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1929|回复: 0

尝试把OpenGL的初始化写成一个类,但是注册窗口就失败了?

[复制链接]

2

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2009-10-21 16:41:00 | 显示全部楼层 |阅读模式
OpenGL的各个初始函式我打算放到一个类里面,于是这样子写:

COpenGL.h
Main.cpp
COpenGL.cpp

COpenGL.h:

#ifndef _COPENGL_H_
#define _COPENGL_H_

class OpenGLInit
{
public:
        int nPixelFormat;   //像素格式变量
        HDC hDC;
        PIXELFORMATDESCRIPTOR pfd;

        void SetupPixelFormat();
        void Render();

protected:
private:
};

#endif


COpenGL.cpp

#include <windows.h>
#include <gl/GL.h>
#include <gl/glaux.h>
#include <gl/GLU.h>
#include <gl/glut.h>

#include "COpenGL.h"

void OpenGLInit::SetupPixelFormat()
{
                this->pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
                this->pfd.nVersion=1;
                this->pfd.dwFlags=PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
                this->pfd.iPixelType=PFD_TYPE_RGBA;
                this->pfd.cColorBits=32;
                this->pfd.cRedBits=this->pfd.cRedShift=0;
                this->pfd.cGreenBits=this->pfd.cGreenShift=0;
                this->pfd.cBlueBits=this->pfd.cBlueShift=0;
                this->pfd.cAlphaBits=this->pfd.cAlphaShift=0;
                this->pfd.cAccumBits=0;
                this->pfd.cAccumBlueBits=this->pfd.cAccumGreenBits=this->pfd.cAccumRedBits=0;
                this->pfd.cDepthBits=16;
                this->pfd.iLayerType=PFD_MAIN_PLANE;
                this->pfd.bReserved=this->pfd.cAccumAlphaBits=0;
                this->pfd.dwDamageMask=this->pfd.dwLayerMask=this->pfd.dwVisibleMask=0;
                this->pfd.bReserved=0;
                this->pfd.cAuxBuffers=0;
               
                this->nPixelFormat=ChoosePixelFormat(this->hDC,&this->pfd);
                SetPixelFormat(this->hDC,this->nPixelFormat,&this->pfd);
}

void OpenGLInit::Render()
{

}

Main.cpp

#define  WIN32_LEAN_AND_MEAN   

#include <windows.h>
#include <gl/GL.h>
#include <gl/glaux.h>
#include <gl/GLU.h>
#include <gl/glut.h>
#include <tchar.h>

#include "COpenGL.h"

#pragma comment(lib,"glaux.lib")
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"glut32.lib")

HDC g_HDC;   //全局设备环境

OpenGLInit OpenGLIn;

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
        static HGLRC hRC;
/*        static HDC hDC;*/
        int width,height;

        switch(message)
        {
        case WM_CREATE:   //创建窗口

                OpenGLIn.hDC=GetDC(hwnd);
                g_HDC=OpenGLIn.hDC;
                OpenGLIn.SetupPixelFormat();  //调用像素格式设置参数

                //创建绘制环境并将其设置为当前的绘制环境
                hRC=wglCreateContext(OpenGLIn.hDC);
                wglMakeCurrent(OpenGLIn.hDC,hRC);
                return 0;
                break;

        case WM_CLOSE:  //关闭窗口

                wglMakeCurrent(OpenGLIn.hDC,NULL);
                wglDeleteContext(hRC);

                PostQuitMessage(0);
                return 0;
                break;

        default:
                break;
        }

        return(DefWindowProc(hwnd,message,wParam,lParam));
}

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
        WNDCLASSEX windowClass;
        HWND hwnd;
        MSG msg;
        bool done;

        windowClass.cbSize=sizeof(WNDCLASSEX);
        windowClass.style=CS_HREDRAW|CS_VREDRAW;
        windowClass.lpfnWndProc=WndProc;
        windowClass.cbClsExtra=0;
        windowClass.cbWndExtra=0;
        windowClass.hInstance=hInstance;
        windowClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
        windowClass.hCursor=LoadCursor(NULL,IDC_ARROW);
        windowClass.hbrBackground=NULL;
        windowClass.lpszClassName=NULL;
        windowClass.lpszClassName=_T("OpenGL");
        windowClass.hIconSm=LoadIcon(NULL,IDI_WINLOGO);

        //注册窗口类
        if(!RegisterClassEx(&windowClass))
                return 0;
        //创建窗口
        hwnd=CreateWindowEx(NULL,_T("OpenGL"),_T("Ruby's OpenGL Light Station"),WS_OVERLAPPEDWINDOW|WS_VISIBLE|WS_SYSMENU|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
                100,100,400,400,NULL,NULL,hInstance,NULL);

        //检查窗口是否创建失败
        if(!hwnd)
                return 0;

        ShowWindow(hwnd,SW_SHOW);  //显示窗口
        UpdateWindow(hwnd);  //刷新窗口

        done=false;

        //主消息循环
        while (!done)
        {
                PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
                if(msg.message==WM_QUIT)  //检测是否收到退出消息
                {
                        done=true;
                }
                else
                {
                        OpenGLIn.Render();
                }
        }

}


就这样子写的,但是运行提示在    if(!RegisterClassEx(&windowClass))
        return 0;  处发生内存读取错误,哪里错误了内?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-20 04:16

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表