Horizontal compass only works once?
by Richard Preziosi · in Torque Game Engine · 10/02/2010 (4:49 pm) · 0 replies
So I incorporated Beefy's Horizontal Compass, the resource is kind of weird to get to, so I will post the .cc and .h files below. The problem is though that it is only working once after a fresh compile, and then after that it crashes without any error reports in the console.
I've also noticed that after the one time it works, if you go into the UI editor and try readding the compass, as soon as you set a bitmap it causes the same crash. This leads me to think that the bitmap is not being set properly, or through proper methods in the coding. Can anyone look at the below and tell me what the problem could be, I'm at a loss.
guiHorizCompassCtrl.h
guiHorizCompassCtrl.cc
I've also noticed that after the one time it works, if you go into the UI editor and try readding the compass, as soon as you set a bitmap it causes the same crash. This leads me to think that the bitmap is not being set properly, or through proper methods in the coding. Can anyone look at the below and tell me what the problem could be, I'm at a loss.
guiHorizCompassCtrl.h
//-----------------------------------------------------------------------------
// Torque Game Engine
//
// Copyright (c) 2001 GarageGames.Com
// Portions Copyright (c) 2001 by Sierra Online, Inc.
//-----------------------------------------------------------------------------
#ifndef _GUIHORIZCOMPASSCTRL_H_
#define _GUIHORIZCOMPASSCTRL_H_
#ifndef _GUIBITMAPCTRL_H_
#include "gui/controls/guiBitmapCtrl.h"
#endif
struct CameraQuery
{
SimObject* object;
F32 nearPlane;
F32 farPlane;
F32 fov;
MatrixF cameraMatrix;
};
class GuiHorizCompassCtrl : public GuiBitmapCtrl
{
private:
typedef GuiBitmapCtrl Parent;
F32 mOffset;
protected:
StringTableEntry mOuterBitmapName;
TextureHandle mOuterTextureHandle;
F32 mFontWidth;
public:
//creation methods
DECLARE_CONOBJECT(GuiHorizCompassCtrl);
GuiHorizCompassCtrl();
static void initPersistFields();
//Parental methods
bool onWake();
void onSleep();
void setOuterBitmap(const char *name);
// adjust graphic position in regard to the font/letter size of the compass
void setFontWidth(const F32 width);
void setOuterBitmap(const TextureHandle &handle);
void onRender(Point2I offset, const RectI &updateRect);
};
#endifguiHorizCompassCtrl.cc
//-----------------------------------------------------------------------------
// Torque Game Engine
//
// Portions Copyright (c) 2001-2002 GarageGames.Com
// Portions Copyright (c) 2001-2002 by Sierra Online, Inc.
//
// Horizontal compass by Stefan "Beffy" Moises
// The basic code (well, almost everything... ;-) for this compass gui
// was written by Xavier "eXoDuS" Amado.
// (with special thanks go to Matt for his compass code)
//-----------------------------------------------------------------------------
#include "console/console.h"
#include "console/consoleTypes.h"
#include "dgl/dgl.h"
#include "game/game.h"
#include "game/gameConnection.h"
#include "game/fps/guiHorizCompassCtrl.h"
IMPLEMENT_CONOBJECT(GuiHorizCompassCtrl);
GuiHorizCompassCtrl::GuiHorizCompassCtrl(void)
{
mOuterBitmapName = StringTable->insert("");
mFontWidth = 16.0f;
}
void GuiHorizCompassCtrl::initPersistFields()
{
Parent::initPersistFields();
addField("OuterRingBitmap", TypeFilename, Offset(mOuterBitmapName, GuiHorizCompassCtrl));
addField("FontWidth", TypeF32, Offset(mFontWidth, GuiHorizCompassCtrl));
removeField("wrap");
removeField("autosize");
removeField("command");
removeField("altcommand");
}
void GuiHorizCompassCtrl::setFontWidth(const F32 width){
mFontWidth = width;
}
bool GuiHorizCompassCtrl::onWake()
{
if (! Parent::onWake())
return false;
setActive(true);
setBitmap(mBitmapName);
setOuterBitmap(mOuterBitmapName);
setFontWidth(mFontWidth);
return true;
}
void GuiHorizCompassCtrl::onSleep()
{
mTextureHandle = NULL;
mOuterTextureHandle = NULL;
Parent::onSleep();
}
void GuiHorizCompassCtrl::setOuterBitmap(const char *name)
{
mOuterBitmapName = StringTable->insert(name);
if (*mOuterBitmapName)
mOuterTextureHandle = TextureHandle(mOuterBitmapName, BitmapTexture, true);
else
mOuterTextureHandle = NULL;
setUpdate();
}
void GuiHorizCompassCtrl::setOuterBitmap(const TextureHandle &handle)
{
mOuterTextureHandle = handle;
}
float Vector2dToOffset(Point3F vector)
{
float offset;
offset = atanf((1.0F * vector.x) / (-1.0F * vector.y)) * (180.0F / M_PI);
if ((-1.0F * vector.y) < 0.0F){
return offset + 180.0f;
}
else{
return offset;
}
}
void GuiHorizCompassCtrl::onRender(Point2I offset, const RectI &updateRect)
{
if (mTextureHandle)
{
dglClearBitmapModulation();
TextureObject* texture = (TextureObject*) mTextureHandle;
mBounds.extent.set(texture->bitmapWidth, texture->bitmapHeight + 2);
struct CameraQuery query;
GameProcessCameraQuery(&query);
MatrixF cameraMatrix = query.cameraMatrix;
Point3F cameraRot;
cameraMatrix.getColumn(1, &cameraRot);
// this rotates the compass by 180°
//cameraRot.neg();
cameraRot.z = 0;
mOffset = Vector2dToOffset(cameraRot);
//RectI dstRect(offset.x + mOffset-270, offset.y, mBounds.extent.x, mBounds.extent.y);
RectI dstRect(offset.x + mOffset-315, offset.y + 2 ,mBounds.extent.x, mBounds.extent.y);
dglDrawBitmapStretch(mTextureHandle, dstRect);
}
if(mOuterTextureHandle)
{
TextureObject* texture = (TextureObject*) mOuterTextureHandle;
mBounds.extent.set(texture->bitmapWidth, texture->bitmapHeight);
RectI outerRect(offset, mBounds.extent);
dglDrawBitmapStretch(mOuterTextureHandle, outerRect);
}
if (mProfile->mBorder || !mTextureHandle && !mOuterTextureHandle)
{
RectI rect(offset.x, offset.y, mBounds.extent.x, mBounds.extent.y);
dglDrawRect(rect, mProfile->mBorderColor);
}
renderChildControls(offset, updateRect);
}