Here is the basic translation to C++:
#ifndef SCROLLING_TEXT_H
#define SCROLLING_TEXT_H
#include "agk.h"
#include <vector>
using namespace AGK;
class boxTextType {
public:
int ID;
char* string;
};
class boxType {
float width, height, textheight, px, py;
int drag, sy, numlines, bg, scrollbar;
float offset, max, velocity;
};
class scrollingText {
public:
float VW, VH;
std::vector<boxTextType> boxText;
boxType box;
int tb_font;
scrollingText() {
VW = agk::GetVirtualWidth();
VH = agk::GetVirtualHeight();
tb_font = 0;
//or use something like this to set the font
//tb_font = loadImage("myFont.png")
}
void createTextBox(char* file,float px,float py,int alignH,int alignV,float width,float height,float txtSize)
{
//check file exists and determine strings
if(agk::GetFileExists(file) > 0)
{
// create background
int spr = agk::CreateSprite((UINT)0);
agk::SetSpriteSize(spr,width,height);
float tx, ty;
switch(alignH)
{
case 0:
tx = px;
break;
case 1:
tx = px - width * 0.5;
break;
case 2:
tx = px + width * 0.5;
break;
}
switch(alignV)
{
case 0:
ty = py;
break;
case 1:
ty = py - height * 0.5;
break;
case 2:
ty = py + height * 0.5;
break;
}
agk::SetSpritePosition(spr,tx,ty);
agk::SetSpriteDepth(spr,3);
int bgs = spr;
box.width = width;
box.height = height;
box.px = tx;
box.py = ty;
box.drag = 0;
box.bg = bgs;
box.textheight = txtSize;
box.offset = box.textheight*0.5;
box.velocity = 0.0f;
width = width - txtSize;
height = height - txtSize;
agk::OpenToRead(1,file);
float txt = agk::CreateText("");
if(tb_font > 0) agk::SetTextFontImage(txt,tb_font);
agk::SetTextSize(txt,txtSize);
int cnt = 0;
while(agk::FileEOF(1) == 0)
{
// check each line of the file
char* line = agk::ReadLine(1);
agk::SetTextString(txt,line);
int bold = 0;
if(agk::Mid(line,1,3) == "[b]")
{
bold = 1;
line = agk::Mid(line,4,agk::Len(line)-3);
}
float w = agk::GetTextTotalWidth(txt);
if( w <= width)
{
char* thisline = line;
cnt++;
int thistxt = addline(cnt,thisline,txtSize,alignH,alignV,width,height,px,py);
if(bold > 0) agk::SetTextColor(thistxt,0,0,0,255);
} else {
int length = agk::Len(line);
int clen = agk::Len(line);
int long = 0; int doneline = 0;
while(doneline == 0)
{
char* cline = agk::Mid(line,1,clen);
agk::SetTextString(txt,cline);
float w = agk::GetTextTotalWidth(txt);
if(clen == 0) {
thisline = "";
cnt++;
int thistxt = addline(cnt,thisline,txtSize,alignH,alignV,width,height,px,py);
if(bold > 0) agk::SetTextColor(thistxt,0,0,0,255);
doneline = 1;
} else {
if((w <= width && long == 0) || (w <= width && long == 1 && agk::Asc(agk::Mid(cline,clen,1))==32))
{
thisline = agk::Mid(cline,1,clen-1);
cnt++;
int thistxt = addline(cnt,thisline,txtSize,alignH,alignV,width,height,px,py);
if(bold > 0) agk::SetTextColor(thistxt,0,0,0,255);
line = agk::Mid(line,clen+1,length-clen);
agk::SetTextString(txt,line);
float w = agk::GetTextTotalWidth(txt);
if(w <= width) {
thisline = line;
cnt++;
int thistxt = addline(cnt,thisline,txtSize,alignH,alignV,width,height,px,py);
if(bold > 0) agk::SetTextColor(thistxt,0,0,0,255);
doneline = 1;
} else {
clen = agk::Len(line);
}
} else {
long = 1;
clen--;
}
}
}
}
}
box.numlines = cnt;
if(cnt > 0) {
box.max = cnt*agk::GetTextTotalHeight(boxText[1].ID);
}
if(box.max>box.height)
{
int spr = agk::CreateSprite((UINT)0);
agk::SetSpriteColor(spr,200,200,200,255);
agk::SetSpriteSize(spr,txtSize*0.3,(box.height/box.max)*box.height);
agk::SetSpriteOffset(spr,agk::GetSpriteWidth(spr),0);
agk::SetSpritePositionByOffset(spr,box.px+box.width,box.py);
agk::SetSpriteDepth(spr,2);
box.scrollbar = spr;
}
agk::CloseFile(1);
agk::DeleteText(txt);
} else {
agk::Message("Error - file not found!");
}
}
void scrollTextBox()
{
// get drag event
float px = agk::GetPointerX();
float py = agk::GetPointerY();
if(agk::GetPointerPressed() > 0)
{
if(px > box.px && px < box.px + box.width)
{
if(py > box.py && py < box.py + box.height)
{
box.drag = 1;
box.sy = py;
}
}
} else {
if(box.drag == 1) {
if(agk::GetPointerState() > 0) {
box.velocity = py - box.sy;
box.sy = px;
} else {
box.drag = 0;
}
} else {
if(box.velocity != 0) {
box.velocity = box.velocity * 0.95;
if(agk::Abs(box.velocity) < 0.1) box.velocity = 0.0f;
}
}
}
box.offset = box.offset + box.velocity;
if(box.offset > box.textheight * 0.5) box.offset = box.textheight * 0.5;
if(box.offset < -box.max + box.height * 0.75) box.offset = -box.max+box.height*0.75;
// position the text
if(agk::Abs(box.velocity) > 0.1)
{
if(box.numlines > 0)
{
float tx = agk::GetTextX(boxText[1].ID);
float h = agk::GetTextTotalHeight(boxText[1].ID);
for(int i=1; i<box.numlines; i++)
{
int txt = boxText[1].ID;
float ty = box.py + (i-1) * h + box.offset;
agk::SetTextPosition(txt,tx,ty);
}
if(box.max > box.height)
{
float y = box.py - (box.offset/box.max) * (box.height-agk::GetSpriteHeight(box.scrollbar)*0.3);
if(y<box.py) y = box.py;
agk::SetSpritePositionByOffset(box.scrollbar,agk::GetSpriteXByOffset(box.scrollbar),y);
}
}
}
}
int addline(int cnt, char* thisline, float txtSize, int alignH, int alignV, float width, float height, float px, float py)
{
boxTextType *boxText;
boxText = new boxTextType[cnt];
boxText[cnt].text = thisline;
int itxt = agk::CreateText(thisline);
agk::SetTextFontImage(itxt,tb_font);
agk::SetTextSize(itxt,txtSize);
float tx, ty;
switch(alignH) {
case 0:
tx = px;
break;
case 1:
tx = px - width * 0.5;
break;
case 2:
tx = px + width * 0.5;
break;
}
switch(alignV) {
case 0:
ty = py;
break;
case 1:
ty = py - height * 0.5;
break;
case 2:
ty = py + height * 0.5;
}
float by = ty;
ty = ty + (cnt - 1)*agk::GetTextTotalHeight(itxt);
agk::SetTextPosition(itxt,tx,ty);
agk::SetTextAlignment(itxt,0);
agk::SetTextDepth(itxt,2);
agk::SetTextColor(itxt,50,50,50,255);
agk::SetTextScissor(itxt,0,by,VW,by+height);
boxText[cnt].ID = itxt;
return itxt;
}
void deleteTextBox() {
agk::DeleteSprite(box.bg);
for(int i=1; i<box.numlines; i++)
{
agk::DeleteText(boxText[i].ID);
}
if(box.max > box.height)
{
agk::DeleteSprite(box.scrollbar);
}
}
};
#endif
Though, logic with boxText you have to figure out yourself, because as for now it probably won't work. I am too tired at the moment, sorry.