Bonjour, j'ai écrit un système de callbacks pour un futur GUI et j'aimerais avoir votre avis/critiques.
De plus, j'ai un doute sur la création d'un GodObject (il y a eut une discussion dessus sur ce forum il n'y a pas si lontemps et j'ai crut comprendre que s'était a proscrire) et la façon d'éviter sa création.

Je vous montre le code (il n'est peut être pas parfaitement const_correct, il fait peut être parfois des copies en trop (manque de passage par référence ?)... n'hésitez pas à l'indiquer)

Il n'a pas de dépendance sur boost mais sur c++0x (avec gcc 4.5 minimum) et la SDL(Image,gfx).

Attention : ce code ne compile pas en -pedantic-errors

Le main (Il se contente de redimensionner une image de fond quand l'écran est redimensionné).

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include "Evenement.hpp"
#include "Callback.hpp"
#include "binding.hpp"
#include "app.hpp"
#include "Object.hpp"
#include <iostream>
#include "Image.h"
 
using namespace std;
 
int main(int argc, char**argv)
{
    app appli;
    Image img("ciel.bmp");
    SDL_Surface*screen=appli.get_screen();
    Image img2(img.copier_redim(640u,480u));
    img2.blit(screen);
    SDL_Flip(screen);
    Image(Image::*cop)(unsigned int,unsigned int)const =&Image::copier_redim;
    void(Image::*blit)(SDL_Surface*, unsigned int, unsigned int)const =&Image::blit;
    appli.add_event("resize",Evenement<int,int>::make_callback(img2,make_member(img,cop),placeholders::_1, placeholders::_2));
    appli.add_event("resize",Evenement<int,int>::make_callback_void(make_member(img2,blit), screen, 0,0));
    appli.add_event("resize",Evenement<int,int>::make_callback_void(SDL_Flip, screen));
    appli.wait();
    SDL_Quit();
    return 0;
}

binding.hpp

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#ifndef BINDING_HPP_INCLUDED
#define BINDING_HPP_INCLUDED
 
#include <functional>
#include <tuple>
 
//Attention : ce fichier est totalement illisible
 
template<typename T, typename R, typename ...Args>
class member_function
{
    private :
        T* obj;
        R(T::*function)(Args...);
    public :
        member_function(T* ob, R(T::*func)(Args...)): obj(ob), function(func){}
        R operator()(Args...arg)
        {
            return (obj->*function)(arg...);
        }
};
 
 
template<typename T, typename R, typename ...Args>
member_function<T,R,Args...> make_member(T* obj, R(T::*func)(Args...))
{
    return member_function<T,R,Args...>(obj, func);
}
 
template<typename T, typename R, typename ...Args>
member_function<T,R,Args...> make_member(T& obj, R(T::*func)(Args...))
{
    return member_function<T,R,Args...>(&obj, func);
}
 
template<typename T, typename R, typename ...Args>
class member_function_3
{
    private :
        const T* obj;
        R(T::*function)(Args...);
    public :
        member_function_3(const T* ob, R(T::*func)(Args...)): obj(ob), function(func){}
        R operator()(Args...arg)
        {
            return (obj->*function)(arg...);
        }
};
 
 
template<typename T, typename R, typename ...Args>
member_function_3<T,R,Args...> make_member(const T* obj, R(T::*func)(Args...))
{
    return member_function_3<T,R,Args...>(obj, func);
}
 
template<typename T, typename R, typename ...Args>
member_function_3<T,R,Args...> make_member(const T& obj, R(T::*func)(Args...))
{
    return member_function_3<T,R,Args...>(&obj, func);
}
 
template<typename T, typename R, typename ...Args>
class member_function_4
{
    private :
        const T* const obj;
        R(T::*function)(Args...);
    public :
        member_function_4(const T* const ob, R(T::*func)(Args...)): obj(ob), function(func){}
        R operator()(Args...arg)
        {
            return (obj->*function)(arg...);
        }
};
 
 
template<typename T, typename R, typename ...Args>
member_function_4<T,R,Args...> make_member(const T* const obj, R(T::*func)(Args...))
{
    return member_function_4<T,R,Args...>(obj, func);
}
 
 
template<typename T, typename R, typename ...Args>
class member_function_2
{
    private :
        T* obj;
        R(T::*function)(Args...)const;
    public :
        member_function_2(T* ob, R(T::*func)(Args...)const): obj(ob), function(func){}
        R operator()(Args...arg)
        {
            return (obj->*function)(arg...);
        }
};
 
 
template<typename T, typename R, typename ...Args>
member_function_2<T,R,Args...> make_member(T* obj, R(T::*func)(Args...)const)
{
    return member_function_2<T,R,Args...>(obj, func);
}
 
template<typename T, typename R, typename ...Args>
member_function_2<T,R,Args...> make_member(T& obj, R(T::*func)(Args...)const)
{
    return member_function_2<T,R,Args...>(&obj, func);
}
 
template<typename T, typename R, typename ...Args>
class member_function_5
{
    private :
        const T* obj;
        R(T::*function)(Args...)const;
    public :
        member_function_5(const T* ob, R(T::*func)(Args...)const): obj(ob), function(func){}
        R operator()(Args...arg)
        {
            return (obj->*function)(arg...);
        }
};
 
 
template<typename T, typename R, typename ...Args>
member_function_5<T,R,Args...> make_member(const T* obj, R(T::*func)(Args...)const)
{
    return member_function_5<T,R,Args...>(obj, func);
}
 
template<typename T, typename R, typename ...Args>
member_function_5<T,R,Args...> make_member(const T& obj, R(T::*func)(Args...)const)
{
    return member_function_5<T,R,Args...>(&obj, func);
}
 
template<typename T, typename R, typename ...Args>
class member_function_6
{
    private :
        const T* const obj;
        R(T::*function)(Args...)const;
    public :
        member_function_6(const T* const ob, R(T::*func)(Args...)const): obj(ob), function(func){}
        R operator()(Args...arg)
        {
            return (obj->*function)(arg...);
        }
};
 
 
template<typename T, typename R, typename ...Args>
member_function_6<T,R,Args...> make_member(const T* const obj, R(T::*func)(Args...)const)
{
    return member_function_6<T,R,Args...>(obj, func);
}
 
 
template<typename R,unsigned int N>
struct get_args_impl_2
{
    template<typename Fc, typename ...Args, typename ...ArgsFE>
    static R apply(Fc function, std::tuple<Args...> E,ArgsFE...finis )
    {
        return get_args_impl_2<R,N-1>::apply(function, E, std::get<N-1>(E), finis...);
    }
};
 
template<typename R>
struct get_args_impl_2<R,0>
{
    template<typename Fc, typename ...Args, typename ...ArgsFE>
    static R apply(Fc function, std::tuple<Args...> E,ArgsFE...finis )
    {
             return function(finis...);
    }
};
 
template<typename R,unsigned int N>
struct get_args_impl
{
    template<typename Fc, typename ...Args, typename ...ArgsF, typename ...ArgsE>
    static R apply(Fc function, std::tuple<Args...> t, std::tuple<ArgsE...> E, ArgsF...argsfait)
    {
        return get_args_impl<R,N-1>::apply(function, t, E, std::get<N-1>(t), argsfait...);
    }
};
 
template<typename R>
struct get_args_impl<R,0>
{
    template<typename Fc, typename ...Args, typename ...ArgsF, typename ...ArgsE>
    static R apply(Fc function, std::tuple<Args...> t, std::tuple<ArgsE...> E, ArgsF...argsfait)
    {
        return get_args_impl_2<R,sizeof...(ArgsE)>::apply(std::bind(function, argsfait...), E);
    }
};
 
template<typename R,typename Fc,typename ...ArgsE>
struct stupid
{
    template<typename ...Args>
    static R my_bind(Fc function,const std::tuple<Args...>& t, std::tuple<ArgsE...> te)
    {
        return get_args_impl<R,sizeof...(Args)>::apply(function, t, te);
    }
};
 
 
 
 
template<unsigned int N>
struct get_args_impl_2_v2
{
    template<typename Fc, typename ...Args, typename ...ArgsFE>
    static void apply(Fc function, std::tuple<Args...> E,ArgsFE...finis )
    {
        get_args_impl_2_v2<N-1>::apply(function, E, std::get<N-1>(E), finis...);
    }
};
 
template<>
struct get_args_impl_2_v2<0>
{
    template<typename Fc, typename ...Args, typename ...ArgsFE>
    static void apply(Fc function, std::tuple<Args...> E,ArgsFE...finis )
    {
             function(finis...);
    }
};
 
template<unsigned int N>
struct get_args_impl_v2
{
    template<typename Fc, typename ...Args, typename ...ArgsF, typename ...ArgsE>
    static void apply(Fc function, std::tuple<Args...> t, std::tuple<ArgsE...> E, ArgsF...argsfait)
    {
        get_args_impl_v2<N-1>::apply(function, t, E, std::get<N-1>(t), argsfait...);
    }
};
 
template<>
struct get_args_impl_v2<0>
{
    template<typename Fc, typename ...Args, typename ...ArgsF, typename ...ArgsE>
    static void apply(Fc function, std::tuple<Args...> t, std::tuple<ArgsE...> E, ArgsF...argsfait)
    {
        get_args_impl_2_v2<sizeof...(ArgsE)>::apply(std::bind(function, argsfait...), E);
    }
};
 
template<typename Fc,typename ...ArgsE>
struct stupid_v2
{
    template<typename ...Args>
    static void my_bind(Fc function,const std::tuple<Args...>& t, std::tuple<ArgsE...> te)
    {
        get_args_impl_v2<sizeof...(Args)>::apply(function, t, te);
    }
};
 
 
#endif // BINDING_HPP_INCLUDED
Callback.hpp

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CALLBACK_HPP_INCLUDED
#define CALLBACK_HPP_INCLUDED
 
#include <functional>
 
class Callback
{
    private :
        std::function<void(void*)> function;
    public :
        inline Callback(std::function<void(void*)> fc) : function(fc)
        {}
        inline void operator()(void*even) const
        {
            function(even);
        }
};
 
#endif // CALLBACK_HPP_INCLUDED

app.hpp (dans le cas d'un GodObject, on la ferait hérité de Object.hpp)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#ifndef APP_HPP_INCLUDED
#define APP_HPP_INCLUDED
 
#include <utility>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include "Callback.hpp"
#include "Evenement.hpp"
#include <SDL/SDL.h>
 
class app
{
    private :
        std::unordered_map<std::string,std::vector<Callback>>Callbacks;
        inline bool has_key(std::string key)const;
        void handle_event(std::string key, void*even)const;
        bool exit;
        SDL_Surface*screen;
    public :
        app();
        template<typename ...Args>
        inline void emit(std::string key, Evenement<Args...> even)const;
        inline void emit(std::string key)const;
        void wait();
        inline void quit();
        bool add_event(std::string key, Callback fc);
        bool add_event(std::string key, Callback fc, unsigned int pos);
        void show_callbacks();//Sert a debbuger
        inline const std::unordered_map<std::string,std::vector<Callback>>& get_callbacks()const;
        SDL_Surface*get_screen()const;
        ~app();
};
 
#endif // APP_HPP_INCLUDED
Object.hpp (Sera soit un GodObject ou n'existera pas en tant que tel mais en tant que 2 classes séparées : GraphicObject et SoundObject)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef OBJECT_HPP_INCLUDED
#define OBJECT_HPP_INCLUDED
 
#include "app.hpp"
 
class Object
{
    private :
        std::unordered_map<std::string,std::vector<Callback>>Callbacks;
        inline bool has_key(std::string key)const;
        void handle_event(std::string key, void*even)const;
        app*application;
    public :
        Object(app& a);
        template<typename ...Args>
        inline void emit(std::string key, Evenement<Args...> even)const;
        inline void emit(std::string key)const;
        bool add_event(std::string key, Callback fc);
        bool add_event(std::string key, Callback fc, unsigned int pos);
        inline const std::unordered_map<std::string,std::vector<Callback>>& get_callbacks()const;
};
 
#endif // OBJECT_HPP_INCLUDED
Image.h (wrapper a SDL_Surface)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef IMAGE_H_INCLUDED
#define IMAGE_H_INCLUDED
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <vector>
#include <string>
 
class Image
{
    private :
        SDL_Surface *img;
    public :
        Image (SDL_Surface *image=NULL, bool copier=true);
        Image (const char *chm_img);
        Image (const Image &image);
        ~Image ();
 
        void blit (SDL_Surface *image, unsigned int x=0, unsigned int y=0) const;
        void blit (Image &image, unsigned int x=0, unsigned int y=0) const;
 
        void redim (double rapport);
        void redim (double rapport_x, double rapport_y);
        void redim (unsigned int nouv_taille);
        void redim (unsigned int nouv_l, unsigned int nouv_h);
 
        Image copier_redim (double rapport) const ;
        Image copier_redim (double rapport_x, double rapport_y) const;
        Image copier_redim (unsigned int nouv_taille) const;
        Image copier_redim_h (unsigned int nouv_taille) const;
        Image copier_redim (unsigned int nouv_l, unsigned int nouv_h) const;
 
        void set_transparency(unsigned int);
        void set_transparency(unsigned int alpha, SDL_Rect zone);
        inline const SDL_Surface *obt_image () const
        { return img; }
 
        void delete_color(std::vector<Uint32> col);
        void delete_color(Uint32 col);
 
        void delete_color(bool (*fc)(Uint8 r, Uint8 v, Uint8 b, Uint8 a));
        void delete_color(bool (*fc)(Uint8 r, Uint8 v, Uint8 b));
        void delete_color(bool (*fc)(Uint8 r, Uint8 v, Uint8 b, Uint8 a, unsigned int x, unsigned int y));
        void delete_color(bool (*fc)(Uint8 r, Uint8 v, Uint8 b, unsigned int x, unsigned int y));
 
 
        void delete_transparent_borders();
        void keep_rectangle(unsigned int ,unsigned int , unsigned int , unsigned int);
        Image operator= (SDL_Surface *image);
        Image operator= (const Image &image);
        //bool saveAsPNG(const std::string filename);
        void set_alpha_perpixel();
 
        void set_color(unsigned int x, unsigned int y, Uint32 col);
        void set_color(unsigned int x, unsigned int y, Uint8 r, Uint8 v, Uint8 b, Uint8 a=0);
 
        Uint32 get_color(unsigned int x, unsigned int y)const;
        void get_color(unsigned int x, unsigned int y, Uint8& r, Uint8& v, Uint8& b, Uint8& a)const;
};
 
 
 
#endif // IMAGE_H_INCLUDED
app.cpp

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "app.hpp"
#include "test_functions.hpp"
 
 
app::~app()
{
    quit();
}
 
SDL_Surface* app::get_screen()const
{
    return screen;
}
 
const std::unordered_map<std::string,std::vector<Callback>>& app::get_callbacks()const
{
    return Callbacks;
}
 
bool app::has_key(std::string key)const
{
    return !(Callbacks.find(key)==Callbacks.end());
}
void app::handle_event(std::string key, void*even)const
{
    const std::vector<Callback>&vect=(Callbacks.find(key))->second;
    for(unsigned int i=0;i<vect.size();i++)
        (vect[i])(even);
}
app::app()
{
    SDL_Init( SDL_INIT_EVERYTHING );
    freopen("CON", "w", stdout);//"Fait marcher" la console
    freopen("CON", "r", stdin);
    freopen("CON", "w", stderr);
    screen=SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE|SDL_RESIZABLE );
    add_event("resize",Evenement<int,int>::make_callback(screen,SDL_SetVideoMode, std::placeholders::_1, std::placeholders::_2,32, SDL_HWSURFACE|SDL_RESIZABLE));
    add_event("resize",Evenement<int,int>::make_callback_void(SDL_Flip, screen));
    add_event("delete_screen",Evenement<>::make_callback_void(SDL_FreeSurface,screen));
    add_event("sdl_quit",Evenement<>::make_callback_void(SDL_FreeSurface,screen));
    add_event("sdl_quit",Evenement<>::make_callback_void(make_member(this,&app::quit)));
}
template<typename ...Args>
void app::emit(std::string key, Evenement<Args...> even)const
{
    if(has_key(key))
        handle_event(key,(void*)(&even));
}
void app::emit(std::string key)const
{
    if(has_key(key))
        handle_event(key,NULL);
}
void app::wait()
{
    SDL_Event event;
    exit=false;
    while(!exit)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_VIDEORESIZE :
                emit("resize", Evenement<int,int>(event.resize.w,event.resize.h));
                break;
            case SDL_KEYDOWN :
                emit("keyboard", Evenement<bool, SDL_keysym>(true,event.key.keysym));
                break;
            case SDL_KEYUP :
                emit("keyboard", Evenement<bool, SDL_keysym>(false,event.key.keysym));
                break;
            case SDL_MOUSEBUTTONDOWN :
                emit("mouse_button", Evenement<bool, Uint8,Uint16,Uint16>(true,event.button.which,event.button.x,event.button.y));
                break;
            case SDL_MOUSEBUTTONUP :
                emit("mouse_button", Evenement<bool, Uint8,Uint16,Uint16>(false,event.button.which,event.button.x,event.button.y));
                break;
            case SDL_MOUSEMOTION :
                emit("mouse_motion", Evenement<bool, Uint16,Uint16,Uint16,Uint16>(event.motion.state==SDL_PRESSED,event.motion.x,event.motion.y,event.motion.xrel,event.motion.yrel));
                break;
            case SDL_VIDEOEXPOSE :
                emit("video_expose");
                break;
            case SDL_ACTIVEEVENT :
                emit("window_sate",Evenement<bool>(event.active.gain==1));
                break;
            case SDL_QUIT :
                emit("sdl_quit");
                break;
        }
    }
}
void app::quit()
{
    exit=true;
}
bool app::add_event(std::string key, Callback fc)
{
    if(has_key(key))
    {
        std::vector<Callback>& vect=Callbacks[key];
        vect.push_back(fc);
        return false;
    }
    else
    {
        std::vector<Callback> vect(1,fc);
        Callbacks.insert(std::pair<std::string,std::vector<Callback>>(key,vect));
        return true;
    }
}
bool app::add_event(std::string key, Callback fc, unsigned int pos)
{
    if(has_key(key))
    {
        std::vector<Callback>& vect=Callbacks[key];
        if(pos<vect.size())
            vect.insert(vect.begin()+pos,fc);
        else
            vect.push_back(fc);
        return false;
    }
    else
    {
        std::vector<Callback> vect(1,fc);
        Callbacks.insert(std::pair<std::string,std::vector<Callback>>(key,vect));
        return true;
    }
}
void app::show_callbacks()
{
    std::cout<<"showing callbacks";
    std::unordered_map<std::string,std::vector<Callback>>::iterator it;
    for(it=Callbacks.begin();it!=Callbacks.end();it++)
    {
        std::cout<<"\n"<<(*it).first<<" nb_func= "<<(*it).second.size();
    }
}

Object.cpp

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "Object.hpp"
 
const std::unordered_map<std::string,std::vector<Callback>>& Object::get_callbacks()const
{
    return Callbacks;
}
 
bool Object::has_key(std::string key)const
{
    return !(Callbacks.find(key)==Callbacks.end());
}
void Object::handle_event(std::string key, void*even)const
{
    const std::vector<Callback>& vect=(Callbacks.find(key))->second;
    for(unsigned int i=0;i<vect.size();i++)
    {
        (vect[i])(even);
    }
}
Object::Object(app& a)
{
    application=&a;
}
template<typename ...Args>
void Object::emit(std::string key, Evenement<Args...> even)const
{
    if(has_key(key))
        handle_event(key,(void*)(&even));
}
void Object::emit(std::string key)const
{
    if(has_key(key))
        handle_event(key,NULL);
}
bool Object::add_event(std::string key, Callback fc)
{
    if(has_key(key))
    {
        std::vector<Callback>& vect=Callbacks[key];
        vect.push_back(fc);
        return false;
    }
    else
    {
        std::vector<Callback> vect(1,fc);
        Callbacks.insert(std::pair<std::string,std::vector<Callback>>(key,vect));
        return true;
    }
}
 
bool Object::add_event(std::string key, Callback fc, unsigned int pos)
{
    if(has_key(key))
    {
        std::vector<Callback>& vect=Callbacks[key];
        if(pos<vect.size())
            vect.insert(vect.begin()+pos,fc);
        else
            vect.push_back(fc);
        return false;
    }
    else
    {
        std::vector<Callback> vect(1,fc);
        Callbacks.insert(std::pair<std::string,std::vector<Callback>>(key,vect));
        return true;
    }
}
Image.cpp (Pas encore optimisé ne fouillait pas dedans, ce n'ai pas la partie qui m'interesse)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
#include "Image.h"
#include <SDL/SDL_image.h>
#include <iostream>
#include <windows.h>
#include <SDL/SDL_rotozoom.h>
#include <limits.h>
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
 
    #define MASQUE_ROUGE 0xff000000
    #define MASQUE_VERT  0x00ff0000
    #define MASQUE_BLEU  0x0000ff00
    #define MASQUE_ALPHA 0x000000ff
 
#else
 
    #define MASQUE_ROUGE 0x000000ff
    #define MASQUE_VERT  0x0000ff00
    #define MASQUE_BLEU  0x00ff0000
    #define MASQUE_ALPHA 0xff000000
 
#endif
 
 
Image::Image (SDL_Surface *image, bool copier)
{
    if (image)
    {
        if (copier)
            img=SDL_DisplayFormatAlpha(image);
        else
            img=image;
    }
    else
    {
        img=SDL_CreateRGBSurface(SDL_HWSURFACE, 100,100,32,0,0,0,0);
        SDL_SetAlpha (img,SDL_SRCALPHA,0);
    }
}
Image::Image (const char *chm_img)
{
    SDL_Surface*tmp=IMG_Load(chm_img);
    if (!tmp)
    {
        img=SDL_CreateRGBSurface(SDL_HWSURFACE, 100,100,32,0,0,0,0);
        SDL_SetAlpha (img,SDL_SRCALPHA,0);
    }
    else
    {
        img=SDL_DisplayFormatAlpha( tmp );
        SDL_FreeSurface(tmp);
    }
}
 
Image::Image (const Image &image)
{
    img=SDL_DisplayFormatAlpha( image.img );
 
}
 
Image::~Image ()
{
    SDL_FreeSurface (img);
}
 
void Image::blit (SDL_Surface *image, unsigned int x, unsigned int y) const
{
    if (image)
    {
        SDL_Rect tmp={(Uint16)x,(Uint16)y,0,0};
        SDL_BlitSurface (img,NULL,image,&tmp);
    }
}
void Image::blit (Image &image, unsigned int x, unsigned int y)const
{
    SDL_Rect tmp={(Uint16)x,(Uint16)y,(Uint16)img->w,(Uint16)img->h};
    SDL_BlitSurface (img,NULL,image.img,&tmp);
}
 
void Image::redim (double rapport)
{ redim (rapport,rapport); }
void Image::redim (double rapport_x, double rapport_y)
{
    if(img->format->alpha==255 || img->format->Amask!=0)
    {
        SDL_Surface *tampon=zoomSurface(img,rapport_x,rapport_y,0);
        SDL_FreeSurface (img);
        img=tampon;
    }
    else
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        SDL_Surface *tampon=zoomSurface(tmp,rapport_x,rapport_y,0);
        SDL_FreeSurface (img);
        SDL_FreeSurface (tmp);
        img=tampon;
    }
}
void Image::redim (unsigned int nouv_taille)
{ redim (nouv_taille,img->h*nouv_taille/img->w); }
 
 
void Image::redim (unsigned int nouv_l, unsigned int nouv_h)
{
    if(img->format->alpha==255 || img->format->Amask!=0)
    {
        SDL_Surface *tampon=zoomSurface(img,(double)nouv_l/img->w,(double)nouv_h/img->h,0);
        SDL_FreeSurface (img);
        img=tampon;
    }
    else
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        SDL_Surface *tampon=zoomSurface(tmp,(double)nouv_l/img->w,(double)nouv_h/img->h,0);
        SDL_FreeSurface (img);
        SDL_FreeSurface (tmp);
        img=tampon;
    }
}
 
Image Image::copier_redim (double rapport)const
{ return copier_redim(rapport,rapport); }
Image Image::copier_redim (double rapport_x, double rapport_y)const
{
    SDL_Surface*tampon;
    if(img->format->alpha==255 || img->format->Amask!=0)
        tampon=zoomSurface(img,rapport_x,rapport_y,0);
    else
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        tampon=zoomSurface(tmp,rapport_x,rapport_y,0);
        SDL_FreeSurface (tmp);
    }
    //Image tmp(tampon,false);
    Image tmp(tampon,true);
    SDL_FreeSurface(tampon);
    return tmp;
}
Image Image::copier_redim (unsigned int nouv_taille)const
{ return copier_redim(nouv_taille,img->h*nouv_taille/img->w); }
 
Image Image::copier_redim_h (unsigned int nouv_taille)const
{ return copier_redim(img->w*nouv_taille/img->h,nouv_taille); }
 
Image Image::copier_redim (unsigned int nouv_l, unsigned int nouv_h)const
{
    SDL_Surface*tampon;
    if(img->format->alpha==255 || img->format->Amask!=0)
        tampon=zoomSurface(img,(double)nouv_l/img->w,(double)nouv_h/img->h,0);
    else
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        tampon=zoomSurface(tmp,(double)nouv_l/img->w,(double)nouv_h/img->h,0);
        SDL_FreeSurface (tmp);
    }
    //Image tmp(tampon,false);
    Image tmp(tampon,true);
    SDL_FreeSurface(tampon);
    return tmp;
}
 
Image Image::operator= (SDL_Surface *image)
{
    if (image)
    {
        SDL_FreeSurface (img);
        img=SDL_DisplayFormatAlpha(image);
    }
    return *this;
}
/*Image Image::operator= (Image &image)
{
    SDL_FreeSurface (img);
    img=SDL_CreateRGBSurface(SDL_HWSURFACE,image.img->w,image.img->h,fenetre->ecran->format->BitsPerPixel,0,0,0,0);
    SDL_BlitSurface (image.img,NULL,img,NULL);
    return *this;
}*/
 
Image Image::operator= (const Image& image)
{
    SDL_FreeSurface (img);
    img=SDL_DisplayFormatAlpha(image.img);
    return *this;
}
 
Uint32 obtenirPixel(const SDL_Surface *surface, unsigned int x, unsigned int y)
{
    /*nbOctetsParPixel représente le nombre d'octets utilisés pour stocker un pixel.
    En multipliant ce nombre d'octets par 8 (un octet = 8 bits), on obtient la profondeur de couleur
    de l'image : 8, 16, 24 ou 32 bits.*/
    int nbOctetsParPixel = surface->format->BytesPerPixel;
    /* Ici p est l'adresse du pixel que l'on veut connaitre */
    /*surface->pixels contient l'adresse du premier pixel de l'image*/
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel;
 
    /*Gestion différente suivant le nombre d'octets par pixel de l'image*/
    switch(nbOctetsParPixel)
    {
        case 1:
            return *p;
 
        case 2:
            return *(Uint16 *)p;
 
        case 3:
            /*Suivant l'architecture de la machine*/
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
                return p[0] << 16 | p[1] << 8 | p[2];
            else
                return p[0] | p[1] << 8 | p[2] << 16;
 
        case 4:
            return *(Uint32 *)p;
 
        /*Ne devrait pas arriver, mais évite les erreurs*/
        default:
            return 0;
    }
}
 
void definirPixel(SDL_Surface *surface, unsigned int x, unsigned int y, Uint32 pixel)
{
    /*nbOctetsParPixel représente le nombre d'octets utilisés pour stocker un pixel.
    En multipliant ce nombre d'octets par 8 (un octet = 8 bits), on obtient la profondeur de couleur
    de l'image : 8, 16, 24 ou 32 bits.*/
    int nbOctetsParPixel = surface->format->BytesPerPixel;
    /*Ici p est l'adresse du pixel que l'on veut modifier*/
    /*surface->pixels contient l'adresse du premier pixel de l'image*/
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel;
 
    /*Gestion différente suivant le nombre d'octets par pixel de l'image*/
    switch(nbOctetsParPixel)
    {
        case 1:
            *p = pixel;
            break;
 
        case 2:
            *(Uint16 *)p = pixel;
            break;
 
        case 3:
            /*Suivant l'architecture de la machine*/
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            {
                p[0] = (pixel >> 16) & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = pixel & 0xff;
            }
            else
            {
                p[0] = pixel & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = (pixel >> 16) & 0xff;
            }
            break;
 
        case 4:
            *(Uint32 *)p = pixel;
            break;
    }
}
 
 
 
void Image::delete_color(Uint32 col)
{
    if(img->format->Amask==0&&img->format->alpha==255)
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        unsigned int x,y;
        Uint8 rs,vs,bs,rc,vc,bc;
        SDL_GetRGB(col, img->format, &rc, &vc, &bc);
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(tmp->format,0,0,0,0);
        if(SDL_MUSTLOCK(tmp))
        {
            for(x=0;x<tmp->w;x++)
            {
                for(y=0;y<tmp->h;y++)
                {
                    pixel=obtenirPixel(tmp,x,y);
                    SDL_GetRGB(pixel, tmp->format, &rs, &vs, &bs);
                    if(rs==rc && vc==vs && bc==bs)
                        definirPixel(tmp,x,y,invisible);
                }
            }
            SDL_UnlockSurface(tmp);
        }
        else
        {
            for(x=0;x<tmp->w;x++)
            {
                for(y=0;y<tmp->h;y++)
                {
                    pixel=obtenirPixel(tmp,x,y);
                    SDL_GetRGB(pixel, tmp->format, &rs, &vs, &bs);
                    if(rs==rc && vc==vs && bc==bs)
                        definirPixel(tmp,x,y,invisible);
                }
            }
        }
        SDL_FreeSurface(img);
        img=tmp;
    }
    else
    {
        unsigned int x,y;
        Uint8 rs,vs,bs,rc,vc,bc;
        SDL_GetRGB(col, img->format, &rc, &vc, &bc);
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(img->format,0,0,0,0);
        if(SDL_MUSTLOCK(img))
        {
            SDL_LockSurface(img);
            for(x=0;x<img->w;x++)
            {
                for(y=0;y<img->h;y++)
                {
                    pixel=obtenirPixel(img,x,y);
                    SDL_GetRGB(pixel, img->format, &rs, &vs, &bs);
                    if(rs==rc && vc==vs && bc==bs)
                        definirPixel(img,x,y,invisible);
                }
            }
            SDL_UnlockSurface(img);
        }
        else
        {
            for(x=0;x<img->w;x++)
            {
                for(y=0;y<img->h;y++)
                {
                    pixel=obtenirPixel(img,x,y);
                    SDL_GetRGB(pixel, img->format, &rs, &vs, &bs);
                    if(rs==rc && vc==vs && bc==bs)
                        definirPixel(img,x,y,invisible);
                }
            }
        }
    }
}
 
 
 
void Image::delete_color(std::vector<Uint32>col)
{
    if(img->format->Amask==0&&img->format->alpha==255)
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        unsigned int x,y,i;
        Uint8 rs,vs,bs,rc[col.size()],vc[col.size()],bc[col.size()];
        for(i=0;i<col.size();i++)
            SDL_GetRGB(col[i], img->format, &rc[i], &vc[i], &bc[i]);
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(tmp->format,0,0,0,0);
        SDL_LockSurface(tmp);
        for(x=0;x<tmp->w;x++)
        {
            for(y=0;y<tmp->h;y++)
            {
                pixel=obtenirPixel(tmp,x,y);
                SDL_GetRGB(pixel, tmp->format, &rs, &vs, &bs);
                for(i=0;i<col.size();i++)
                {
                    if(rs==rc[i] && vc[i]==vs && bc[i]==bs)
                    {
                        definirPixel(tmp,x,y,invisible);
                        break;
                    }
                }
            }
        }
        SDL_UnlockSurface(tmp);
        SDL_FreeSurface(img);
        img=tmp;
    }
    else
    {
        unsigned int x,y,i;
        Uint8 rs,vs,bs,rc[col.size()],vc[col.size()],bc[col.size()];
        for(i=0;i<col.size();i++)
            SDL_GetRGB(col[i], img->format, &rc[i], &vc[i], &bc[i]);
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(img->format,0,0,0,0);
        SDL_LockSurface(img);
        for(x=0;x<img->w;x++)
        {
            for(y=0;y<img->h;y++)
            {
                pixel=obtenirPixel(img,x,y);
                SDL_GetRGB(pixel, img->format, &rs, &vs, &bs);
                for(i=0;i<col.size();i++)
                {
                    if(rs==rc[i] && vc[i]==vs && bc[i]==bs)
                    {
                        definirPixel(img,x,y,invisible);
                        break;
                    }
                }
            }
        }
        SDL_UnlockSurface(img);
    }
}
 
 
void Image::set_transparency(unsigned int i)
{
    if(img->format->Amask==0)
        SDL_SetAlpha(img,SDL_SRCALPHA,i);
    else
    {
        unsigned int x,y;
        Uint32 pixel,pixel1;
        Uint8 rs,bs,vs,as;
        SDL_LockSurface(img);
        for(x=0;x<img->w;x++)
        {
            for(y=0;y<img->h;y++)
            {
                pixel=obtenirPixel(img,x,y);
                SDL_GetRGBA(pixel, img->format,&rs,&vs,&bs,&as);
                if(as!=0)
                {
                    pixel1=SDL_MapRGBA(img->format,rs,vs,bs,i);
                    definirPixel(img,x,y,pixel1);
                }
            }
        }
        SDL_UnlockSurface(img);
    }
}
 
void Image::set_transparency(unsigned int i, SDL_Rect zone)
{
    set_alpha_perpixel();
    SDL_LockSurface(img);
    Uint8 r,v,b,a;
    unsigned int x,y;
    Uint32 pixel;
    for(x=zone.x;x<=zone.x+zone.w;x++)
    {
        for(y=zone.y;y<=zone.y+zone.h;y++)
        {
            pixel=obtenirPixel(img,x,y);
            SDL_GetRGBA(pixel, img->format,&r,&v,&b,&a);
            if(a!=0)
                definirPixel(img,x,y,SDL_MapRGBA(img->format,r,v,b,i));
        }
    }
    SDL_UnlockSurface(img);
}
 
 
 
 
 
 
 
 
 
 
 
void Image::delete_color(bool (*fc)(Uint8 ,Uint8,Uint8,Uint8))
{
    if(img->format->Amask==0&&img->format->alpha==255)
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        unsigned int x,y;
        Uint8 rs,vs,bs,as;
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(tmp->format,0,0,0,0);
        SDL_LockSurface(tmp);
        for(x=0;x<tmp->w;x++)
        {
            for(y=0;y<tmp->h;y++)
            {
                pixel=obtenirPixel(tmp,x,y);
                SDL_GetRGBA(pixel, tmp->format, &rs, &vs, &bs,&as);
                if(fc(rs,vs,bs,as))
                    definirPixel(tmp,x,y,invisible);
            }
        }
        SDL_UnlockSurface(tmp);
        SDL_FreeSurface(img);
        img=tmp;
    }
    else
    {
        unsigned int x,y;
        Uint8 rs,vs,bs,as;
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(img->format,0,0,0,0);
        SDL_LockSurface(img);
        for(x=0;x<img->w;x++)
        {
            for(y=0;y<img->h;y++)
            {
                pixel=obtenirPixel(img,x,y);
                SDL_GetRGBA(pixel, img->format, &rs, &vs, &bs,&as);
                if(fc(rs,vs,bs,as))
                    definirPixel(img,x,y,invisible);
            }
        }
        SDL_UnlockSurface(img);
    }
}
 
void Image::delete_color(bool (*fc)(Uint8 ,Uint8,Uint8,Uint8, unsigned int, unsigned int))
{
    if(img->format->Amask==0&&img->format->alpha==255)
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        unsigned int x,y;
        Uint8 rs,vs,bs,as;
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(tmp->format,0,0,0,0);
        SDL_LockSurface(tmp);
        for(x=0;x<tmp->w;x++)
        {
            for(y=0;y<tmp->h;y++)
            {
                pixel=obtenirPixel(tmp,x,y);
                SDL_GetRGBA(pixel, tmp->format, &rs, &vs, &bs,&as);
                if(fc(rs,vs,bs,as,x,y))
                    definirPixel(tmp,x,y,invisible);
            }
        }
        SDL_UnlockSurface(tmp);
        SDL_FreeSurface(img);
        img=tmp;
    }
    else
    {
        unsigned int x,y;
        Uint8 rs,vs,bs,as;
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(img->format,0,0,0,0);
        SDL_LockSurface(img);
        for(x=0;x<img->w;x++)
        {
            for(y=0;y<img->h;y++)
            {
                pixel=obtenirPixel(img,x,y);
                SDL_GetRGBA(pixel, img->format, &rs, &vs, &bs,&as);
                if(fc(rs,vs,bs,as,x,y))
                    definirPixel(img,x,y,invisible);
            }
        }
        SDL_UnlockSurface(img);
    }
}
 
void Image::delete_color(bool (*fc)(Uint8 ,Uint8,Uint8))
{
    if(img->format->Amask==0&&img->format->alpha==255)
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        unsigned int x,y;
        Uint8 rs,vs,bs;
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(tmp->format,0,0,0,0);
        SDL_LockSurface(tmp);
        for(x=0;x<tmp->w;x++)
        {
            for(y=0;y<tmp->h;y++)
            {
                pixel=obtenirPixel(tmp,x,y);
                SDL_GetRGB(pixel, tmp->format, &rs, &vs, &bs);
                if(fc(rs,vs,bs))
                    definirPixel(tmp,x,y,invisible);
            }
        }
        SDL_UnlockSurface(tmp);
        SDL_FreeSurface(img);
        img=tmp;
    }
    else
    {
        unsigned int x,y;
        Uint8 rs,vs,bs;
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(img->format,0,0,0,0);
        SDL_LockSurface(img);
        for(x=0;x<img->w;x++)
        {
            for(y=0;y<img->h;y++)
            {
                pixel=obtenirPixel(img,x,y);
                SDL_GetRGB(pixel, img->format, &rs, &vs, &bs);
                if(fc(rs,vs,bs))
                    definirPixel(img,x,y,invisible);
            }
        }
        SDL_UnlockSurface(img);
    }
}
 
 
void Image::delete_color(bool (*fc)(Uint8 ,Uint8,Uint8,unsigned int, unsigned int))
{
    if(img->format->Amask==0&&img->format->alpha==255)
    {
        SDL_Surface*tmp=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,32,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        blit(tmp);
        unsigned int x,y;
        Uint8 rs,vs,bs;
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(tmp->format,0,0,0,0);
        SDL_LockSurface(tmp);
        for(x=0;x<tmp->w;x++)
        {
            for(y=0;y<tmp->h;y++)
            {
                pixel=obtenirPixel(tmp,x,y);
                SDL_GetRGB(pixel, tmp->format, &rs, &vs, &bs);
                if(fc(rs,vs,bs,x,y))
                    definirPixel(tmp,x,y,invisible);
            }
        }
        SDL_UnlockSurface(tmp);
        SDL_FreeSurface(img);
        img=tmp;
    }
    else
    {
        unsigned int x,y;
        Uint8 rs,vs,bs;
        Uint32 pixel;
        Uint32 invisible=SDL_MapRGBA(img->format,0,0,0,0);
        SDL_LockSurface(img);
        for(x=0;x<img->w;x++)
        {
            for(y=0;y<img->h;y++)
            {
                pixel=obtenirPixel(img,x,y);
                SDL_GetRGB(pixel, img->format, &rs, &vs, &bs);
                if(fc(rs,vs,bs,x,y))
                    definirPixel(img,x,y,invisible);
            }
        }
        SDL_UnlockSurface(img);
    }
}
 
 
void Image::keep_rectangle(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
{
    if(img->format->Amask==0)
    {
        SDL_Surface*tampon=SDL_CreateRGBSurface(SDL_HWSURFACE,x2-x1,y2-y1,32,0,0,0,0);
        SDL_Rect rect={(Uint16)x1,(Uint16)y1,(Uint16)(x2-1),(Uint16)(y2-1)};
        SDL_BlitSurface(img,&rect,tampon,NULL);
        tampon->format->alpha=img->format->alpha;
        SDL_FreeSurface(img);
        img=tampon;
    }
    else
    {
        SDL_Surface*tampon=SDL_CreateRGBSurface(SDL_HWSURFACE,x2-x1,y2-y1,img->format->BitsPerPixel,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
        unsigned int x,y;
        Uint8 r,v,b,a;
        Uint32 pixelS,pixel;
        SDL_LockSurface(img);
        SDL_LockSurface(tampon);
        for(x=x1;x<x2;x++)
        {
            for(y=y1;y<y2;y++)
            {
                pixelS=obtenirPixel(img,x,y);
                SDL_GetRGBA(pixelS,img->format,&r,&v,&b,&a);
                pixel=SDL_MapRGBA(tampon->format,r,v,b,a);
                definirPixel(tampon,x-x1,y-y1,pixel);
            }
        }
        SDL_UnlockSurface(img);
        SDL_UnlockSurface(tampon);
        SDL_FreeSurface(img);
        img=tampon;
    }
}
 
 
 
void Image::delete_transparent_borders()
{
    unsigned int i,j;
    Uint32 Pixel;
    Uint8 t1,t2,t3,a;
    bool empty=true;
    SDL_LockSurface(img);
    for(i=0;i<img->h&&empty;i++)
    {
        for(j=0;j<img->w;j++)
        {
            Pixel=obtenirPixel(img,j,i);
            SDL_GetRGBA(Pixel,img->format,&t1,&t2,&t3,&a);
            if(a!=0)
            {
                empty=false;
                break;
            }
        }
    }
    SDL_UnlockSurface(img);
    keep_rectangle(0,i-1,img->w,img->h);
    empty=true;
    SDL_LockSurface(img);
    for(i=0;i<img->w&&empty;i++)
    {
        for(j=0;j<img->h;j++)
        {
            Pixel=obtenirPixel(img,i,j);
            SDL_GetRGBA(Pixel,img->format,&t1,&t2,&t3,&a);
            if(a!=0)
            {
                empty=false;
                break;
            }
        }
    }
    SDL_UnlockSurface(img);
    keep_rectangle(i-1,0,img->w,img->h);
    empty=true;
    SDL_LockSurface(img);
    for(i=img->h-1;i!=INT_MAX&&empty;i--)
    {
        for(j=0;j<img->w;j++)
        {
            Pixel=obtenirPixel(img,j,i);
            SDL_GetRGBA(Pixel,img->format,&t1,&t2,&t3,&a);
            if(a!=0)
            {
                empty=false;
                break;
            }
        }
    }
    SDL_UnlockSurface(img);
    keep_rectangle(0,0,img->w,i+1);
    empty=true;
    SDL_LockSurface(img);
    for(i=img->w-1;i!=INT_MAX&&empty;i--)
    {
        for(j=0;j<img->h;j++)
        {
            Pixel=obtenirPixel(img,i,j);
            SDL_GetRGBA(Pixel,img->format,&t1,&t2,&t3,&a);
            if(a!=0)
            {
                empty=false;
                break;
            }
        }
    }
    SDL_UnlockSurface(img);
    keep_rectangle(0,0,i+1,img->h);
 
}
 
 
 
 
 
 
 
 
 
 
Uint32 getPixel(SDL_Surface *surface, int x, int y)
{
    int bpp = surface->format->BytesPerPixel;
    //Here p is the address to the pixel we want to retrieve
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
 
    switch(bpp) {
    case 1:
        return *p;
 
    case 2:
        return *(Uint16 *)p;
 
    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;
 
    case 4:
        return *(Uint32 *)p;
 
    default:
        return 0; //shouldn't happen, but avoids warnings
    }
}
 
using namespace std;
 
/*bool Image::saveAsPNG(string fileName)
{
    set_alpha_perpixel();
    SDL_Surface*toSave=img;
 
     FILE* file = fopen(fileName.c_str(), "wb");
 
     if(!file)
     {
          cout<<"Impossible d'ouvrir le fichier "<<fileName<<" pour enregistrer le screenshot"<<endl;
          return false;
     }
 
     png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
     if(!png_ptr)
     {
          fclose(file);
          cout<<"Error1";
          return false;
     }
 
     png_infop   png_info = png_create_info_struct(png_ptr);
 
     if(!png_info)
     {
          png_destroy_write_struct(&png_ptr, &png_info);
          fclose(file);
          cout<<"Error2";
          return false;
     }
 
     if(setjmp(png_jmpbuf(png_ptr)))
     {
          png_destroy_write_struct(&png_ptr, &png_info);
          fclose(file);
          cout<<"Error3";
          return false;
     }
 
     //données de l'image
     png_init_io(png_ptr, file);
 
     // type de compression
     png_set_compression_level(png_ptr, Z_BEST_SPEED);
 
     // infos de l'image
     png_set_IHDR(png_ptr, png_info, static_cast<png_uint_32>(toSave->w),
                  static_cast<png_uint_32>(toSave->h), 8,
                  PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
     // date de création
     png_time  modtime;
     png_convert_from_time_t(&modtime, time(NULL));
     png_set_tIME(png_ptr, png_info, &modtime);
 
     // commentaires de l'image
     png_text text[6];
 
     text[0].key = "Title";
     text[0].text = "";
     text[0].compression = PNG_TEXT_COMPRESSION_NONE;
 
     text[1].key = "Author";
     text[1].text = "";
     text[1].compression = PNG_TEXT_COMPRESSION_NONE;
 
     text[2].key = "Description";
     text[2].text = "";
     text[2].compression = PNG_TEXT_COMPRESSION_NONE;
 
     text[3].key = "Copyright";
     text[3].text = "";
     text[3].compression = PNG_TEXT_COMPRESSION_NONE;
 
     text[4].key = "E-mail";
     text[4].text = "";
     text[4].compression = PNG_TEXT_COMPRESSION_NONE;
 
     text[5].key = "Url";
     text[5].text = "";
     text[5].compression = PNG_TEXT_COMPRESSION_NONE;
 
     png_set_text(png_ptr, png_info, text, 6);
 
     // écriture de l'image
     png_write_info(png_ptr, png_info);
 
     png_byte* line;
     line = new png_byte[toSave->w * 4];
     Uint8 r,v,b,a;
     SDL_LockSurface(toSave);
     for(int i = 0; i < toSave->h; ++i)
     {
          for(int p = 0, j = 0; j < toSave->w; ++j)
          {
               // extraction du pixel de la SDL_Surface*
               Uint32 pixel = getPixel(toSave, j, i);
               SDL_GetRGBA(pixel,toSave->format,&r,&v,&b,&a);
               line[p++]=r;
               line[p++]=v;
               line[p++]=b;
               line[p++]=a;
          }
          // écriture de la ligne dans le fichier
          png_write_row(png_ptr, line);
     }
     SDL_UnlockSurface(toSave);
     delete[] line;
 
     png_write_end(png_ptr, png_info);
 
     png_destroy_write_struct(&png_ptr, &png_info);
     fclose(file);
     return true;
}*/
 
void Image::set_alpha_perpixel()
{
    if(img->format->Amask==0)
    {
        if(img->format->alpha==0)
        {
            SDL_Surface*tampon=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,img->format->BitsPerPixel,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
            SDL_BlitSurface(img,NULL,tampon,NULL);
            SDL_FreeSurface(img);
            img=tampon;
        }
        else
        {
            SDL_Surface*tampon=SDL_CreateRGBSurface(SDL_HWSURFACE,img->w,img->h,img->format->BitsPerPixel,MASQUE_ROUGE,MASQUE_VERT,MASQUE_BLEU,MASQUE_ALPHA);
            unsigned int x,y;
            Uint8 r,v,b,a=img->format->alpha;
            Uint32 pixel;
            SDL_LockSurface(img);
            SDL_LockSurface(tampon);
            for(x=0;x<img->w;x++)
            {
                for(y=0;y<img->h;y++)
                {
                    pixel=obtenirPixel(img,x,y);
                    SDL_GetRGB(pixel,img->format,&r,&v,&b);
                    definirPixel(tampon,x,y,SDL_MapRGBA(tampon->format,r,v,b,a));
                }
            }
            SDL_UnlockSurface(img);
            SDL_UnlockSurface(tampon);
            SDL_FreeSurface(img);
            img=tampon;
        }
    }
}
 
 
void Image::set_color(unsigned int x, unsigned int y, Uint32 col)
{
    Uint8 d,b,c,a;
    SDL_GetRGBA(col,img->format,&d,&c,&b,&a);
    if(a>0)
        set_alpha_perpixel();
    definirPixel(img,x,y,col);
}
 
void Image::set_color(unsigned int x, unsigned int y, Uint8 r, Uint8 v, Uint8 b, Uint8 a)
{
    if(a>0)
    {
        set_alpha_perpixel();
        definirPixel(img,x,y,SDL_MapRGBA(img->format,r,v,b,a));
    }
    else
    {
        definirPixel(img,x,y,SDL_MapRGB(img->format,r,v,b));
    }
}
 
 
Uint32 Image::get_color(unsigned int x, unsigned int y)const
{
    return obtenirPixel(img,x,y);
}
 
void Image::get_color(unsigned int x, unsigned int y, Uint8& r, Uint8& v, Uint8& b, Uint8& a)const
{
    Uint32 pixel=obtenirPixel(img,x,y);
    SDL_GetRGBA(pixel,img->format,&r,&v,&b,&a);
}

Merci de me donner vos avis : flexibilité/performances/...
De plus, dans le cas ou je ne créé pas de GodObject, je me retrouve a recopier 3 fois les mêmes fonctions...