Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

MenuTest.cpp

00001 //Licence: LGPL 00002 //Copyright: Visti Andresen 00003 00004 #include "FontGl.h" 00005 #include "Menu.h" 00006 00007 //#exe 00008 00009 #include "MenuTest.h" 00010 00011 00012 SDL_Surface *display_; 00013 Menu *menu_; 00014 FontGl *font_; 00015 Button *button1_, *button2_; 00016 Key *key_; 00017 TextEntry *textEntry_; 00018 TextSelection *textSelection_; 00019 00020 int width_=800; 00021 int height_=600; 00022 00023 int main(int argv, char **argc){ 00024 //Prepare SDL and OpenGL 00025 setupGL(); 00026 00027 //Perare a font 00028 font_ = new FontGl(); 00029 font_->loadFontGfx("FontTestGfx/font.ppm"); 00030 font_->setScreenSize(width_,height_, width_,height_); 00031 00032 //Create a menu 00033 menu_ = new Menu("Main Menu", "mainmenu", font_); 00034 menu_->setVirtualScreenSize(width_, height_); 00035 00036 //Add a sub menu 00037 Menu *subMenu = new Menu("Sub Menu", "submenu", font_); 00038 menu_->addChild(subMenu); 00039 subMenu->setVirtualScreenSize(width_, height_); 00040 button2_ = new Button("Button 2", "button2", font_); 00041 subMenu->addChild(button2_); 00042 subMenu->addChild(new BackButton("Back", "", font_)); 00043 00044 //Add a button 00045 button1_ = new Button("Button 1", "button1", font_); 00046 menu_->addChild(button1_); 00047 00048 //Add a Key 00049 key_ = new Key("Key", "key", font_); 00050 menu_->addChild(key_); 00051 00052 //Add a TextEntry 00053 textEntry_ = new TextEntry("Text ", "textentry", font_); 00054 menu_->addChild(textEntry_); 00055 00056 //Add a TextSelection 00057 vector<string> choices; 00058 choices.push_back("Choice 0"); 00059 choices.push_back("Choice 1"); 00060 choices.push_back("Choice 2"); 00061 textSelection_ = new TextSelection("Text Sel. ", "textsel", choices, 00062 font_); 00063 menu_->addChild(textSelection_); 00064 00065 //Add a quit button 00066 menu_->addChild(new BackButton("Exit", "", font_)); 00067 00068 bool quit=false; 00069 SDL_Event event; 00070 font_->prepareDrawing(); 00071 menu_->setSelected(true); 00072 menu_->renderMenu(); 00073 while(menu_->getSelected() && !quit){ 00074 glClear(GL_COLOR_BUFFER_BIT); 00075 font_->prepareDrawing(); 00076 menu_->renderMenu(); 00077 while(SDL_PollEvent(&event)){ 00078 if (event.type == SDL_QUIT){ 00079 quit = true; 00080 } 00081 if (!menu_->handleKeyEvent(&event)){ 00082 key_->handleGameKeyboardEvent(&event); 00083 } 00084 } 00085 SDL_GL_SwapBuffers(); 00086 00087 testMenus(); 00088 } 00089 //Destruct 00090 delete menu_; 00091 delete font_; 00092 SDL_Quit(); 00093 return 0; 00094 } 00095 00096 void setupGL() 00097 { 00098 if( SDL_Init( SDL_INIT_VIDEO ) < 0 ){ 00099 cout << "Failed initializing SDL Video" << endl; 00100 exit(-1); 00101 } 00102 00103 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); 00104 00105 Uint32 flags = SDL_OPENGL|SDL_HWSURFACE; 00106 00107 00108 display_ = SDL_SetVideoMode(width_, height_, 16, flags); 00109 if (display_ == 0) { 00110 SDL_Quit(); 00111 cout << "Could not set video mode" << endl; 00112 exit(-1); 00113 } 00114 00115 glViewport(0,0,width_,height_); 00116 00117 /*glMatrixMode(GL_PROJECTION); 00118 glLoadIdentity(); 00119 gluOrtho2D(0, 00120 virtualWidth-1, 00121 virtualHeight-1, 00122 0); 00123 00124 */ 00125 glMatrixMode(GL_MODELVIEW); 00126 glLoadIdentity(); 00127 00128 glEnable(GL_TEXTURE_2D); 00129 glEnable(GL_BLEND); 00130 00131 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 00132 glDisable(GL_DEPTH_TEST); 00133 00134 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 00135 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 00136 00137 } 00138 00139 void testMenus() 00140 { 00141 bool press = false; 00142 if (button1_->wasClicked()){ 00143 press = true; 00144 cout << "Button 1 was pressed"<<endl; 00145 } 00146 if (button2_->wasClicked()){ 00147 press = true; 00148 cout << "Button 2 was pressed"<<endl; 00149 } 00150 if (key_->isPressed()){ 00151 cout << "Keyboard input key is pressed"<<endl; 00152 } 00153 00154 if (press){ 00155 cout << "Text entry = \"" << textEntry_->getValue()<<"\""<<endl; 00156 cout << "Text selection = item " << textSelection_->getSelection()<<endl; 00157 cout << endl; 00158 } 00159 00160 }

Generated on Wed Sep 1 14:46:57 2004 by doxygen 1.3.7