Hi everyone, I've been having some troubles with this code (I'm a beginner). All errors are resolved and it runs, but as soon as it starts it says "main.exe has stopped working" and closes. I'd appreciate if someone could look over it and tell me what's wrong.

main.cpp:

#include "SDL/SDL.h"
#include "player.h"

#define SCREEN_WIDTH  1280
#define SCREEN_HEIGHT 736
#define SPRITE_SIZE    32

int gameover;
player plyr;

void HandleEvent(SDL_Event event)
{

  switch (event.type) {
    case SDL_QUIT:
      gameover = 1;
      break;

    case SDL_KEYDOWN:
      switch (event.key.keysym.sym) {
        case SDLK_ESCAPE:
        case SDLK_q:
          gameover = 1;
          break;
      }
      break;
  }
}

int main ( int argc, char *argv[] )
{

  SDL_Surface *screen, *grass, *temp, *playerS;
  SDL_Rect rcGrass;
  SDL_Rect rcSrc, rcPlayer;

  SDL_Init(SDL_INIT_VIDEO);

  SDL_WM_SetCaption("Untitled", "Untitled");

  screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);

  temp  = SDL_LoadBMP("grass.bmp");
  grass = SDL_DisplayFormat(temp);
  SDL_FreeSurface(temp);

    SDL_EnableKeyRepeat(25, 25);

  gameover = 0;

  while (!gameover)
  {
    SDL_Event event;

    plyr.input();
    plyr.movement();

    if (SDL_PollEvent(&event)) {
      HandleEvent(event);
    }


    for (int x = 0; x < SCREEN_WIDTH / SPRITE_SIZE; x++) {
      for (int y = 0; y < SCREEN_HEIGHT / SPRITE_SIZE; y++) {
        rcGrass.x = x * SPRITE_SIZE;
        rcGrass.y = y * SPRITE_SIZE;
        SDL_BlitSurface(grass, NULL, screen, &rcGrass);
      }
    }
    SDL_BlitSurface(playerS, &rcSrc, screen, &rcPlayer);

    SDL_UpdateRect(screen, 0, 0, 0, 0);
  }

  SDL_FreeSurface(playerS);
  SDL_FreeSurface(grass);
  SDL_Quit();

  return 0;
}

player.h:

#ifndef _PLAYER_H_
#define _PLAYER_H_

#define SCREEN_WIDTH  1280
#define SCREEN_HEIGHT 736
#define SPRITE_SIZE    32

class player
{
    public:

        player();
        void input();
        void movement();
        void collision();
        void show();

    private:

        SDL_Surface *screen, *temp, *playerS;
        int colorkey;
        Uint8 *keystate;
        SDL_Rect rcSrc, rcPlayer;
        bool can_jump;
};

player::player()
{
    rcSrc.x = 0;
    rcSrc.y = 0;
    rcSrc.w = SPRITE_SIZE;
    rcSrc.h = SPRITE_SIZE;

    rcPlayer.x = 150;
    rcPlayer.y = 150;

    can_jump=true;

    temp    = SDL_LoadBMP("characters.bmp");
    playerS = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
    SDL_SetColorKey(playerS, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
}

void player::input()
{
        keystate = SDL_GetKeyState(NULL);
    if (keystate[SDLK_LEFT] ) {
      if ( rcSrc.x == 32 )
            rcSrc.y = 0;
          else
            rcSrc.x = 32;
      rcPlayer.x -= 3;
    }
    if (keystate[SDLK_RIGHT] ) {
         if ( rcSrc.x == 0 )
            rcSrc.y = 0;
          else
            rcSrc.x = 0;
      rcPlayer.x += 3;
    }
    if (keystate[SDLK_SPACE] ) {
        if ( rcSrc.y == 384 )
            rcSrc.y = 384;
          else
            rcSrc.y = 384;
            rcPlayer.y -= -15;
    }

}

void player::movement()
{
    if ( rcPlayer.x < 0 ) {
      rcPlayer.x = 0;
    }
    else if ( rcPlayer.x > SCREEN_WIDTH-SPRITE_SIZE ) {
      rcPlayer.x = SCREEN_WIDTH-SPRITE_SIZE;
    }
    if ( rcPlayer.y < 0 ) {
      rcPlayer.y = 0;
    }
    else if ( rcPlayer.y > SCREEN_HEIGHT-SPRITE_SIZE ) {
      rcPlayer.y = SCREEN_HEIGHT-SPRITE_SIZE;
    }

    while (can_jump=true)
    {
    rcPlayer.y += -15;
    }
}


#endif

Thanks :)

Edited 4 Years Ago by claywin: Typo

All errors are resolved and it runs, but as soon as it starts it says "main.exe has stopped working" and closes.

That's what I'd call "doesn't run", given that it crashes immediately upon loading. What you want to do is run your program in a debugger so that execution can be stopped on the offending line of code.

OK, claywin, we really can't help you the way you want us to. Not with the information we have. So here's what we're going to do:

    SDL_Rect rcSrc, rcPlayer;
    std::cout<<"Player object created!\n";

    SDL_Init(SDL_INIT_VIDEO);
    std::cout<<"Video initialized!\n";
    SDL_WM_SetCaption("Untitled", "Untitled");
    std::cout<<"Caption set!\n";
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
    std::cout<<"Video Mode set!\n";
    temp  = SDL_LoadBMP("grass.bmp");
    std::cout<<"Grass texture loaded!\n";
    grass = SDL_DisplayFormat(temp);
    std::cout<<"Grass object created!\n";
    SDL_FreeSurface(temp);
    std::cout<<"Grass texture freed!\n";
    SDL_EnableKeyRepeat(25, 25);
    std::cout<<"Key repeat enabled!\n";
    gameover = 0;

(Make sure to #include<iostream.h>)
Check points have been an invaluable service in my debugging.

I definately think that you should try debugging this. Windows gives the error your seeing usually when it has a sigseg fault. This means that at some point you get a NULL pointer and try using it as if it were not NULL (either dereferencing it manually, or passing it to a function that dereferences it). I would suggest adding checks the way that lazyfoo tutorials teaches (lazyfoo has great SDL tutorials) Here is what you should do for example at line 41-42 in main.cpp:

screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT,0,0);
if (screen==NULL)//uh-oh, error!
    return -1;//now if you see 'process finished with return value -1' you know that your screen didn't load!
Comments
So I should just do this with every line of code? One at a time?
This article has been dead for over six months. Start a new discussion instead.