Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » switching between functions

Credits go to BAF for helping out!
This thread is locked; no one can reply to it. rss feed Print
switching between functions
Money
Member #6,730
December 2005
avatar

//main.cpp
---------

#SelectExpand
1#include "dec.h" 2 3 4 5int main() 6{ 7 init(); 8 while(!key[KEY_ESC]) 9 { 10 11 mainMenu(); 12 13 blit(buffer,screen,0,0,0,0,800,600); 14 15 } 16 cleanUp(); 17 18 19 20return 0; 21} 22END_OF_MAIN()

//mainMenu.cpp
--------------

#SelectExpand
1#include "dec.h" 2 3/*Choices 4------------ 5 61.) start game 7 82.) options 9 103.) instructions 11 124.) controls 13 145.) credits 15 16---------------- 17 18*/ 19 20unsigned short choice; 21 22void checkChoice(); 23 24 25 26void mainMenu() 27{ 28 while(!key[KEY_ESC]) 29 { 30 31 mainMenu: 32 33 checkChoice(); 34 35 if( key[KEY_UP] && menu == 0) 36 { 37 menu = 0; 38 while(key[KEY_UP]){ rest(1); } 39 } 40 else if( key[KEY_DOWN] && menu == 5 ) 41 { 42 menu = 5; 43 while(key[KEY_DOWN]) { rest(1); } 44 } 45 else if(key[KEY_DOWN] ) 46 { 47 menu++; 48 while(key[KEY_DOWN]){ rest(1); } 49 } 50 else if(key[KEY_UP]) 51 { 52 menu--; 53 while(key[KEY_UP]){ rest(1); } 54 } 55 56 57 58 59 60 switch(menu) 61 { 62 case 0: clear_bitmap(buffer); 63 blit(start,buffer,0,0,0,0,800,600); 64 if( key[KEY_ENTER] ){ choice = 2; } 65 if( samplePlayed == true ){ play_sample(blip,255,0,1000,0); samplePlayed = false; } 66 break; 67 68 case 1: clear_bitmap(buffer); 69 blit(options,buffer,0,0,0,0,800,600); 70 if( key[KEY_ENTER] ){ choice = 3;} 71 if( samplePlayed == false ){play_sample(blip,255,0,1000,0); samplePlayed = true; } 72 break; 73 74 case 2: clear_bitmap(buffer); 75 blit(instructions,buffer,0,0,0,0,800,600); 76 if( samplePlayed == true ){ play_sample(blip,255,0,1000,0); samplePlayed = false; } 77 break; 78 79 case 3: clear_bitmap(buffer); 80 blit(controls,buffer,0,0,0,0,800,600); 81 if( samplePlayed == false ){ play_sample(blip,255,0,1000,0); samplePlayed = true; } 82 break; 83 84 case 4: clear_bitmap(buffer); 85 blit(credits,buffer,0,0,0,0,800,600); 86 if( samplePlayed == true ){ play_sample(blip,255,0,1000,0); samplePlayed = false; } 87 break; 88 89 case 5: clear_bitmap(buffer); 90 blit(quit,buffer,0,0,0,0,800,600); 91 if( samplePlayed == false ){ play_sample(blip,255,0,1000,0); samplePlayed = true; } 92 if( key[KEY_ENTER] ){ play_sample(beep,255,0,1000,0); rest(100); exit(0); } 93 break; 94 } 95 96 blit(buffer,screen,0,0,0,0,800,600); 97 } 98 99 100 101 102 103 104 105 106 107 108} 109 110 111void checkChoice() 112{ 113 switch(choice) 114 { 115 116 case 1: 117 118 mainMenu(); 119 break; 120 121 122 case 2: 123 124 classicGame(); 125 break; 126 127 case 3: 128 129 optionsMenu(); 130 break; 131 } 132 133}

//options.cpp
//-------------

#SelectExpand
1#include "dec.h" 2 3 4 5 6unsigned int opMenu = 0; 7 8void drawOpMenu(); 9 10 11 12unsigned short opChoice = 0; 13 14void checkOpChoice(); 15 16 17 18 19 20void optionsMenu() 21{ 22 23 24 25 while(!key[KEY_ESC]) 26 { 27 28 checkOpChoice(); 29 30 graphics->setFont(guiFont); 31 graphics->_beginDraw(); 32 33 34 35 36 if(opMenu == 0 && key[KEY_UP]) 37 { 38 opMenu = 0; 39 while(key[KEY_UP]){ rest(1);} 40 } 41 else if(key[KEY_DOWN]) 42 { 43 opMenu++; 44 while(key[KEY_DOWN]){ rest(1); } 45 } 46 else if(key[KEY_UP]) 47 { 48 opMenu--; 49 while(key[KEY_UP]){ rest(1); } 50 } 51 else if(opMenu == 4 && key[KEY_DOWN]) 52 { 53 opMenu = 4; 54 while(key[KEY_DOWN]){ rest(1); } 55 } 56 57 58 59 switch(opMenu) 60 { 61 case 0: 62 clear_bitmap(buffer); 63 drawOpMenu(); 64 graphics->drawText("MUSIC VOLUME: ",19,100,Graphics::LEFT); 65 break; 66 67 case 1: 68 clear_bitmap(buffer); 69 drawOpMenu(); 70 graphics->drawText("SOUND FX VOLUME: ",19,160,Graphics::LEFT); 71 break; 72 73 case 2: 74 clear_bitmap(buffer); 75 drawOpMenu(); 76 graphics->drawText("LIVES: ",19,220); 77 break; 78 79 case 3: 80 clear_bitmap(buffer); 81 drawOpMenu(); 82 graphics->drawText("MUSIC:",19,280); 83 break; 84 85 case 4: 86 clear_bitmap(buffer); 87 drawOpMenu(); 88 graphics->drawText("BACK TO MAIN",261+(.5),500); 89 if(key[KEY_ENTER]){ opChoice = 1; } 90 break; 91 92 } 93 94 graphics->_endDraw(); 95 blit(buffer,screen,0,0,0,0,800,600); 96 97 } 98 99mainMenu(); 100 101} 102 103 104 105 106 107 108 109void drawOpMenu() 110{ 111 graphics->drawText("music volume: ",20,100,Graphics::LEFT); 112 graphics->drawText("sound fx Volume: ",20,160,Graphics::LEFT); 113 graphics->drawText("lives: ",20,220,Graphics::LEFT); 114 graphics->drawText("music: ",20,280,Graphics::LEFT); 115 graphics->drawText("back to main",260,500,Graphics::LEFT); 116} 117 118void checkOpChoice() 119{ 120 if(opChoice == 1) 121 { 122 clear_bitmap(buffer); 123 mainMenu(); 124 } 125}

ok, im in options menu, and when the user selects back to main, it goes back to mainMenu()

so i use some variable, cause someone says thatcalling functions directly from other functions, im gonna run ou tof stack memory

so i tried this

but when i hit back to main, it just freezes

i want to be able to when the suer hits back to main, it just goes back to mainMenu() cleanly, and not with all these probs

BAF
Member #2,981
December 2002
avatar

Uhm, why aren't you doing what we told you in #Allegro (make the menu functions return a value, then act on that value, spawning other menus as necessary)? :-/

Money
Member #6,730
December 2005
avatar

oh, ok, i got it, that worked perfectly, thanks baf

Go to: