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
|
#include <SDL.h>
#include "screen.h"
#include "exceptions.h"
#include "unicode.h"
Screen::Screen()
{
screen = NULL;
mouseImage = NULL;
mouseSave = NULL;
mouseVisible = false;
regionsList = NULL;
maxRegionsList = 0;
}
Screen::~Screen()
{
SDL_SetCursor(cursor);
if (mouseImage) SDL_FreeSurface(mouseImage);
if (mouseSave) SDL_FreeSurface(mouseSave);
if (regionsList) free(regionsList);
}
const VideoMode Screen::getVideoMode() const
{
return VideoMode(screen->w, screen->h, screen->format->BitsPerPixel, fullScreen);
}
void Screen::setMode(const VideoMode& mode)
{
fullScreen = mode.isFullScreen();
int flags = SDL_SWSURFACE /*| SDL_OPENGL*/;
if (fullScreen)
flags = flags | SDL_FULLSCREEN;
screen = SDL_SetVideoMode(mode.getWidth(), mode.getHeight(), mode.getBpp(), flags);
if (! screen)
throw Exception(L"Couldn't set video mode: " +
fromMbcs((SDL_GetError())));
}
std::vector<VideoMode> Screen::getFullScreenModes() const
{
std::vector<VideoMode> modes;
return modes;
}
int Screen::getWidth() const
{
if (screen)
return screen->w;
else
throw Exception(L"No video mode selected");
}
int Screen::getHeight() const
{
if (screen)
return screen->h;
else
throw Exception(L"No video mode selected");
}
void Screen::centerMouse()
{
if (screen)
SDL_WarpMouse(screen->w / 2, screen->h / 2);
else
throw Exception(L"No video mode selected");
}
void Screen::setMouseImage(SDL_Surface *image)
{
if (mouseImage) {
SDL_FreeSurface(mouseImage);
mouseImage = NULL;
}
if (mouseSave) {
SDL_FreeSurface(mouseSave);
mouseSave = NULL;
}
if (! image) return;
mouseImage = SDL_DisplayFormat(image);
if (! mouseImage)
throw Exception(L"Error creating surface");
//mouseSave = SDL_DisplayFormat(image);
mouseSave = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCCOLORKEY,
image->w, image->h, screen->format->BitsPerPixel,
screen->format->Rmask, screen->format->Gmask,
screen->format->Bmask, screen->format->Amask);
if (! mouseSave) {
SDL_FreeSurface(mouseImage);
throw Exception(L"Error creating buffer surface");
}
SDL_SetColorKey(mouseImage, SDL_SRCCOLORKEY,
SDL_MapRGB(mouseImage->format, 0, 0, 0));
}
void Screen::hideMouse()
{
if (! mouseVisible)
return;
if (! niceCursor) {
mouseVisible = false;
return;
}
if (mouseSave) {
SDL_Rect src = { 0, 0, mouseSave->w, mouseSave->h };
SDL_Rect dst = { saveX, saveY, mouseSave->w, mouseSave->h };
if (src.w > 0) {
SDL_BlitSurface(mouseSave, &src, screen, &dst);
addRegionToUpdate(dst.x, dst.y, dst.w, dst.h);
}
}
mouseVisible = false;
}
void Screen::showMouse()
{
if (mouseVisible)
return;
if (! niceCursor) {
mouseVisible = true;
return;
}
if (mouseImage && mouseSave) {
int x, y;
SDL_GetMouseState(&x, &y);
saveX = x;
saveY = y;
SDL_Rect src = { 0, 0, mouseSave->w, mouseSave->h };
SDL_Rect dst = { x, y, mouseImage->w, mouseImage->h };
if (src.w > 0) {
SDL_BlitSurface(screen, &dst, mouseSave, &src);
SDL_BlitSurface(mouseImage, &src, screen, &dst);
addRegionToUpdate(dst.x, dst.y, dst.w, dst.h);
}
}
mouseVisible = true;
}
void Screen::updateMouse()
{
hideMouse();
showMouse();
}
void Screen::flush()
{
if (! regions.size()) return;
if (! regionsList) {
regionsList = (SDL_Rect*)malloc(sizeof(SDL_Rect) * regions.size());
if (! regionsList) {
regions.clear();
throw Exception(L"Error allocating regions buffer");
}
maxRegionsList = regions.size();
} else {
if (maxRegionsList < (int)regions.size()) {
SDL_Rect *r = (SDL_Rect*)realloc(regionsList,
sizeof(SDL_Rect) * regions.size());
if (! r) {
regions.clear();
free(regionsList);
throw Exception(L"Error incrementing regions buffer");
}
regionsList = r;
maxRegionsList = regions.size();
}
}
int j = 0;
for (std::list<SDL_Rect>::iterator i = regions.begin();
i != regions.end(); i++, j++)
regionsList[j] = *i;
SDL_UpdateRects(screen, regions.size(), regionsList);
regions.clear();
}
void Screen::addRegionToUpdate(int x, int y, int w, int h)
{
if (((x >= getWidth()) || (y >= getHeight())) || (0 >= w) || (0 >= h))
return;
if ((x + w < 0) || (y + h < 0))
return;
if (x + w > getWidth())
w = getWidth() - x;
if (y + h > getHeight())
h = getHeight() - y;
if (0 > x) {
w = w + x;
x = 0;
}
if (0 > y) {
h = h + y;
y = 0;
}
SDL_Rect r = { x, y, w, h };
regions.push_back(r);
}
void Screen::setPixel(int x, int y, int r, int g, int b)
{
SDL_LockSurface(screen);
int bpp = screen->format->BytesPerPixel;
Uint32 pixel = SDL_MapRGB(screen->format, r, g, b);
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8*)screen->pixels + y * screen->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
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;
}
SDL_UnlockSurface(screen);
}
void Screen::draw(int x, int y, SDL_Surface *tile)
{
SDL_Rect src = { 0, 0, tile->w, tile->h };
SDL_Rect dst = { x, y, tile->w, tile->h };
SDL_BlitSurface(tile, &src, screen, &dst);
}
void Screen::setCursor(bool nice)
{
if (nice == niceCursor)
return;
bool oldVisible = mouseVisible;
if (mouseVisible)
hideMouse();
niceCursor = nice;
if (niceCursor)
SDL_SetCursor(emptyCursor);
else
SDL_SetCursor(cursor);
if (oldVisible)
showMouse();
}
void Screen::initCursors()
{
cursor = SDL_GetCursor();
Uint8 t = 0;
emptyCursor = SDL_CreateCursor(&t, &t, 8, 1, 0, 0);
}
void Screen::doneCursors()
{
if (niceCursor)
SDL_SetCursor(cursor);
SDL_FreeCursor(emptyCursor);
}
SDL_Surface* Screen::createSubimage(int x, int y, int width, int height)
{
SDL_Surface *s = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCCOLORKEY,
width, height, screen->format->BitsPerPixel,
screen->format->Rmask, screen->format->Gmask,
screen->format->Bmask, screen->format->Amask);
if (! s)
throw Exception(L"Error creating buffer surface");
SDL_Rect src = { x, y, width, height };
SDL_Rect dst = { 0, 0, width, height };
SDL_BlitSurface(screen, &src, s, &dst);
return s;
}
|