TEXT management plugin DLL code

Posted By: eleroux

TEXT management plugin DLL code - 03/23/06 22:05

Hi! I just coded a set of functions on the SDK. They are meant to cover the lack of TEXT* pointers in C-Script. (Similar functions can be done with panels in C-Script, but not with text objects).

The only function that would need an explanation is dllText_scale(txt,scalefactor). This was meant to scale proportionally both the size and position of the text object.
This allows you to reduce a text that was made for use in a 1024x768 screen in another resolution, say, 800x600 or 640x480 or 1152x864. This would need not only scaling the text but also repositioning it proportionally on the new sreen size. That's what this particular function does.

To calculate the scalefactor, just divide 800/1024 = 0,78125 or 1152/1024 = 1.125. How do you implement this 'video_set' and scale all needed panels and texts is up to you.

The other functions are very simple and easy to understand. There are still some functions to do if needed, par example, dllText_getTranslucent(txt) to get the translucent flag of a text.. but there are enough examples of this on the code, so it will be easy for you to add these functions.

Thanks to TripleX on all help for getting started with this!

Code:

// dllText.cpp
// Text functions
// Written by Emilio Le Roux

#include "stdafx.h"

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>

// engine specific header file
#define DLL_USE // always define before including adll.h
#include "adll.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
engine_bind();
return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif



//TXT FLAG DEFINES (use these instead of the generic defines in atypes.h)

#define TXT_NARROW (1<<13) // draw text condensed
#define TXT_CONDENSED (1<<1) //draw text condensed
#define TXT_VISIBLE (1<<14) // visible
#define TXT_TRANSLUCENT (1<<10) //translucent for pcx or bmp only (tga w alpha always translucent)
#define TXT_CENTER_X (1<<8) // center text horizontally - bmp and ttf text
#define TXT_CENTER_Y (1<<9) // center text vertically -bmp text only
#define TXT_FILTER (1<<20) // filter pixels (good for big scaled fonts)


///////////////////////////////////////
// TEXT ACCESS FUNCTIONS


DLLFUNC void dllText_setAlpha(TEXT* txt, var alphavar)
{
if (!txt || !alphavar) return;
txt->alpha = alphavar;
}

DLLFUNC void dllText_setPos (TEXT* txt, var xvar, var yvar)
{
if (!txt || !xvar || !yvar) return;
txt->pos_x = xvar;
txt->pos_y = yvar;
}


DLLFUNC void dllText_setScaleX (TEXT* txt, var scalevar)
{
if (!txt || !scalevar) return;
txt->scale_x = scalevar;
}

DLLFUNC void dllText_setScaleY (TEXT* txt, var scalevar)
{
if (!txt || !scalevar) return;
txt->scale_y = scalevar;
}

//scale XY proportionally (does both of the above)
DLLFUNC void dllText_setScale (TEXT* txt, var scalevar)
{
if (!txt || !scalevar) return;
txt->scale_x = scalevar;
txt->scale_y = scalevar;
}


//Rescales both size and position. Doesn't use the above functions.
DLLFUNC void dllText_scale(TEXT* txt, var scalefactor)
{
if (!txt ) return;
float oldscale = max( float(txt->scale_x),0.0001f); //prevent div error if current scale is zero

float newscale = float(scalefactor)/oldscale; //revert old position rescaling

txt->scale_x = scalefactor;
txt->scale_y = scalefactor; //set scale
txt->pos_x = txt->pos_x * newscale; //adjust position x
txt->pos_y = txt->pos_y * newscale; //adjust position y
}


DLLFUNC var dllText_setString(TEXT* txt, var strindex, char* str)
{
//copy string to text.string[strindex]
if (!txt || !str) return (0);
if (strindex >= txt->strings ) return (0);
str_cpy(txt->pstrings[strindex],str);
return(_VAR(1));
}

DLLFUNC var dllText_setFont(TEXT* txt, FONT* fnt)
{
if (!txt || !fnt) return (0);
txt->font = fnt;
return (_VAR(1));
}


DLLFUNC void dllText_setVisible(TEXT* txt, var state)
{
if (!txt) return;
if (state)
{
txt->flags |= TXT_VISIBLE;
//for test only
txt->flags |= (1<<15);
}
else
{
txt->flags &= ~TXT_VISIBLE;
}
}

DLLFUNC void dllText_setCenter_X (TEXT* txt, var state)
{
if (!txt) return;
if (state)
{
txt->flags |= TXT_CENTER_X;
}
else
{
txt->flags &= ~TXT_CENTER_X;
}
}

DLLFUNC void dllText_setCenter_Y (TEXT* txt, var state)
{
if (!txt) return;
if (state)
{
txt->flags |= TXT_CENTER_Y;
}
else
{
txt->flags &= ~TXT_CENTER_Y;
}
}

DLLFUNC void dllText_setCondensed (TEXT* txt, var state)
{
if (!txt) return;
if (state)
{
txt->flags |= TXT_CONDENSED;//
}
else
{
txt->flags &= ~TXT_CONDENSED;
}
}

DLLFUNC void dllText_setNarrow (TEXT* txt, var state)
{
if (!txt) return;
if (state)
{
txt->flags |= NARROW;
}
else
{
txt->flags &= ~NARROW;
}
}

DLLFUNC void dllText_setFilter (TEXT* txt, var state)
{
if (!txt) return;
if (state)
{
txt->flags |= TXT_FILTER;//
}
else
{
txt->flags &= ~TXT_FILTER;
}
}

DLLFUNC void dllText_setTranslucent (TEXT* txt, var state)
{
if (!txt) return;
if (state)
{
txt->flags |= TXT_TRANSLUCENT;
}
else
{
txt->flags &= ~TXT_TRANSLUCENT;
}
}


DLLFUNC var dllText_getAlpha(TEXT* txt)
{
if (!txt) return (_VAR(-1));
return (txt->alpha);
}


//Is the flag on? (0) false (1) true;
DLLFUNC var dllText_getVisible(TEXT* txt)
{
if (!txt) return (_VAR(-1));
return ((txt->flags & TXT_VISIBLE) != 0); //I suppose you can test flags this way? (bitwise AND)
}

//copies the text at string[strindex] into the given wdl string.
DLLFUNC var dllText_getString(STRING* str,TEXT* txt, var strindex)
{
if (!txt || !str) return (0);
if (strindex >= txt->strings ) return (0);
str_cpy(str,(txt->pstrings[strindex])->chars);
return(_VAR(1));
}




Usage:
Build the DLL, put it on the proper folder.
Declare the functions in your WDL:
Code:

//dllTEXT functions
dllfunction dllText_setAlpha(txt,alphavar);
dllfunction dllText_setPos (txt,xvar,yvar);
dllfunction dllText_setScaleX (txt,scalevar);
dllfunction dllText_setScaleY (txt,scalevar);
dllfunction dllText_setScale (txt,scalevar);
dllfunction dllText_scale(txt,scalefactor);
dllfunction dllText_setString(txt,strindex,str);
dllfunction dllText_setFont(txt,fnt);
dllfunction dllText_setVisible(txt,state);
dllfunction dllText_setCenter_X(txt,state);
dllfunction dllText_setCenter_Y(txt,state);
dllfunction dllText_setNarrow(txt,state);
dllfunction dllText_setCondensed(txt,state);
dllfunction dllText_setTranslucent(txt,state);
dllfunction dllText_setFilter(txt,state);

dllfunction dllText_getAlpha(txt);
dllfunction dllText_getVisible(txt);

dllfunction dllText_getString(str,txt,strindex);




Examples:

Code:

//You can use these functions with normally declared texts or with handles for created texts:

TEXT normaltext
{
strings=1;
string="the text";
}

var textHandle;

function example()
{
textHandle = txt_create(1,1);

dllText_setVisible(normaltext,1); //set normaltext visible
dllText_setVisible(textHandle,0); //set textHandle invisible

if (dllText_getVisible())
{
dllText_setString(normaltext,0,"This text is visible");
}

//etc
}



Posted By: Excessus

Re: TEXT management plugin DLL code - 03/24/06 16:29

Thanks alot for sharing this! I really needed something like this.

I don't have a compiler, could you make a compiled version available? Or is there a free compiler that will compile this?

I saw this line: #include "stdafx.h". I'm not a C++ coder, but shouldn't you share that file aswell or is it a comon file?
Posted By: eleroux

Re: TEXT management plugin DLL code - 03/24/06 16:43

I'm uploading a zip file with the DLL file along with the .CPP source file here:

dmGame plugin


I don't have any prior experience on distributing plugins. Are there some guidelines, or I just can upload the .dll file to someplace people can download it from?

the "stdafx.h" file is a common file.
Posted By: TripleX

Re: TEXT management plugin DLL code - 03/24/06 16:44

some more functions:

Code:


DLLFUNC var GEditTextClearStrings(TEXT* txt)
{
for(int a=0;a<_INT(txt->strings);a++)
txt->string[a] = 0;

return(_VAR(1));
}

DLLFUNC var GEditTextClearString(TEXT* txt,var num)
{
//memset(&txt->string[_INT(num)],0,sizeof(&txt->string[_INT(num)]));
txt->string[_INT(num)] = 0;
return(_VAR(1));
}


DLLFUNC STRING* GEditTextGetString(TEXT* txt,var num)
{
if(txt->strings <= num) { return((STRING*)0); }

return((STRING*)txt->string[_INT(num)]);
}

DLLFUNC var GEditTextCreateString(TEXT* txt,var num,STRING* content)
{
if(txt->strings <= num) { return(_VAR(-1)); }

txt->string[_INT(num)] = str_create(content->chars);
return(_VAR(1));
}

DLLFUNC var GEditTextRemoveString(TEXT* txt,var num)
{
if(txt->strings <= num) { return(_VAR(-1)); }

str_remove(txt->string[_INT(num)]);
txt->string[_INT(num)] = 0;

return(_VAR(1));
}

DLLFUNC var GEditTextSetString(TEXT* txt,var num,STRING* content)
{
if(txt->strings <= num) { return(_VAR(-1)); }
str_cpy(txt->string[_INT(num)],content->chars);
return(str_len(txt->string[_INT(num)]->chars));
}



I'll document them tomorrow.
Posted By: FRAJO

Re: TEXT management plugin DLL code - 03/24/06 17:47

DLLFUNC var GEditTextClearStrings(TEXT* txt)
{
for(int a=0;a<_INT(txt->strings);a++)
txt->string[a] = 0;
return(_VAR(1));
}

But what happens to the string itself? This could lead in a high memory usage.
Posted By: TripleX

Re: TEXT management plugin DLL code - 03/25/06 14:33

"But what happens to the string itself? This could lead in a high memory usage. "

this code is NOT for clearing all strings out of memory but ONLY the string pointers.
Example: You create a text. Now every stringpointer have a random value which don't leads to an string (because the strings aren't created with the texts) and this will lead to an empty pointer message if you try to access one of those pointers.

This function is to intializie a text. After this function GEditTextGetString(TEXT* txt,var num) will return 0 if a string don't exists.

For removing strings use:


DLLFUNC var GEditTextRemoveString(TEXT* txt,var num)
{
if(txt->strings <= num) { return(_VAR(-1)); }

str_remove(txt->string[_INT(num)]);
txt->string[_INT(num)] = 0;

return(_VAR(1));
}

BTW: Thanks a lot for the code above.. saved me some writing-work
Posted By: eleroux

Re: TEXT management plugin DLL code - 03/27/06 02:17

Great, TripleX!
It's good that we're progressively 'filling up' these code.
I'm learning a lot from specific lines in your code, like string management.

Note that there are some differences on the code - I was using an SDK version prior to 6.40.2 thus the string elements used to be txt->pstrings[1] instead of txt->string[1].
© 2024 lite-C Forums