Thread: exe not working outside compiler

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

    exe not working outside compiler

    Hello friends,
    I have created a small game. Everything is working, but I cannot run exe outside codeblocks. I have placed all the necessary .dll and files in debug, release, and root folder. Here is the code.
    Code:
    #include <iostream>
    #include <SDL/SDL_mixer.h>
    #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;
    Mix_Music *mainMusic  = NULL;
    Mix_Music *ballCollection  = NULL;;
    
    
    bool checkCollision(SDL_Rect Sprite1, SDL_Rect Sprite2);
    void musicDone();
    void handleEvents(SDL_Event event);                                                                                             // Function to handle Events
    
    int main (int argc, char *argv[]){
        if (SDL_Init(SDL_INIT_EVERYTHING) == -1)                                                                                // Initializing SDL and Creating Window
            return false;
        SDL_Surface *screen = SDL_SetVideoMode(WINDOWS_Width, WINDOWS_Height, 0, SDL_SWSURFACE | SDL_DOUBLEBUF);
        SDL_WM_SetCaption(WINDOWS_Title, "Ball Game");
    
        int audio_rate = 22050;
        Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
        int audio_channels = 2;
        int audio_buffers = 4096;
    
        if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
            printf("Unable to open audio!\n");
            exit(1);
        }
    
    
    
        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", 20);
        SDL_Color white = {2, 2, 2};
    
    
            //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 (mainMusic == NULL) {
                mainMusic = Mix_LoadMUS("./mainSound.wav");
                Mix_PlayMusic(mainMusic, -1);
                Mix_HookMusicFinished(musicDone);
            }
    
            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 < 20)
                rcSprite.y = 20;
            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.str(std::string());
                pScore <<"Score :" << score;
                SDL_Flip(screen);
                ballCollection = Mix_LoadMUS("./ballCollected.wav");
                Mix_PlayMusic(ballCollection, 0);
    
            }
    
            scores = TTF_RenderText_Blended(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_BlitSurface(scores, NULL, screen, &rcScores);
    
            SDL_UpdateRect(screen, 0, 0, 0, 0);                                                                 //Updating Screen
        }
        SDL_FreeSurface(screen);
        SDL_FreeSurface(ball);
        SDL_FreeSurface(grass);
        SDL_FreeSurface(sprite);
        TTF_CloseFont(times);
        TTF_Quit();
        musicDone();
        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;
    }
    
    void musicDone() {
      Mix_HaltMusic();
      Mix_FreeMusic(mainMusic);
      mainMusic = NULL;
      Mix_FreeMusic(ballCollection);
      ballCollection = NULL;
    }

  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
    But what is your error message ?

    "It doesn't work" doesn't help us figure out what to suggest.

    > temp = SDL_LoadBMP("./sprite.bmp");
    Like I said in your other thread, add some error checks!

    Code:
    if ( temp == NULL ) {
    // printing getcwd() as well, to help you figure out if you looked in the wrong place
            printf("Unable to load bitmap - sdl err=%s\n",SDL_GetError());
            exit(1);
    }
    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. The compiler stopped working
    By akif13 in forum C++ Programming
    Replies: 4
    Last Post: 08-03-2013, 07:08 PM
  2. compiler working
    By harsha26 in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2011, 02:47 PM
  3. Function not working, is it my compiler
    By bill.thompson65 in forum C++ Programming
    Replies: 13
    Last Post: 11-23-2007, 05:45 AM
  4. Help working this compiler...
    By skim in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2004, 03:58 AM
  5. Compiler not working...
    By Geo-Fry in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2003, 10:58 PM

Tags for this Thread