回 帖 发 新 帖 刷新版面

主题:练习C++编的一个小程序

空格开始,场景是鱼缸里,代码再检查一下再帖吧

回复列表 (共18个回复)

沙发

如果缺少GLUT的话下载一个GLUT32.DLL

板凳

新程序出炉,发来贺电共通庆祝~

3 楼

我主要是想让大家帮我挑挑错,今天回家把代码贴出来,昨天太晚了

4 楼

好消息就是cxx忙完已经重出江湖了~有他在你的代码就有人查了:)

5 楼

主要是按照我目前所理解的C++来写的,对于一些规范还在学习中,希望大家多帮我,谢谢.
OopGame.hpp:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<tchar.h>
#include<gl/gl.h>
#include<gl/glext.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<windows.h>
#include<vector>
#include<typeinfo>
#include<string>
#include<istream>
#include<ostream>
#include<fstream>
#include<iostream>
using namespace std;

6 楼

OopGame.cpp:
#define MAX_OBJECT 1000
#define MAX_TYPE 100
//
#include "OopGame.hpp"
//
char TextureFileName[][15]={"Bg.bmp","Fish1.bmp","Fish2.bmp","Buble.bmp"};
char *p_TextureBuffer[MAX_TYPE]={NULL};
char TempString[50];
//
#include "Class_GetResource.hpp"
#include "Class_InterFace.hpp"
InterFace *pInterFace[MAX_OBJECT]={NULL};
#include "Class_Light.hpp"
#include "Class_Draw.hpp"
#include "Class_Fish.hpp"
#include "Class_Buble.hpp"
#include "Class_Enviroment.hpp"
#include "Class_Wnd.hpp"
//
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR CmdLine,int CmdShow)
{
    GetResource *pGetResource=new GetResource();
    pGetResource->GetTextures();
    OpenGL_Window *pOpenGL_Window=new OpenGL_Window(hInst);
    delete pGetResource;
    delete pOpenGL_Window;
    return 0;
}

7 楼

Class_Wnd.hpp:
class OpenGL_Window
{
    friend LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    private:
        HINSTANCE hInst;
        HWND hwnd;
        WNDCLASSEX wnd;
        MSG msg;
        HDC hdc;
        HGLRC hrc;
        PIXELFORMATDESCRIPTOR pfd;
        int iPixelFormat;
    protected:
        //
    public:
        OpenGL_Window(HINSTANCE);
        virtual ~OpenGL_Window();
        //Member functions
        static LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
        void MakeWindow();
        void BeOpenGL();
        void InitOpenGL();
        void Display();
        static void InitGame();
};
/****************************************************************/
/*******************成员函数*************************************/
/****************************************************************/
inline OpenGL_Window::OpenGL_Window(HINSTANCE hInstance)
{
    hInst=hInstance;
    MakeWindow();
    return;
}
//
inline OpenGL_Window::~OpenGL_Window()
{
    wglMakeCurrent(NULL,NULL);
    wglDeleteContext(hrc);
    ReleaseDC(hwnd,hdc);
    return;
}
//
inline LRESULT CALLBACK OpenGL_Window::WndProc(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM lParam)
{
    switch(umsg)
    {
        case WM_CREATE:
            srand(time(NULL));
            SetTimer(hwnd,0,500,NULL);
            break;
        case WM_DESTROY:
            KillTimer(hwnd,0);
            PostQuitMessage(0);
            break;
        case WM_TIMER:
            for(int i=0;i<MAX_OBJECT;i++)
            {
                if(pInterFace[i]==NULL)
                {
                    pInterFace[i]=new Buble(3);
                    pInterFace[i]->ShouldExist=true;
                    break;
                }
            }
            break;
        case WM_KEYUP:
            if(wParam==VK_ESCAPE)
            {
                PostMessage(hwnd,WM_DESTROY,0,0);
            }
            else if(wParam==VK_SPACE)
            {
                InitGame();
            }
            break;
        case WM_SIZE:
            glViewport(0,0,LOWORD(lParam),HIWORD(lParam));
            break;
        default:
            return DefWindowProc(hwnd,umsg,wParam,lParam);
    }
    return 0;
}
//
inline void OpenGL_Window::MakeWindow()
{
    wnd.cbSize=sizeof(WNDCLASSEX);
    wnd.hInstance=hInst;
    wnd.lpszClassName="MyClass";
    wnd.lpfnWndProc=OpenGL_Window::WndProc;
    wnd.style=CS_HREDRAW|CS_VREDRAW;
    wnd.cbClsExtra=0;
    wnd.cbWndExtra=0;
    wnd.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wnd.hIconSm=wnd.hIcon;
    wnd.hCursor=LoadCursor(NULL,IDC_ARROW);
    wnd.lpszMenuName=NULL;
    wnd.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    RegisterClassEx(&wnd);

8 楼

hwnd=CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,"MyClass","OopGame",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,NULL,NULL,hInst,NULL);
    ShowWindow(hwnd,SW_SHOWDEFAULT);
    UpdateWindow(hwnd);
    BeOpenGL();
    InitOpenGL();
    while(msg.message!=WM_QUIT)
    {
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            Display();
        }
    }
    return;
}
//
inline void OpenGL_Window::Display()//Display function
{
    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    for(int i=0;i<MAX_OBJECT;i++)
    {
        if(pInterFace[i]!=NULL&&pInterFace[i]->ShouldExist==true)
        {
            pInterFace[i]->CallEveryTime();
        }
        if(pInterFace[i]!=NULL&&pInterFace[i]->ShouldExist==false)
        {
            delete pInterFace[i];
            pInterFace[i]=NULL;
        }
    }
    SwapBuffers(hdc);
    return;
}
//
inline void OpenGL_Window::BeOpenGL()
{
    hdc=GetDC(hwnd);
    pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion=1;
    pfd.dwFlags=PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
    pfd.iPixelType=PFD_TYPE_RGBA;
    pfd.cColorBits=24;
    pfd.cRedBits=0;
    pfd.cRedShift=0;
    pfd.cGreenBits=0;
    pfd.cGreenShift=0;
    pfd.cBlueBits=0;
    pfd.cBlueShift=0;
    pfd.cAlphaBits=0;
    pfd.cAlphaShift=0;
    pfd.cAccumBits=0;
    pfd.cAccumRedBits=0;
    pfd.cAccumGreenBits=0;
    pfd.cAccumBlueBits=0;
    pfd.cAccumAlphaBits=0;
    pfd.cDepthBits=32;
    pfd.cStencilBits=0;
    pfd.cAuxBuffers=0;
    pfd.iLayerType=PFD_MAIN_PLANE;
    pfd.bReserved=0;
    pfd.dwLayerMask=0;
    pfd.dwVisibleMask=0;
    pfd.dwDamageMask=0;
    iPixelFormat=ChoosePixelFormat(hdc,&pfd);
    SetPixelFormat(hdc,iPixelFormat,&pfd);
    hrc=wglCreateContext(hdc);
    wglMakeCurrent(hdc,hrc);
    return;
}
inline void OpenGL_Window::InitOpenGL()
{
    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER,.5);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_TEXTURE_2D);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(120,1.33,1,80000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,20000,0,0,0,0,1,0);
    return;
}
//
inline void OpenGL_Window::InitGame()
{
    pInterFace[0]=new Enviroment(0);
    pInterFace[0]->ShouldExist=true;
    for(int i=1;i<25;i++)
    {
        pInterFace[i]=new Fish(rand()-20000,rand()-20000,0,15000,15000,1);
        pInterFace[i]->ShouldExist=true;
    }
    return;
}

9 楼

Class_Fish.hpp:
class Fish:public Draw
{
    private:
        float D_x,D_y;
    protected:
        //
    public:
        Fish(float,float,float,int);
        Fish(float,float,float,float,float,int);
        virtual ~Fish();
        void CallEveryTime();
        void Swim();
};
//
inline Fish::Fish(float _x,float _y,float _z,int i_Texture)
{
    x=_x;
    y=_y;
    z=_z;
    BindTexture(p_TextureBuffer[i_Texture]);
    ShouldExist=true;
    return;
}
//
inline Fish::Fish(float _x,float _y,float _z,float _Width,float _Height,int i_Texture)
{
    x=_x;
    y=_y;
    D_x=(rand()-20000)*2;
    D_y=(rand()-20000)*2;
    z=_z;
    Width=_Width;
    Height=_Height;
    BindTexture(p_TextureBuffer[i_Texture]);
    ShouldExist=true;
    return;
}
//
inline Fish::~Fish()
{
    //
    return;
}
//
inline void Fish::CallEveryTime()
{
    Swim();
    DrawToScreen();
    return;
}
//
inline void Fish::Swim()
{
    float Temp_x;
    if(x<D_x+100&&x>D_x-100)
    {
        D_x=(rand()-16384)*2.5;
        D_y=(rand()-16384)*2.5;
    }
    if(x>D_x)
    {
        RotateAngel=180;
        x-=50;
    }
    else if(x<D_x)
    {
        RotateAngel=0;
        x+=50;
    }
    if(y>D_y)
    {
        y-=50;
    }
    else if(y<D_y)
    {
        y+=50;
    }
    return;
}

10 楼

Class_Draw.hpp:
class Draw:public InterFace
{
    private:
        GLuint iTexture;
    protected:
        void BindTexture(char *);
    public:
        float RotateAngel;
        float x,y,z,Width,Height;
        Draw();
        virtual ~Draw();
        virtual void DrawToScreen();
};
//
inline Draw::Draw()
{
    Width=4000;
    Height=6000;
    return;
}
//
inline Draw::~Draw()
{
    glDeleteTextures(1,&iTexture);
    return;
}
//
inline void Draw::DrawToScreen()
{
    float TempX;
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
        glRotatef(RotateAngel,0,1,0);
        if(RotateAngel==0)
        {
            TempX=x;
        }
        else if(RotateAngel==180)
        {
            TempX=0-x;
        }
        glTranslatef(TempX,y,z);
        glBindTexture(GL_TEXTURE_2D,iTexture);
        glBegin(GL_QUADS);
            glTexCoord2f(0,1);
            glVertex3f(-Width/2,Height/2,0);
            glTexCoord2f(1,1);
            glVertex3f(Width/2,Height/2,0);
            glTexCoord2f(1,0);
            glVertex3f(Width/2,-Height/2,0);
            glTexCoord2f(0,0);
            glVertex3f(-Width/2,-Height/2,0);
        glEnd();
    glPopMatrix();
    return;
}
//
inline void Draw::BindTexture(char *TextureBuffer)
{
    glGenTextures(1,&iTexture);
    glBindTexture(GL_TEXTURE_2D,iTexture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,256,256,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,TextureBuffer+0x36);
    return;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册