Skip to main content
gamedev.net
🔒 Locked

SDL "console"

Started by FernandoLujan Jan 6, 2007 at 11:58 PM 7 replies 1.9k views
Original Post
FernandoLujan
FernandoLujan
So, I'm trying to make a little console object, but whenever I toggle for it to be visible, my program shuts down. I get no errors, but I simply don't know what the problem is. I'm really sorry about the posting problems thing. This is probably something really dumb again, but I can't seem to find the problem. Here's my code: main.cpp
#include <iostream>

#include "engine.h"

using namespace std;

int main(int argc, char *args[])
{
    Engine::cEngine myGame(800, 600, 32);
    myGame.Initialize();
    
    while(myGame.Exit() != true)
    {
        myGame.HandleInput();
        myGame.Render();
    }
    
    myGame.DeInitialize();
    return 0;
}


engine.h
#include <iostream>

#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_ttf.h>

namespace Engine {
    enum 
        {
            CONSOLE_DISABLED = 0,
            CONSOLE_ENABLED
        };
    
    class cConsole {
        int state;
        
        SDL_Rect bounds;
        
        SDL_Surface *render;
        TTF_Font *font;
        
        char *text;
    public:
        cConsole(int);
        
        int State(void);
        void Toggle(int);
        
        void Render(int, int, int, int, SDL_Surface *);
        void Close();
    };

    class cEngine {
        SDL_Event events;

        SDL_Surface *screen;
        SDL_Surface *render;

        cConsole myConsole;
        
        int screen_width;
        int screen_height;
        int screen_bpp;
    
        bool engineExit;
    public:
        cEngine(int, int, int);
    
        void Initialize(void);
        void DeInitialize(void);
    
        void HandleInput(void);
        void Render(void);
        
        bool Exit(void);
    };
};



engine.cpp
#include <iostream>

#include "engine.h"
#include "video.h"

#include <SDL/SDL.h>        // Simple DirectMedia Layer
#include <SDL/SDL_image.h>  // Image formats(*.png, *.tga, etc...)
#include <SDL/SDL_mixer.h>  // Music and sound effects
#include <SDL/SDL_ttf.h>    // TrueType Fonts
using namespace std;

namespace Engine {
    cConsole::cConsole(int STATE = CONSOLE_DISABLED)
    {
        state = STATE;
        
        render = NULL;
        font = TTF_OpenFont("console.ttf", 12);
        
        text = "Console opened...\n";
    };
    
    int cConsole::State()
    {
        return state;
    }
    
    void cConsole::Toggle(int STATE)
    {
        state = STATE;
    }
    
    void cConsole::Render(int x, int y, int w, int h, SDL_Surface *dest)
    {
        if(state == CONSOLE_ENABLED)
        {
            SDL_color color = { 255, 255, 255 };
            bounds.x = x;
            bounds.y = y;
            bounds.w = w;
            bounds.h = h;
            
            SDL_FillRect(render, &bounds, SDL_MapRGB(render->format, 255, 255, 255));
            Video::ApplySurface(x, y, render, dest, 120);
            render = TTF_RenderText_Solid(font, text, color);
            Video::ApplySurface(x + 5, y + 5, render, dest)
        }
    }
    
    void cConsole::Close()
    {
        TTF_CloseFont(font);
        TTF_Quit();
    };
//----------------------------------------------------------------------------//
    cEngine::cEngine(int width, int height, int bpp)
    {
        screen_width = width;
        screen_height = height;
        screen_bpp = bpp;
        
        engineExit = false;
        
        screen = NULL;
        render = NULL;
    }
    
    void cEngine::Initialize()
    {
        SDL_Init(SDL_INIT_EVERYTHING);
        TTF_Init();
    
        SDL_WM_SetCaption("Extreme Pong", NULL);
        screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_HWSURFACE | SDL_FULLSCREEN);
    }

    void cEngine::HandleInput()
    {
        while(SDL_PollEvent(&events))
        {
            switch(events.type)
            {
                case SDL_KEYDOWN:
                {
                    if(events.key.keysym.sym == SDLK_ESCAPE)
                    {
                        engineExit = true;
                        break;
                    }
                    if(events.key.keysym.sym == SDLK_BACKQUOTE)
                    {
                        if(myConsole.State() == CONSOLE_DISABLED)
                            myConsole.Toggle(CONSOLE_ENABLED);
                        else
                            myConsole.Toggle(CONSOLE_DISABLED);
                        break;
                    }
                }
            }
        }
    }

    void cEngine::Render()
    {
        myConsole.Render(10, 10, 200, 100, screen);
        SDL_Flip(screen);
    }

    void cEngine::DeInitialize()
    {
        myConsole.Close();
        TTF_Quit();
    
        SDL_FreeSurface(screen);
        SDL_FreeSurface(render);
        SDL_Quit();
    }
    
    bool cEngine::Exit()
    {
        return engineExit;
    }
};


video.h
#include <iostream>

#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>  
#include <SDL/SDL_ttf.h>

namespace Video {
    SDL_Surface *LoadImage(char *);
    void ApplySurface(int, int, SDL_Surface *, SDL_Surface *, int alpha = SDL_ALPHA_OPAQUE, SDL_Rect *clip = NULL);
};



video.cpp
#include <iostream>

#include <SDL/SDL.h>        // Simple DirectMedia Layer
#include <SDL/SDL_image.h>  // Image formats(*.png, *.tga, etc...)
#include <SDL/SDL_mixer.h>  // Music and sound effects
#include <SDL/SDL_ttf.h>    // TrueType Fonts

using namespace std;

namespace Video {
    SDL_Surface *LoadImage(char *filename)
    {
        SDL_Surface *loadedImage = NULL;
        SDL_Surface *optimizedImage = NULL;
    
        loadedImage = IMG_Load(filename);
        optimizedImage = SDL_DisplayFormat(loadedImage);
    
        Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 255, 0, 255);
        SDL_SetColorKey(optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);
    
        SDL_FreeSurface(loadedImage);
        return optimizedImage;
    }

    void ApplySurface(int x, int y, SDL_Surface *source, SDL_Surface *dest, int alpha = SDL_ALPHA_OPAQUE, SDL_Rect *clip = NULL)
    {
        SDL_Rect offset;
        offset.x = x;
        offset.y = y;
    
        SDL_SetAlpha(source, SDL_RLEACCEL | SDL_SRCALPHA, alpha);
        SDL_BlitSurface(source, clip, dest, &offset);
    }
};



Anyway, I'm still fairly new to programming considering the experience everyone else here has. I think the problem is most likely found in cConsole::Render() [Edited by - FernandoLujan on January 7, 2007 12:18:54 AM]
Mushu
Mushu
Err, where's the definition of cConsole::toggle? :/
FernandoLujan
FernandoLujan
Yeah, sorry about that. It was originally Update. But just before posting I changed it to Toggle because it made a lot more sense than Update. I forgot to change the definition from Update to Toggle though.

The problem still occurs. Any ideas?
Mushu
Mushu
I'm still looking at the code, but the behavior you described makes it probable that the error is in the console's render function (since it crashes when you try to render it). This line looks especially suspicious -

(in cConsole::render)
SDL_FillRect(render, &bounds, SDL_MapRGB(render->format, 255, 255, 255));

I don't see where render is being set - if it isn't being set to anything, that's a problem. (still looking over code). Yeah, looks like cEngine never passes its main SDL_Surface* to cConsole, so render is uninitialized, and causes the program to crash when you attempt to dereference it.
FernandoLujan
FernandoLujan
there's an SDL_Surface *render; in cConsole as well.
gumpy
gumpy
Quote:
Original post by Mushu
I'm still looking at the code, but the behavior you described makes it probable that the error is in the console's render function (since it crashes when you try to render it). This line looks especially suspicious -

(in cConsole::render)
SDL_FillRect(render, &bounds, SDL_MapRGB(render->format, 255, 255, 255));

I don't see where render is being set - if it isn't being set to anything, that's a problem. (still looking over code). Yeah, looks like cEngine never passes its main SDL_Surface* to cConsole, so render is uninitialized, and causes the program to crash when you attempt to dereference it.


i concur.

Program received signal SIGSEGV, Segmentation fault.0x08048b42 in Engine::cConsole::Render (this=0xbfedec70, x=10, y=10, w=200,    h=100, dest=0x8056f58) at ./engine.cpp:4343                  SDL_FillRect(render, &bounds, SDL_MapRGB(render->format, 255, 255, 255));(gdb) print render$1 = (SDL_Surface *) 0x0
This space for rent.
ne0_kamen
ne0_kamen
I'm not sure but you probably can make a SDL_Surface* GetMainSurface(); public function in your CEngine class and declare as follows
SDL_Surface* Engine::CEngine::GetMainSurface()
{
return screen;
}
and then before your SDL_FillRect(render, &bounds, SDL_MapRGB(render->format, 255, 255, 255));
call render = SDL_DisplayFormatAlpha(GetMainSurface());
Alternatively you can call render = SDL_CreateRGBSurface() with the needed parameters insted making the above.Again I'm not sure but that may save the day.
Servant of the Lord
Servant of the Lord
Quote:
Original post by FernandoLujan
there's an SDL_Surface *render; in cConsole as well.

Yes, but as Mushu says, you never point it to anything other than NULL. At least that I can see. You can't draw on a NULL surface, were you expecting a blank surface? You could create a blank surface with SDL_CreateRGBSurface and draw onto that.

[Edit:] NM, ne0_kamen got there first. [smile]
FernandoLujan
FernandoLujan
sadly, my friends... I just figured out the problem...

    cConsole::cConsole(int STATE = CONSOLE_DISABLED)    {        state = STATE;                render = NULL;        font = TTF_OpenFont("console.ttf", 12);                text = "Console opened...\n";    };


was replaced by

    cConsole::cConsole(int STATE = CONSOLE_DISABLED)    {        state = STATE;                render = NULL;        TTF_Init();        font = TTF_OpenFont("console.ttf", 12);                text = "Console opened...\n";    };


This is both comical and embarassing...

Topic Locked

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

Sign in to reply to this topic.