~ubuntu-branches/ubuntu/lucid/balder2d/lucid

« back to all changes in this revision

Viewing changes to src/menu/samplemapwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bjørn Hansen
  • Date: 2006-10-14 15:33:42 UTC
  • Revision ID: james.westby@ubuntu.com-20061014153342-un0jglolcs01gcow
Tags: upstream-1.0~rc1
Import upstream version 1.0~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2006 by Bjorn Hansen                                    *
 
3
 *   holomorph@users.sourceforge.net                                       *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
 ***************************************************************************/
 
20
 
 
21
//#include <guichan/rectangle.hpp>
 
22
//#include <guichan/widgets/icon.hpp>
 
23
#include <guichan/sdl/sdlgraphics.hpp>
 
24
#include <SDL/SDL.h>
 
25
#include <SDL/SDL_image.h>
 
26
#include <SDL/SDL_rotozoom.h>
 
27
#include "../../include/menu/samplemapwidget.h"
 
28
 
 
29
using namespace Balder;
 
30
 
 
31
SampleMapWidget::SampleMapWidget(std::string iniMapName, unsigned int X, unsigned int Y, unsigned int W, unsigned int H)
 
32
{
 
33
    setX(X);
 
34
    setY(Y);
 
35
    setWidth(W);
 
36
    setHeight(H);
 
37
    setSampleMap(iniMapName);
 
38
}
 
39
 
 
40
SampleMapWidget::~SampleMapWidget()
 
41
{
 
42
    if (map != 0) {
 
43
        SDL_FreeSurface(map);
 
44
    }
 
45
}
 
46
 
 
47
void SampleMapWidget::setSampleMap(std::string name)
 
48
{
 
49
    // load map background.
 
50
    std::string mapFileName = "maps/" + name + "/background.png";
 
51
    SDL_Surface* tempSurf = IMG_Load(mapFileName.c_str());
 
52
    if (tempSurf == 0) {
 
53
        throw "could not load sample background!";
 
54
    }
 
55
 
 
56
    // get percentage to scale map size down to based off of widget size.
 
57
    double scaledX;
 
58
    double scaledY;
 
59
    double scaled;
 
60
    double src_pix_perc;
 
61
    double trg_pix_perc;
 
62
 
 
63
    src_pix_perc = tempSurf->w; // must convert int to double before preforming division to allow for floating point.
 
64
    src_pix_perc /= 100;
 
65
    trg_pix_perc = getWidth(); // must convert int to double before preforming division to allow for floating point.
 
66
    trg_pix_perc /= 100;
 
67
 
 
68
    scaledX = trg_pix_perc / src_pix_perc;
 
69
 
 
70
    src_pix_perc = tempSurf->h; // must convert int to double before preforming division to allow for floating point.
 
71
    src_pix_perc /= 100;
 
72
    trg_pix_perc = getHeight(); // must convert int to double before preforming division to allow for floating point.
 
73
    trg_pix_perc /= 100;
 
74
 
 
75
    scaledY = trg_pix_perc / src_pix_perc;
 
76
 
 
77
    if (scaledX < scaledY) scaled = scaledX;
 
78
    else scaled = scaledY;
 
79
 
 
80
    // scale down map to widget size.
 
81
        //zoom to rect.
 
82
    map = zoomSurface(tempSurf, scaled, scaled, /* anti-aliasing SMOOTHING_OFF=0 SMOOTHING_ON=1 */SMOOTHING_OFF);
 
83
}
 
84
 
 
85
void SampleMapWidget::draw(gcn::Graphics* graphics)
 
86
{
 
87
    // get screen that the menu is drawn on.
 
88
    SDL_Surface *screen = dynamic_cast<gcn::SDLGraphics*>(graphics)->getTarget();
 
89
 
 
90
    // get location of the widget on the menu screen.
 
91
    int x,y;
 
92
    gcn::Widget::getAbsolutePosition(x,y);
 
93
 
 
94
    // draw widget onto menu screen at widget location.
 
95
    SDL_Rect rectSource = {0, 0, getWidth(), getHeight()};
 
96
    SDL_Rect rectDestination = {x+((getWidth()-map->w)/2), y+((getHeight()-map->h)/2), /*width is ignored*/0, /*height is ignored*/0};
 
97
    SDL_BlitSurface(map, &rectSource, screen, &rectDestination);
 
98
}
 
99