SDL texture array?

1

I am new to sdl. I am in the process of making a blackjack game. I want to make an array of textures. I was wondering if anyone would be able to assist me. Here is what i have been trying to do:

// array of textures for the extra player cards
    SDL_Texture *hitCardsText[] = { NULL };

// this does not give me errors but i dont know if it is right

    hitCardsText[0] = loadTexture(ren, cards[dynamicPlayerCards[0]]);
    hitCardsText[1] = loadTexture(ren, cards[dynamicPlayerCards[1]]);

// i get an error here

SDL_DestroyTexture(hitCardsText[0]);
SDL_DestroyTexture(hitCardsText[1]);

i get this error where i indicated above in the code (my file is called introSDL.exe btw):

Unhandled exception at 0x6C78CE9A (SDL2.dll) in introSDL.exe: 0xC0000005: Access violation reading location 0x00000050.

c++
arrays
sdl
asked on Stack Overflow Apr 10, 2016 by soso

1 Answer

2

You are writing out of bounds of your array.

SDL_Texture *hitCardsText[] = { NULL };

That only has 1 element. If you want more than that, you either need to add more elements to the initializer list, or specify the exact amount in the square brackets.

If you want a dynamically sized array, then use std::vector.

std::vector<SDL_Texture*> hitCardsText;
hitCardsText.push_back(loadTexture(ren, cards[dynamicPlayerCards[0]]));
hitCardsText.push_back(loadTexture(ren, cards[dynamicPlayerCards[1]]));
answered on Stack Overflow Apr 10, 2016 by Benjamin Lindley • edited Apr 10, 2016 by Benjamin Lindley

User contributions licensed under CC BY-SA 3.0