Click here to Skip to main content
15,898,134 members
Articles / Mobile Apps / Android

Diggers: cross-platform 2D game

Rate me:
Please Sign up or sign in to vote.
4.80/5 (17 votes)
5 Nov 2012CPOL4 min read 33.5K   1   43  
2D cross-platfrom game using SDL and Open GLES 2.0
#include "GUI/GUIWindows.h"
#include "AI/Dwarfs.h"



Window* Window::m_CurrentWindow;
unsigned char Window::m_Exit;
bool Window::m_NeedUpdateDayTime;



WindowContent::WindowContent(float _xLeft, float _yDown, float _xRight, float _yUp, GLuint _textureID)
			: Sprite(_xLeft, _yDown, _xRight, _yUp, _textureID)
{
	m_XLeft = _xLeft;
	m_YDown = _yDown;
	m_XRight = _xRight;
	m_YUp = _yUp;
};


WindowContent::~WindowContent(void)
{
};


bool WindowContent::Update(float _dt)
{
	Sprite::Update(_dt);
	return false;
};



WindowCaption::WindowCaption(float _xLeft, float _yDown, float _xRight, float _yUp, GLuint _textureID)
			: Sprite(_xLeft, _yDown, _xRight, _yUp, _textureID)
{
	m_XLeft = _xLeft;
	m_YDown = _yDown;
	m_XRight = _xRight;
	m_YUp = _yUp;
};


WindowCaption::~WindowCaption(void)
{
};


bool WindowCaption::Update(float _dt)
{
	Sprite::Update(_dt);
	return false;
};



Window::Window(void)
{
	m_WindowCaption = NULL;
	m_TextSprite = NULL;
	m_TextSpriteCaption = NULL;
	m_WindowContent = NULL;
	m_ButtonWindowOk = NULL;
	m_ButtonWindowCancel = NULL;
};


Window::~Window(void)
{
	if (NULL != m_WindowCaption) delete m_WindowCaption;
	if (NULL != m_TextSprite) delete m_TextSprite;
	if (NULL != m_TextSpriteCaption) delete m_TextSpriteCaption;
	if (NULL != m_WindowContent) delete m_WindowContent;
	if (NULL != m_ButtonWindowOk) delete m_ButtonWindowOk;
	if (NULL != m_ButtonWindowCancel) delete m_ButtonWindowCancel;
};


bool Window::Update(float _dt)
{
	if ((m_NeedUpdateDayTime) && (NULL != m_TextSprite))
	{
		std::wstring strDate;
		DateTime::DayToString(strDate);
		std::wstring strTime;
		DateTime::TimeToString(strTime);
		strDate += strTime;

		m_TextSprite->SetMultiText(strDate.c_str());
	}

	if (NULL != m_WindowContent) m_WindowContent->Update(_dt);
	if (NULL != m_WindowCaption) m_WindowCaption->Update(_dt);
	if (NULL != m_TextSprite) m_TextSprite->Update(_dt);
	if (NULL != m_TextSpriteCaption) m_TextSpriteCaption->Update(_dt);
	if (NULL != m_ButtonWindowOk) m_ButtonWindowOk->Update(_dt);
	if (NULL != m_ButtonWindowCancel) m_ButtonWindowCancel->Update(_dt);

	return false;
};


void WindowGUI::CalculateUICoords(int _x, int _y, float& _xlRel, float& _ydRel, float& _xrRel, float& _yuRel)
{
	float dXRel = 1.0f / Sprite::m_ScreenWidth;
	float dYRel = 1.0f / Sprite::m_ScreenHeight;

	float signX = 1.0f;
	float signY = 1.0f;
	if (_x < 0)
		signX = -1.0f;
	if (_y < 0)
		signY = -1.0f;

	_xlRel = (-1.0f + signX * dXRel * _x * Sprite::m_ButtonSize);
	_xrRel = (_xlRel + signX * dXRel * Sprite::m_ButtonSize);

	_xlRel *= signX * 0.99f - _x * 0.01f;
	_xrRel *= signX * 0.99f - _x * 0.01f;

	_yuRel = 1.0f - signY * dYRel * _y * Sprite::m_ButtonSize;
	_ydRel = _yuRel - signY * dYRel * Sprite::m_ButtonSize;

	_yuRel *= signY * 0.99f;
	_ydRel *= signY * 0.99f;
};


WindowGUI::WindowGUI(void)
{
	float xlRel = 0;
	float xrRel = 0;
	float yuRel = 0;
	float ydRel = 0;

	CalculateUICoords(0, 0, xlRel, ydRel, xrRel, yuRel);
	m_IndicatorDayNightTime = new IndicatorDayNightTime(xlRel, ydRel, xrRel, yuRel);
	m_IndicatorDayNightTime->m_TextureID = Texture::m_TextureIDSun;

	for (unsigned char i = 0; i < Sprite::m_HappynessGradesCount; i++)
	{
		CalculateUICoords(0, Sprite::m_HappynessGradesCount - i, xlRel, ydRel, xrRel, yuRel);
		m_IndicatorsHappyness[i] = new IndicatorHappyness(xlRel, ydRel, xrRel, yuRel, i);
	}

	m_IndicatorsHappyness[5]->m_TextureID = Texture::m_TextureIDEmoticonsCoolFace;
	m_IndicatorsHappyness[4]->m_TextureID = Texture::m_TextureIDEmoticonsSmilingFace;
	m_IndicatorsHappyness[3]->m_TextureID = Texture::m_TextureIDEmoticonsSimpleFace;
	m_IndicatorsHappyness[2]->m_TextureID = Texture::m_TextureIDEmoticonsWorriedFace;
	m_IndicatorsHappyness[1]->m_TextureID = Texture::m_TextureIDEmoticonsSadFace;
	m_IndicatorsHappyness[0]->m_TextureID = Texture::m_TextureIDEmoticonsEvilFace;


	CalculateUICoords(-1, 2, xlRel, ydRel, xrRel, yuRel);
	m_ButtonDebugSpeed = new ButtonDebugSpeed(xlRel, ydRel, xrRel, yuRel);
	m_ButtonDebugSpeed->m_TextureID = Texture::m_TextureIDDebugSpeedOFF;

	CalculateUICoords(-1, 3, xlRel, ydRel, xrRel, yuRel);
	m_ButtonDebugReveal = new ButtonDebugReveal(xlRel, ydRel, xrRel, yuRel);
	m_ButtonDebugReveal->m_TextureID = Texture::m_TextureIDDebugRevealOFF;

	CalculateUICoords(-1, 4, xlRel, ydRel, xrRel, yuRel);
	m_ButtonDebugRain = new ButtonDebugRain(xlRel, ydRel, xrRel, yuRel);
	m_ButtonDebugRain->m_TextureID = Texture::m_TextureIDDebugRainOFF;

	CalculateUICoords(0, -1, xlRel, ydRel, xrRel, yuRel);
	m_ButtonPick = new ButtonPick(xlRel, ydRel, xrRel, yuRel);
	m_ButtonPick->m_TextureID = Texture::m_TextureIDPickUnSelected;

//	CalculateUICoords(1, -1, xlRel, ydRel, xrRel, yuRel);
//	m_ButtonAxe = new ButtonAxe(xlRel, ydRel, xrRel, yuRel);
//	m_ButtonAxe->m_TextureID = Texture::m_TextureIDAxeUnSelected;

	CalculateUICoords(1, -1, xlRel, ydRel, xrRel, yuRel);
	m_ButtonStop = new ButtonStop(xlRel, ydRel, xrRel, yuRel);
	m_ButtonStop->m_TextureID = Texture::m_TextureIDStopUnSelected;

	CalculateUICoords(3, -1, xlRel, ydRel, xrRel, yuRel);
	m_ButtonFurnace = new ButtonFurnace(xlRel, ydRel, xrRel, yuRel);
	m_ButtonFurnace->m_TextureID = Texture::m_TextureIDFurnaceUnSelected;

	CalculateUICoords(-1, -1, xlRel, ydRel, xrRel, yuRel);
	m_ButtonHistory = new ButtonScroll(xlRel, ydRel, xrRel, yuRel);
	m_ButtonHistory->m_TextureID = Texture::m_TextureIDButtonScroll;

	CalculateUICoords(-2, -1, xlRel, ydRel, xrRel, yuRel);
	m_ButtonAward = new ButtonAward(xlRel, ydRel, xrRel, yuRel);
	m_ButtonAward->m_TextureID = Texture::m_TextureIDButtonAward;

	CalculateUICoords(-3, -1, xlRel, ydRel, xrRel, yuRel);
	m_ButtonSmelting = new ButtonSmelting(xlRel, ydRel, xrRel, yuRel);
	m_ButtonSmelting->m_TextureID = Texture::m_TextureIDButtonSmelting;

	Window::m_CurrentWindow = NULL;
	Window::m_Exit = Window::eWindowExitNotExists;
	Window::m_NeedUpdateDayTime = false;
};


WindowGUI::~WindowGUI(void)
{
	delete m_ButtonDebugSpeed;
	delete m_ButtonDebugReveal;
	delete m_ButtonDebugRain;
	delete m_ButtonPick;
//	delete m_ButtonAxe;
	delete m_ButtonStop;
	delete m_ButtonFurnace;
	delete m_ButtonAward;
	delete m_ButtonSmelting;
	delete m_ButtonHistory;
	delete m_IndicatorDayNightTime;
	for (unsigned char i = 0; i < Sprite::m_HappynessGradesCount; i++)
	{
		delete m_IndicatorsHappyness[i];
	}

};


bool WindowGUI::Update(float _dt)
{
	m_ButtonDebugSpeed->Update(_dt);
	m_ButtonDebugReveal->Update(_dt);
	m_ButtonDebugRain->Update(_dt);
	m_ButtonPick->Update(_dt);
//	m_ButtonAxe->Update(_dt);
	m_ButtonStop->Update(_dt);
	m_ButtonFurnace->Update(_dt);
	m_ButtonAward->Update(_dt);
	m_ButtonSmelting->Update(_dt);
	m_ButtonHistory->Update(_dt);
	m_IndicatorDayNightTime->Update(_dt);

	for (unsigned char i = 0; i < Sprite::m_HappynessGradesCount; i++)
	{
		m_IndicatorsHappyness[i]->Update(_dt);
	}
	Sprite::m_NeedRecreateHappynessIndicator = false;

	return false;
};



WindowText::WindowText(const wchar_t* _caption, const wchar_t* _text)
	:Window()
{
	float xTextLeft = -0.5f; float yTextDown = 0.0f; float xTextRight = 0.5f; float yTextUp = 0.5f;

	GLuint textureText;
	glActiveTexture(GL_TEXTURE0);
	glGenTextures(1, &textureText);

	m_TextSprite = new TextSprite(xTextLeft, yTextDown, xTextRight, yTextUp, textureText);
	m_TextSprite->SetMultiText(_text, TextSprite::m_FontCommon);
};


WindowText::~WindowText(void)
{
};


bool WindowText::Update(float _dt)
{
	Window::Update(_dt);
	m_TextSprite->Update(_dt);
	return false;
};


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions