exception thrown when using a SDL_Rect pointer varible

  c++, sdl

Code producing exception:

//Sets tiles from tile map
bool setTiles(Tile* tiles[])
{
    //Success flag
    bool tilesLoaded = true;

    //The tile offsets
    int x = 0, y = 0;

    //Open the map
    std::ifstream map("Assets/lazy.map");

    //If the map couldn't be loaded
    if (map.fail())
    {
        printf("Unable to load map file!n");
        tilesLoaded = false;
    }
    else
    {
        //Initialize the tiles
        for (int i = 0; i < TOTAL_TILES; ++i)
        {
            //Determines what kind of tile will be made
            int tileType = -1;

            //Read tile from map file
            map >> tileType;

            //If the was a problem in reading the map
            if (map.fail())
            {
                //Stop loading map
                printf("Error loading map: Unexpected end of file!n");
                tilesLoaded = false;
                break;
            }

            //If the number is a valid tile number
            if ((tileType >= 0) && (tileType < TOTAL_TILE_SPRITES))
            {
                tiles[i] = new Tile(x, y, tileType);
            }
            //If we don't recognize the tile type
            else
            {
                //Stop loading map
                printf("Error loading map: Invalid tile type at %d!n", i);
                tilesLoaded = false;
                break;
            }
            //Move to next tile spot
            x += TILE_WIDTH;

            //If we've gone too far
            if (x >= LEVEL_WIDTH)
            {
                //Move back
                x = 0;

                //Move to the next row
                y += TILE_HEIGHT;
            }
        }
        //Clip the sprite sheet
        if (tilesLoaded)
        {

            TileClips[TILE_GRASS1]->x = 0; //<- line making the exception. I assume the same thing happens to the next lines too... 
            TileClips[TILE_GRASS1]->y = 0;
            TileClips[TILE_GRASS1]->w = TILE_WIDTH;
            TileClips[TILE_GRASS1]->h = TILE_HEIGHT;

            TileClips[TILE_GRASS2]->x = 80;
            TileClips[TILE_GRASS2]->y = 0;
            TileClips[TILE_GRASS2]->w = TILE_WIDTH;
            TileClips[TILE_GRASS2]->h = TILE_HEIGHT;

            TileClips[TILE_TP]->x = 1200;
            TileClips[TILE_TP]->y = 0;
            TileClips[TILE_TP]->w = TILE_WIDTH;
            TileClips[TILE_TP]->h = TILE_HEIGHT;

        }
    }

    //Close the file
    map.close();

    //If the map was loaded fine
    return tilesLoaded;
}

I have tried to make the exception in another Visual Studio project, however, even though I think that I have everything to make the exception, it doesn’t happen.

This is the code in another header/cpp file that is used in the function above.

Varibles.h:

#pragma once

#ifndef GAME_VARIBLES_H
#define GAME_VARIBLES_H

//screen dimensions
extern const int SCREEN_WIDTH, SCREEN_HEIGHT;
//The dimensions of the level
extern const int LEVEL_WIDTH;
extern const int LEVEL_HEIGHT;
//Tile constants
extern const int TILE_WIDTH;
extern const int TILE_HEIGHT;
extern const int TOTAL_TILES;
extern const int TOTAL_TILE_SPRITES;
//The different tile sprites
extern const int TILE_GRASS1;
extern const int TILE_GRASS2;
extern const int TILE_TP;

extern SDL_Rect* TileClips[];

#endif

Varibles.cpp:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#include "Varibles.h"

//screen dimensions
const int SCREEN_WIDTH = 1280, SCREEN_HEIGHT = 720;
//The dimensions of the level
const int LEVEL_WIDTH = 1280;
const int LEVEL_HEIGHT = 720;
//Tile constants
const int TILE_WIDTH = 80;
const int TILE_HEIGHT = 80;
const int TOTAL_TILES = 144;
const int TOTAL_TILE_SPRITES = 3;
//The different tile sprites
const int TILE_GRASS1 = 0;
const int TILE_GRASS2 = 1;
const int TILE_TP = 2;


SDL_Rect* TileClips[TOTAL_TILE_SPRITES];

Source: Windows Questions C++

LEAVE A COMMENT