My old C++ code reads a spritesheet (rows = different buttons, cols = different button states) one row at a time using for loops and divides them into individual sprite clips. All the sprites are the same size. Works fine.
Now, I want to put the elements of the clipping for loops into a struct so that I can write a single clipping function to iterate through every graphic on a given spritesheet. Running into a few problems getting everything to work. Something changed with the enums when I put them in the struct, and I think I screwed up the nested for loop.
Old Code
const int LONGBUTTON_HEIGHT = 128;
const int LONGBUTTON_WIDTH = 256;
enum CreateButtonState { CREATE_DEFAULT, CREATE_HOVER, CREATE_PRESSED, CREATE_TOTAL };
enum CreditsButtonState { CREDITS_DEFAULT, CREDITS_HOVER, CREDITS_PRESSED, CREDITS_TOTAL };
enum MenuButtonState { MENU_DEFAULT, MENU_HOVER, MENU_PRESSED, MENU_TOTAL };
main
{
//Load long button spritesheet texture
SDL_Texture* longbutton_image = loadTexture("longbuttonSpriteSheet.png", renderer);
//Long Buttons
//Create Button Setup
SDL_Rect create_clips[CreateButtonState::CREATE_TOTAL];
for (int i = 0; i < CreateButtonState::CREATE_TOTAL; i++)
{
create_clips[i].x = i * LONGBUTTON_WIDTH;
create_clips[i].y = 0;
create_clips[i].w = LONGBUTTON_WIDTH;
create_clips[i].h = LONGBUTTON_HEIGHT;
}
int useCreate_Clip = CREATE_DEFAULT;
// Credits Button Setup
SDL_Rect credits_clips[CreditsButtonState::CREDITS_TOTAL];
for (int i = 0; i < CreditsButtonState::CREDITS_TOTAL; i++)
{
credits_clips[i].x = i * LONGBUTTON_WIDTH;
credits_clips[i].y = 1 * LONGBUTTON_HEIGHT;
credits_clips[i].w = LONGBUTTON_WIDTH;
credits_clips[i].h = LONGBUTTON_HEIGHT;
}
int useCredits_Clip = CREDITS_DEFAULT;
//Menu Button Setup
SDL_Rect menu_clips[MenuButtonState::MENU_TOTAL];
for (int i = 0; i < MenuButtonState::MENU_TOTAL; i++)
{
menu_clips[i].x = i * LONGBUTTON_WIDTH;
menu_clips[i].y = 2 * LONGBUTTON_HEIGHT;
menu_clips[i].w = LONGBUTTON_WIDTH;
menu_clips[i].h = LONGBUTTON_HEIGHT;
}
int useMenu_Clip = MENU_DEFAULT;
return 0;
}
New Code
int originalspriteH = 128;
int originalspriteW = 256;
int spritesheetcols = 2;
struct Graphic
{
typedef enum buttonstate {DEFAULT, HOVER, PRESSED, TOTAL};
SDL_Rect clip;
int useClip;
};
void clipSprites(int spritesheetcols, int originalspriteH, int originalspriteW, [Graphic &graphic])
{
for (int j =0; j < spritesheetcols; j++)
{
graphic.clip[graphic.buttonstate::TOTAL];
}
for (int i = 0, i < graphic.buttonstate::TOTAL; i++)
{
graphic.clip[i].x = i * originalspriteW;
graphic.clip[i].y = j * originalspriteH;
graphic.clip[i].h = originalspriteW;
graphic.clip[i].w = originalspriteH;
}
graphic.useClip = DEFAULT;
}
main
{
clipSprites(2, 128, 256, [create, credits, menu])
return 0;
}