浏览代码

* New files - CGameInterface.h and CGameInterface.cpp - add them to your project
* Fixed several memory leaks
* Added support for player colors in CMessage (still need some work)
* Moved declarations of CPreGame buttons to CPreGame.h
* started making player interface (new button classes etc)

Michał W. Urbańczyk 18 年之前
父节点
当前提交
1380a7fd34
共有 10 个文件被更改,包括 224 次插入124 次删除
  1. 二进制
      CDefHandler.cpp
  2. 二进制
      CDefHandler.h
  3. 8 0
      CGameInterface.cpp
  4. 62 0
      CGameInterface.h
  5. 50 25
      CMessage.cpp
  6. 4 98
      CMessage.h
  7. 二进制
      CPreGame.cpp
  8. 97 0
      CPreGame.h
  9. 1 1
      SDL_Extensions.cpp
  10. 2 0
      SDL_Extensions.h

二进制
CDefHandler.cpp


二进制
CDefHandler.h


+ 8 - 0
CGameInterface.cpp

@@ -0,0 +1,8 @@
+#include "stdafx.h"
+#include "CGameInterface.h"
+using namespace CSDL_Ext;
+void CButtonBase::show()
+{
+	blitAt(imgs[state],pos.x,pos.y);
+	updateRect(&pos);
+}

+ 62 - 0
CGameInterface.h

@@ -0,0 +1,62 @@
+#include "SDL.h"
+#include "CDefHandler.h"
+#include "SDL_Extensions.h"
+class CGameInterface
+{
+};
+class CAdvMapInt : public CGameInterface //adventure map interface
+{
+	SDL_Surface * bg;
+};
+class CAICallback : public CGameInterface // callback for AI
+{
+};
+class CIntObject //interface object
+{
+public:
+	SDL_Rect pos;
+	int ID;
+};
+class CButtonBase : public CIntObject
+{
+public:
+	int type;
+	bool abs;
+	struct Offset
+	{
+		int x, y;
+	}  *offset;
+	CIntObject * ourObj;
+	int state;
+	std::vector<SDL_Surface*> imgs;
+	virtual void show() ;
+	CButtonBase(){abs=true;ourObj=NULL;}
+};
+class ClickableL : public virtual CButtonBase //for left-clicks
+{
+	bool pressed;
+	virtual void press (bool down)=0;
+};
+class ClickableR : public virtual CButtonBase //for right-clicks
+{
+	bool pressed;
+	virtual void click (bool down)=0;
+};
+class Hoverable : public virtual CButtonBase 
+{
+	bool hovered;
+	virtual void hover (bool on)=0;
+};
+class KeyInterested : public virtual CButtonBase 
+{
+	virtual void keyPressed (SDL_KeyboardEvent & key)=0;
+};
+class CPlayerInterface
+{
+	static CGameInterface * gamein;
+	std::vector<ClickableL*> lclickable;
+	std::vector<ClickableR*> rclickable;
+	std::vector<Hoverable*> hoverable;
+	std::vector<KeyInterested*> keyinterested;
+	void handleEvent(SDL_Event * sEvent);
+};

+ 50 - 25
CMessage.cpp

@@ -13,18 +13,23 @@ SDL_Rect genRect(int hh, int ww, int xx, int yy);
 extern SDL_Surface * ekran;
 extern TTF_Font * TNRB16, *TNR, *GEOR13;
 SDL_Color genRGB(int r, int g, int b, int a=0);
-void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=ekran);
 //void printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=ekran, unsigned char quality = 2);
 extern CPreGame * CPG;
-void updateRect (SDL_Rect * rect, SDL_Surface * scr = ekran);
 bool isItIn(const SDL_Rect * rect, int x, int y);
+
+
 CMessage::CMessage()
 {
 	piecesOfBox = CGI->spriteh->giveDef("DIALGBOX.DEF");
 	background = CGI->bitmaph->loadBitmap("DIBOXBCK.BMP");
 	SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
 }
-SDL_Surface * CMessage::drawBox1(int w, int h)
+CMessage::~CMessage()
+{
+	delete piecesOfBox;
+	SDL_FreeSurface(background);
+}
+SDL_Surface * CMessage::drawBox1(int w, int h, int playerColor)
 {
 	//prepare surface
 	SDL_Surface * ret = SDL_CreateRGBSurface(ekran->flags, w, h, ekran->format->BitsPerPixel, ekran->format->Rmask, ekran->format->Gmask, ekran->format->Bmask, ekran->format->Amask);
@@ -34,52 +39,63 @@ SDL_Surface * CMessage::drawBox1(int w, int h)
 			SDL_BlitSurface(background,&genRect(background->h,background->w-1,1,0),ret,&genRect(h,w,j,i));
 	}
 	//SDL_Flip(ekran);
-	CSDL_Ext::update(ekran);
+	//CSDL_Ext::update(ekran);
+	
+	std::vector<SDL_Surface*> pieces;
+	for (int i=0;i<piecesOfBox->ourImages.size();i++)
+	{
+		pieces.push_back(piecesOfBox->ourImages[i].bitmap);
+		if (playerColor!=1)
+		{
+			CSDL_Ext::blueToPlayersAdv(pieces[pieces.size()-1],playerColor);
+		}
+	}
 	//obwodka I-szego rzedu pozioma
-	for (int i=0; i<w; i+=piecesOfBox->ourImages[6].bitmap->w)
+	for (int i=0; i<w; i+=pieces[6]->w)
 	{
 		SDL_BlitSurface
-			(piecesOfBox->ourImages[6].bitmap,NULL,
-			ret,&genRect(piecesOfBox->ourImages[6].bitmap->h,piecesOfBox->ourImages[6].bitmap->w,i,0));
+			(pieces[6],NULL,ret,&genRect(pieces[6]->h,pieces[6]->w,i,0));
 		SDL_BlitSurface
-			(piecesOfBox->ourImages[7].bitmap,NULL,
-			ret,&genRect(piecesOfBox->ourImages[7].bitmap->h,piecesOfBox->ourImages[7].bitmap->w,i,h-piecesOfBox->ourImages[7].bitmap->h));
+			(pieces[7],NULL,ret,&genRect(pieces[7]->h,pieces[7]->w,i,h-pieces[7]->h));
 	}
 	//obwodka I-szego rzedu pionowa
 	for (int i=0; i<h; i+=piecesOfBox->ourImages[4].bitmap->h)
 	{
 		SDL_BlitSurface
-			(piecesOfBox->ourImages[4].bitmap,NULL,
-			ret,&genRect(piecesOfBox->ourImages[4].bitmap->h,piecesOfBox->ourImages[4].bitmap->w,0,i));
+			(pieces[4],NULL,ret,&genRect(pieces[4]->h,pieces[4]->w,0,i));
 		SDL_BlitSurface
-			(piecesOfBox->ourImages[5].bitmap,NULL,
-			ret,&genRect(piecesOfBox->ourImages[5].bitmap->h,piecesOfBox->ourImages[5].bitmap->w,w-piecesOfBox->ourImages[5].bitmap->w,i));
+			(pieces[5],NULL,ret,&genRect(pieces[5]->h,pieces[5]->w,w-pieces[5]->w,i));
 	}
 	//corners
 	SDL_BlitSurface
-		(piecesOfBox->ourImages[0].bitmap,NULL,
-		ret,&genRect(piecesOfBox->ourImages[0].bitmap->h,piecesOfBox->ourImages[0].bitmap->w,0,0));
+		(pieces[0],NULL,ret,&genRect(pieces[0]->h,pieces[0]->w,0,0));
 	SDL_BlitSurface
-		(piecesOfBox->ourImages[1].bitmap,NULL,
-		ret,&genRect(piecesOfBox->ourImages[1].bitmap->h,piecesOfBox->ourImages[1].bitmap->w,w-piecesOfBox->ourImages[1].bitmap->w,0));
+		(pieces[1],NULL,ret,&genRect(pieces[1]->h,pieces[1]->w,w-pieces[1]->w,0));
 	SDL_BlitSurface
-		(piecesOfBox->ourImages[2].bitmap,NULL,
-		ret,&genRect(piecesOfBox->ourImages[2].bitmap->h,piecesOfBox->ourImages[2].bitmap->w,0,h-piecesOfBox->ourImages[2].bitmap->h));
+		(pieces[2],NULL,ret,&genRect(pieces[2]->h,pieces[2]->w,0,h-pieces[2]->h));
 	SDL_BlitSurface
-		(piecesOfBox->ourImages[3].bitmap,NULL,
-		ret,&genRect(piecesOfBox->ourImages[3].bitmap->h,piecesOfBox->ourImages[3].bitmap->w,w-piecesOfBox->ourImages[3].bitmap->w,h-piecesOfBox->ourImages[3].bitmap->h));
+		(pieces[3],NULL,ret,&genRect(pieces[3]->h,pieces[3]->w,w-pieces[3]->w,h-pieces[3]->h));
 	//box gotowy!
-
 	return ret;
 }
 
-std::vector<std::string> * CMessage::breakText(std::string text, int line)
+std::vector<std::string> * CMessage::breakText(std::string text, int line, bool userBreak)
 {
 	std::vector<std::string> * ret = new std::vector<std::string>();
 	while (text.length()>line)
 	{
 		int whereCut = -1;
-		for (int i=line; i>0; i--)
+		bool pom = true;
+		for (int i=0; i<line; i++)
+		{
+			if (text[i]==10) //end of line sign
+			{
+				whereCut=i+1;
+				pom=false;
+				break;
+			}
+		}
+		for (int i=line; i>0&&pom; i--)
 		{
 			if (text[i]==' ')
 			{
@@ -90,6 +106,15 @@ std::vector<std::string> * CMessage::breakText(std::string text, int line)
 		ret->push_back(text.substr(0,whereCut));
 		text.erase(0,whereCut);
 	}
+	for (int i=0;i<text.length();i++)
+	{		
+		if (text[i]==10) //end of line sign
+		{
+			ret->push_back(text.substr(0,i));
+			text.erase(0,i);
+			i=0;
+		}
+	}
 	if (text.length() > 0)
 		ret->push_back(text);
 	return ret;
@@ -118,7 +143,7 @@ SDL_Surface * CMessage::genMessage
 		hh+=70;
 	}
 
-	SDL_Surface * ret = drawBox1(ww,hh);
+	SDL_Surface * ret = drawBox1(ww,hh,0);
 	//prepare title text
 	
 	if (title.length())

+ 4 - 98
CMessage.h

@@ -4,116 +4,22 @@
 #include "SDL_TTF.h"
 #include "CSemiDefHandler.h"
 #include "CDefHandler.h"
+#include "CGameInterface.h"
 enum EWindowType {infoOnly, infoOK, yesOrNO};
 class CPreGame;
 class MapSel;
-typedef void(CPreGame::*ttt)();
-template <class T=ttt> class CGroup;
-template <class T=ttt> class CPoinGroup ;
-struct OverButton
-{	
-	int ID;
-	int type;
-	SDL_Rect pos;
-	CDefHandler* imgs;
-	int state;
-	virtual void show() ;
-	virtual void press(bool down=true);
-	OverButton(){state=0;}
-};
-struct HighButton: public OverButton
-{
-	HighButton( SDL_Rect Pos, CDefHandler* Imgs, bool Sel=false, int id=-1)
-		{type=0;imgs=Imgs;selectable=Sel;selected=false;state=0;pos=Pos;ID=id;highlightable=false;};
-	HighButton(){}
-	bool selectable, selected;
-	bool highlightable, highlighted;
-	virtual	void hover(bool on=true)=0;
-	virtual void select(bool on=true)=0;
-};
-template <class T=ttt> struct Button: public HighButton
-{
-	CGroup<T> * ourGroup;
-	Button( SDL_Rect Pos, T Fun,CDefHandler* Imgs, bool Sel=false, CGroup<T>* gr=NULL, int id=-1)
-		:HighButton(Pos,Imgs,Sel,id),ourGroup(gr),fun(Fun){type=1;};
-	Button(){ourGroup=NULL;type=1;};
-	T fun;
-	virtual	void hover(bool on=true);
-	virtual void select(bool on=true);
-};	
-template <class T=ttt> struct SetrButton: public Button<T>
-{
-	int key, * poin;
-	virtual void press(bool down=true);
-	SetrButton(){type=0;selectable=selected=false;state=0;highlightable=false;}
-};
-template<class T=CPreGame>  class Slider
-{ //
-public:
-	bool vertical; // false means horizontal
-	SDL_Rect pos; // position
-	Button<void(Slider::*)()> up, down, //or left/right
-		slider; 
-	int positionsAmnt, capacity;// capacity - amount of positions dispplayed at once
-	int whereAreWe; // first displayed thing
-	bool moving;
-	void(T::*fun)(int);
-	void clickDown(int x, int y, bool bzgl=true);
-	void clickUp(int x, int y, bool bzgl=true);
-	void mMove(int x, int y, bool bzgl=true);
-	void moveUp();
-	void moveDown();
-	void deactivate();
-	void activate();
-	Slider(int x, int y, int h, int amnt, int cap, bool ver);
-	void updateSlid();
-	void handleIt(SDL_Event sev);
 
-};
-//template<class T=void(CPreGame::*)(int)>
-template<class T=ttt>  struct IntBut: public Button<T>
-{
-public:
-	int key;
-	int * what;
-	IntBut(){type=2;fun=NULL;highlightable=false;};
-	IntBut( SDL_Rect Pos, T Fun,CDefHandler* Imgs, bool Sel, int Key, int * What)
-		: Button(Pos,Fun,Imgs,Sel,gr),key(My),key(Key),what(What){ourGroup=gr;type=2;fun=NULL;};
-	void set(){*what=key;};
-};
-template<class T=ttt>  struct IntSelBut: public Button<T>
-{
-public:
-	CPoinGroup<T> * ourGroup;
-	int key;
-	IntSelBut(){};
-	IntSelBut( SDL_Rect Pos, T Fun,CDefHandler* Imgs, bool Sel=false, CPoinGroup<T>* gr=NULL, int My=-1)
-		: Button(Pos,Fun,Imgs,Sel,gr),key(My){ourGroup=gr;type=1;};
-	void select(bool on=true) {(*this).Button::select(on);ourGroup->setYour(this);}
-};
-template <class T=ttt> class CPoinGroup :public CGroup<T>
-{
-public:
-	int * gdzie; //where (po polsku, bo by by³o s³owo kluczowe :/)
-	void setYour(IntSelBut<T> * your){*gdzie=your->key;};
-};
-template <class T=ttt> class CGroup
-{
-public:
-	Button<T> * selected;
-	int type; // 1=sinsel
-	CGroup():selected(NULL),type(0){};
-};
 class CMessage
 {
 public:
-	static std::vector<std::string> * breakText(std::string text, int line=30);
+	static std::vector<std::string> * breakText(std::string text, int line=30, bool userBreak=true); //line - chars per line
 	CDefHandler * piecesOfBox;
 	SDL_Surface * background;
 	SDL_Surface * genMessage(std::string title, std::string text, EWindowType type=infoOnly, 
 								std::vector<CDefHandler*> *addPics=NULL, void * cb=NULL);
-	SDL_Surface * drawBox1(int w, int h);
+	SDL_Surface * drawBox1(int w, int h, int playerColor=1);
 	CMessage();
+	~CMessage();
 };
 
 //

二进制
CPreGame.cpp


+ 97 - 0
CPreGame.h

@@ -10,6 +10,103 @@
 class CPreGame;
 extern CPreGame * CPG;
 enum Ebonus {brandom=-1,bartifact, bgold, bresource};
+
+typedef void(CPreGame::*ttt)();
+template <class T=ttt> class CGroup;
+template <class T=ttt> class CPoinGroup ;
+
+
+struct HighButton
+{
+	int ID;
+	int type;
+	SDL_Rect pos;
+	CDefHandler* imgs;
+	int state;
+	HighButton( SDL_Rect Pos, CDefHandler* Imgs, bool Sel=false, int id=-1)
+		{type=0;imgs=Imgs;selectable=Sel;selected=false;state=0;pos=Pos;ID=id;highlightable=false;};
+	HighButton(){}
+	bool selectable, selected;
+	bool highlightable, highlighted;
+	virtual void show();
+	virtual void press(bool down=true);
+	virtual	void hover(bool on=true)=0;
+	virtual void select(bool on=true)=0;
+};
+template <class T=ttt> struct Button: public HighButton
+{
+	CGroup<T> * ourGroup;
+	Button( SDL_Rect Pos, T Fun,CDefHandler* Imgs, bool Sel=false, CGroup<T>* gr=NULL, int id=-1)
+		:HighButton(Pos,Imgs,Sel,id),ourGroup(gr),fun(Fun){type=1;};
+	Button(){ourGroup=NULL;type=1;};
+	T fun;
+	virtual	void hover(bool on=true);
+	virtual void select(bool on=true);
+};	
+template <class T=ttt> struct SetrButton: public Button<T>
+{
+	int key, * poin;
+	virtual void press(bool down=true);
+	SetrButton(){type=0;selectable=selected=false;state=0;highlightable=false;}
+};
+template<class T=CPreGame>  class Slider
+{ //
+public:
+	bool vertical; // false means horizontal
+	SDL_Rect pos; // position
+	Button<void(Slider::*)()> up, down, //or left/right
+		slider; 
+	int positionsAmnt, capacity;// capacity - amount of positions dispplayed at once
+	int whereAreWe; // first displayed thing
+	bool moving;
+	void(T::*fun)(int);
+	void clickDown(int x, int y, bool bzgl=true);
+	void clickUp(int x, int y, bool bzgl=true);
+	void mMove(int x, int y, bool bzgl=true);
+	void moveUp();
+	void moveDown();
+	void deactivate();
+	void activate();
+	Slider(int x, int y, int h, int amnt, int cap, bool ver);
+	void updateSlid();
+	void handleIt(SDL_Event sev);
+
+};
+//template<class T=void(CPreGame::*)(int)>
+template<class T=ttt>  struct IntBut: public Button<T>
+{
+public:
+	int key;
+	int * what;
+	IntBut(){type=2;fun=NULL;highlightable=false;};
+	IntBut( SDL_Rect Pos, T Fun,CDefHandler* Imgs, bool Sel, int Key, int * What)
+		: Button(Pos,Fun,Imgs,Sel,gr),key(My),key(Key),what(What){ourGroup=gr;type=2;fun=NULL;};
+	void set(){*what=key;};
+};
+template<class T=ttt>  struct IntSelBut: public Button<T>
+{
+public:
+	CPoinGroup<T> * ourGroup;
+	int key;
+	IntSelBut(){};
+	IntSelBut( SDL_Rect Pos, T Fun,CDefHandler* Imgs, bool Sel=false, CPoinGroup<T>* gr=NULL, int My=-1)
+		: Button(Pos,Fun,Imgs,Sel,gr),key(My){ourGroup=gr;type=1;};
+	void select(bool on=true) {(*this).Button::select(on);ourGroup->setYour(this);}
+};
+template <class T=ttt> class CPoinGroup :public CGroup<T>
+{
+public:
+	int * gdzie; //where (po polsku, bo by by³o s³owo kluczowe :/)
+	void setYour(IntSelBut<T> * your){*gdzie=your->key;};
+};
+template <class T=ttt> class CGroup
+{
+public:
+	Button<T> * selected;
+	int type; // 1=sinsel
+	CGroup():selected(NULL),type(0){};
+};
+
 struct StartInfo
 {
 	struct PlayerSettings

+ 1 - 1
SDL_Extensions.cpp

@@ -41,7 +41,7 @@ SDL_Color genRGB(int r, int g, int b, int a=0)
 	ret.unused=a;
 	return ret;
 }
-void updateRect (SDL_Rect * rect, SDL_Surface * scr = ekran)
+void updateRect (SDL_Rect * rect, SDL_Surface * scr)
 {
 	SDL_UpdateRect(scr,rect->x,rect->y,rect->w,rect->h);
 }

+ 2 - 0
SDL_Extensions.h

@@ -7,6 +7,8 @@
 extern SDL_Surface * ekran;
 extern SDL_Color tytulowy, tlo, zwykly ;
 void blitAtWR(SDL_Surface * src, int x, int y, SDL_Surface * dst=ekran);
+void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=ekran);
+void updateRect (SDL_Rect * rect, SDL_Surface * scr = ekran);
 namespace CSDL_Ext
 {
 	void SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC=0, Uint8 A = 255); //myC influences the start of reading pixels