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
|
/*
Copyright (C) 2007 by David White <dave@whitevine.net>
Part of the Silver Tree Project
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 or later.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef SURFACE_HPP_INCLUDED
#define SURFACE_HPP_INCLUDED
#include <iostream>
#include "graphics.hpp"
#include "scoped_resource.hpp"
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define SURFACE_MASK 0xFF,0xFF00,0xFF0000,0xFF000000
#define SURFACE_MASK_RGB 0xFF,0xFF00,0xFF0000,0x0
#else
#define SURFACE_MASK 0xFF000000,0xFF0000,0xFF00,0xFF
#define SURFACE_MASK_RGB 0xFF0000,0xFF00,0xFF,0x0
#endif
namespace graphics
{
struct surface
{
private:
static void sdl_add_ref(SDL_Surface *surf)
{
if (surf != NULL)
++surf->refcount;
}
struct free_sdl_surface {
void operator()(SDL_Surface *surf) const
{
if (surf != NULL) {
SDL_FreeSurface(surf);
}
}
};
typedef util::scoped_resource<SDL_Surface*,free_sdl_surface> scoped_sdl_surface;
public:
surface() : surface_(NULL)
{}
surface(SDL_Surface *surf) : surface_(surf)
{
}
surface(const surface& o) : surface_(o.surface_.get())
{
sdl_add_ref(surface_.get());
}
static surface create(int w, int h);
void assign(const surface& o)
{
SDL_Surface *surf = o.surface_.get();
sdl_add_ref(surf); // need to be done before assign to avoid corruption on "a=a;"
surface_.assign(surf);
}
surface& operator=(const surface& o)
{
assign(o);
return *this;
}
operator SDL_Surface*() const { return surface_.get(); }
SDL_Surface* get() const { return surface_.get(); }
SDL_Surface* operator->() const { return surface_.get(); }
void assign(SDL_Surface* surf) { surface_.assign(surf); }
bool null() const { return surface_.get() == NULL; }
surface convert_opengl_format() const;
surface clone() const;
private:
scoped_sdl_surface surface_;
};
inline bool operator==(const surface& a, const surface& b)
{
return a.get() == b.get();
}
inline bool operator<(const surface& a, const surface& b)
{
return a.get() < b.get();
}
}
#endif
|