diff --git a/CMT.cpp b/CMT.cpp index 2c658fcb7..a4cad965f 100644 --- a/CMT.cpp +++ b/CMT.cpp @@ -1,8 +1,10 @@ // CMT.cpp : Defines the entry point for the console application. // -#include "SDL.h" -#include #include "stdafx.h" +#include "SDL.h" +#include "SDL_TTF.h" +#include "CBuildingHandler.h" +#include #include #include #include @@ -39,6 +41,7 @@ const char * NAME = "VCMI 0.2"; version of the library linked do not match, or Z_ERRNO if there is an error reading or writing the files. */ SDL_Surface * ekran; +TTF_Font * TNRB; int def(FILE *source, FILE *dest, int level, int winBits=15, int memLevel =8) { int ret, flush; @@ -207,7 +210,9 @@ int _tmain(int argc, _TCHAR* argv[]) float i; if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER/*|SDL_INIT_EVENTTHREAD*/)==0) { - + TTF_Init(); + atexit(TTF_Quit); + TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",18); screen = SDL_SetVideoMode(800,600,24,SDL_HWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/); ekran = screen; //FILE * zr = fopen("mal.txt","r"); diff --git a/CMessage.cpp b/CMessage.cpp new file mode 100644 index 000000000..c0bd39b8f --- /dev/null +++ b/CMessage.cpp @@ -0,0 +1,122 @@ +#include "stdafx.h" +#include "CMessage.h" +#include "SDL_TTF.h" +#include "CSemiDefHandler.h" +extern TTF_Font * TNRB; +extern SDL_Surface * ekran; + +SDL_Rect genRect(int hh, int ww, int xx, int yy); +CMessage::CMessage() +{ + piecesOfBox = new CSemiDefHandler(); + piecesOfBox->openDef("DIALGBOX.DEF","H3sprite.lod"); + background = SDL_LoadBMP("H3bitmap.lod\\DIBOXBCK.BMP"); + SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255)); + tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0; + zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr + tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0; +} +SDL_Surface * CMessage::drawBox1(int w, int h) +{ + //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); + for (int i=0; ih)//background + { + for (int j=0; jw-1) + SDL_BlitSurface(background,&genRect(background->h,background->w-1,1,0),ret,&genRect(h,w,j,i)); + }SDL_Flip(ekran); + //obwodka I-szego rzedu pozioma + for (int i=0; iourImages[6].bitmap->w) + { + SDL_BlitSurface + (piecesOfBox->ourImages[6].bitmap,NULL, + ret,&genRect(piecesOfBox->ourImages[6].bitmap->h,piecesOfBox->ourImages[6].bitmap->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)); + } + //obwodka I-szego rzedu pionowa + for (int i=0; iourImages[4].bitmap->h) + { + SDL_BlitSurface + (piecesOfBox->ourImages[4].bitmap,NULL, + ret,&genRect(piecesOfBox->ourImages[4].bitmap->h,piecesOfBox->ourImages[4].bitmap->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)); + } + //corners + SDL_BlitSurface + (piecesOfBox->ourImages[0].bitmap,NULL, + ret,&genRect(piecesOfBox->ourImages[0].bitmap->h,piecesOfBox->ourImages[0].bitmap->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)); + 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)); + 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)); + //box gotowy! + + return ret; +} + +std::vector * CMessage::breakText(std::string text) +{ + std::vector * ret = new std::vector(); + while (text.length()>30) + { + int whereCut = -1; + for (int i=30; i>0; i--) + { + if (text[i]==' ') + { + whereCut = i; + break; + } + } + ret->push_back(text.substr(0,whereCut)); + text.erase(0,whereCut); + } + if (text.length() > 0) + ret->push_back(text); + return ret; +} +SDL_Surface * CMessage::genMessage(std::string title, std::string text, EWindowType type) +{ +//max x 320 okolo 30 znakow + std::vector * tekst; + if (text.length() < 30) //nie trzeba polamac + { + tekst = new std::vector(); + tekst->push_back(text); + } + else tekst = breakText(text); + int ww, hh; //wymiary boksa + if (319>60*13*text.length()) + ww = 60*13*text.length(); + else ww = 319; + if (title.length()) + hh=130+(21*tekst->size()); + else hh=80+(21*tekst->size()); + + SDL_Surface * ret = drawBox1(ww,hh); + //prepare title text + SDL_Surface * titleText = TTF_RenderText_Shaded(TNRB,title.c_str(),tytulowy,tlo); + //draw title + SDL_Rect tytul = genRect(titleText->h,titleText->w,((ret->w/2)-(titleText->w/2)),37); + SDL_BlitSurface(titleText,NULL,ret,&tytul); + SDL_FreeSurface(titleText); + //draw text + for (int i=0; isize(); i++) + { + SDL_Surface * tresc = TTF_RenderText_Shaded(TNRB,(*tekst)[i].c_str(),zwykly,tlo); + SDL_Rect trescRect = genRect(tresc->h,tresc->w,((ret->w/2)-(tresc->w/2)),77+i*21); + SDL_BlitSurface(tresc,NULL,ret,&trescRect); + SDL_FreeSurface(tresc); + } + delete tekst; + return ret; +} \ No newline at end of file diff --git a/CMessage.h b/CMessage.h new file mode 100644 index 000000000..05d2a5c06 --- /dev/null +++ b/CMessage.h @@ -0,0 +1,16 @@ +#include "SDL_TTF.h" +#include "CSemiDefHandler.h" +enum EWindowType {infoOnly, infoOK, yesOrNO}; +class CMessage +{ + SDL_Color tytulowy ; + SDL_Color tlo; + SDL_Color zwykly ; +public: + std::vector * breakText(std::string text); + CSemiDefHandler * piecesOfBox; + SDL_Surface * background; + SDL_Surface * genMessage(std::string title, std::string text, EWindowType type=infoOnly); + SDL_Surface * drawBox1(int w, int h); + CMessage(); +}; \ No newline at end of file diff --git a/CObjectHandler.cpp b/CObjectHandler.cpp index 40ad1cf7e..c0d87753c 100644 --- a/CObjectHandler.cpp +++ b/CObjectHandler.cpp @@ -1,5 +1,5 @@ -#include "CObjectHandler.h" #include "stdafx.h" +#include "CObjectHandler.h" void CObjectHandler::loadObjects() { diff --git a/CPreGame.cpp b/CPreGame.cpp index ff7286a99..9da06df76 100644 --- a/CPreGame.cpp +++ b/CPreGame.cpp @@ -1,7 +1,9 @@ #include "stdafx.h" #include "CPreGame.h" #include "SDL.h" +#include "CMessage.h" extern SDL_Surface * ekran; +SDL_Rect genRect(int hh, int ww, int xx, int yy); bool isItIn(const SDL_Rect * rect, int x, int y) { if ((x>rect->x && xx+rect->w) && (y>rect->y && yy+rect->h)) @@ -10,6 +12,7 @@ bool isItIn(const SDL_Rect * rect, int x, int y) } CPreGame::CPreGame() { + currentMessage=NULL; initMainMenu(); showMainMenu(); } @@ -100,6 +103,7 @@ void CPreGame::highlightButton(int which, int on) } void CPreGame::runLoop() { + CMessage * cmh = new CMessage(); SDL_Event sEvent; while(true) { @@ -198,7 +202,7 @@ void CPreGame::runLoop() ourMainMenu->highlighted=5; } } - else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && SDL_BUTTON(1)) + else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT)) { if (isItIn(&ourMainMenu->lNewGame,sEvent.motion.x,sEvent.motion.y)) { @@ -226,7 +230,7 @@ void CPreGame::runLoop() ourMainMenu->highlighted=5; } } - else if ((sEvent.type==SDL_MOUSEBUTTONUP) && SDL_BUTTON(1)) + else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT)) { if (isItIn(&ourMainMenu->lNewGame,sEvent.motion.x,sEvent.motion.y)) { @@ -254,32 +258,57 @@ void CPreGame::runLoop() ourMainMenu->highlighted=5; } } + else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT)) + { + if (isItIn(&ourMainMenu->lNewGame,sEvent.motion.x,sEvent.motion.y)) + { + } + else if (isItIn(&ourMainMenu->lLoadGame,sEvent.motion.x,sEvent.motion.y)) + { + } + else if (isItIn(&ourMainMenu->lHighScores,sEvent.motion.x,sEvent.motion.y)) + { + } + else if (isItIn(&ourMainMenu->lCredits,sEvent.motion.x,sEvent.motion.y)) + { + } + else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y)) + { + SDL_Surface * infoBox = cmh->genMessage("Quit", "Return to Windows"); + //SDL_Surface * infoBox = cmh->genMessage("Quit", "View the legendary Heores of Might and Magic abbbba"); + SDL_Rect pos = genRect(infoBox->h,infoBox->w, + (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2)); + SDL_BlitSurface(infoBox,NULL,ekran,&pos); + SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h); + SDL_FreeSurface(infoBox); + currentMessage = new SDL_Rect(pos); + } + } + else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT)) + { + if (isItIn(&ourMainMenu->lNewGame,sEvent.motion.x,sEvent.motion.y)) + { + } + else if (isItIn(&ourMainMenu->lLoadGame,sEvent.motion.x,sEvent.motion.y)) + { + } + else if (isItIn(&ourMainMenu->lHighScores,sEvent.motion.x,sEvent.motion.y)) + { + } + else if (isItIn(&ourMainMenu->lCredits,sEvent.motion.x,sEvent.motion.y)) + { + } + else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y)) + { + SDL_BlitSurface(ourMainMenu->background,currentMessage,ekran,currentMessage); + SDL_UpdateRect + (ekran,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h); + } + } else if (sEvent.type==SDL_KEYDOWN) { switch (sEvent.key.keysym.sym) { - //case SDLK_LEFT: - // { - // if(ourMainMenu->lQuit.x>0) - // ourMainMenu->lQuit.x--; - // break; - // } - //case (SDLK_RIGHT): - // { - // ourMainMenu->lQuit.x++; - // break; - // } - //case (SDLK_UP): - // { - // if(ourMainMenu->lQuit.y>0) - // ourMainMenu->lQuit.y--; - // break; - // } - //case (SDLK_DOWN): - // { - // ourMainMenu->lQuit.y++; - // break; - // } case (SDLK_q): { return ; diff --git a/CPreGame.h b/CPreGame.h index ed90af47b..b1bff6195 100644 --- a/CPreGame.h +++ b/CPreGame.h @@ -16,6 +16,7 @@ class CPreGame { public: + SDL_Rect * currentMessage; enum EState { //where are we? mainMenu, ScenarioList } state; diff --git a/CSemiDefHandler.cpp b/CSemiDefHandler.cpp index b612bba92..108116192 100644 --- a/CSemiDefHandler.cpp +++ b/CSemiDefHandler.cpp @@ -129,6 +129,7 @@ void CSemiDefHandler::openImg(const char *name) if(!image) { printf("IMG_Load: %s\n", IMG_GetError()); + return; // handle error } Cimage vinya; diff --git a/SDL_Extensions.cpp b/SDL_Extensions.cpp index f5b176bf7..87214cefa 100644 --- a/SDL_Extensions.cpp +++ b/SDL_Extensions.cpp @@ -1,6 +1,14 @@ #include "stdafx.h" #include "SDL_Extensions.h" - +SDL_Rect genRect(int hh, int ww, int xx, int yy) +{ + SDL_Rect ret; + ret.h=hh; + ret.w=ww; + ret.x=xx; + ret.y=yy; + return ret; +} void CSDL_Ext::SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC) { Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC; diff --git a/VCMI.exe b/VCMI.exe index a58c17772..deed00b88 100644 Binary files a/VCMI.exe and b/VCMI.exe differ