Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

877330 Posts in 32857 Topics- by 24295 Members - Latest Member: raithza

May 19, 2013, 07:29:31 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)SDL game crashes!
Pages: [1]
Print
Author Topic: SDL game crashes!  (Read 420 times)
indietom
Level 2
**

Tom


View Profile Email
« on: July 28, 2012, 03:30:47 AM »

Hi!
My code crashes when I run it. I'm using SDL_tff, SDL, C++.

Here's my code!
Code:
#include "sdl\sdl.h"
#include "sdl\sdl_ttf.h"
#include "SystemFont.h"
#include "iostream"
#undef main

const int WIN_W = 640;
const int WIN_H = 480;
const char* WIN_T = " Sentoki's quest ";

void putpixel(SDL_Surface* screen, int x, int y)
{
Uint32* pixels = (Uint32*)screen->pixels;
Uint32* pixel = pixels + y*screen->pitch/4 + x; // offset of pointer
*pixel = SDL_MapRGB(screen->format,0xff,0xff,0xff);
}

int showmenu(SDL_Surface* screen,TTF_Font* font)
{
    int x,y;
    const int NUMMENU = 2;
    const char* labels[NUMMENU] = {"Play Game","Exit Game"};
    SDL_Surface* menus[NUMMENU];
    bool selected[NUMMENU]={0,0};
    SDL_Color color[2] = {{255,255,255},{255,0,0}};

    menus[0] = TTF_RenderText_Solid(font,labels[0],color[0]);
    menus[1] = TTF_RenderText_Solid(font,labels[1],color[1]);
    SDL_Rect pos[NUMMENU];
    pos[1].x=screen->clip_rect.w/2 - menus[0]->clip_rect.w/2;
    pos[1].y=screen->clip_rect.h/2 - menus[0]->clip_rect.h;

    pos[0].x=screen->clip_rect.w/2 - menus[0]->clip_rect.w/2;
    pos[0].y=screen->clip_rect.h/2 - menus[0]->clip_rect.h;

    SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0));



    SDL_Event event;
    while(1);
    {
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                for(int i = 0;i<NUMMENU;i++)
                 SDL_FreeSurface(menus[1]);
                return 1;
                case SDL_MOUSEMOTION:
                x = event.motion.x;
                y = event.motion.y;
                for(int i = 0;i<NUMMENU;i++)

                    if(x >= pos[i].x && x <= pos[i].w && y >= pos[i].y && y <= pos[i].h)
                      {
                       if(!selected[i])
                       {
                           selected[i]=1;
                           SDL_FreeSurface(menus[i]);
                           menus[1]=TTF_RenderText_Solid(font,labels[i],color[1]);
                       }else{
                           if(selected[i])
                           {
                               selected[i]=0;
                               SDL_FreeSurface(menus[i]);
                               menus[i] = TTF_RenderText_Solid(font,labels[i],color[0]);
                           }

                       }
                }
                break;
                case SDL_MOUSEBUTTONDOWN:
                 x = event.button.x;
                 y = event.button.y;
                 for(int i = 0;i<NUMMENU;i++)

                    if(x >= pos[i].x && x <= pos[i].w && y >= pos[i].y && y <= pos[i].h)
                    {
                        for(int j=0;j<NUMMENU;j++)
                        SDL_FreeSurface(menus[j]);
                        return i;
                    }

                 break;
                case SDL_KEYDOWN:
                 if(event.key.keysym.sym==SDLK_ESCAPE)
                   {
                        for(int i=0;i<NUMMENU;i++)
                        SDL_FreeSurface(menus[i]);
                        return 0;
                    }
                 }

            }
            for(int i=0;i<NUMMENU;i++)
             SDL_BlitSurface(menus[i],NULL,screen,&pos[i]);
            SDL_Flip(screen);
        }

    }



 bool collision(SDL_Rect* playerC, SDL_Rect* ene2C)
    {
         if(playerC->y >= ene2C->y + ene2C->h)
         return 0;
         if(playerC->x >= ene2C->x + ene2C->w)
         return 0;
         if(playerC->y + playerC->h <= ene2C->y)
         return 0;
         if(playerC->x + playerC->w <= ene2C->x)
         return 0;
        return 1;
    }

    bool collision2(SDL_Rect* bulC, SDL_Rect* ene2C)
    {
         if(bulC->y >= ene2C->y + ene2C->h)
         return 0;
         if(bulC->x >= ene2C->x + ene2C->w)
         return 0;
         if(bulC->y + bulC->h <= ene2C->y)
         return 0;
         if(bulC->x + bulC->w <= ene2C->x)
         return 0;
        return 1;
    }

        bool collision3(SDL_Rect* playerC, SDL_Rect* eneBC)
    {
         if(playerC->y >= eneBC->y + eneBC->h)
         return 0;
         if(playerC->x >= eneBC->x + eneBC->w)
         return 0;
         if(playerC->y + playerC->h <= eneBC->y)
         return 0;
         if(playerC->x + playerC->w <= eneBC->x)
         return 0;
        return 1;
    }

    bool collision4(SDL_Rect* bulC, SDL_Rect* eneC)
    {
        if(bulC->y >= eneC->y + eneC->h)
        return 0;
        if(bulC->x >= eneC->x + eneC->w)
        return 0;
        if(bulC->y + bulC->h <= eneC->y)
        return 0;
        if(bulC->x + bulC->w <= eneC->x)
        return 0;
    return 1;
    }
        bool collision5(SDL_Rect* playerC, SDL_Rect* ene3BC)
    {
        if(playerC->y >= ene3BC->y + ene3BC->h)
        return 0;
        if(playerC->x >= ene3BC->x + ene3BC->w)
        return 0;
        if(playerC->y + playerC->h <= ene3BC->y)
        return 0;
        if(playerC->x + playerC->w <= ene3BC->x)
        return 0;
    return 1;
    }

            bool collision6(SDL_Rect* playerC, SDL_Rect* foreC)
    {
        if(playerC->y >= foreC->y + foreC->h)
        return 0;
        if(playerC->x >= foreC->x + foreC->w)
        return 0;
        if(playerC->y + playerC->h <= foreC->y)
        return 0;
        if(playerC->x + playerC->w <= foreC->x)
        return 0;
    return 1;
    }

    bool collision7(SDL_Rect* bulC, SDL_Rect* bossE1C)
    {
        if(bulC->y >= bossE1C->y + bossE1C->h)
        return 0;
        if(bulC->x >= bossE1C->x + bossE1C->w)
        return 0;
        if(bulC->y + bulC->h <= bossE1C->y)
        return 0;
        if(bulC->x + bulC->w <= bossE1C->x)
        return 0;
    return 1;
    }


void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height);

using namespace std;

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Surface* screen = SDL_SetVideoMode(WIN_W,WIN_H, 0, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption(WIN_T, 0);
    SDL_Surface* bitmap = SDL_LoadBMP("sprite.bmp");
    SDL_Surface* ene2 = NULL;
    SDL_Surface* eneB = NULL;
    SDL_Surface* ene = NULL;
    SDL_Surface* back = SDL_LoadBMP("back.bmp");
    SDL_Surface* bul = NULL;
    SDL_Surface* ene3 = NULL;
    SDL_Surface* ene3B = NULL;
    SDL_Surface* text;
    SDL_Surface* boss = SDL_LoadBMP("boss.bmp");
    SDL_Surface* gui2 = SDL_LoadBMP("GUI2.bmp");
    SDL_Surface* gui1 = SDL_LoadBMP("GUI1.bmp");
    SDL_Surface* fore = SDL_LoadBMP("fore.bmp");
    SDL_Surface* rand = SDL_LoadBMP("rand.bmp");
    SDL_SetColorKey(bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255));
    SDL_SetColorKey(gui1, SDL_SRCCOLORKEY, SDL_MapRGB(gui1->format, 255, 0, 255));
    SDL_SetColorKey(fore, SDL_SRCCOLORKEY, SDL_MapRGB(fore->format, 255, 0, 255));
    SDL_SetColorKey(gui2, SDL_SRCCOLORKEY, SDL_MapRGB(gui2->format, 255, 0, 255));
    SDL_SetColorKey(boss, SDL_SRCCOLORKEY, SDL_MapRGB(boss->format, 255, 0, 255));
    SDL_SetColorKey(rand, SDL_SRCCOLORKEY, SDL_MapRGB(rand->format, 255, 0, 255));
    TTF_Font* font=TTF_OpenFont("calibri.ttf", 32);

    int playerX = 300;
    int playerY = 200;
    int playerW = 32;
    int playerH = 46;
    int playerImx = 9;
    int playerImy = 0;

    int bulX = 10000;
    int bulY = 10000;
    int bulW = 57;
    int bulH = 37;
    int bulImx = 51;
    int bulImy = 80;

    int backX = 0;
    int backY = 0;
    int backW = 1280;
    int backH = 480;
    int backImx = 1;
    int backImy = 1;

    int eneX = 1000;
    int eneY = 200;
    int eneW = 50;
    int eneH = 50;
    int eneImx = 592;
    int eneImy = 351;

    int ene2X = 700;
    int ene2Y = 200;
    int ene2W = 50;
    int ene2H = 50;
    int ene2Imx = 504;
    int ene2Imy = 352;

    int ene2He = 50;

    int foreX = -10000000000000;
    int foreY = 0;
    int foreW = 640;
    int foreH = 480;
    int foreImx = 0;
    int foreImy = 0;

    int gui1X = 40000;
    int gui1Y = 440;
    int gui1W = 120;
    int gui1H = 36;
    int gui1Imx = 21;
    int gui1Imy = 33;

    int eneBX = 100000;
    int eneBY = 100000;
    int eneBW = 50;
    int eneBH = 50;
    int eneBImx = 54;
    int eneBImy = 109;

    int ene3X = 300;
    int ene3Y = 300;
    int ene3W = 50;
    int ene3H = 50;
    int ene3Imx = 427;
    int ene3ImY = 360;

    int ene3BX = 1000000;
    int ene3BY = 1000000;
    int ene3BW = 50;
    int ene3BH = 50;
    int ene3BImx = 55;
    int ene3BImy = 202;

    int gui2X = 100000000000000;
    int gui2Y = 100000000000000;
    int gui2W = 640;
    int gui2H = 480;
    int gui2Imx = 0;
    int gui2Imy = 0;

    int bossX = 500;
    int bossY = 100;
    int bossW = 200;
    int bossH = 300;
    int bossImx = 49;
    int bossImy = 48;

    int randX = 5000;
    int randY = 1000;
    int randW = 20;
    int randH = 20;
    int randImx = 116;
    int randImy = 15;

    int bossE1 = 10;
    int bossE2 = 50;
    int bossE3 = 50;
    int bossE4 = 50;
    int bossE5 = 50;
    int bossE6 = 50;
    int bossTotal = 6;


     SDL_Event event;
     bool gameRunning = true;
     bool keysHeld[323] = {false};
     //TTF_Font* font=TTF_OpenFont("calibri.ttf", 32);
     int i=showmenu(screen,font);
     if(i==1)
      gameRunning=false;

     while(gameRunning)
     {
         if(SDL_PollEvent(&event))
         {
             if(event.type == SDL_QUIT)
             {
                 gameRunning = false;
             }
             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_ESCAPE])
         {
             int i=showmenu(screen, font);
             int(i==1);
             gameRunning = false;
         }
         if(keysHeld[SDLK_UP])
         {
             playerY -= 1;
             cout << "up \n";
         }
         if(keysHeld[SDLK_LEFT])
         {
             playerX -= 1;
             cout << "left \n";
         }
         if(keysHeld[SDLK_RIGHT])
         {
             playerX += 1;
             cout << "Right \n";
         }
         if(keysHeld[SDLK_DOWN])
         {
             playerY += 1;
             cout << "down \n";
         }
         if(keysHeld[SDLK_SPACE])
         {
             bulX = playerX -10;
             bulY = playerY +22;
         }

         if(keysHeld[SDLK_r])
         {
         ene2X = 700;
         ene2Y = 20;
         gui1X = 320;
         gui1Y = 240;
         }


            SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));




      drawSprite(back,
                 screen,
                 backImx, backImy,
                 backX, backY,
                 backW, backH);

      drawSprite(bitmap,
                 screen,
                 playerImx, playerImy,
                 playerX, playerY,
                 playerW, playerH);

      drawSprite(bitmap,
                 screen,
                 bulImx, bulImy,
                 bulX, bulY,
                 bulW, bulH);

         drawSprite(gui1,
                screen,
                gui1Imx, gui1Imy,
                gui1X, gui1Y,
                gui1W, gui1H);

     drawSprite(bitmap,
                screen,
                ene2Imx, ene2Imy,
                ene2X, ene2Y,
                ene2W, ene2H);
     drawSprite(bitmap,
                screen,
                eneImx, eneImy,
                eneX, eneY,
                eneW, eneH);

     drawSprite(fore,
                screen,
                foreImx, foreImy,
                foreX, foreY,
                foreW, foreH);
     drawSprite(bitmap,
                screen,
                eneBImx, eneBImy,
                eneBX, eneBY,
                eneBW, eneBH);
     drawSprite(bitmap,
                screen,
                ene3Imx, ene3ImY,
                ene3X, ene3Y,
                ene3W, ene3H);
    drawSprite(bitmap,
               screen,
               ene3BImx, ene3BImy,
               ene3BX, ene3BY,
               ene3BW, ene3BW);
    drawSprite(gui2,
               screen,
               gui2Imx, gui2Imy,
               gui2X, gui2Y,
               gui2W, gui2H);

    drawSprite(boss,
               screen,
               bossImx,bossImy,
               bossX, bossY,
               bossW, bossH);
    drawSprite(rand,
               screen,
               randImx, randImy,
               randX, randY,
               randW, randH);



SDL_Rect playerC;
//SDL_FillRect  (screen,&playerC, SDL_MapRGB(screen->format, 255, 0, 255));
playerC.x = 400;
playerC.y = 100;
playerC.w = 50;
playerC.h = 50;


   SDL_Rect ene2C;
  // SDL_FillRect(screen,&ene2C, SDL_MapRGB(screen->format, 255, 0, 255));
   ene2C.x = 1;
   ene2C.y = 1;
   ene2C.w = 50;
   ene2C.h = 50;


   SDL_Rect bulC;
   //SDL_FillRect(screen,&bulC, SDL_MapRGB(screen->format, 255, 0, 255));
   bulC.x = 10000;
   bulC.y = 10000;
   bulC.w = 31;
   bulC.h = 24;

   SDL_Rect eneC;
   //SDL_FillRect(screen, &eneC, SDL_MapRGB(screen->format, 255, 0, 255));l
   eneC.x = 10000;
   eneC.y = 10000;
   eneC.w = 50;
   eneC.h = 50;

   SDL_Rect eneBC;
   //SDL_FillRect(screen,&eneBC, SDL_MapRGB(screen->format, 255, 0, 255));
   eneBC.x = 10000;
   eneBC.y = 10000;
   eneBC.w = 50;
   eneBC.h = 50;

   SDL_Rect ene3C;
   //SDL_FillRect(screen,&ene3C, SDL_MapRGB(screen->format, 255, 0, 255));
   ene3C.x = 10000;
   ene3C.y = 10000;
   ene3C.w = 50;
   ene3C.h = 50;

      SDL_Rect foreC;
   //SDL_FillRect
   foreC.x = 10000;
   foreC.y = 10000;
   foreC.w = 50;
   foreC.h = 640;


   SDL_Rect ene3BC;
   ene3BC.x = 10000;
   ene3BC.y = 10000;
   ene3BC.w = 50;
   ene3BC.h = 50;

   SDL_Rect bossE1C;
   SDL_FillRect(screen,&bossE1C, SDL_MapRGB(screen->format, 255, 0, 255));
   bossE1C.x = 10000;
   bossE1C.y = 10000;
   bossE1C.w = 50;
   bossE1C.h = 50;

      bulX += 3;
      ene2X -= 1;
      eneX -= 1;
      eneBX -= 5;



      if(keysHeld[SDLK_1])
      {
          playerC.x++;
      }

      ene2C.x = ene2X;
      ene2C.y = ene2Y;

      playerC.x = playerX;
      playerC.y = playerY;

      bulC.x = bulX;
      bulC.y = bulY;

      eneBC.x = eneBX;
      eneBC.y = eneBY;

      eneC.x = eneX;
      eneC.y = eneY;

      ene3C.x = ene3X;
      ene3C.y = ene3Y;

      ene3BC.x = ene3BX;
      ene3BC.y = ene3BY;

      foreC.x = foreX;
      foreC.y = foreY;

      bossE1C.x = bossX + 100;
      bossE1C.y = bossY + 30;

      if(playerY >= ene2Y);
      else ene2Y -= 1;

      if(playerY >= eneY);
      {
          eneBX = eneX;
          eneBY = eneY;
      }

      if(playerY <= ene2Y);
      else ene2Y += 1;

      if(bossE1 <= 1)
      {
          bossTotal += 1;
          bossTotal -= 1;
          cout << bossTotal << "\n";
      }

      if(playerY >= 71);
      else playerY += 1;

      if(playerY + 44 <= 397);
      else playerY -= 1;

      if(ene2He <= 1)
      {
          ene3X = 1000;
      }

      if(bossE1 <= 1);
      {
          //playerY += 1;
      }

      if(eneY == playerY);
      {
          eneBX = eneX;
          eneBY = eneY;
      }

      if(collision(&playerC,&ene2C))
      {
         gui1X = 320;
         gui1Y = 240;
         playerX = 100000;
      }

      if(collision2(&bulC,&ene2C))
      {
          ene2X = 800;
         // x = ( (int)rand() % 608 );
         // y = ( (int)rand() % 448 );
      }

      if(collision4(&bulC,&eneC))
      {
          eneX = -900;
      }

      if(collision6(&playerC,&foreC))
      {
          gui2X = 0;
          gui2Y = 0;
          playerX = 10000;
      }
      if(collision5(&bulC,&ene3C))
      {
          ene2He -= 1;
          cout << ene2He << "\n";
      }
      if(collision7(&bulC,&bossE1C))
      {
          bossE1 -= 1;
          cout << bossE1 << "\n" ;
      }

            SDL_Flip(screen);
            }
    SDL_FreeSurface(bitmap);
    SDL_FreeSurface(bul);
    SDL_FreeSurface(gui1);
    SDL_FreeSurface(fore);
    SDL_FreeSurface(eneB);
    SDL_FreeSurface(ene2);
    SDL_FreeSurface(ene);
    SDL_FreeSurface(ene3);
    SDL_FreeSurface(ene3B);
    SDL_FreeSurface(gui2);
    SDL_FreeSurface(boss);
    SDL_FreeSurface(rand);

    SDL_Quit();
    return 0;
}


void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height)


{
   SDL_Rect srcRect;
   srcRect.x = srcX;
   srcRect.y = srcY;
   srcRect.w = width;
   srcRect.h = height;
   SDL_Rect dstRect;
   dstRect.x = dstX;
   dstRect.y = dstY;
   dstRect.w = width;
   dstRect.h = height;
   SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
}



Here's where I think the problem can be somewhere between line 17 and 102.
That's my last added code!
I get no errors!

Thanks for any help!  Coffee
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Liosan
Level 2
**


View Profile
« Reply #1 on: July 28, 2012, 03:40:05 AM »

Quote
Here's where I think the problem can be somewhere between line 17 and 102.
Oh, it's somewhere in the first 100 lines of that terribly formatted blob of text... Lips Sealed

Let's try it differently Smiley Where does it crash? Visual studio or most other IDEs should point you to a line of code, or at least give an error message.

Liosan
Logged

indietom
Level 2
**

Tom


View Profile Email
« Reply #2 on: July 28, 2012, 04:02:09 AM »

Quote
Here's where I think the problem can be somewhere between line 17 and 102.
Oh, it's somewhere in the first 100 lines of that terribly formatted blob of text... Lips Sealed

Let's try it differently Smiley Where does it crash? Visual studio or most other IDEs should point you to a line of code, or at least give an error message.

Liosan


I'm using Code::Blocks, and I get no errors at all Sad
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Liosan
Level 2
**


View Profile
« Reply #3 on: July 28, 2012, 04:05:49 AM »

Then maybe you should try using a debugger a step-over the code to see where it crashes exactly.

Liosan
Logged

indietom
Level 2
**

Tom


View Profile Email
« Reply #4 on: July 28, 2012, 04:22:49 AM »

It's telling me that something is wrong on line 30...
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Liosan
Level 2
**


View Profile
« Reply #5 on: July 28, 2012, 04:29:54 AM »

It's telling me that something is wrong on line 30...
Yay, you learned something new already Wink

Either the 'screen' param is invalid (I don't think it is, seeing as you call the function), or menus[0] is NULL. It seems possible, since TTF_RenderText_Solid can return NULL on errors. You should try TTF_GetError or something (at least this is what I googled in 5s, I have never used SDL Wink).

Liosan
Logged

rivon
Level 10
*****



View Profile
« Reply #6 on: July 28, 2012, 11:39:27 AM »

When the game crashes, just run it through the debugger (Debug->Start or F8 or Debug/Continue on the debug toolbar). If this doesn't help, run it through Valgrind Memcheck (Valgrind->Run Valgrind::Memcheck).

And as Liosan said, either the screen or menus[0] is NULL.
Logged
indietom
Level 2
**

Tom


View Profile Email
« Reply #7 on: July 28, 2012, 12:27:48 PM »

Guys, I fixed it!
Thanks for the help   Coffee
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Evan Balster
Level 10
*****


dreaming close to metal


View Profile WWW Email
« Reply #8 on: July 28, 2012, 12:54:54 PM »

When the game crashes, just run it through the debugger (Debug->Start or F8 or Debug/Continue on the debug toolbar). If this doesn't help, run it through Valgrind Memcheck (Valgrind->Run Valgrind::Memcheck).

And as Liosan said, either the screen or menus[0] is NULL.

I don't think C::B has Valgrind support under Windows yet.  Theeeen again, what kind of doof uses Code::Blocks under Windows?  Who, Me?
Logged

Infinite Blank, SoundSelf, Cave Story+, Wreath
voice, accordion, mandolin, (oboe, soon)
Game audio programming consultant.
<plaid/audio>: opensource audio framework
rivon
Level 10
*****



View Profile
« Reply #9 on: July 28, 2012, 01:06:36 PM »

Me? Though I develop in Linux and only build finished stuff in Windows.
Logged
Cheezmeister
Level 0
***



View Profile
« Reply #10 on: July 29, 2012, 05:08:44 PM »

A braver doof than ye Wink
Logged

Procrastinating on: Nextris, Chromathud, Spheres of Influence | More at http://luchenlabs.com/projects
My heart goes out to you for asking a simple question and getting a million complicated answers; it's sort of how this forum works... -Evan Balster
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic