gkernel.cpp

来自「巫魔问题求解」· C++ 代码 · 共 292 行

CPP
292
字号
#include "GKernel.h"
#include <fstream>
#include <Wum_Server/src/ServerMap.h>

using namespace _graphics;
//////////////////////////////////////////////
static class Initer
{
public:
    Initer(void) { GKernel::TheGKernel().Init(); }
} staIniter;

//////////////////////////////////////////////
GKernel::GKernel(void)
{
    this->mIsBindedExplorer = false;
    this->mpExplorer = NULL;
    this->mpGE = NULL;
}

//////////////////////////////////////////////
GKernel::GKernel(const GKernel &from)
{
    
}

//////////////////////////////////////////////
GKernel::~GKernel(void)
{
}

//////////////////////////////////////////////
GKernel &GKernel::TheGKernel(void) 
{
    static GKernel staTheOnlyInstance;
    return staTheOnlyInstance;
}

//////////////////////////////////////////////
void GKernel::GL_Init(void)
{
    glEnable(GL_TEXTURE_2D);
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_FLAT);
}

//////////////////////////////////////////////
void GKernel::Init(void)
{
    static bool is_first = true;
    if (!is_first) 
    {
        return; 
    }
    int argc = 1;
    char **argv;
    char *name = "Wumpus2D";
    argv = &name;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    GL_Init();
    glutDisplayFunc(GLUT_Display);
    glutReshapeFunc(GLUT_Reshape);
    glutMouseFunc(GLUT_MouseFunc);
    glutKeyboardFunc(GLUT_KeyFunc);
}

//////////////////////////////////////////////
void GKernel::GLUT_Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	glLoadIdentity();									// Reset The View
	glTranslatef(0.0f,0.0f,-10.0f);



    if (TheGKernel().mpGE)
    {
        _base::Map &map = TheGKernel().mpGE->TheWorldModel().TheServerMap();
        for (unsigned int i = 0; i < map.GetXBorder(); i++)
        {
            for (unsigned int j = 0; j < map.GetYBorder(); j++)
            {
                if (TheGKernel().mIsBindedExplorer &&
                    TheGKernel().mpExplorer &&
                    TheGKernel().mpExplorer->GetX() == i &&
                    TheGKernel().mpExplorer->GetY() == j)
                {
                    TMapUintString::const_iterator iter = TheGKernel().mMapExplorerName.find(TheGKernel().mpExplorer->GetDirection());
                    if (iter != TheGKernel().mMapExplorerName.end())
                    {
                        TheGKernel().DrawRect(i,j,1,1,iter->second);
                    }
                }
                else
                {
                    TMapUintString::const_iterator iter = TheGKernel().mMapEnumName.find(map.GetCell(i,j).GetDrawInfo());
                    if (iter != TheGKernel().mMapEnumName.end())
                    {
                        TheGKernel().DrawRect(i,j,1,1,iter->second);
                    }
                }
            }
        }
    }
    glutSwapBuffers();
}

//////////////////////////////////////////////
void GKernel::GLUT_Reshape(GLsizei w, GLsizei h)
{
    glViewport(0,0,(GLsizei) w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,4.0,0.0,4.0,0.5,10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

//////////////////////////////////////////////
void GKernel::GLUT_MouseFunc(int button, int state, int x, int y)
{
    if (GKernel::TheGKernel().mMouseFuncs.find(button) != GKernel::TheGKernel().mMouseFuncs.end() &&
        GKernel::TheGKernel().mMouseFuncs[button].find(state) != GKernel::TheGKernel().mMouseFuncs[button].end())
    {

        for (unsigned int i=0; i<GKernel::TheGKernel().mMouseFuncs[button][state].size(); i++)
        {
            (*GKernel::TheGKernel().mMouseFuncs[button][state][i])();
        }
        glutPostRedisplay();
    }
}

//////////////////////////////////////////////
void GKernel::GLUT_KeyFunc(unsigned char key, int x, int y)
{
    if (GKernel::TheGKernel().mKeyFuncs.find(key) != GKernel::TheGKernel().mKeyFuncs.end())
    {

        for (unsigned int i=0; i<GKernel::TheGKernel().mKeyFuncs[key].size(); i++)
        {
            (*GKernel::TheGKernel().mKeyFuncs[key][i])();
        }
    }
    for (unsigned int i=0; i<GKernel::TheGKernel().mKeyFuncsAll.size(); i++)
    {
        (*GKernel::TheGKernel().mKeyFuncsAll[i])();
    }
    glutPostRedisplay();
}

/////////////////////////////////////////////
bool GKernel::BindBMP(unsigned int type, char *filename)
{
    AUX_RGBImageRec *pRGB = NULL;

    std::ifstream is;					
	if (!filename)
	{
		return false;
	}

    is.open((std::string("../../Textures/") + filename + ".bmp").c_str());

	if (!is)
	{
        return false;
	}
    pRGB = auxDIBImageLoad((std::string("../../Textures/") + filename + ".bmp").c_str());

    this->mMapEnumName.insert(TMapUintString::value_type(type,filename));
    this->mMapNameTexture.insert(TMapStringGLuint::value_type(filename, GLuint()));
    GLuint &textureid = mMapNameTexture.find(filename)->second;
    glGenTextures(1, &textureid);
    glBindTexture(GL_TEXTURE_2D, textureid);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, pRGB->sizeX, pRGB->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pRGB->data);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	if (pRGB)									// If Texture Exists
	{
		if (pRGB->data)							// If Texture Image Exists
		{
			free(pRGB->data);					// Free The Texture Image Memory
		}

		free(pRGB);								// Free The Image Structure
	}

	return true;
}

/////////////////////////////////////////////
bool GKernel::BindBMP_Explorer(unsigned int type, char *filename)
{
    AUX_RGBImageRec *pRGB = NULL;

    std::ifstream is;					
	if (!filename)
	{
		return false;
	}

    is.open((std::string("../../Textures/") + filename + ".bmp").c_str());

	if (!is)
	{
        return false;
	}
    pRGB = auxDIBImageLoad((std::string("../../Textures/") + filename + ".bmp").c_str());

    this->mMapExplorerName.insert(TMapUintString::value_type(type,filename));
    this->mMapNameTexture.insert(TMapStringGLuint::value_type(filename, GLuint()));
    GLuint &textureid = mMapNameTexture.find(filename)->second;
    glGenTextures(1, &textureid);
    glBindTexture(GL_TEXTURE_2D, textureid);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, pRGB->sizeX, pRGB->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pRGB->data);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	if (pRGB)									// If Texture Exists
	{
		if (pRGB->data)							// If Texture Image Exists
		{
			free(pRGB->data);					// Free The Texture Image Memory
		}

		free(pRGB);								// Free The Image Structure
	}

	return true;
}

///////////////////////////////////////////////
void GKernel::BindGameEngine(_server::GameEngine &ge)
{
    this->mpGE = &ge;
}

///////////////////////////////////////////////
void GKernel::BindExplorer(_server::Explorer &explorer)
{
    this->mIsBindedExplorer = true;
    this->mpExplorer = &explorer;
}


///////////////////////////////////////////////
void GKernel::MainLoop(void)
{
    glutMainLoop();
}

///////////////////////////////////////////////
bool GKernel::DrawRect(int x, int y, int width, int height, std::string texture_name)
{
    TMapStringGLuint::const_iterator texture_iter = TheGKernel().mMapNameTexture.find(texture_name);
    if (texture_iter == TheGKernel().mMapNameTexture.end())
    {
        return false;
    }
	glBindTexture(GL_TEXTURE_2D, texture_iter->second);
	glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f( x,  y,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( x+width,  y,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( x+width,  y+height,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( x,  y+height,  1.0f);
	glEnd();
    return true;
}

//////////////////////////////////////////////////////////
void GKernel::RegistKeyFunction(unsigned char key, TVoidFunc func)
{
    this->mKeyFuncs[key].push_back(func);
}

//////////////////////////////////////////////////////////
void GKernel::RegistMouseFunction(int button, int eve, TVoidFunc func)
{
    this->mMouseFuncs[button][eve].push_back(func);
}

//////////////////////////////////////////////////////////
void GKernel::RegistKeyFunction_All(TVoidFunc func)
{
    this->mKeyFuncsAll.push_back(func);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?