00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef SDL_UTILS_INCLUDED
00019 #define SDL_UTILS_INCLUDED
00020
00021 #include "scoped_resource.hpp"
00022 #include "util.hpp"
00023
00024 #include "SDL.h"
00025
00026 #include <cstdlib>
00027 #include <iosfwd>
00028 #include <map>
00029 #include <string>
00030
00031
00032
00033
00034 #ifndef SDL_BUTTON_WHEELUP
00035 #define SDL_BUTTON_WHEELUP 4
00036 #endif
00037
00038 #ifndef SDL_BUTTON_WHEELDOWN
00039 #define SDL_BUTTON_WHEELDOWN 5
00040 #endif
00041
00042 #ifndef SDL_BUTTON_WHEELLEFT
00043 #define SDL_BUTTON_WHEELLEFT 6
00044 #endif
00045
00046 #ifndef SDL_BUTTON_WHEELRIGHT
00047 #define SDL_BUTTON_WHEELRIGHT 7
00048 #endif
00049
00050 namespace {
00051 const SDL_Rect empty_rect = { 0, 0, 0, 0 };
00052 }
00053
00054 SDLKey sdl_keysym_from_name(std::string const &keyname);
00055
00056 bool point_in_rect(int x, int y, const SDL_Rect& rect);
00057 bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2);
00058 SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2);
00059 SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2);
00060
00061
00062
00063
00064
00065
00066
00067 SDL_Rect create_rect(const int x, const int y, const int w, const int h);
00068
00069 struct surface
00070 {
00071 private:
00072 static void sdl_add_ref(SDL_Surface *surf)
00073 {
00074 if (surf != NULL)
00075 ++surf->refcount;
00076 }
00077
00078 struct free_sdl_surface {
00079 void operator()(SDL_Surface *surf) const
00080 {
00081 if (surf != NULL)
00082 SDL_FreeSurface(surf);
00083 }
00084 };
00085
00086 typedef util::scoped_resource<SDL_Surface*,free_sdl_surface> scoped_sdl_surface;
00087 public:
00088 surface() : surface_(NULL)
00089 {}
00090
00091 surface(SDL_Surface *surf) : surface_(surf)
00092 {}
00093
00094 surface(const surface& o) : surface_(o.surface_.get())
00095 {
00096 sdl_add_ref(surface_.get());
00097 }
00098
00099 void assign(const surface& o)
00100 {
00101 SDL_Surface *surf = o.surface_.get();
00102 sdl_add_ref(surf);
00103 surface_.assign(surf);
00104 }
00105
00106 surface& operator=(const surface& o)
00107 {
00108 assign(o);
00109 return *this;
00110 }
00111
00112 operator SDL_Surface*() const { return surface_.get(); }
00113
00114 SDL_Surface* get() const { return surface_.get(); }
00115
00116 SDL_Surface* operator->() const { return surface_.get(); }
00117
00118 void assign(SDL_Surface* surf) { surface_.assign(surf); }
00119
00120 bool null() const { return surface_.get() == NULL; }
00121
00122 private:
00123 scoped_sdl_surface surface_;
00124 };
00125
00126 bool operator<(const surface& a, const surface& b);
00127
00128 inline void sdl_blit(const surface& src, SDL_Rect* src_rect, surface& dst, SDL_Rect* dst_rect){
00129 SDL_BlitSurface(src, src_rect, dst, dst_rect);
00130 }
00131
00132 inline void sdl_fill_rect(surface& dst, SDL_Rect* dst_rect, const Uint32 color){
00133 SDL_FillRect(dst, dst_rect, color);
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 bool is_neutral(const surface& surf);
00146
00147 surface make_neutral_surface(const surface &surf);
00148 surface create_neutral_surface(int w, int h);
00149 surface create_optimized_surface(const surface &surf);
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 surface stretch_surface_horizontal(
00168 const surface& surf, const unsigned w, const bool optimize = true);
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 surface stretch_surface_vertical(
00187 const surface& surf, const unsigned h, const bool optimize = true);
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 surface scale_surface(const surface &surf, int w, int h, bool optimize=true);
00200
00201 surface adjust_surface_color(const surface &surf, int r, int g, int b, bool optimize=true);
00202 surface greyscale_image(const surface &surf, bool optimize=true);
00203
00204 surface shadow_image(const surface &surf, bool optimize=true);
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 surface recolor_image(surface surf, const std::map<Uint32, Uint32>& map_rgb,
00220 bool optimize=true);
00221
00222 surface brighten_image(const surface &surf, fixed_t amount, bool optimize=true);
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 surface get_surface_portion(const surface &surf, SDL_Rect &rect,
00236 bool optimize_format=false);
00237
00238 surface adjust_surface_alpha(const surface &surf, fixed_t amount, bool optimize=true);
00239 surface adjust_surface_alpha_add(const surface &surf, int amount, bool optimize=true);
00240
00241
00242 surface mask_surface(const surface &surf, const surface &mask, bool* empty_result = NULL, const std::string& filename = std::string());
00243
00244
00245 bool in_mask_surface(const surface &surf, const surface &mask);
00246
00247
00248
00249
00250
00251
00252
00253
00254 surface submerge_alpha(const surface &surf, int depth, float alpha_base, float alpha_delta, bool optimize=true);
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 surface light_surface(const surface &surf, const surface &lightmap, bool optimize=true);
00266
00267
00268 surface blur_surface(const surface &surf, int depth = 1, bool optimize=true);
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 void blur_surface(surface& surf, SDL_Rect rect, unsigned depth = 1);
00279
00280
00281
00282
00283
00284
00285
00286 surface blur_alpha_surface(const surface &surf, int depth = 1, bool optimize=true);
00287
00288
00289 surface cut_surface(const surface &surf, SDL_Rect const &r);
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 surface blend_surface(
00308 const surface &surf
00309 , const double amount
00310 , const Uint32 color
00311 , const bool optimize = true);
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 surface rotate_180_surface(const surface &surf, bool optimize=true);
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 surface rotate_90_surface(const surface &surf, bool clockwise, bool optimize=true);
00333
00334 surface flip_surface(const surface &surf, bool optimize=true);
00335 surface flop_surface(const surface &surf, bool optimize=true);
00336 surface create_compatible_surface(const surface &surf, int width = -1, int height = -1);
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 void blit_surface(const surface& src,
00357 const SDL_Rect* srcrect, surface& dst, const SDL_Rect* dstrect);
00358
00359 void fill_rect_alpha(SDL_Rect &rect, Uint32 color, Uint8 alpha, surface &target);
00360
00361 SDL_Rect get_non_transparent_portion(const surface &surf);
00362
00363 bool operator==(const SDL_Rect& a, const SDL_Rect& b);
00364 bool operator!=(const SDL_Rect& a, const SDL_Rect& b);
00365 bool operator==(const SDL_Color& a, const SDL_Color& b);
00366 bool operator!=(const SDL_Color& a, const SDL_Color& b);
00367 SDL_Color inverse(const SDL_Color& color);
00368 SDL_Color int_to_color(const Uint32 rgb);
00369
00370 SDL_Color create_color(const unsigned char red
00371 , unsigned char green
00372 , unsigned char blue
00373 , unsigned char unused = 255);
00374
00375
00376
00377
00378
00379
00380 struct surface_lock
00381 {
00382 surface_lock(surface &surf);
00383 ~surface_lock();
00384
00385 Uint32* pixels() { return reinterpret_cast<Uint32*>(surface_->pixels); }
00386 private:
00387 surface& surface_;
00388 bool locked_;
00389 };
00390
00391 struct const_surface_lock
00392 {
00393 const_surface_lock(const surface &surf);
00394 ~const_surface_lock();
00395
00396 const Uint32* pixels() const { return reinterpret_cast<const Uint32*>(surface_->pixels); }
00397 private:
00398 const surface& surface_;
00399 bool locked_;
00400 };
00401
00402 struct surface_restorer
00403 {
00404 surface_restorer();
00405 surface_restorer(class CVideo* target, const SDL_Rect& rect);
00406 ~surface_restorer();
00407
00408 void restore() const;
00409 void restore(SDL_Rect const &dst) const;
00410 void update();
00411 void cancel();
00412
00413 const SDL_Rect& area() const { return rect_; }
00414
00415 private:
00416 class CVideo* target_;
00417 SDL_Rect rect_;
00418 surface surface_;
00419 };
00420
00421 struct clip_rect_setter
00422 {
00423
00424 clip_rect_setter(const surface &surf, const SDL_Rect* r, bool operate = true) : surface_(surf), rect_(), operate_(operate)
00425 {
00426 if(operate_){
00427 SDL_GetClipRect(surface_, &rect_);
00428 SDL_SetClipRect(surface_, r);
00429 }
00430 }
00431
00432 ~clip_rect_setter() {
00433 if (operate_)
00434 SDL_SetClipRect(surface_, &rect_);
00435 }
00436
00437 private:
00438 surface surface_;
00439 SDL_Rect rect_;
00440 const bool operate_;
00441 };
00442
00443
00444 void draw_rectangle(int x, int y, int w, int h, Uint32 color, surface tg);
00445
00446 void draw_solid_tinted_rectangle(int x, int y, int w, int h,
00447 int r, int g, int b,
00448 double alpha, surface target);
00449
00450
00451
00452 void draw_centered_on_background(surface surf, const SDL_Rect& rect,
00453 const SDL_Color& color, surface target);
00454
00455 std::ostream& operator<<(std::ostream& s, const SDL_Rect& rect);
00456
00457 #endif