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 :)