Initial commit. Not even a game yet :P
[cToan.git] / src / SDLWrap_Surface.hpp
blobd7d4215b5d4f6c45932cd6559616aeb9bdb38981
1 #ifndef _SDLWRAP_SURFACE_H_
2 #define _SDLWRAP_SURFACE_H_
4 #include <string>
6 #include "SDL/SDL.h"
8 #include "ErrorHandler.hpp"
10 namespace SDLWrap {
12 /* This class acts as a wrapper around the SDL_Surface structure and defines the functions you would normally use with
13 * SDL_Surfaces such as SDL_BlitSurface() and also provides useful shortcuts like a constructor that takes a BMP file
14 * path.
16 class Surface
18 public:
19 /* When caught it creates an error std::string which it sends to the Error Handler with the
20 * FATAL_CONSTRUCTOR_ERROR flag, because the lack of a BMP file means the SDL_Surface pointer is set to NULL,
21 * and you can't blit a NULL surface can you :P ? Also, the game will be using any images it loads, there is
22 * no substitute for a BMP file. If it can't be used/found, then what's the point, you might not see something
23 * important/useful. The game needs them.
25 class SurfaceCreationError {};
27 Surface() { this->me = NULL; } // TODO: In here or in .cpp or initializer list???
29 /* This constructor constructs a Surface object and uses SDL_LoadBMP to make the Surface hold a bitmap image.
30 * If the bitmap file fails to load for whatever reason, be it a non existant file or not a BMP file or
31 * whatever, the constructor will throw a SurfaceCreationError.
33 * The image is originally held in a temporary SDL_Surface, and if the load succeeds then it is copied into
34 * the class' private SDL_Surface with a call to SDL_DisplayFormat to ensure the surface has the same format as
35 * the display surface. This way blitting is done as fast as possible since SDL does not have to convert the
36 * surface every time it wants to blit it.
38 * BmpFilePath is the file path to a bitmap image file.
40 * TODO: The function currently uses SDL_DisplayFormat() which works on 24-bit images and since pretty much all
41 * graphics cards these days deal with 32-bit (alpha channel support), using SDL_DisplayFormatAlpha()
42 * would be faster. Maybe I should switch to using that?
44 Surface(const std::string BMPFilePath);
46 /* Destructs a Surface object and frees it.
47 * That's really all it does... frees the surface.
49 ~Surface();
51 /* This entire surface gets blitted onto Destination. If this surface is larger than the destinated surace,
52 * then clipping will occur, as usual removing the bottom-most and right-most parts of the image.
54 * Destination is the Address of the surface on which this surface is to be blitted on.
55 * The function returns 0 on success, otherwise -1. If any of the two surfaces were in video memory, it returns
56 * -2, but the Blit() function takes care of that by reloading the surface and re-blitting.
58 * And... that was the SDL_BlitSurface() function heh. As for Blit() here, it returns a bool to signal success
59 * or failure. Probably not gonna use it for anything but debug purposes though...
61 * TODO: Since there will be a lot of repeated code, probably think of letting all Blit()'s call a single Blit()
63 bool Blit(const Surface &Destination);
65 /* Blits the Surface onto Destination at co-ordinates (X,Y). */
66 bool Blit(const Surface &Destination, int DestXOffset, int DestYOffset);
68 public:
69 SDL_Surface *me; // Declared protected so it can be inherited by the Display and FontSurface classes.
71 private:
72 bool RealBlit(const Surface &Destination, SDL_Rect *SourceRect, SDL_Rect *DestRect); // Used internally.
73 std::string FilePath; // Maybe remove as it'll be stored somewhere else and you'll end up with TONS of them when creating bullets.
78 #endif