Arg. Almost the same problem I was having
here. I learnt from my mistake, and have gone over everything I can think of.
The problem, to be clear, is that as soon as a function is called, all the parameters are all fuxed, and then the resulting code eventually dies a horrible death.
This is the function I am calling. Just does a basic double buffered GDI call.
void TRenderer::RenderText( std::string text, int x, int y, COLORREF color)
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(m_hwnd, &ps);
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, m_screenWidth, m_screenHeight);
HBITMAP hOldBmp = (HBITMAP)SelectObject(memDC, hMemBmp);
LPCSTR ptr2 = text.c_str();
SetBkMode(memDC, TRANSPARENT);
SetTextColor(memDC, color);
//make sure the number is no larger than −2,147,483,648 to +2,147,483,647
TextOut(memDC, x, y, ptr2, (int)(text.length()));
// As a last step copy the memdc to the hdc
BitBlt(hdc, 0, 0, m_screenWidth,m_screenHeight, memDC, 0, 0, SRCCOPY);
EndPaint(m_hwnd, &ps);
// Always select the old bitmap back into the device context
SelectObject(memDC, hOldBmp);
DeleteObject(hMemBmp);
DeleteDC(memDC);
}
my fantastic AS test code:
#include "TKeyCodes.tscript"
void main()
{
while(1==1)
{
if(Input.IsKeyDown(T_LEFT) == true)
{
Screen.RenderText("Left key pressed!",30,30,65535);
}
//Return temporary control to the engine
//Without this call, the engine will hang.
Engine.Pause();
}
}
Function is registered like thus:
m_script.RegisterObject("TRenderer");
m_script.RegisterObjectMethod("TRenderer","void RenderText(const string& text, int x, int y, int color)",asMETHOD(TRenderer,RenderText));
m_script.RegisterObjectMethod("TRenderer","int RGB(int8 r, int8 g, int8 b)",asMETHOD(TRenderer,BuildRGB));
m_script.RegisterAsGlobal("TRenderer Screen",(void*)m_renderer);
here are the inner workings of these functions, but I'm 99.9% sure they work,a s I'm registering other functions just fine.
void TScript::RegisterObject( std::string declaration,int size/*=0*/,asDWORD flags/*=0*/ )
{
int r = m_engine->RegisterObjectType(declaration.c_str(),size,flags);
r < 0 ? throw TFatalException("Failed to register object" + declaration + " with Angel Script. \n\n In:\n TScript::RegisterObject") : ++r;
}
void TScript::RegisterObjectMethod( std::string obj, std::string declaration, asUPtr fnPtr)
{
int r = m_engine->RegisterObjectMethod(obj.c_str(),declaration.c_str(),fnPtr,asCALL_THISCALL);
r < 0 ? throw TFatalException("Failed to register object method " + declaration + " with Angel Script. \n\n In:\n TScript::RegisterObjectMethod") : ++r;
}
void TScript::RegisterAsGlobal( std::string declaration, void* ptr )
{
if(ptr)
{
int r = m_engine->RegisterGlobalProperty(declaration.c_str(),ptr);
r < 0 ? throw TFatalException("Failed to register global property" + declaration + " with Angel Script. \n\n In:\n TScript::RegisterGloalProperty") : ++r;
}
else
{
throw TFatalException("Failed to register global property " + declaration + " with Angel Script. Reason: Void function pointer. \n\n In:\n TScript::RegisterGloalProperty");
}
}
void TScript::RegisterAsGlobal( std::string declaration, asUPtr fnPtr)
{
int r = m_engine->RegisterGlobalFunction(declaration.c_str(), fnPtr ,asCALL_CDECL);
r < 0 ? throw TFatalException("Failed to register function " + declaration + " with Angel Script. \n\n In:\n TScript::RegisterAsGlobal") : ++r;
}
m_renderer is a pointer. So I'm not making the same mistake as last time!
m_renderer is initialized before the above code is called, and is valid (not null). I can call code on it from within c++
the this ptr doesn't seem to be screwed up at all.
example of the strange parameters:
any ideas?