wesnoth/sdl_utils.cpp
Dave White 5fd5346808 Allow resolution to be configurable in preferences file...
...using xresolution and yresolution options
2003-09-18 12:47:17 +00:00

70 lines
2.1 KiB
C++

/* $Id$ */
/*
Copyright (C) 2003 by David White <davidnwhite@optusnet.com.au>
Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "sdl_utils.hpp"
SDL_Surface* scale_surface(SDL_Surface* surface, int w, int h)
{
SDL_Surface* const dest = SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,
surface->format->BitsPerPixel,
surface->format->Rmask,
surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask);
if(dest == NULL)
return NULL;
const double xratio = static_cast<double>(surface->w)/
static_cast<double>(w);
const double yratio = static_cast<double>(surface->h)/
static_cast<double>(h);
const int srcxpad = (surface->w%2) == 1 ? 1:0;
const int dstxpad = (dest->w%2) == 1 ? 1:0;
double ysrc = 0.0;
for(int ydst = 0; ydst != h; ++ydst, ysrc += yratio) {
double xsrc = 0.0;
for(int xdst = 0; xdst != w; ++xdst, xsrc += xratio) {
int xsrcint = static_cast<int>(xsrc);
const int ysrcint = static_cast<int>(ysrc);
xsrcint += srcxpad*ysrcint;
const int dstpad = dstxpad*ydst;
reinterpret_cast<short*>(dest->pixels)[ydst*w + xdst + dstpad] =
reinterpret_cast<short*>(surface->pixels)[
ysrcint*surface->w + xsrcint];
}
}
return dest;
}
SDL_Surface* get_surface_portion(SDL_Surface* src, SDL_Rect& area)
{
if(area.x + area.w >= src->w || area.y + area.h >= src->h)
return NULL;
const SDL_PixelFormat* const fmt = src->format;
SDL_Surface* const dst = SDL_CreateRGBSurface(0,area.w,area.h,
fmt->BitsPerPixel,fmt->Rmask,
fmt->Gmask,fmt->Bmask,
fmt->Amask);
SDL_Rect dstarea = {0,0,0,0};
SDL_BlitSurface(src,&area,dst,&dstarea);
return dst;
}