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

« back to all changes in this revision

Viewing changes to src/systems/base/mouse_cursor.cc

  • 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) 2008 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
 
 
28
#include "systems/base/mouse_cursor.h"
 
29
 
 
30
#include "systems/base/event_system.h"
 
31
#include "systems/base/graphics_system.h"
 
32
#include "systems/base/surface.h"
 
33
#include "systems/base/system.h"
 
34
 
 
35
const int CURSOR_SIZE_INT = 32;
 
36
const Size CURSOR_SIZE = Size(CURSOR_SIZE_INT, CURSOR_SIZE_INT);
 
37
 
 
38
const int HOTSPOTMASK_X_OFFSET = 8;
 
39
const int HOTSPOTMASK_Y_OFFSET = 48;
 
40
 
 
41
// -----------------------------------------------------------------------
 
42
// MouseCursor (public)
 
43
// -----------------------------------------------------------------------
 
44
MouseCursor::MouseCursor(System& system,
 
45
                         const std::shared_ptr<const Surface>& cursor_surface,
 
46
                         int count,
 
47
                         int speed)
 
48
    : cursor_surface_(cursor_surface),
 
49
      count_(count),
 
50
      frame_speed_(speed / count_),
 
51
      current_frame_(0),
 
52
      last_time_frame_incremented_(system.event().GetTicks()) {
 
53
  // TODO(erg): Technically, each frame might have a hotspot. In practice, the
 
54
  // hotspot is in the same place every frame.
 
55
  FindHotspot();
 
56
 
 
57
  int alphaR, alphaG, alphaB;
 
58
  cursor_surface->GetDCPixel(Point(0, 0), alphaR, alphaG, alphaB);
 
59
 
 
60
  cursor_surface_ = cursor_surface->ClipAsColorMask(
 
61
      Rect(8, 8, Size(CURSOR_SIZE_INT * count_, CURSOR_SIZE_INT)),
 
62
      alphaR,
 
63
      alphaG,
 
64
      alphaB);
 
65
}
 
66
 
 
67
MouseCursor::~MouseCursor() {}
 
68
 
 
69
void MouseCursor::Execute(System& system) {
 
70
  unsigned int cur_time = system.event().GetTicks();
 
71
 
 
72
  if (last_time_frame_incremented_ + frame_speed_ < cur_time) {
 
73
    last_time_frame_incremented_ = cur_time;
 
74
 
 
75
    system.graphics().MarkScreenAsDirty(GUT_MOUSE_MOTION);
 
76
 
 
77
    current_frame_++;
 
78
    if (current_frame_ >= count_)
 
79
      current_frame_ = 0;
 
80
  }
 
81
}
 
82
 
 
83
void MouseCursor::RenderHotspotAt(const Point& mouse_location) {
 
84
  Point render_point = GetTopLeftForHotspotAt(mouse_location);
 
85
  cursor_surface_->RenderToScreen(
 
86
      Rect(current_frame_ * CURSOR_SIZE_INT, 0, CURSOR_SIZE),
 
87
      Rect(render_point, CURSOR_SIZE));
 
88
}
 
89
 
 
90
// -----------------------------------------------------------------------
 
91
// MouseCursor (private)
 
92
// -----------------------------------------------------------------------
 
93
 
 
94
Point MouseCursor::GetTopLeftForHotspotAt(const Point& mouse_location) {
 
95
  return mouse_location - hotspot_offset_;
 
96
}
 
97
 
 
98
void MouseCursor::FindHotspot() {
 
99
  int r, g, b;
 
100
 
 
101
  for (int x = HOTSPOTMASK_X_OFFSET; x < HOTSPOTMASK_X_OFFSET + CURSOR_SIZE_INT;
 
102
       ++x) {
 
103
    for (int y = HOTSPOTMASK_Y_OFFSET;
 
104
         y < HOTSPOTMASK_Y_OFFSET + CURSOR_SIZE_INT;
 
105
         ++y) {
 
106
      cursor_surface_->GetDCPixel(Point(x, y), r, g, b);
 
107
 
 
108
      if (r == 255 && g == 255 && b == 255) {
 
109
        hotspot_offset_ =
 
110
            Size(x - HOTSPOTMASK_X_OFFSET, y - HOTSPOTMASK_Y_OFFSET);
 
111
        return;
 
112
      }
 
113
    }
 
114
  }
 
115
}