Thread: Simple SDL2 made menu

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    6

    Simple SDL2 made menu

    Hi!

    I'm newbie to programming and I'm learning to program SDL2 and C++.

    Here is a code I wrote:

    Code:
    include <GL/glew.h>
    #include <SDL2/SDL.h>
    #include <SDL2/SDL_opengl.h>
    #include <SDL2/SDL_ttf.h>
    #include <GL/gl.h>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <limits.h>
    
    using namespace std;
    
    SDL_Window *gWindow;
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;
    SDL_Event event;
    
    class Menu
    {
    	SDL_Surface* screen;
    	TTF_Font* font;
    	int x, y; 					// Mouse position
    	int NUMMENUS = 0;
    	vector<string> labels{};
    	vector<SDL_Surface*> menus{};
    	vector<SDL_Rect> pos{};
    	SDL_Surface* ekraan;
    	int suurim = 0;
    	SDL_Rect ajutine;
    
    	public:
    	Menu ( TTF_Font* SisendFont, vector<string> iLabels)
    	{
    		ekraan = SDL_GetWindowSurface(gWindow);
    		font =  SisendFont;
    		labels = iLabels;
    		int suurus = 0;
    		int ajutine_suurus = 0;
    
    		for (vector<string>::iterator it = iLabels.begin();\
    			 it != iLabels.end(); it++)
    		{
    			NUMMENUS++;
    		};
    
    		for (int i = 0; i < NUMMENUS; i++)
    		{	
    			ajutine_suurus = labels.at(i).size();
    			if (suurus < ajutine_suurus)
    			{
    				suurim = i;
    				suurus = ajutine_suurus;
    			};
    		};
    
    		for (int i = 0; i < NUMMENUS; i++)
    			menus.push_back(TTF_RenderText_Solid(font, labels.at(i).c_str(), {255,255,255}));
    	};
    
    	// Event loop
    	int valja()
    	{
          ekraan = SDL_GetWindowSurface(gWindow);
    
          ajutine.w = menus.at(suurim)->w;
    		ajutine.h = menus.at(suurim)->h;
    		pos = {};
    		for (int i = 0; i < NUMMENUS; i++)
          {  
             ajutine.x = ekraan->w/2 - menus.at(suurim)->w/2;
             ajutine.y = ekraan->h/2 + (menus.at(suurim)->h)*(i-1);
             pos.push_back(ajutine);
          }
    
    //		SDL_Event event;
    		SDL_Rect raam;
    		raam.x = ekraan->w/2 - menus.at(suurim)->w/2;
    		raam.y = ekraan->h/2 - menus.at(suurim)->h;
    		raam.w = menus.at(suurim)->w;
    		raam.h = (menus.at(suurim)->h)*NUMMENUS;
    
    		SDL_FillRect(ekraan, /*&screen->clip_rect*/ &raam, SDL_MapRGB(ekraan->format, 128, 128, 128));
          SDL_UpdateWindowSurface(gWindow);
    		
    		// Väljastamine
    		for(int i=0; i < NUMMENUS; i++)
             if((SDL_BlitSurface(menus.at(i), NULL, ekraan, &pos.at(i)))<0)
    				printf("Blit error %s\n", SDL_GetError());
    		SDL_UpdateWindowSurface(gWindow);
    
    		
    		while ((SDL_PollEvent(&event) != 0))
    		{
    			cout<<"Poll outcome\n";
    			if (/*event.type== SDL_MOUSEBUTTONDOWN && */event.button.button == SDL_BUTTON_LEFT)
    			{
    				x=event.button.x;
    				y=event.button.y;
    				for(int i = 0; i<NUMMENUS; i++)
    					if(x>=pos.at(i).x && x<=pos.at(i).x+pos.at(i).w &&
    						y>=pos.at(i).y && y<=pos.at(i).y+pos.at(i).h)
    						return i;
    					else
    						return -1;
    			};
    		};
    	};
    };
    
    // Initialize SDL
    bool init()
    {
       //Initialization flag
       bool success = true;
    
       //Initialize SDL
       if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0 )
       {
          printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
          success = false;
       } else {
          //Create window
          gWindow = SDL_CreateWindow( "Create Triangle", SDL_WINDOWPOS_UNDEFINED,
    					 SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 
    					 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE );
          if( gWindow == NULL )
          {
             printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
             success = false;
          }
    	}	
    	if(TTF_Init()==-1)
    	{
    		printf("TTF_Init: %s\n", TTF_GetError());
    		success = false;
    	};
    	return success;
    };
    
    void Exit()
    {
    	TTF_Quit();
    	SDL_DestroyWindow(gWindow);
    	SDL_Quit();
    }
    
    
    int main (int argc, char* argv[])
    {
    	if( !init() )
       {
    		printf( "Failed to initialize!\n" );
    	} else 
    	{
    		// Fondi avamine
    		TTF_Font* Font;
    		Font = TTF_OpenFont("Lohit-Devanagari.ttf",20);
    		if (!Font)
    			printf("TTF_OpenFont: %s\n", TTF_GetError());
    
    		// Menüü loomine
    		SDL_Surface *screen;
    		screen = SDL_GetWindowSurface(gWindow);
    		if(!screen)
    		{
    			printf("SDL_GetWindowSurface error: %s\n", SDL_GetError());
    		}
    
    		vector<string> nimed{};
    		nimed.push_back("Esimene");
          nimed.push_back("Teine");
    		nimed.push_back("Seitsmes");
          nimed.push_back("Kolmas");
    		Menu menu(Font, nimed);
    
          bool quit = false;
    //      SDL_Event e;
    
    		while ( !quit )
    		{
             while( SDL_PollEvent(&event) != 0)
             {
                switch ( event.type )
                   {
                   case SDL_QUIT:
                         quit = true;
    							Exit();
                         break;
    					case SDL_MOUSEBUTTONDOWN:
    							if (event.button.button == SDL_BUTTON_RIGHT)
    							{
    							int j = menu.valja();
    								switch (j)
    								{
    									case 0:
    										cout<<"Esimene\n";
    										break;
    									case 1:
    										cout<<"Teine\n";
    										break;
    									case 2:
    										cout<<"Kolmas\n";
    										break;
    									case -1:
    										cout<<"Mujale\n";
    										break;
    								}
    							}
    							break;
                   }
             }
    		}
    	}
    	return 0;
    }
    I have a problem with SDL_PollEvent.
    Where is the problem?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    First of all, you need better indentation.
    Indentation style - Wikipedia

    Also, you're calling SDL_PollEvent in TWO places, where you should only be calling it in one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I've made simple code the plus calulation but it doesn't work.
    By Youngbin Shin in forum C Programming
    Replies: 3
    Last Post: 10-14-2015, 11:20 PM
  2. can this program be made simple?? if yes pls help out!
    By recklezz in forum C++ Programming
    Replies: 13
    Last Post: 04-16-2015, 07:16 AM
  3. Simple program I made - Question about code
    By PersianStyle in forum C++ Programming
    Replies: 22
    Last Post: 07-13-2009, 04:09 AM
  4. little problem with a simple shell program i made.
    By keira in forum Linux Programming
    Replies: 2
    Last Post: 02-26-2008, 11:42 PM
  5. who has made simple searching engine?
    By oshindeler in forum C++ Programming
    Replies: 2
    Last Post: 08-03-2006, 10:41 AM

Tags for this Thread