Code:
#include "dialogue.h"
#include <allegro.h>
//#include <winalleg.h>
#include <fstream>
#include <string.h>
#include <math.h>
using namespace std;
//default constructor
Dialogue::Dialogue()
{
PicPath = NULL;
Dial = NULL;
}
//Setting the top x,y coord, and the text to display pointer, and the picture pointer.
void Dialogue::Initalize(int xx,int yy,char* Dia, char* Picture)
{
if(PicPath)delete PicPath;
PicPath = NULL;
if(Picture != NULL) //NULL is valid it just says we have no picture to display.
{
PicPath = new char [strlen(Picture)+1];
strcpy(PicPath,Picture); //save us a copy of the picture's path from the exe
}
if(Dial)delete Dial;
Dial = new char [strlen(Dia)+1];
strcpy(Dial,Dia); //save us a copy of the dialogue text
x = xx; //set coords
y = yy;
}
//The actual drawing of the dialogue all it uses is the pointer to a surface a font pointer, and a pointer to a RGB struct.
void Dialogue::DrawDialogue(BITMAP *BUFFER,FONT *big_font,RGB *PICPAL)
{
const int maxlngth = 35; //the number of characters per line in the dialogue
BITMAP *PIC; //making a local bitmap for the dialogue
int col;
int r1,r2,b1,b2,g1,g2; //vars used in the blend
char *path; //the path to the picture
path = NULL; //set to NULL for now in case there is no PIC
if(PicPath != NULL) //if we have no picpath we dont need to copy into path
{
path = new char[strlen(PicPath)+1];
strcpy(path,PicPath); //copy into path
path[strlen(PicPath)] = '\0';
}
r1 = 45; //Just choosing the rgb values for the dialogue background
b1 = 70; //Alpha blending might look better I may test that later
g1 = 45;
if(PicPath != NULL) //dont try to load without a picpath
{
PIC = load_bitmap(path,PICPAL); //load the picture for the dialogue
}
for(int yy = y;yy < y+160;yy++) //the actual draw part
{
for(int xx = x;xx < x+512; xx++)
{
col = getpixel(BUFFER,xx,yy);
r2 = getr32(col);
g2 = getg32(col);
b2 = getb32(col);
r2 += r1;
r2 = r2/2; //get avg red
g2 += g1;
g2 = g2/2; //get avg green
b2 += b1;
b2 = b2/2; //get avg blue
col = makecol(r2,r2,r2); //get the avg color
putpixel(BUFFER,xx,yy,col);
}
}
if(PicPath != NULL && PIC)blit(PIC,BUFFER,0,0,x,y,96,96); //draw the picture onto the buffer if there is one to draw
//textouts here 160 high with 20 pixels per line so thats 7 lines 8th is for Press Enter
text_mode(-1); //to make the color around the textout transparent
//new line every maxlngth chars
int length = strlen(Dial);
if(length > maxlngth)
{
double bla=(length / maxlngth);
int linenum = 1 + ceil(bla);
char templine[maxlngth+1];
templine[maxlngth] = '\0';
for(int za = 0;za < linenum;za++)
{
for(int ca = 0;ca < maxlngth;ca++)
{
templine[ca] = Dial[(za*maxlngth)+ca];
templine[maxlngth] = '\0';
}
try //try to output with our font
{
textout(BUFFER,big_font,templine,x+105,y+(20*za),makecol(255,255,255));
}
catch(...) //if we can't we will use the default
{
textout(BUFFER,font,templine,x+105,y+(20*za),makecol(255,255,255));
}
}
}
else
{
try
{
textout(BUFFER,big_font,Dial,x+105,y,makecol(255,255,255));
}
catch(...)
{
textout(BUFFER,font,Dial,x+105,y,makecol(255,255,255));
}
}
try
{
textout(BUFFER,big_font,"PRESS ENTER TO CONTINUE",x+105,y+140,makecol(255,255,255));
}
catch(...)
{
textout(BUFFER,font,"PRESS ENTER TO CONTINUE",x+105,y+140,makecol(255,255,255));
}
while(!key[KEY_ENTER])blit(BUFFER,screen,0,0,0,0,1024,768); //wait on enter we need to redraw this is in case they alt+tab
while(key[KEY_ENTER]);
if(path != NULL)delete[] path; // destroy only if we need to
if(PicPath != NULL)destroy_bitmap(PIC); //although we aren't looking at pic to delete we are looking at if it would have been loaded earlier
}
Dialogue::~Dialogue()
{
try //for some reason MSVC++ doesn't like deleting an already deleted pointer so this code is quick way to identify the problem
{
if(PicPath)delete[] PicPath;
if(Dial)delete[] Dial;
}
catch(...)
{
ofstream errorlog;
errorlog.open("ERROR.txt");
allegro_message("An error was encountered and saved to ERROR.txt");
errorlog<<"An error was encountered during the destructor of the Dialogue class."<<endl<<"Dumping relevant data"<<endl;
errorlog<<"PicPath: "<<&PicPath<<endl;
errorlog<<"Dial: "<<&Dial<<endl;
errorlog.close();
exit(2);
}
}