Example #1
0
void Menu::newToggle(const char* text, TOGGLE toggle_type, bool on)
{
    this->buttons.push_back(new Button);

    if(this->centerText)
    {
        this->buttons[LAST]->normalButton = renderMultiLineText(
            this->font, text, this->hlColor, this->fgColor,
            ALIGN_CENTER | TRANSPARENT_BG | CREATE_SURFACE);

        this->buttons[LAST]->highlightedButton = renderMultiLineText(
            this->font, text, this->fgColor, this->hlColor,
            ALIGN_CENTER | TRANSPARENT_BG | CREATE_SURFACE);

        this->buttons[LAST]->x = ((SCREEN_WIDTH / 2) - 
            (this->buttons[LAST]->normalButton->clip_rect.w / 2));

        this->inbetween = this->buttons[LAST]->normalButton->clip_rect.h;
        this->buttons[LAST]->y = this->y;
        this->y += this->inbetween;
    }
    else
    {
        this->buttons[LAST]->normalButton = renderMultiLineText(
            this->font, (string)text, this->hlColor, this->fgColor,
            TRANSPARENT_BG | CREATE_SURFACE);

        this->buttons[LAST]->highlightedButton = renderMultiLineText(
            this->font, (string)text, this->fgColor, this->hlColor,
            TRANSPARENT_BG | CREATE_SURFACE);

        this->inbetween = this->buttons[LAST]->normalButton->clip_rect.h;
        this->buttons[LAST]->x = this->x;
        this->buttons[LAST]->y = this->y;
        this->y += this->inbetween;
    }

    this->buttons[LAST]->mouseOverBox = createRect(this->buttons[LAST]->x,
        this->buttons[LAST]->y,
        this->buttons[LAST]->normalButton->clip_rect.w,
        this->buttons[LAST]->normalButton->clip_rect.h);

    this->buttons[LAST]->type = TOGGLE_MENU;
    this->buttons[LAST]->button_text = new char[strlen(text) + 1];
    strncpy(this->buttons[LAST]->button_text, text, strlen(text) + 1);
    this->buttons[LAST]->action = NO_ACTION;
    this->buttons[LAST]->toggle = toggle_type;
    this->buttons[LAST]->highlights = true;
    this->buttons[LAST]->switch_status = on;
}
Example #2
0
void Menu::newSubMenu(const char* text, Menu* nextMenu)
{
    this->buttons.push_back(new Button);

    if(this->centerText)
    {
        this->buttons[LAST]->normalButton = renderMultiLineText(
            this->font, text, this->hlColor, this->fgColor,
            ALIGN_CENTER | TRANSPARENT_BG | CREATE_SURFACE);

        this->buttons[LAST]->highlightedButton = renderMultiLineText(
            this->font, text, this->fgColor, this->hlColor,
            ALIGN_CENTER | TRANSPARENT_BG | CREATE_SURFACE);

        this->buttons[LAST]->x = ((SCREEN_WIDTH / 2) - 
            (this->buttons[LAST]->normalButton->clip_rect.w / 2));

        this->inbetween = this->buttons[LAST]->normalButton->clip_rect.h;
        this->buttons[LAST]->y = this->y;
        this->y += this->inbetween;
    }
    else
    {
        this->buttons[LAST]->normalButton = renderMultiLineText(
            this->font, (string)text, this->hlColor, this->fgColor,
            TRANSPARENT_BG | CREATE_SURFACE);

        this->buttons[LAST]->highlightedButton = renderMultiLineText(
            this->font, (string)text, this->fgColor, this->hlColor,
            TRANSPARENT_BG | CREATE_SURFACE);

        this->inbetween = this->buttons[LAST]->normalButton->clip_rect.h;
        this->buttons[LAST]->x = this->x;
        this->buttons[LAST]->y = this->y;
        this->y += this->inbetween;
    }

    this->buttons[LAST]->mouseOverBox = createRect(this->buttons[LAST]->x,
        this->buttons[LAST]->y,
        this->buttons[LAST]->normalButton->clip_rect.w,
        this->buttons[LAST]->normalButton->clip_rect.h);

    this->buttons[LAST]->type = SUB_MENU;
    this->buttons[LAST]->nextMenu = nextMenu;
    this->buttons[LAST]->action = NO_ACTION;
    this->buttons[LAST]->toggle = NO_TOGGLE;
    this->buttons[LAST]->highlights = true;
}
Example #3
0
int displayAbout(SDL_Surface *screen, dataStore *data)
{
	int done, mouseX, mouseY;
	SDL_Event event;

	SDL_Color textColor = { 255, 255, 255,0};
	
	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ));

	myButton button;
	button.rect.x = screen->clip_rect.w/2-BUTTONWIDTH/2;
	button.rect.y = screen->clip_rect.h-BUTTONHEIGHT-100;
	button.rect.w = BUTTONWIDTH;
	button.rect.h = BUTTONHEIGHT;
	button.name="Back";
	
	drawButton(screen, &button);
	
	TTF_Font *font = theFont(20);
	char aboutText[10][100] = {"Hedgewood is a game written by:"," - toco"," - tk"," - JTR"," "," we hope you enjoy it."," ","Thanks to:"," - friend of tk for the grapics"," - our great tutor Arne"};
	
	if (!(renderMultiLineText(font, 150, 100, &aboutText[0],10, textColor,screen)))
		printf("%s\n",TTF_GetError());
	
	
	SDL_Flip(screen);
	TTF_CloseFont(font);

//	SDL_FreeSurface(message);
	unsigned int startTime, stopTime, diffTime;
	unsigned int innerStartTime, innerStopTime;
	
	done = 0;
	while ( !done ) {
		startTime = SDL_GetTicks();
		/* Check for events */
		while ( SDL_PollEvent(&event) ) {
			innerStartTime = SDL_GetTicks();
			switch (event.type) {
					
				case SDL_MOUSEMOTION:
					break;
				case SDL_MOUSEBUTTONUP:
					
					SDL_GetMouseState(&mouseX,&mouseY);
					if (isButtonClicked(&button, mouseX, mouseY)) {
						
						done = 1;
					}
#if (DEBUG==1)
					printf("Cusor-Position x: %d y: %d\n",mouseX,mouseY);
#endif
					break;
				case SDL_KEYDOWN:
					/* Any keypress quits the app... */
					switch( event.key.keysym.sym )
				{
					case SDLK_f:
						break;
						
					case SDLK_0:
						printf ("Music off\n");
						Mix_HaltMusic();
						Mix_HaltChannel(-1);
						data->soundEnabled=0;
						break;
					case 	SDLK_m:
						printf ("Music on /Pause\n");				
						if( Mix_PlayingMusic() == 0 )  
							Mix_PlayMusic( data->ingamemusic, -1);			
						if( Mix_PausedMusic() == 1 )
							Mix_ResumeMusic(); 
						else Mix_PauseMusic();						
						break;
					case SDLK_ESCAPE:
					case SDLK_q:
						done = 1;
						quitSDL(data);
						break;
					default:
						break;
						
				}	
					break;
/*				case SDL_QUIT:
					done = 1;
					break;
*/				default:
					break;
			}
			innerStopTime = SDL_GetTicks();
			diffTime=(innerStopTime-innerStartTime);
			//25 Frames per second (40 Milliseconds per frame)
			if (MS_FRAMETIME>diffTime) 
				SDL_Delay(MS_FRAMETIME-diffTime);
		}
		stopTime = SDL_GetTicks();
		diffTime = (stopTime-startTime);
		//25 Frames per second (40 Milliseconds per frame)
		if (MS_FRAMETIME>diffTime) 
			SDL_Delay(MS_FRAMETIME-diffTime);
		
	}
	return 0;
}
Example #4
0
int Menu::runNoBlock(const int frame_tmp)
{
    int frame   = frame_tmp;
    int counter = 0;
    int mouseX, mouseY;
    int mouseClickX, mouseClickY;
    bool clicked;
    int status;

    if(this->music != NULL)
    {
        if(Mix_PlayMusic(this->music, -1) == -1)
        {
            handleError("In-game music disabled! Click OK for full error", false);
            handleError(Mix_GetError(), false);
        }
    }
    else if(this->music_chunk != NULL)
    {
        int channel = Mix_PlayChannel(-1, this->music_chunk, -1);
    }

    while(counter < frame && !this->quit)
    {
        counter++;
        this->eventHandler->handleMenuEvents(&this->quit, &mouseX, &mouseY,
            &mouseClickX, &mouseClickY, &clicked);

        this->checkMouseOver(mouseX, mouseY);

        /* Check where the user clicked */
        if(clicked)
            status = this->checkClick(mouseClickX, mouseClickY);
        else
            status = -1;

        if(status != -1)
        {
            if(this->buttons[status]->type == SUB_MENU)
            {
                if(this->buttons[status]->nextMenu->run() == -1)
                    return -1;
            }

            else if(this->buttons[status]->type == ACTION_MENU)
            {
                if(this->buttons[status]->action == QUIT_GAME)
                    return -1;
                else if(this->buttons[status]->action == RETURN_TO_LAST)
                    return 1;
                else if(this->buttons[status]->action == LOAD_GAME)
                    return 3;
                else if(this->buttons[status]->action == PLAY_GAME)
                {
                    Mix_HaltMusic();
                    return 0;
                }
                else if(this->buttons[status]->action == PLAY_MULTI_GAME)
                {
                    Mix_HaltMusic();
                    return 2;
                }
            }

            else if(this->buttons[status]->type == TOGGLE_MENU)
            {
                string tmp(this->buttons[status]->button_text);

                if(this->buttons[status]->switch_status)
                {
                    this->buttons[status]->switch_status = false;
                    tmp = tmp.substr(0, tmp.find("ON"));
                    tmp += "OFF";
                }
                else
                {
                    this->buttons[status]->switch_status = true;
                    tmp = tmp.substr(0, tmp.find("OFF"));
                    tmp += "ON";
                }

                delete[] this->buttons[status]->button_text;
                this->buttons[status]->button_text = new char[tmp.length() + 1];
                strncpy(this->buttons[status]->button_text, tmp.c_str(), tmp.length() + 1);

                if(this->centerText)
                {
                    this->buttons[status]->normalButton = renderMultiLineText(
                        this->font, tmp, this->hlColor, this->fgColor,
                        ALIGN_CENTER | TRANSPARENT_BG | CREATE_SURFACE);

                    this->buttons[status]->highlightedButton = renderMultiLineText(
                        this->font, tmp, this->fgColor, this->hlColor,
                        ALIGN_CENTER | TRANSPARENT_BG | CREATE_SURFACE);

                    this->buttons[status]->x = ((SCREEN_WIDTH / 2) - 
                        (this->buttons[status]->normalButton->clip_rect.w / 2));
                }

                else
                {
                    this->buttons[status]->normalButton = renderMultiLineText(
                        this->font, tmp, this->hlColor, this->fgColor,
                        TRANSPARENT_BG | CREATE_SURFACE);

                    this->buttons[status]->highlightedButton = renderMultiLineText(
                        this->font, tmp, this->fgColor, this->hlColor,
                        TRANSPARENT_BG | CREATE_SURFACE);
                }
            }
        }

        BLIT(this->bg, 0, 0);

        /* Display each menu option */
        for(unsigned int i=0; i < this->buttons.size(); i++)
        {
            BLIT(this->buttons[i]->displayButton,
                this->buttons[i]->x, 
                this->buttons[i]->y);
        }

        /* Update the screen */
        this->display->update();
    }

    /* This means that the user actually wants to
     * quit, not that we just broke out of the while()
     * loop with a break statement.
    */
    if(this->quit == true)
        return -1;
    else
        return 0;
}