Thread: SDL_ttf Process terminated with status 3

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    6

    SDL_ttf Process terminated with status 3

    Hello guys, I am creating a very simple game. I have used SDL and Everything works fine, but whenever I try to use SDL_TTf to print scores, Programs terminates with process status 3. Without SDL_TTF code, everything works. Here is the code :-

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <SDL/SDL.h>
    #include <SDL/SDL_ttf.h>
    #include <sstream>
    
    const int WINDOWS_Height = 480;
    const int WINDOWS_Width = 640;
    const int SPRITE_SIZE = 32;
    const int BALL_SIZE=15;
    const char* WINDOWS_Title = "Ball Collecting Game";
    
    using namespace std;
    SDL_Surface *grass, *sprite, *temp, *ball, *scores;
    SDL_Rect rcSprite, rcGrass, rcSrc, rcBall, rcScores;
    int colorkey, score=0, bc=0;
    bool runGame = true;
    stringstream pScore;
    
    bool checkCollision(SDL_Rect Sprite1, SDL_Rect Sprite2);
    void handleEvents(SDL_Event event);                                                                 // Function to handle Events
    
    int main (int argc, char *argv[]){
        SDL_Init(SDL_INIT_VIDEO);                                                                                // Initializing SDL and Creating Window
        SDL_Surface *screen = SDL_SetVideoMode(WINDOWS_Width, WINDOWS_Height, 0, SDL_HWSURFACE | SDL_DOUBLEBUF);
        SDL_WM_SetCaption(WINDOWS_Title, "Ball Game");
    
    
        temp = SDL_LoadBMP("sprite.bmp");                                                                //Loading Sprite Sheet
        sprite = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
    
        SDL_EnableKeyRepeat(70, 70);                                                                    //Enabling Key Repeat
    
        colorkey = SDL_MapRGB(screen->format, 255, 0, 255);                             //Setting up sprite colorkey
        SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
    
        temp = SDL_LoadBMP("grass.bmp");                                                            //Loading Grass
        grass = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
    
        temp = SDL_LoadBMP("ball.bmp");
        ball = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
    
        colorkey = SDL_MapRGB(screen->format, 192, 192, 192);                             //Setting up ball color-key
        SDL_SetColorKey(ball, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
    
        rcSprite.x = 150;                                                                                           //Setting Position of sprite
        rcSprite.y = 150;
    
        rcBall.x = rand() % 600 + 30;                                                                                //Random Position of Ball
        rcBall.y = rand() % 450 + 30;
    
        rcSrc.x =  128;                                                                                             //setting frames for animation
        rcSrc.y = 0;
        rcSrc.w = SPRITE_SIZE;
        rcSrc.h = SPRITE_SIZE;
    
        rcScores.x = rcScores.y = 0;
        rcScores.h = 20;
        rcScores.w = WINDOWS_Width;
    
        TTF_Init();
        TTF_Font *times;
        times = TTF_OpenFont("Lato.ttf", 10);
        SDL_Color white = {255, 255, 255};
    
            //Game Loop
        while (runGame) {
    
            SDL_Event event;
    
            if (SDL_PollEvent(&event)) {                                                                         //Here we call function handle Event
                handleEvents(event);
                if (event.key.keysym.sym == SDLK_q || event.key.keysym.sym == SDLK_ESCAPE) {
                    return 0;
        }
            }
    
            if (rcSprite.x < 0)                                                                                            //Collision with Corners
                rcSprite.x = 0;
            else if (rcSprite.x > WINDOWS_Width-SPRITE_SIZE)
                rcSprite.x = WINDOWS_Width-SPRITE_SIZE;
            if (rcSprite.y < 0)
                rcSprite.y = 0;
            else if (rcSprite.y > WINDOWS_Height-SPRITE_SIZE)
                rcSprite.y = WINDOWS_Height-SPRITE_SIZE;
    
            if  (checkCollision(rcBall, rcSprite) == true) {
    
                rcBall.x = rand() % 600 + 30;
                rcBall.y = rand() % 400 + 30;
                score += 10;
                pScore << score;
                SDL_Flip(screen);
            }
            scores = TTF_RenderText_Solid(times, pScore.str().c_str(), white);
            for (int x = 0; x < WINDOWS_Width/SPRITE_SIZE; x++) {
                for (int y = 0; y <WINDOWS_Height/SPRITE_SIZE; y++ ) {
                    rcGrass.x = x * SPRITE_SIZE;
                    rcGrass.y = y  *SPRITE_SIZE ;
                    SDL_BlitSurface(grass, NULL, screen, &rcGrass);                                     //Drawing Grass on Screen
                }
            }
            SDL_BlitSurface(sprite, &rcSrc, screen, &rcSprite);                                           //Drawing Sprite on Screen
    
            SDL_BlitSurface(ball, NULL, screen, &rcBall);                                                   //Drawing ball on Screen
    
            SDL_FillRect(screen, &rcScores, 0x221122);
    
            SDL_BlitSurface(scores, NULL, screen, &rcScores);
    
            SDL_UpdateRect(screen, 0, 0, 0, 0);                                                                 //Updating Screen
        }
        SDL_FreeSurface(grass);
        SDL_FreeSurface(sprite);
        TTF_Quit();
        SDL_Quit();
    
        return 0;
    }
    
    void handleEvents(SDL_Event event) {
        bool keysHeld[323] = {false};
        if (event.type == SDL_QUIT) {
            runGame = false;
            return;
        }
        if (event.type == SDL_KEYDOWN){
            keysHeld[event.key.keysym.sym] = true;
        }
        if (event.type == SDL_KEYUP){
            keysHeld[event.key.keysym.sym] = false;
        }
        if (keysHeld[SDLK_UP] ) {
            if(rcSrc.x == 0)
                rcSrc.x = 32;
            else
                rcSrc.x = 0;
            rcSprite.y -= 7;
        }
    
        if (keysHeld[SDLK_DOWN]) {
            if (rcSrc.x == 128)
                rcSrc.x = 160;
            else
                rcSrc.x = 128;
            rcSprite.y += 7;
        }
    
        if (keysHeld[SDLK_LEFT]) {
            if (rcSrc.x == 192)
                rcSrc.x = 224;
            else
                rcSrc.x = 192;
            rcSprite.x -= 7;
        }
    
        if (keysHeld[SDLK_RIGHT]) {
            if (rcSrc.x == 64)
                rcSrc.x = 96;
            else
                rcSrc.x = 64;
            rcSprite.x += 7;
        }
    }
    
    
    bool checkCollision( SDL_Rect A, SDL_Rect B ) {
                                                                                                                        //The sides of the rectangles
        int leftA, leftB;
        int rightA, rightB;
        int topA, topB;
        int bottomA, bottomB;
                                                                                                                        //Calculate the sides of rect A
        leftA = A.x;
        rightA = A.x + A.w;
        topA = A.y;
        bottomA = A.y + A.h;
    
        leftB = B.x;                                                                                         //Calculate the sides of rect B
        rightB = B.x + B.w;
        topB = B.y;
        bottomB = B.y + B.h;
                                                                                                                    //If any of the sides from A are outside of B
        if( bottomA <= topB ) {
            return false;
        }
        if( topA >= bottomB ) {
            return false;
        }
        if( rightA <= leftB ) {
            return false;
        }
        if( leftA >= rightB ) {
            return false;
        }
                                                                                                                //If none of the sides from A are outside B
        return true;
    }
    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > times = TTF_OpenFont("Lato.ttf", 10);
    You need to check whether this is NULL or not.
    If it is NULL, then you didn't get the font you wanted.

    Also, it's a good idea to write another program which just tests the font aspect.
    In essence, the "hello world" version of the problem you're trying to write.

    Writing small test programs to make sure you understand a problem completely is a useful technique.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    Thank you Salem,
    Problem is resolved but now as I touch the ball score not clears. I want to be score like this.
    10
    20 (after a ball is collected) and so on, but it goes something like this 10
    1020(after a ball is collected).

    Thank you again.


    problem Solved .....
    Just have to empty pScore before reassigning.
    Last edited by darpan1118; 04-13-2014 at 06:44 AM.

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    Why my .exe not working outside Codeblocks?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps it has a different idea as to what the "current directory" is.

    So when you do this
    temp = SDL_LoadBMP("sprite.bmp");
    It will always look in the "current directory".

    IIRC, double clicking on a .exe in explorer makes the current directory the location of the .exe
    Within the IDE, it could be just set to the project root.
    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.

  6. #6
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    I have already changed all of them from "sprite.bmp" to "./sprite.bmp".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-13-2013, 01:03 AM
  2. memory leak and process terminated
    By learn in forum Linux Programming
    Replies: 2
    Last Post: 10-15-2012, 01:48 AM
  3. Checking if a process has terminated?
    By Elysia in forum Linux Programming
    Replies: 26
    Last Post: 11-22-2011, 06:29 PM
  4. Process Terminated with status -1073741510
    By Varethien in forum C Programming
    Replies: 5
    Last Post: 08-09-2011, 04:16 AM
  5. Process terminated with status -1073741819
    By smithx in forum C Programming
    Replies: 5
    Last Post: 11-01-2010, 11:13 PM

Tags for this Thread