C Board  

Go Back   C Board > General Programming Boards > Game Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-04-2006, 09:20 PM   #1
Registered User
 
Join Date: Jun 2004
Posts: 134
Dialogue class code for RPG

The following code draws a dialogue box on the screen with an image for the character you are talking to. It has no errors and compiles fine using allegro and MSVC++ .net. Feel free to use it or mod it some and if you mod it much toss me a copy so I can see what yea did. Feedback is welcome as well.

Special notes for this class:
It will hold the game up ex. a fireball is about to incinerate the player the player runs to a NPC and talks - he is safe untill he is done talking.

if BIG_FONT is not defined everything will be drawn in the small default font.
to see the dialogue class in action check out these screen shots (bottom two):
http://www.geocities.com/meanguy76012/PASss.html

Dialogue.h
Code:
#ifndef DIALOGUE_H
#define DIALOGUE_H
#include <allegro.h>
#include <fstream>

#include <string.h>
class Dialogue
{
	public:
		 Dialogue();
	void Initalize(int x,int y,char* Dia, char* Picture);
	void DrawDialogue(BITMAP *BUFFER,FONT *big_font,RGB *PICPAL);
	     ~Dialogue();
	private:
	int x;
	int y;
	char* Dial;
	char* PicPath;
};
#endif
Dialogue.cpp
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);
	}
}
__________________
New to the BBS? Go Here:
http://postingandyou.info/

I'll bet I get flamed for this.
~Kyo~ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mixing C++ class and non-class code? Depthcharge101 C++ Programming 1 05-27-2009 12:11 AM
any suggestions for this piece of code as a database class? ExDHaos C++ Programming 2 05-14-2009 04:13 PM
Class design problem h3ro C++ Programming 10 12-19-2008 09:10 AM
Default class template problem Elysia C++ Programming 5 07-11-2008 08:44 AM
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM


All times are GMT -6. The time now is 03:24 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22