sprite.cpp

来自「小型的3D游戏引擎」· C++ 代码 · 共 64 行

CPP
64
字号
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "sprite.h"
#include "../global.h"


////////////////////////////////////////////////////////////////////////////////////////////

GcSprite::GcSprite():
width(0),
height(0)
{
	// Constructing the sprite object
}

////////////////////////////////////////////////////////////////////////////////////////////

GcSprite::~GcSprite()
{
}

////////////////////////////////////////////////////////////////////////////////////////////

bool GcSprite::Load(char *fileName, int sWidth, int sHeight)
{
	// Save the width and height
	width = sWidth;
	height = sHeight;
	
	// Create the texture
	if(!texture.Create(fileName))
	{
		MessageBox(NULL, "Unable to load sprite.", "ERROR", MB_OK);
		return false;
	}

	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////

void GcSprite::Draw(float x, float y)
{
	glPushMatrix();

		// Move to the correct palce
		glTranslatef(x - width / 2, y - height / 2, 0.0f);

		// Set the correct texture
		texture.Bind();

		// Draw the sprite (a textured quad)
		glBegin(GL_QUADS);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f((float)width, 0.0f, 0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f((float)width, (float)height, 0.0f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, (float)height, 0.0f);
		glEnd();

	glPopMatrix();
}

////////////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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