SDL Clips

Hi,
Ive recently been using SDL(for graphics and sound) to create a game.
Ive been using LazyFoos tutorials and everything works as expected.
My problem is that with like 20 buttons in the game the "clips" take up a lot of space.
I was wondering if i could just create a separate .cpp file just for the clips and include it
or whether i should create a .txt file and read the clips from that although i'm unsure how to do this.

Any advice would be helpful :)
You are using lazyfoo's button class? If each kind of button is stored in different images you could let the button class store the 4 clips as an array inside the class, and have the them initialized in the constructor.
Hey,
Thanks for the reply :)
Sorry i dont think you understood what i meant.
I have made my own class and i already have a function to pass the clips to the class.
Its just that writing the clips in main takes up a lot of space and looks untidy and i was just wondering where i could store them out of the way.
I dont know if you're allowed to include a .cpp or if there are problems with it. And i have always been told not to assign values in a header.
So you are storing all buttons in the same image? I just thought that there might be a way to automate the creation of the clips.

If you just want to put the clips in another file you can declare the clips as extern in a header file and define the clips in a source file.
1
2
// clips.h
extern SDL_Rect clips[4];


1
2
3
4
5
6
7
8
// clips.cpp
SDL_Rect clips[4] =
{
	{0, 0, 10, 10},
	{10, 0, 10, 10},
	{0, 10, 10, 10},
	{10, 10, 10, 10}
};


Last edited on
Peter87 wrote:
So you are storing all buttons in the same image?

No i have multiple images.
They are different sizes though so i have different clips for each one.
So i would have to create multiple SDL_Rects to make them and this looked messy.
Sorry if there was a misunderstanding :)
But yeah i think that extern may be what i was looking for.
Im going to look it up now.

Peter87 wrote:
I just thought that there might be a way to automate the creation of the clips.

I was just saying i already had a function to read in the clips i had made
1
2
3
4
5
6
7
void set_clips(SDL_Rect Clip[4])
{
    Bclips[ MOUSEOVER ].x =  Clip[ MOUSEOVER ].x;
    Bclips[ MOUSEOVER ].y = Clip[ MOUSEOVER ].y;
    Bclips[ MOUSEOVER ].w = Clip[ MOUSEOVER ].w;
    Bclips[ MOUSEOVER ].h = Clip[ MOUSEOVER ].h; 
//etc... 

Where Bclips is a class member used later on.
Is the way i was doing it.
If you have a way of automating it that would really help.

Also thanks for the help you have already given :)

edit.
Just realised you can do this for my function
 
 Bclips[ MOUSEOVER ] =  Clip[ MOUSEOVER ];
Last edited on
If the clips for each button has the same size and the clips is stored in the same order for all clips you could calculate the clips from the width and height of the button. This could be calculated in the constructor and stored in an array or it could be calculated on the fly when the button is drawn.
Had you looked at using SFML ? It is a C++ library , cross-platform , easier , less messier , faster and probably recent.
P.S : I dont want any arguments over sdl vs sfml ,any flames etc.I am suggesting my opinion.
The buttons are all different sizes which was why i was having a problem.
I have changed it so the clips are calculated in the constructor though.
Extern also helped with my other problem.
So thank you :)
If you calculate the clips in the constructor you don't need to declare the clips using extern. Just make the clips a member of the button class.
Would you be able to provide me with some code showing me because im having trouble understanding what you mean ?
Currently i have this
1
2
3
4
5
6
SDL_Rect PlayClip[4] = {{0, 0, 120, 40},
                                    {120, 40, 120, 40},
                                    {0, 40, 120, 40},
                                    {0, 0, 120, 40}};

Button PlayButton( 20, 20, 120, 40, "Playbutton.png", PlayClip );

Which works it was just all the rects took up space.
You said the clips was calculated in the constructor. Here is some very simplified code to demonstrate what I mean:
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
class Button
{
public:
	Button(int w, int h); 
private:
	SDL_Rect clips[4]
};

Button::Button(int w, int h); 
{
	int clipW = w / 2;
	int clipH = h / 2;
	
	clips[CLIP_MOUSEOVER].x = 0;
	clips[CLIP_MOUSEOVER].y = 0;
	clips[CLIP_MOUSEOVER].w = clipW;
	clips[CLIP_MOUSEOVER].h = clipH;
	clips[CLIP_MOUSEOUT].x = clipW;
	clips[CLIP_MOUSEOUT].y = 0;
	clips[CLIP_MOUSEOUT].w = clipW;
	clips[CLIP_MOUSEOUT].h = clipH;
	clips[CLIP_MOUSEDOWN].x = 0;
	clips[CLIP_MOUSEDOWN].y = clipH;
	clips[CLIP_MOUSEDOWN].w = clipW;
	clips[CLIP_MOUSEDOWN].h = clipH;
	clips[CLIP_MOUSEUP].x = clipW;
	clips[CLIP_MOUSEUP].y = clipH;
	clips[CLIP_MOUSEUP].w = clipW;
	clips[CLIP_MOUSEUP].h = clipH; 
}
Yeah i understand what you mean now.
That would be far easier.
Thanks :)
Topic archived. No new replies allowed.