Having some trouble with this code. Compiles fine, but the window crashes.
It seems to fail on line 105 right after loading my first sprite. The texture exists and it's in the folder. It's 24 bit 608x152 pixels, but each frame is 152x152 and there are 4 frames total.

Do you guys see anything wrong with my code? It's my first time playing with the SDL 2.0 API.


#include <stdio.h>
#include <windows.h>
#include <SDL.h>
#include <SDL_image.h>

#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
//This is assuming that the sprite is a square and width & height is the same. In our case this is true.
#define SPRITE_SIZE 152

int gameover;

/* source and destination rectangles */
SDL_Rect rcSrc, rcSprite;

void HandleEvent(SDL_Event event)
{
switch (event.type)
{
/* close button clicked */
case SDL_QUIT:
gameover = 1;
break;

/* handle the keyboard */
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:

case SDLK_q:
gameover = 1;
break;

case SDLK_LEFT:
rcSrc.x = 0;
rcSprite.x -= 5; //Movement speed
break;

case SDLK_RIGHT:
rcSrc.x = 304;
rcSprite.x += 5; //Movement speed
break;

case SDLK_UP:
rcSrc.x = 456;
rcSprite.y -= 5; //Movement speed
break;

case SDLK_DOWN:
rcSrc.x = 152;
rcSprite.y += 5; //Movement speed
break;
}
break;
}
}

int main(int argc, char *argv[])
{
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Surface* temp = NULL;
SDL_Surface* sprite = NULL;
SDL_Rect rcGrass;
char buffer[128];

// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
sprintf_s(buffer, "%s", SDL_GetError());
MessageBox(0, (LPCWSTR)buffer, (LPCWSTR)"Error", MB_OK);
return 1;
}
else
{
// create window
window = SDL_CreateWindow("My Tank Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN /*0*/);

if(window == NULL)
{
sprintf_s(buffer, "%s", SDL_GetError());
MessageBox(0, (LPCWSTR)buffer, (LPCWSTR)"Error", MB_OK);
}
else
{
// We must call SDL_CreateRenderer in order for draw calls to affect this window.
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED/* | SDL_RENDERER_TARGETTEXTURE*/);
}
}

/* load sprite */
/*
SDL_Surface *surface = IMG_Load("tank.png");
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
*/
SDL_Surface* surface = SDL_LoadBMP("sprite2.bmp");

// set the transparent color to red (r = 255, g = 0, b = 0)
//SDL_SetColorKey(sprite, SDL_TRUE, SDL_MapRGB(sprite->format, 255, 0, 0));
SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(temp->format, 255, 0, 0));

// make a texture out of it (this will handle the color key correctly)
SDL_Texture* tank = SDL_CreateTextureFromSurface( renderer, surface );

// no longer need the surface, since we have the texture.
SDL_FreeSurface( surface );

/* load grass */
surface = SDL_LoadBMP("grass.bmp");

SDL_Texture* grass = SDL_CreateTextureFromSurface( renderer, surface );

// no longer need the surface, since we have the texture.
SDL_FreeSurface( surface );

/* set sprite position */
//This is where the sprite is spawned in the game based on the x,y coordinates
rcSprite.x = 150;
rcSprite.y = 150;

/* set animation frame */
rcSrc.x = 152;
rcSrc.y = 0;
rcSrc.w = SPRITE_SIZE;
rcSrc.h = SPRITE_SIZE;

gameover = 0;

/* message pump */
while (!gameover)
{
SDL_Event event;

/* look for an event */
if (SDL_PollEvent(&event))
{
HandleEvent(event);
}

/* collide with edges of screen */
if (rcSprite.x <= 0)
{
rcSprite.x = 0;
}
if (rcSprite.x >= SCREEN_WIDTH - SPRITE_SIZE)
{
rcSprite.x = SCREEN_WIDTH - SPRITE_SIZE;
}
if (rcSprite.y <= 0)
{
rcSprite.y = 0;
}
if (rcSprite.y >= SCREEN_HEIGHT - SPRITE_SIZE)
{
rcSprite.y = SCREEN_HEIGHT - SPRITE_SIZE;
}

/* draw the grass */
//grass size is 32x32 pixels so that's what we need to multiply x & y by.
for(int x = 0; x <= SCREEN_WIDTH; x++)
{
for(int y = 0; y <= SCREEN_HEIGHT; y++)
{
rcGrass.x = (x * 32);
rcGrass.y = (y * 32);
SDL_RenderCopy(renderer, grass, NULL, NULL);
}
}

/* draw the sprite */
SDL_RenderCopy(renderer, tank, NULL, NULL);

/* update the screen */
SDL_RenderPresent(renderer);
}

/* clean up */
SDL_Quit();

return 0;
}