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) 2007 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/text_key_cursor.h"
29 
30 #include <string>
31 #include <vector>
32 
33 #include "libreallive/gameexe.h"
34 #include "systems/base/event_system.h"
35 #include "systems/base/graphics_system.h"
36 #include "systems/base/surface.h"
37 #include "systems/base/system.h"
38 #include "systems/base/text_window.h"
39 
40 using std::endl;
41 using std::string;
42 using std::vector;
43 
44 // -----------------------------------------------------------------------
45 // TextKeyCursor
46 // -----------------------------------------------------------------------
TextKeyCursor(System & system,int in_curosr_number)47 TextKeyCursor::TextKeyCursor(System& system, int in_curosr_number)
48     : cursor_number_(in_curosr_number),
49       current_frame_(0),
50       last_time_frame_incremented_(system.event().GetTicks()),
51       system_(system) {
52   Gameexe& gexe = system.gameexe();
53   GameexeInterpretObject cursor = gexe("CURSOR", in_curosr_number);
54 
55   if (cursor("NAME").Exists()) {
56     SetCursorImage(system, cursor("NAME"));
57     SetCursorSize(cursor("SIZE"));
58     SetCursorFrameCount(cursor("CONT"));
59     SetCursorFrameSpeed(cursor("SPEED"));
60   }
61 }
62 
63 // -----------------------------------------------------------------------
64 
~TextKeyCursor()65 TextKeyCursor::~TextKeyCursor() {}
66 
67 // -----------------------------------------------------------------------
68 
Execute()69 void TextKeyCursor::Execute() {
70   unsigned int cur_time = system_.event().GetTicks();
71 
72   if (cursor_image_ && last_time_frame_incremented_ + frame_speed_ < cur_time) {
73     last_time_frame_incremented_ = cur_time;
74 
75     system_.graphics().MarkScreenAsDirty(GUT_TEXTSYS);
76 
77     current_frame_++;
78     if (current_frame_ >= frame_count_)
79       current_frame_ = 0;
80   }
81 }
82 
83 // -----------------------------------------------------------------------
84 
Render(TextWindow & text_window,std::ostream * tree)85 void TextKeyCursor::Render(TextWindow& text_window, std::ostream* tree) {
86   if (cursor_image_) {
87     // Get the location to render from text_window
88     Point keycur = text_window.KeycursorPosition(frame_size_);
89 
90     cursor_image_->RenderToScreen(
91         Rect(Point(current_frame_ * frame_size_.width(), 0), frame_size_),
92         Rect(keycur, frame_size_),
93         255);
94 
95     if (tree) {
96       *tree << "  Key Cursor #" << cursor_number_ << endl
97             << "    Cursor name: " << cursor_image_file_ << endl
98             << "    Cursor location: " << Rect(keycur, frame_size_) << endl;
99     }
100   }
101 }
102 
103 // -----------------------------------------------------------------------
104 
SetCursorImage(System & system,const std::string & name)105 void TextKeyCursor::SetCursorImage(System& system, const std::string& name) {
106   if (name != "") {
107     cursor_image_ = system.graphics().GetSurfaceNamed(name);
108     cursor_image_file_ = name;
109   } else {
110     cursor_image_.reset();
111     cursor_image_file_ = "";
112   }
113 }
114 
115 // -----------------------------------------------------------------------
116 
SetCursorSize(const std::vector<int> & image_size)117 void TextKeyCursor::SetCursorSize(const std::vector<int>& image_size) {
118   frame_size_ = Size(image_size.at(0), image_size.at(1));
119 }
120 
121 // -----------------------------------------------------------------------
122 
SetCursorFrameCount(const int frame_count)123 void TextKeyCursor::SetCursorFrameCount(const int frame_count) {
124   frame_count_ = frame_count;
125 }
126 
127 // -----------------------------------------------------------------------
128 
SetCursorFrameSpeed(const int speed)129 void TextKeyCursor::SetCursorFrameSpeed(const int speed) {
130   if (frame_count_)
131     frame_speed_ = speed / frame_count_;
132   else
133     frame_speed_ = speed;
134 }
135