~ubuntu-branches/ubuntu/vivid/rlvm/vivid

« back to all changes in this revision

Viewing changes to src/Utilities/Graphics.cpp

  • Committer: Package Import Robot
  • Author(s): Ying-Chun Liu (PaulLiu)
  • Date: 2014-10-22 03:24:19 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20141022032419-yqxls9ky4n1w811n
Tags: 0.14-1
* New upstream release
* Bump Standards-Version to 3.9.6: nothing needs to be changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
 
// vi:tw=80:et:ts=2:sts=2
3
 
//
4
 
// -----------------------------------------------------------------------
5
 
//
6
 
// This file is part of RLVM, a RealLive virtual machine clone.
7
 
//
8
 
// -----------------------------------------------------------------------
9
 
//
10
 
// Copyright (C) 2009 Elliot Glaysher
11
 
//
12
 
// This program is free software; you can redistribute it and/or modify
13
 
// it under the terms of the GNU General Public License as published by
14
 
// the Free Software Foundation; either version 3 of the License, or
15
 
// (at your option) any later version.
16
 
//
17
 
// This program is distributed in the hope that it will be useful,
18
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
// GNU General Public License for more details.
21
 
//
22
 
// You should have received a copy of the GNU General Public License
23
 
// along with this program; if not, write to the Free Software
24
 
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25
 
// -----------------------------------------------------------------------
26
 
 
27
 
#include "Utilities/Graphics.hpp"
28
 
 
29
 
#include <iostream>
30
 
#include <sstream>
31
 
#include <vector>
32
 
#include <boost/assign/list_of.hpp>
33
 
 
34
 
#include "MachineBase/RLMachine.hpp"
35
 
#include "Systems/Base/Rect.hpp"
36
 
#include "Systems/Base/System.hpp"
37
 
#include "Systems/Base/SystemError.hpp"
38
 
#include "libReallive/gameexe.h"
39
 
 
40
 
using namespace std;
41
 
using namespace boost::assign;
42
 
 
43
 
std::vector<int> getSELEffect(RLMachine& machine, int selNum) {
44
 
  Gameexe& gexe = machine.system().gameexe();
45
 
  vector<int> selEffect;
46
 
 
47
 
  if (gexe("SEL", selNum).exists()) {
48
 
    selEffect = gexe("SEL", selNum).to_intVector();
49
 
    grpToRecCoordinates(selEffect[0], selEffect[1],
50
 
                        selEffect[2], selEffect[3]);
51
 
  } else if (gexe("SELR", selNum).exists()) {
52
 
    selEffect = gexe("SELR", selNum).to_intVector();
53
 
  } else {
54
 
    // Can't find the specified #SEL effect. See if there's a #SEL.000 effect:
55
 
    if (gexe("SEL", 0).exists()) {
56
 
      selEffect = gexe("SEL", 0).to_intVector();
57
 
      grpToRecCoordinates(selEffect[0], selEffect[1],
58
 
                          selEffect[2], selEffect[3]);
59
 
    } else if (gexe("SELR", 0).exists()) {
60
 
      selEffect = gexe("SELR", 0).to_intVector();
61
 
    } else {
62
 
      // Crap! Couldn't fall back on the default one either, so instead return
63
 
      // a SEL vector that is a screenwide, short fade because we absolutely
64
 
      // can't fail here.
65
 
      Size screen = getScreenSize(gexe);
66
 
      selEffect = list_of(0)(0)(screen.width())(screen.height())(0)(0)(1000)
67
 
                  (000)(0)(0)(0)(0)(0)(0)(255)(0);
68
 
    }
69
 
  }
70
 
 
71
 
  return selEffect;
72
 
}
73
 
 
74
 
void getSELPointAndRect(RLMachine& machine, int selNum, Rect& rect,
75
 
                        Point& point) {
76
 
  vector<int> selEffect = getSELEffect(machine, selNum);
77
 
  rect = Rect::REC(selEffect[0], selEffect[1], selEffect[2], selEffect[3]);
78
 
  point = Point(selEffect[4], selEffect[5]);
79
 
}
80
 
 
81
 
Size getScreenSize(Gameexe& gameexe) {
82
 
  std::vector<int> graphicsMode = gameexe("SCREENSIZE_MOD");
83
 
  if (graphicsMode.size()) {
84
 
    if (graphicsMode[0] == 0) {
85
 
      return Size(640, 480);
86
 
    } else if (graphicsMode[0] == 1) {
87
 
      return Size(800, 600);
88
 
    } else if (graphicsMode[0] == 999 && graphicsMode.size() >= 3) {
89
 
      return Size(graphicsMode[1], graphicsMode[2]);
90
 
    } else {
91
 
      ostringstream oss;
92
 
      oss << "Illegal #SCREENSIZE_MOD value: " << graphicsMode[0] << endl;
93
 
      throw SystemError(oss.str());
94
 
    }
95
 
  }
96
 
 
97
 
  ostringstream oss;
98
 
  oss << "Missing #SCREENSIZE_MOD key";
99
 
  throw SystemError(oss.str());
100
 
}
101
 
 
102
 
void clamp(float& var, float min, float max) {
103
 
  if (var < min)
104
 
    var = min;
105
 
  else if (var > max)
106
 
    var = max;
107
 
}
108
 
 
109
 
void ClipDestination(const Rect& clip_rect, Rect& src, Rect& dest) {
110
 
  Rect intersection = clip_rect.intersection(dest);
111
 
  if (intersection.size().isEmpty()) {
112
 
    src = Rect();
113
 
    dest = Rect();
114
 
    return;
115
 
  }
116
 
 
117
 
  // TODO(erg): Doesn't deal with clipping the right side because we don't
118
 
  // really want an intersection here; we want it clipped to.
119
 
 
120
 
  if (src.size() == dest.size()) {
121
 
    Size top_left_offset = intersection.origin() - dest.origin();
122
 
    dest = intersection;
123
 
    src = Rect(src.origin() + top_left_offset, intersection.size());
124
 
  } else {
125
 
    cerr << "Doesn't deal with different sizes in ClipDestination!" << endl;
126
 
  }
127
 
}