aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-03-16 08:48:50 -0400
committerClyne Sullivan <tullivan99@gmail.com>2016-03-16 08:48:50 -0400
commit8dae5eeac2c6254bb8288c0479c193ab185a312f (patch)
treeb9cac413e7da7b955c598ef39e61e6f2beecc124 /src
parent93d6035dad1eb60fb01648232809e55059dd1cfa (diff)
string'd some stuff
Diffstat (limited to 'src')
-rw-r--r--src/ui.cpp473
-rw-r--r--src/world.cpp73
2 files changed, 257 insertions, 289 deletions
diff --git a/src/ui.cpp b/src/ui.cpp
index 4148238..f69bfe8 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -6,7 +6,7 @@
#define SDL_KEY e.key.keysym.sym
-extern std::vector<menuItem>optionsMenu;
+extern std::vector<menuItem> optionsMenu;
extern SDL_Window *window;
@@ -102,13 +102,13 @@ void Menu::gotoChild(){
}
namespace ui {
-
+
/*
* Mouse coordinates.
*/
-
+
vec2 mouse;
- static vec2 premouse={0,0};
+ static vec2 premouse={0,0};
/*
* Variety of keydown bools
@@ -118,7 +118,7 @@ namespace ui {
/*
* Debugging flags.
*/
-
+
bool debug=false;
bool posFlag=false;
bool dialogPassive = false;
@@ -126,23 +126,23 @@ namespace ui {
int dialogPassiveTime = 0;
Trade merchTrade;
-
+
/*
* Dialog stuff that needs to be 'public'.
*/
-
+
bool dialogBoxExists = false;
bool dialogImportant = false;
unsigned char dialogOptChosen = 0;
unsigned char merchOptChosen = 0;
-
+
unsigned int textWrapLimit = 110;
-
+
/*
* Current font size. Changing this WILL NOT change the font size, see setFontSize() for
* actual font size changing.
*/
-
+
unsigned int fontSize;
/*
@@ -164,20 +164,20 @@ namespace ui {
sanic = Mix_LoadWAV("assets/sounds/sanic.wav");
//Mix_Volume(1,50);
}
-
+
void destroyFonts(void){
FT_Done_Face(ftf);
FT_Done_FreeType(ftl);
-
+
Mix_FreeChunk(dialogClick);
Mix_FreeChunk(battleStart);
Mix_FreeChunk(sanic);
}
-
+
/*
* Sets a new font family to use (*.ttf).
*/
-
+
void setFontFace(const char *ttf){
if(FT_New_Face(ftl,ttf,0,&ftf)){
std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl;
@@ -187,55 +187,55 @@ namespace ui {
DEBUG_printf("Using font %s\n",ttf);
#endif // DEBUG
}
-
+
/*
* Sets a new font size (default: 12).
*/
-
+
void setFontSize(unsigned int size){
unsigned int i,j;
unsigned char *buf;
-
+
fontSize=size;
FT_Set_Pixel_Sizes(ftf,0,fontSize);
-
+
/*
* Pre-render 'all' the characters.
*/
-
+
glDeleteTextures(93,ftex); // delete[] any already-rendered textures
glGenTextures(93,ftex); // Generate new texture name/locations?
-
+
for(i=33;i<126;i++){
-
+
/*
* Load the character from the font family file.
*/
-
+
if(FT_Load_Char(ftf,i,FT_LOAD_RENDER)){
std::cout<<"Error! Unsupported character "<<(char)i<<" ("<<i<<")."<<std::endl;
abort();
}
-
+
/*
* Transfer the character's bitmap (?) to a texture for rendering.
*/
-
+
glBindTexture(GL_TEXTURE_2D,ftex[i-33]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S ,GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T ,GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER ,GL_LINEAR );
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER ,GL_LINEAR );
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
-
+
/*
* The just-created texture will render red-on-black if we don't do anything to it, so
* here we create a buffer 4 times the size and transform the texture into an RGBA array,
* making it white-on-black.
*/
-
+
buf = new unsigned char[ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows * 4];
-
+
for(j=0;j<ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows;j++){
buf[j*4 ]=255;//fontColor[0];
buf[j*4+1]=255;//fontColor[1];
@@ -243,51 +243,51 @@ namespace ui {
buf[j*4+3]=ftf->glyph->bitmap.buffer[j] ? 255 : 0;
//buf[j*4+3]=ftf->glyph->bitmap.buffer[j];
}
-
+
ftexwh[i-33].x=ftf->glyph->bitmap.width;
ftexwh[i-33].y=ftf->glyph->bitmap.rows;
ftexbl[i-33].x=ftf->glyph->bitmap_left;
ftexbl[i-33].y=ftf->glyph->bitmap_top;
ftexad[i-33].x=ftf->glyph->advance.x>>6;
ftexad[i-33].y=ftf->glyph->advance.y>>6;
-
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
-
+
+ glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
+
delete[] buf; //free(buf);
}
}
-
+
/*
* Set a color for font rendering (default: white).
*/
-
+
void setFontColor(unsigned char r,unsigned char g,unsigned char b){
fontColor[0]=r;
fontColor[1]=g;
fontColor[2]=b;
}
-
+
/*
* Draws a character at the specified coordinates, aborting if the character is unknown.
*/
-
+
vec2 putChar(float xx,float yy,char c){
vec2 c1,c2;
int x = xx, y = yy;
-
+
/*
* Get the width and height of the rendered character.
*/
-
+
c1={(float)floor(x)+ftexbl[c-33].x,
(float)floor(y)+ftexbl[c-33].y};
c2=ftexwh[c-33];
-
+
/*
* Draw the character:
*/
-
+
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,ftex[c-33]);
glPushMatrix();
@@ -301,169 +301,168 @@ namespace ui {
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
-
+
/*
* return the width.
*/
-
+
return ftexad[c-33];//(vec2){c2.x,ftexad[c-33].y};
}
-
+
/*
* Draw a string at the specified coordinates.
*/
-
- float putString(const float x,const float y,const char *s){
+
+ float putString( const float x, const float y, std::string s ) {
unsigned int i=0;
- float xo=x,yo=y;
- vec2 add;
- //vec2 off = { (float)floor(x), (float)floor(y) };
-
+ vec2 add, o = {x, y};
+
/*
* Loop on each character:
*/
-
- do{
+
+ do {
if(i && ((i / 110.0) == (i / 110))){
- yo-=fontSize*1.05;
- xo=x;
+ o.y -= fontSize * 1.05f;
+ o.x = x;
if(s[i] == ' ')
i++;
}
+
if(i && (i / (float)textWrapLimit == i / textWrapLimit)){
- yo -= fontSize * 1.05;
- xo = x;
-
+ o.y -= fontSize * 1.05f;
+ o.x = x;
+
// skip a space if it's there since we just newline'd
if(s[i] == ' ')
i++;
}
- if(s[i] == '\n'){
- yo-=fontSize*1.05;
- xo=x;
- }else if(s[i] == '\r' || s[i] == '\t'){
- /*if(s[i] == '\n'){
- yo-=fontSize*1.05;
- xo=x;
- */}else if(s[i]==' '){ // Handle spaces
- xo+=fontSize/2;
- }else if(s[i]=='\b'){ // Handle backspaces?
- xo-=add.x;
- }else{
- add=putChar(floor(xo),floor(yo),s[i]);
- xo+=add.x;
- yo+=add.y;
+
+ switch ( s[i] ) {
+ case '\n':
+ o.y -= fontSize * 1.05f;
+ o.x = x;
+ break;
+ case '\r':
+ break;
+ case '\t':
+ break;
+ case '\b':
+ o.x -= add.x;
+ break;
+ case ' ':
+ o.x += fontSize / 2;
+ break;
+ default:
+ add = putChar( floor(o.x), floor(o.y), s[i] );
+ o.x += add.x;
+ o.y += add.y;
+ break;
}
+
}while(s[++i]);
-
- return xo; // i.e. the string width
+
+ return o.x; // i.e. the string width
}
-
- float putStringCentered(const float x,const float y,const char *s){
+
+ float putStringCentered( const float x, const float y, std::string s ) {
unsigned int i = 0;
float width = 0;
-
- do{
- if(s[i]=='\n'){ // Handle newlines
+
+ do {
+ switch ( s[i] ) {
+ case '\n':
// TODO
- }else if(s[i]==' '){ // Handle spaces
- width+=fontSize/2;
- }else if(s[i]=='\b'){ // Handle backspaces?
- // Why?
- // Cuz
- }else{
- width+=ftexwh[i].x+fontSize*.1;
+ break;
+ case '\b':
+ break;
+ case ' ':
+ width += fontSize / 2;
+ break;
+ default:
+ width += ftexwh[i].x + fontSize * 0.1f;
+ break;
}
- }while(s[++i]);
-
+ } while(s[++i]);
+
putString(floor(x-width/2),y,s);
return width;
}
-
+
/*
* Draw a string in a typewriter-esque fashion. Each letter is rendered as calls are made
* to this function. Passing a different string to the function will reset the counters.
*/
- static char *ret = NULL;
- char *typeOut(char *str){
+ std::string ret;
+ std::string typeOut(char *str){
static unsigned int sinc, // Acts as a delayer for the space between each character.
linc=0, // Contains the number of letters that should be drawn.
size=0; // Contains the full size of the current string.
- //static char *ret = NULL;
-
- /*
- * Create a well-sized buffer if we haven't yet.
- */
-
- if(!ret){
- ret = new char[512]; //(char *)calloc(512,sizeof(char));
- memset(ret,0,512*sizeof(char));
- }
-
+
/*
* Reset values if a new string is being passed.
*/
-
- if(strncmp(ret,str,linc-1)){
- memset(ret,0,512); // Zero the buffer
+
+ if(strncmp(ret.c_str(),str,linc-1)){
+ ret.clear(); // Zero the buffer
size=strlen(str); // Set the new target string size
linc=0; // Reset the incrementers
sinc=1;
typeOutDone = false;
}
-
+
/*
* Draw the next letter if necessary.
*/
-
+
if(typeOutDone)
return str;
else if(++sinc==2){
sinc=0;
-
- strncpy(ret+linc,str+linc,1); // Get next character
-
+
+ ret.append( 1, *(str + linc) );
+
if(linc<size)
linc++;
else
typeOutDone = true;
}
-
+
return ret; // The buffered string.
}
-
+
/*
* Draw a formatted string to the specified coordinates.
*/
-
+
float putText(const float x,const float y,const char *str,...){
va_list args;
char *buf;
float width;
-
+
/*
* Create a wimpy buffer.
*/
-
+
buf = new char[512]; //(char *)calloc(128,sizeof(char));
memset(buf,0,512*sizeof(char));
-
+
/*
* Handle the formatted string, printing it to the buffer.
*/
-
+
va_start(args,str);
vsnprintf(buf,512,str,args);
va_end(args);
-
+
/*
* Draw the string, free resources, return the width of the string.
*/
-
+
width=putString(x,y,buf);
delete[] buf; //free(buf);
-
+
return width;
}
void dialogBox(const char *name,const char *opt,bool passive,const char *text,...){
@@ -471,32 +470,32 @@ namespace ui {
va_list dialogArgs;
unsigned int len;
char *sopt,*soptbuf;
-
+
dialogPassive = passive;
-
+
/*
* Set up the text buffer.
*/
-
+
memset(dialogBoxText,0,512);
-
+
/*
* Get the text ready for rendering.
*/
-
+
len=strlen(name);
strcpy(dialogBoxText ,name);
strcpy(dialogBoxText+len,": ");
len+=2;
-
+
va_start(dialogArgs,text);
vsnprintf(dialogBoxText+len,512-len,text,dialogArgs);
va_end(dialogArgs);
-
+
/*
* Set up option text.
*/
-
+
while(dialogOptCount){
if(dialogOptText[dialogOptCount]){
delete[] dialogOptText[dialogOptCount]; //free(dialogOptText[dialogOptCount]);
@@ -508,32 +507,31 @@ namespace ui {
dialogOptCount = 0;
dialogOptChosen = 0;
memset(&dialogOptLoc,0,sizeof(float)*12);
-
+
if(opt != NULL){
-
+
soptbuf = new char[strlen(opt)+1];
strcpy(soptbuf,opt);
-
+
sopt=strtok(soptbuf,":");
while(sopt != NULL){
dialogOptText[dialogOptCount] = new char[strlen(sopt)+1]; //(char *)malloc(strlen(sopt));
strcpy(dialogOptText[dialogOptCount++],sopt);
sopt=strtok(NULL,":");
}
-
+
delete[] soptbuf;
}
-
+
/*
- * Tell draw() that the box is ready.
+ * Tell draw() that the box is ready.
*/
-
+
dialogBoxExists = true;
dialogImportant = false;
-
- if(ret)
- ret[0] = '\0';
+
+ ret.clear();
}
@@ -541,38 +539,38 @@ namespace ui {
std::cout << "Buying and selling on the bi-weekly!" << std::endl;
va_list dialogArgs;
size_t len;
-
+
dialogPassive = passive;
std::cout << "Market Trading: " << trade.quantity[0] << " " << trade.item[0] << " for " << trade.quantity[1] << " " << trade.item[1] << std::endl;
merchTrade = trade;
-
+
// clear the buffer
memset(dialogBoxText, '\0', 512);
-
+
// create the string
strcpy(dialogBoxText, name);
strcat(dialogBoxText, ": ");
-
+
len=strlen(dialogBoxText);
va_start(dialogArgs,text);
vsnprintf(dialogBoxText + len, 512 - len, text, dialogArgs);
va_end(dialogArgs);
-
+
// free old option text
while(dialogOptCount){
if(dialogOptText[dialogOptCount]){
delete[] dialogOptText[dialogOptCount];
dialogOptText[dialogOptCount] = NULL;
}
-
+
dialogOptCount--;
};
dialogOptChosen = 0;
memset(&dialogOptLoc, 0, sizeof(float) * 12);
-
+
// handle options if desired
if(opt){
//std::unique_ptr<char[]> soptbuf (new char[strlen(opt) + 1]);
@@ -586,27 +584,25 @@ namespace ui {
sopt = strtok(NULL,":");
}
}
-
+
// allow box to be displayed
dialogBoxExists = true;
dialogImportant = false;
dialogMerchant = true;
textWrapLimit = 50;
-
- // kill the string created by typeOut if it contains something
- if(ret)
- *ret = '\0';
+
+ ret.clear();
}
-
+
void merchantBox(){
textWrapLimit = 50;
dialogMerchant = true;
}
-
+
/**
* Wait for a dialog box to be dismissed.
*/
-
+
void waitForDialog(void){
do{
mainLoop();
@@ -664,10 +660,10 @@ namespace ui {
void draw(void){
unsigned char i;
float x,y,tmp;
- char *rtext;
-
+ std::string rtext;
+
if ( pageTex ) {
-
+
glEnable( GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, pageTex );
glBegin( GL_QUADS );
@@ -677,11 +673,11 @@ namespace ui {
glTexCoord2i( 0, 1 ); glVertex2i( offset.x - 300, SCREEN_HEIGHT - 600 );
glEnd();
glDisable( GL_TEXTURE_2D);
-
+
} else if (dialogBoxExists){
-
+
rtext=typeOut(dialogBoxText);
-
+
if(dialogImportant){
setFontColor(255,255,255);
if(dialogPassive){
@@ -694,7 +690,7 @@ namespace ui {
}
if(fadeIntensity == 255 || dialogPassive){
setFontSize(24);
- putStringCentered(offset.x,offset.y,rtext);
+ putStringCentered(offset.x,offset.y,rtext.c_str());
setFontSize(16);
}
}else if(dialogMerchant){
@@ -702,8 +698,8 @@ namespace ui {
x=offset.x-SCREEN_WIDTH/6;
y=(offset.y+SCREEN_HEIGHT/2)-HLINE*8;
-
-
+
+
glColor3ub(255,255,255);
glBegin(GL_LINE_STRIP);
glVertex2f(x-1 ,y+1);
@@ -712,10 +708,10 @@ namespace ui {
glVertex2f(x-1,y-1-SCREEN_HEIGHT*.6);
glVertex2f(x - 1,y+1);
glEnd();
-
+
glColor3ub(0,0,0);
glRectf(x,y,x+SCREEN_WIDTH/3,y-SCREEN_HEIGHT*.6);
-
+
// draw typeOut'd text
putString(x + HLINE, y - fontSize - HLINE, (rtext = typeOut(dialogBoxText)));
@@ -759,8 +755,8 @@ namespace ui {
merchAOptLoc[1][2] = offset.x + (SCREEN_WIDTH / 8.5);
for(i = 0; i < 2; i++){
- if(((merchAOptLoc[i][0] < merchAOptLoc[i][2]) ?
- (mouse.x > merchAOptLoc[i][0] && mouse.x < merchAOptLoc[i][2]) :
+ if(((merchAOptLoc[i][0] < merchAOptLoc[i][2]) ?
+ (mouse.x > merchAOptLoc[i][0] && mouse.x < merchAOptLoc[i][2]) :
(mouse.x < merchAOptLoc[i][0] && mouse.x > merchAOptLoc[i][2])) &&
mouse.y > merchAOptLoc[i][1] - 8 && mouse.y < merchAOptLoc[i][1] + 8){
glColor3ub(255,255, 0);
@@ -774,19 +770,19 @@ namespace ui {
glEnd();
}
-
+
// draw / handle dialog options if they exist
for(i = 0; i < dialogOptCount; i++){
setFontColor(255, 255, 255);
-
+
// draw option
tmp = putStringCentered(offset.x, dialogOptLoc[i][1], dialogOptText[i]);
-
+
// get coordinate information on option
dialogOptLoc[i][2] = offset.x + tmp;
dialogOptLoc[i][0] = offset.x - tmp;
dialogOptLoc[i][1] = y - SCREEN_HEIGHT / 2 - (fontSize + HLINE) * (i + 1);
-
+
// make text yellow if the mouse hovers over the text
if(mouse.x > dialogOptLoc[i][0] && mouse.x < dialogOptLoc[i][2] &&
mouse.y > dialogOptLoc[i][1] && mouse.y < dialogOptLoc[i][1] + 16 ){
@@ -794,13 +790,13 @@ namespace ui {
putStringCentered(offset.x, dialogOptLoc[i][1], dialogOptText[i]);
}
}
-
+
setFontColor(255, 255, 255);
}else{ //normal dialog box
-
+
x=offset.x-SCREEN_WIDTH/2+HLINE*8;
y=(offset.y+SCREEN_HEIGHT/2)-HLINE*8;
-
+
// draw white border
glColor3ub(255, 255, 255);
@@ -811,14 +807,14 @@ namespace ui {
glVertex2i(x-1 ,y-1-SCREEN_HEIGHT/4);
glVertex2i( x - 1, y + 1 );
glEnd();
-
+
glColor3ub(0,0,0);
glRectf(x,y,x+SCREEN_WIDTH-HLINE*16,y-SCREEN_HEIGHT/4);
-
+
rtext=typeOut(dialogBoxText);
-
+
putString(x+HLINE,y-fontSize-HLINE,rtext);
-
+
for(i=0;i<dialogOptCount;i++){
setFontColor(255,255,255);
tmp = putStringCentered(offset.x,dialogOptLoc[i][1],dialogOptText[i]);
@@ -835,17 +831,16 @@ namespace ui {
}
setFontColor(255,255,255);
}
-
- if(strcmp(rtext,dialogBoxText)){
+
+ if ( rtext != dialogBoxText )
Mix_PlayChannel(1,dialogClick,0);
- }
-
+
}if(!fadeIntensity){
vec2 hub = {
(SCREEN_WIDTH/2+offset.x)-fontSize*10,
(offset.y+SCREEN_HEIGHT/2)-fontSize
};
-
+
putText(hub.x,hub.y,"Health: %u/%u",player->health>0?(unsigned)player->health:0,
(unsigned)player->maxHealth
);
@@ -862,21 +857,21 @@ namespace ui {
hub.x+(player->health/player->maxHealth * 150),
hub.y+12);
}
-
+
/*
* Lists all of the quests the player is currently taking.
*/
-
+
if(player->inv->invOpen){
hub.y = player->loc.y + fontSize * 8;
hub.x = player->loc.x;// + player->width / 2;
-
+
putStringCentered(hub.x,hub.y,"Current Quests:");
-
+
for(auto &c : player->qh.current){
hub.y -= fontSize * 1.15;
putStringCentered(hub.x,hub.y,c.title.c_str());
- }
+ }
}
}
}
@@ -889,7 +884,7 @@ namespace ui {
updateConfig();
saveConfig();
}
-
+
menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f){
menuItem temp;
temp.member = 0;
@@ -962,7 +957,7 @@ namespace ui {
setFontSize(24);
updateConfig();
SDL_Event e;
-
+
mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2);
mouse.y=(offset.y+SCREEN_HEIGHT/2)-premouse.y;
@@ -998,15 +993,15 @@ namespace ui {
//draw the button background
glColor3f(m.button.color.red,m.button.color.green,m.button.color.blue);
- glRectf(offset.x+m.button.loc.x,
- offset.y+m.button.loc.y,
- offset.x+m.button.loc.x + m.button.dim.x,
+ glRectf(offset.x+m.button.loc.x,
+ offset.y+m.button.loc.y,
+ offset.x+m.button.loc.x + m.button.dim.x,
offset.y+m.button.loc.y + m.button.dim.y);
//draw the button text
putStringCentered(offset.x + m.button.loc.x + (m.button.dim.x/2),
(offset.y + m.button.loc.y + (m.button.dim.y/2)) - ui::fontSize/2,
m.button.text);
-
+
//tests if the mouse is over the button
if(mouse.x >= offset.x+m.button.loc.x && mouse.x <= offset.x+m.button.loc.x + m.button.dim.x){
if(mouse.y >= offset.y+m.button.loc.y && mouse.y <= offset.y+m.button.loc.y + m.button.dim.y){
@@ -1061,9 +1056,9 @@ namespace ui {
}
//draw the background of the slider
glColor4f(m.slider.color.red,m.slider.color.green,m.slider.color.blue, .5f);
- glRectf(offset.x+m.slider.loc.x,
- offset.y+m.slider.loc.y,
- offset.x+m.slider.loc.x + m.slider.dim.x,
+ glRectf(offset.x+m.slider.loc.x,
+ offset.y+m.slider.loc.y,
+ offset.x+m.slider.loc.x + m.slider.dim.x,
offset.y+m.slider.loc.y + m.slider.dim.y);
//draw the slider handle
@@ -1084,7 +1079,7 @@ namespace ui {
//draw the now combined slider text
putStringCentered(offset.x + m.slider.loc.x + (m.slider.dim.x/2), (offset.y + m.slider.loc.y + (m.slider.dim.y/2)) - ui::fontSize/2, outSV);
- }
+ }
//test if mouse is inside of the slider's borders
if(mouse.x >= offset.x+m.slider.loc.x && mouse.x <= offset.x+m.slider.loc.x + m.slider.dim.x){
if(mouse.y >= offset.y+m.slider.loc.y && mouse.y <= offset.y+m.slider.loc.y + m.slider.dim.y){
@@ -1123,18 +1118,18 @@ namespace ui {
*m.slider.var = (((mouse.y-offset.y) - m.slider.loc.y)/m.slider.dim.y)*100;
//draw a white box over the handle
glColor3f(1.0f,1.0f,1.0f);
- glRectf(offset.x+m.slider.loc.x,
- offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05),
- offset.x+m.slider.loc.x + sliderW,
+ glRectf(offset.x+m.slider.loc.x,
+ offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05),
+ offset.x+m.slider.loc.x + sliderW,
offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05) + sliderH);
}else{
*m.slider.var = (((mouse.x-offset.x) - m.slider.loc.x)/m.slider.dim.x)*100;
//draw a white box over the handle
glColor3f(1.0f,1.0f,1.0f);
- glRectf(offset.x+m.slider.loc.x + m.slider.sliderLoc,
- offset.y+m.slider.loc.y,
- offset.x+m.slider.loc.x + (m.slider.sliderLoc + sliderW),
+ glRectf(offset.x+m.slider.loc.x + m.slider.sliderLoc,
+ offset.y+m.slider.loc.y,
+ offset.x+m.slider.loc.x + (m.slider.sliderLoc + sliderW),
offset.y+m.slider.loc.y + m.slider.dim.y);
}
}
@@ -1151,7 +1146,7 @@ namespace ui {
void takeScreenshot(GLubyte* pixels){
std::vector<GLubyte> bgr (SCREEN_WIDTH * SCREEN_HEIGHT * 3, 0);
-
+
for(uint x = 0; x < SCREEN_WIDTH*SCREEN_HEIGHT*3; x+=3){
bgr[x] = pixels[x+2];
bgr[x+1] = pixels[x+1];
@@ -1211,19 +1206,19 @@ namespace ui {
fwrite(&bgr, 1,3*SCREEN_WIDTH*SCREEN_HEIGHT,bmp);
delete[] pixels;
-
+
fclose(bmp);
}
void dialogAdvance(void){
unsigned char i;
-
+
if ( pageTex ) {
glDeleteTextures( 1, &pageTex );
pageTex = 0;
return;
}
-
+
if(!typeOutDone){
typeOutDone = true;
return;
@@ -1239,7 +1234,7 @@ namespace ui {
}
}
DONE:
-
+
// handle important text
if(dialogImportant){
dialogImportant = false;
@@ -1256,25 +1251,25 @@ DONE:
World *tmp;
vec2 oldpos,tmppos;
SDL_Event e;
-
+
// update mouse coords
mouse.x = premouse.x + offset.x - ( SCREEN_WIDTH / 2 );
mouse.y = ( offset.y + SCREEN_HEIGHT / 2 ) - premouse.y;
-
+
while(SDL_PollEvent(&e)){
switch(e.type){
-
+
// escape - quit game
case SDL_QUIT:
gameRunning=false;
break;
-
+
// mouse movement - update mouse vector
case SDL_MOUSEMOTION:
premouse.x=e.motion.x;
premouse.y=e.motion.y;
break;
-
+
// mouse clicks
case SDL_MOUSEBUTTONDOWN:
// right click advances dialog
@@ -1284,10 +1279,10 @@ DONE:
if ( ( e.button.button & SDL_BUTTON_LEFT ) && !dialogBoxExists )
player->inv->usingi = true;
break;
-
+
// key presses
case SDL_KEYDOWN:
-
+
// space - make player jump
if ( SDL_KEY == SDLK_SPACE ) {
if ( player->ground ) {
@@ -1311,16 +1306,16 @@ DONE:
player->right = false;
left = true;
right = false;
- if(currentWorld->toLeft){
+ if ( !currentWorld->toLeft.empty() ) {
oldpos = player->loc;
if((tmp = currentWorld->goWorldLeft(player)) != currentWorld){
tmppos = player->loc;
player->loc = oldpos;
-
+
toggleBlackFast();
waitForCover();
player->loc = tmppos;
-
+
currentWorld = tmp;
toggleBlackFast();
}
@@ -1333,16 +1328,16 @@ DONE:
player->left = false;
left = false;
right = true;
- if(currentWorld->toRight){
+ if ( !currentWorld->toRight.empty() ) {
oldpos = player->loc;
if((tmp = currentWorld->goWorldRight(player)) != currentWorld){
tmppos = player->loc;
player->loc = oldpos;
-
+
toggleBlackFast();
waitForCover();
player->loc = tmppos;
-
+
currentWorld = tmp;
toggleBlackFast();
}
@@ -1351,37 +1346,17 @@ DONE:
case SDLK_s:
break;
case SDLK_w:
- if(inBattle){
+ if ( inBattle ) {
tmp = currentWorld;
- currentWorld = ((Arena *)currentWorld)->exitArena(player);
- if(tmp != currentWorld){
- //delete &tmp;
+ currentWorld = ((Arena *)currentWorld)->exitArena( player );
+ if ( tmp != currentWorld )
toggleBlackFast();
- }
- }else{
- if((tmp = currentWorld->goInsideStructure(player)) != currentWorld)
+ } else if( (tmp = currentWorld->goInsideStructure( player )) != currentWorld )
currentWorld = tmp;
- }
break;
case SDLK_i:
- /*currentWorld=currentWorld->goWorldBack(player); // Go back a layer if possible
- if(tmp!=currentWorld){
- currentWorld->detect(player);
- player->vel.y=.2;
- player->loc.y+=HLINE*5;
- player->ground=false;
- }*/
player->health -= 5;
break;
- case SDLK_k:
- /*currentWorld=currentWorld->goWorldFront(player); // Go forward a layer if possible
- if(tmp!=currentWorld){
- currentWorld->behind->detect(player);
- player->vel.y=.2;
- player->loc.y+=HLINE*5;
- player->ground=false;
- }*/
- break;
case SDLK_LSHIFT:
if(debug){
Mix_PlayChannel(1,sanic,-1);
@@ -1422,7 +1397,7 @@ DONE:
/*
* KEYUP
*/
-
+
case SDL_KEYUP:
if(SDL_KEY == SDLK_ESCAPE){
//gameRunning = false;
@@ -1501,16 +1476,16 @@ DONE:
default:
break;
}
-
+
if(!left&&!right)
player->vel.x=0;
-
+
break;
default:
break;
}
}
-
+
// Flush preloaded AI functions if necessary
if ( !dialogBoxExists && AIpreaddr.size() ) {
while ( !AIpreaddr.empty() ) {
@@ -1520,7 +1495,7 @@ DONE:
}
}
}
-
+
void toggleBlack(void){
fadeEnable ^= true;
fadeWhite = false;
@@ -1540,7 +1515,7 @@ DONE:
fadeEnable ^= true;
fadeWhite = true;
fadeFast = true;
-
+
Mix_PlayChannel( 1, battleStart, 0 );
}
}
diff --git a/src/world.cpp b/src/world.cpp
index 097847f..dec9a64 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -151,9 +151,6 @@ World::
World( void )
{
bgmObj = NULL;
-
- toLeft = NULL;
- toRight = NULL;
}
/**
@@ -222,9 +219,6 @@ World::
delete bgTex;
- delete[] toLeft;
- delete[] toRight;
-
deleteEntities();
}
@@ -792,14 +786,15 @@ singleDetect( Entity *e )
* Handle gravity if the entity is above the line.
*/
- }else{
+ } else {
if(e->type == STRUCTURET && e->loc.y > 2000){
e->loc.y = worldData[i].groundHeight;
e->vel.y = 0;
e->ground = true;
return;
- }else if(e->vel.y > -2)e->vel.y-=.003 * deltaTime;
+ } else if ( e->vel.y > -2 )
+ e->vel.y -= .003 * deltaTime;
}
@@ -974,30 +969,23 @@ addParticle( float x, float y, float w, float h, float vx, float vy, Color color
void World::addLight(vec2 loc, Color color){
Light l;
- if(light.size() < 64){
+ if ( light.size() < 64 ) {
l.loc = loc;
l.color = color;
light.push_back(l);
}
}
-char *World::setToLeft(const char *file){
- if(toLeft)
- delete[] toLeft;
- if(!file)
- return (toLeft = NULL);
-
- strcpy((toLeft = new char[strlen(file) + 1]),file);
- return toLeft;
+std::string World::
+setToLeft( std::string file )
+{
+ return (toLeft = file);
}
-char *World::setToRight(const char *file){
- if(toRight)
- delete[] toRight;
- if(!file)
- return (toRight = NULL);
-
- strcpy((toRight = new char[strlen(file) + 1]),file);
- return toRight;
+
+std::string World::
+setToRight( std::string file )
+{
+ return (toRight = file);
}
World *World::
@@ -1006,7 +994,7 @@ goWorldLeft( Player *p )
World *tmp;
// check if player is at world edge
- if( toLeft && p->loc.x < worldStart + HLINE * 15.0f ) {
+ if( !toLeft.empty() && p->loc.x < worldStart + HLINE * 15.0f ) {
// load world (`toLeft` conditional confirms existance)
tmp = loadWorldFromXML(toLeft);
@@ -1026,7 +1014,7 @@ goWorldRight( Player *p )
{
World *tmp;
- if( toRight && p->loc.x + p->width > -worldStart - HLINE * 15 ) {
+ if( !toRight.empty() && p->loc.x + p->width > -worldStart - HLINE * 15 ) {
tmp = loadWorldFromXML(toRight);
p->loc.x = tmp->worldStart - HLINE * -15.0f;
@@ -1042,14 +1030,19 @@ World *World::
goInsideStructure( Player *p )
{
World *tmp;
- char *current;
- if(inside.empty()){
- for(auto &b : build){
- if(p->loc.x > b->loc.x &&
- p->loc.x + p->width < b->loc.x + b->width ){
- inside.push_back((std::string)(currentXML.c_str() + 4));
+ std::string current;
- tmp = loadWorldFromXML(b->inside.c_str());
+ if ( inside.empty() ) {
+ for ( auto &b : build ) {
+ if ( p->loc.x > b->loc.x &&
+ p->loc.x + p->width < b->loc.x + b->width ) {
+
+ if ( b->inside.empty() )
+ return this;
+
+ inside.push_back(currentXML.c_str() + 4);
+
+ tmp = loadWorldFromXML( b->inside );
ui::toggleBlackFast();
ui::waitForCover();
@@ -1058,11 +1051,11 @@ goInsideStructure( Player *p )
return tmp;
}
}
- }else{
- strcpy((current = new char[strlen((const char *)(currentXML.c_str() + 4)) + 1]),(const char *)(currentXML.c_str() + 4));
- tmp = loadWorldFromXML(inside.back().c_str());
- for(auto &b : tmp->build){
- if(!strcmp(current,b->inside.c_str())){
+ } else {
+ current = currentXML.c_str() + 4;
+ tmp = loadWorldFromXML( inside.back() );
+ for ( auto &b : tmp->build ) {
+ if ( current == b->inside ) {
inside.pop_back();
ui::toggleBlackFast();
@@ -1075,8 +1068,8 @@ goInsideStructure( Player *p )
return tmp;
}
}
- delete[] current;
}
+
return this;
}