Creating a nested for loop clipping function

My old 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        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;
	}
Last edited on
its not the same code...
there is nothing at all like the line 14 for loop in the original.
the original has 1* (bug or derp?) and you have j*

the first thing to do if you want the same answer is to do the same things. If you try to move it to a new format AND change it at the same time, it will inevitably be difficult to get right. Once it does the same thing, then you can tweak it to do something similar but different and test that.
Registered users can post here. Sign in or register to post.