Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL Menu

Started by Norbo11 Mar 21, 2009 at 3:08 AM 2 replies 3.4k views
Original Post
Norbo11
Norbo11
Hello, im new to these forums so dont bitch me out pls. I have tried to make an SDL menu, which u could navigate through by using your up and down keys. However i got some errors, and im not sure what they mean or why do they occur. If someone could help me, i would be gratefull. My code:
#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_image.h"
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;

//The attributes of the screen 
const int SCREEN_WIDTH = 800; 
const int SCREEN_HEIGHT = 600; 
const int SCREEN_BPP = 32; 

SDL_Surface *screen = NULL;
SDL_Surface *bg = NULL;
SDL_Event event;
int currentButton = 1;
//Get the keystates 
Uint8 *keystates = SDL_GetKeyState( NULL ); 
bool mainMenu = true;

//File stream for the game log. 
std::ofstream logger( "log.txt" ); 
//===============================================================================================

class Button {
    public:
    SDL_Rect offset;
    SDL_Surface *imageUp;
    SDL_Surface *imageOver;
    int id;
    void handle_input();
    void show(std::string button);
};

//===============================================================================================
//Function

SDL_Surface *load_image( std::string filename )
{ 
    //The image that's loaded 
    SDL_Surface* loadedImage = NULL; 
    //The optimized image that will be used 
    SDL_Surface* optimizedImage = NULL; 
    //Load the image using SDL_image 
    loadedImage = IMG_Load( filename.c_str() ); 
    //If the image loaded 
    if( loadedImage != NULL ) 
    { 
        //Create an optimized image 
        optimizedImage = SDL_DisplayFormat( loadedImage ); 
        //Free the old image 
        SDL_FreeSurface( loadedImage ); 
    } 
    //Return the optimized image 
    return optimizedImage; 
} 

void draw_image(int x,int y,SDL_Surface *source,SDL_Surface *destination)
{
    //Make a temporary rectangle to hold the offsets 
    SDL_Rect offset; 
    
    //Give the offsets to the rectangle 
    offset.x = x; 
    offset.y = y; 
        
    SDL_BlitSurface(source,NULL,destination,&offset); 
}

void Button::handle_input()
{
    //If up is pressed 
    if( keystates[ SDLK_UP ] ) 
    { 
        currentButton--;
    } 
    //If down is pressed 
    if( keystates[ SDLK_DOWN ] ) 
    { 
       currentButton++;
    }
}

void initButtons()
{
    Button play;
    Button howto;
    Button options;
    Button credits;
    Button exit;
    Button ok;
    Button back;
    play.id = 1;
    howto.id = 2;
    options.id = 3;
    credits.id = 4;
    exit.id = 5;
    ok.id = 6;
    back.id = 7;
    play.offset.x = 100;
    play.offset.y = 100;
    howto.offset.x = 100;
    howto.offset.y = 200;
    options.offset.x = 100;
    options.offset.y = 300;
    credits.offset.x = 100;
    credits.offset.y = 400;    
    exit.offset.x = 100;
    exit.offset.y = 500;
    ok.offset.x = 100;
    ok.offset.y = 600;
    back.offset.x = 100;
    back.offset.y = 700;
    back.imageOver = load_image("graphics/back_mouseover.bmp");
    back.imageUp = load_image("graphics/back_up.bmp");
    credits.imageOver = load_image("graphics/credits_mouseover.bmp");
    credits.imageUp = load_image("graphics/credits_up.bmp");
    exit.imageOver = load_image("graphics/exit_mouseover.bmp");
    exit.imageUp = load_image("graphics/exit_up.bmp");
    howto.imageOver = load_image("graphics/howto_mouseover.bmp");
    howto.imageUp = load_image("graphics/howto_up.bmp");
    ok.imageOver = load_image("graphics/ok_button mouse_over.bmp");
    ok.imageUp = load_image("graphics/ok_button_up.bmp");
    play.imageOver = load_image("graphics/playgame_mouseover.bmp");
    play.imageUp = load_image("graphics/playgame_up.bmp");
}

void Button::show(std::string button)
{
    if (currentButton==id) {
        draw_image(button.c_str().offset.x,button.c_str()offset.y,button.c_str().imageOver,screen);
    }
    else {
        draw_image(button.c_str().offset.x,button.c_str().offset.y,button.c_str().imageUp,screen);
    }
}

void clean_up() 
{ 
    SDL_FreeSurface(bg);
    //Quit SDL 
    SDL_Quit(); 
} 

bool init()
{
    //Initialize all SDL subsystems 
    if(SDL_Init(SDL_INIT_EVERYTHING)==-1) { 
        return false; 
    } 
    //Set up the screen 
    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE); 
    if (screen == NULL)
    {
        return false;
    }
    initButtons();
    //Set the window caption 
    SDL_WM_SetCaption("yo",NULL); 
    return true;
}

#undef main
int main( int argc, char* args[] ) 
{
    bool quit = false;
    if (init == false){
        return 1;
    }
    while (quit == false)
    {
        while (mainMenu)
        {
            play.show("play");
            howto.show("howto");
            options.show("options");
            credits.show("credits");
            exit.show("exit");
            ok.show("ok");
            back.show("back");
        }
        //While there's an event to handle 
        while(SDL_PollEvent(&event)) 
        { 
            //If the user has Xed out the window 
            if(event.type == SDL_QUIT) 
            { 
                //Quit the program 
                quit = true; 
            } 
        } 
    }
    if (quit==true) {
        clean_up();
    }
    return 0;
} 


compiler says:


#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_image.h"
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;

//The attributes of the screen 
const int SCREEN_WIDTH = 800; 
const int SCREEN_HEIGHT = 600; 
const int SCREEN_BPP = 32; 

SDL_Surface *screen = NULL;
SDL_Surface *bg = NULL;
SDL_Event event;
int currentButton = 1;
//Get the keystates 
Uint8 *keystates = SDL_GetKeyState( NULL ); 
bool mainMenu = true;

//File stream for the game log. 
std::ofstream logger( "log.txt" ); 
//===============================================================================================

class Button {
    public:
    SDL_Rect offset;
    SDL_Surface *imageUp;
    SDL_Surface *imageOver;
    int id;
    void handle_input();
    void show(std::string button);
};

//===============================================================================================
//Function

SDL_Surface *load_image( std::string filename )
{ 
    //The image that's loaded 
    SDL_Surface* loadedImage = NULL; 
    //The optimized image that will be used 
    SDL_Surface* optimizedImage = NULL; 
    //Load the image using SDL_image 
    loadedImage = IMG_Load( filename.c_str() ); 
    //If the image loaded 
    if( loadedImage != NULL ) 
    { 
        //Create an optimized image 
        optimizedImage = SDL_DisplayFormat( loadedImage ); 
        //Free the old image 
        SDL_FreeSurface( loadedImage ); 
    } 
    //Return the optimized image 
    return optimizedImage; 
} 

void draw_image(int x,int y,SDL_Surface *source,SDL_Surface *destination)
{
    //Make a temporary rectangle to hold the offsets 
    SDL_Rect offset; 
    
    //Give the offsets to the rectangle 
    offset.x = x; 
    offset.y = y; 
        
    SDL_BlitSurface(source,NULL,destination,&offset); 
}

void Button::handle_input()
{
    //If up is pressed 
    if( keystates[ SDLK_UP ] ) 
    { 
        currentButton--;
    } 
    //If down is pressed 
    if( keystates[ SDLK_DOWN ] ) 
    { 
       currentButton++;
    }
}

void initButtons()
{
    Button play;
    Button howto;
    Button options;
    Button credits;
    Button exit;
    Button ok;
    Button back;
    play.id = 1;
    howto.id = 2;
    options.id = 3;
    credits.id = 4;
    exit.id = 5;
    ok.id = 6;
    back.id = 7;
    play.offset.x = 100;
    play.offset.y = 100;
    howto.offset.x = 100;
    howto.offset.y = 200;
    options.offset.x = 100;
    options.offset.y = 300;
    credits.offset.x = 100;
    credits.offset.y = 400;    
    exit.offset.x = 100;
    exit.offset.y = 500;
    ok.offset.x = 100;
    ok.offset.y = 600;
    back.offset.x = 100;
    back.offset.y = 700;
    back.imageOver = load_image("graphics/back_mouseover.bmp");
    back.imageUp = load_image("graphics/back_up.bmp");
    credits.imageOver = load_image("graphics/credits_mouseover.bmp");
    credits.imageUp = load_image("graphics/credits_up.bmp");
    exit.imageOver = load_image("graphics/exit_mouseover.bmp");
    exit.imageUp = load_image("graphics/exit_up.bmp");
    howto.imageOver = load_image("graphics/howto_mouseover.bmp");
    howto.imageUp = load_image("graphics/howto_up.bmp");
    ok.imageOver = load_image("graphics/ok_button mouse_over.bmp");
    ok.imageUp = load_image("graphics/ok_button_up.bmp");
    play.imageOver = load_image("graphics/playgame_mouseover.bmp");
    play.imageUp = load_image("graphics/playgame_up.bmp");
}

void Button::show(std::string button)
{
    if (currentButton==id) {
        draw_image(button.c_str().offset.x,button.c_str()offset.y,button.c_str().imageOver,screen);
    }
    else {
        draw_image(button.c_str().offset.x,button.c_str().offset.y,button.c_str().imageUp,screen);
    }
}

void clean_up() 
{ 
    SDL_FreeSurface(bg);
    //Quit SDL 
    SDL_Quit(); 
} 

bool init()
{
    //Initialize all SDL subsystems 
    if(SDL_Init(SDL_INIT_EVERYTHING)==-1) { 
        return false; 
    } 
    //Set up the screen 
    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE); 
    if (screen == NULL)
    {
        return false;
    }
    initButtons();
    //Set the window caption 
    SDL_WM_SetCaption("yo",NULL); 
    return true;
}

#undef main
int main( int argc, char* args[] ) 
{
    bool quit = false;
    if (init == false){
        return 1;
    }
    while (quit == false)
    {
        while (mainMenu)
        {
            play.show("play");
            howto.show("howto");
            options.show("options");
            credits.show("credits");
            exit.show("exit");
            ok.show("ok");
            back.show("back");
        }
        //While there's an event to handle 
        while(SDL_PollEvent(&event)) 
        { 
            //If the user has Xed out the window 
            if(event.type == SDL_QUIT) 
            { 
                //Quit the program 
                quit = true; 
            } 
        } 
    }
    if (quit==true) {
        clean_up();
    }
    return 0;
} 


compiler says:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\sda\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\sda\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"  -I"C:/SDL-1.2.13/include"  -I"C:/Dev-Cpp/include"  -I"C:/Dev-Cpp/include/c++"  -I"C:/SDL-1.2.13/include"   

main.cpp: In member function `void Button::show(std::string)':

main.cpp:134: error: `offset' is not a type
main.cpp:134: error: request for member of non-aggregate type before '.' token
main.cpp:134: error: expected `)' before "offset"

main.cpp:134: error: `imageOver' is not a type

main.cpp:134: error: request for member of non-aggregate type before ',' token

main.cpp:137: error: `offset' is not a type
main.cpp:137: error: request for member of non-aggregate type before '.' token
main.cpp:137: error: `offset' is not a type
main.cpp:137: error: request for member of non-aggregate type before '.' token
main.cpp:137: error: `imageUp' is not a type
main.cpp:137: error: request for member of non-aggregate type before ',' token
main.cpp: In function `int main(int, char**)':
main.cpp:177: error: `play' undeclared (first use this function)
main.cpp:177: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:178: error: `howto' undeclared (first use this function)
main.cpp:179: error: `options' undeclared (first use this function)
main.cpp:180: error: `credits' undeclared (first use this function)
main.cpp:181: error: request for member `show' in `exit', which is of non-class type `void ()(int)'
main.cpp:182: error: `ok' undeclared (first use this function)
main.cpp:183: error: `back' undeclared (first use this function)

make.exe: *** [main.o] Error 1

Execution terminated

Thanks, Norbo EDIT: Okay i managed to fix all my compile time errors, but when i run my game it crashes immedietly. crashes i mean the little window from microsoft pops out and wants me to send a raport to them. i have no clue why. please help me ;( my new code:
#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_image.h"
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;

//The attributes of the screen 
const int SCREEN_WIDTH = 800; 
const int SCREEN_HEIGHT = 600; 
const int SCREEN_BPP = 32; 

SDL_Surface *screen = NULL;
SDL_Surface *bg = NULL;
SDL_Event event;
int currentButton = 1;
//Get the keystates 
Uint8 *keystates = SDL_GetKeyState( NULL ); 
bool mainMenu = true;

//File stream for the game log. 
std::ofstream logger( "log.txt" ); 
//===============================================================================================

class Button {
    public:
    SDL_Rect offset;
    SDL_Surface *imageUp;
    SDL_Surface *imageOver;
    int id;
    void handle_input();
    void show(std::string button);
};

    Button play;
    Button howto;
    Button options;
    Button credits;
    Button exit1;
    Button ok;
    Button back;

//===============================================================================================
//Function

SDL_Surface *load_image( std::string filename )
{ 
    //The image that's loaded 
    SDL_Surface* loadedImage = NULL; 
    //The optimized image that will be used 
    SDL_Surface* optimizedImage = NULL; 
    //Load the image using SDL_image 
    loadedImage = IMG_Load( filename.c_str() ); 
    //If the image loaded 
    if( loadedImage != NULL ) 
    { 
        //Create an optimized image 
        optimizedImage = SDL_DisplayFormat( loadedImage ); 
        //Free the old image 
        SDL_FreeSurface( loadedImage ); 
    } 
    //Return the optimized image 
    return optimizedImage; 
} 

void draw_image(int x,int y,SDL_Surface *source,SDL_Surface *destination)
{
    //Make a temporary rectangle to hold the offsets 
    SDL_Rect offset; 
    
    //Give the offsets to the rectangle 
    offset.x = x; 
    offset.y = y; 
        
    SDL_BlitSurface(source,NULL,destination,&offset); 
}

void Button::handle_input()
{
    //If up is pressed 
    if( keystates[ SDLK_UP ] ) 
    { 
        currentButton--;
    } 
    //If down is pressed 
    if( keystates[ SDLK_DOWN ] ) 
    { 
       currentButton++;
    }
}

void initButtons()
{
    play.id = 1;
    howto.id = 2;
    options.id = 3;
    credits.id = 4;
    exit1.id = 5;
    ok.id = 6;
    back.id = 7;
    play.offset.x = 100;
    play.offset.y = 100;
    howto.offset.x = 100;
    howto.offset.y = 200;
    options.offset.x = 100;
    options.offset.y = 300;
    credits.offset.x = 100;
    credits.offset.y = 400;    
    exit1.offset.x = 100;
    exit1.offset.y = 500;
    ok.offset.x = 100;
    ok.offset.y = 600;
    back.offset.x = 100;
    back.offset.y = 700;
    back.imageOver = load_image("graphics/back_mouseover.bmp");
    back.imageUp = load_image("graphics/back_up.bmp");
    credits.imageOver = load_image("graphics/credits_mouseover.bmp");
    credits.imageUp = load_image("graphics/credits_up.bmp");
    exit1.imageOver = load_image("graphics/exit_mouseover.bmp");
    exit1.imageUp = load_image("graphics/exit_up.bmp");
    howto.imageOver = load_image("graphics/howto_mouseover.bmp");
    howto.imageUp = load_image("graphics/howto_up.bmp");
    ok.imageOver = load_image("graphics/ok_button mouse_over.bmp");
    ok.imageUp = load_image("graphics/ok_button_up.bmp");
    play.imageOver = load_image("graphics/playgame_mouseover.bmp");
    play.imageUp = load_image("graphics/playgame_up.bmp");
}

void Button::show(string button)
{
    if (currentButton==id) {
        draw_image(offset.x,offset.y,imageOver,screen);
    }
    else {
        draw_image(offset.x,offset.y,imageUp,screen);
    }
}

void clean_up() 
{ 
    SDL_FreeSurface(bg);
    //Quit SDL 
    SDL_Quit(); 
} 

bool init()
{
    //Initialize all SDL subsystems 
    if(SDL_Init(SDL_INIT_EVERYTHING)==-1) { 
        return false; 
    } 
    //Set up the screen 
    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE); 
    if (screen == NULL)
    {
        return false;
    }
    //Set the window caption 
    SDL_WM_SetCaption("yo",NULL); 
    return true;
}

#undef main
int main( int argc, char* args[] ) 
{
    bool quit = false;
    if (init == false){
        return 1;
    }
    while (quit == false)
    {
        while (mainMenu)
        {
            initButtons();
            play.show("play");
            howto.show("howto");
            options.show("options");
            credits.show("credits");
            exit1.show("exit");
            ok.show("ok");
            back.show("back");
        }
        //While there's an event to handle 
        while(SDL_PollEvent(&event)) 
        { 
            //If the user has Xed out the window 
            if(event.type == SDL_QUIT) 
            { 
                //Quit the program 
                quit = true; 
            } 
        } 
    }
    if (quit==true) {
        clean_up();
    }
    return 0;
} 

[Edited by - Norbo11 on March 21, 2009 3:29:17 AM]
evolutional
evolutional
A quick glance over the code shows me that:


bool quit = false;
if (init == false){
return 1;
}



Should be...


bool quit = false;
if (init() == false){
return 1;
}



Norbo11
Norbo11
Quote:
Original post by evolutional
A quick glance over the code shows me that:


bool quit = false;
if (init == false){
return 1;
}



Should be...


bool quit = false;
if (init() == false){
return 1;
}


<333333333

Thank you so much! no more crashing
but the screen is blank, no images :(

rip-off
rip-off
You have no error checking when you load the images. Test whether the pointer returned by IMG_Load is NULL and use the IMG_GetError() function to find out why it happened.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.