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) 2006 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 "modules/module_obj_management.h"
29 
30 #include "machine/general_operations.h"
31 #include "machine/properties.h"
32 #include "machine/rloperation.h"
33 #include "machine/rlmachine.h"
34 #include "machine/rlmodule.h"
35 #include "modules/module_obj.h"
36 #include "systems/base/graphics_object.h"
37 #include "systems/base/graphics_system.h"
38 #include "systems/base/parent_graphics_object_data.h"
39 #include "systems/base/system.h"
40 
41 // -----------------------------------------------------------------------
42 
43 namespace {
44 
45 struct objCopyFgToBg_0 : public RLOpcode<IntConstant_T> {
operator ()__anon3111238a0111::objCopyFgToBg_046   void operator()(RLMachine& machine, int buf) {
47     GraphicsSystem& sys = machine.system().graphics();
48     GraphicsObject& go = sys.GetObject(OBJ_FG, buf);
49     sys.SetObject(OBJ_BG, buf, go);
50   }
51 };
52 
53 struct objCopyFgToBg_1 : public RLOpcode<IntConstant_T, IntConstant_T> {
operator ()__anon3111238a0111::objCopyFgToBg_154   void operator()(RLMachine& machine, int start, int end) {
55     GraphicsSystem& sys = machine.system().graphics();
56 
57     for (int i = start; i <= end; ++i) {
58       GraphicsObject& go = sys.GetObject(OBJ_FG, i);
59       sys.SetObject(OBJ_BG, i, go);
60     }
61   }
62 };
63 
64 struct objCopy : public RLOpcode<IntConstant_T, IntConstant_T> {
65   int from_fgbg_, to_fgbg_;
objCopy__anon3111238a0111::objCopy66   objCopy(int from, int to) : from_fgbg_(from), to_fgbg_(to) {}
67 
operator ()__anon3111238a0111::objCopy68   void operator()(RLMachine& machine, int sbuf, int dbuf) {
69     GraphicsSystem& sys = machine.system().graphics();
70     GraphicsObject& go = sys.GetObject(from_fgbg_, sbuf);
71     sys.SetObject(to_fgbg_, dbuf, go);
72   }
73 };
74 
75 struct SetWipeCopyTo_0 : public RLOpcode<IntConstant_T> {
76   int val_;
SetWipeCopyTo_0__anon3111238a0111::SetWipeCopyTo_077   explicit SetWipeCopyTo_0(int value) : val_(value) {}
78 
operator ()__anon3111238a0111::SetWipeCopyTo_079   void operator()(RLMachine& machine, int buf) {
80     GetGraphicsObject(machine, this, buf).SetWipeCopy(val_);
81   }
82 };
83 
84 struct SetWipeCopyTo_1 : public RLOpcode<IntConstant_T, IntConstant_T> {
85   int val_;
SetWipeCopyTo_1__anon3111238a0111::SetWipeCopyTo_186   explicit SetWipeCopyTo_1(int value) : val_(value) {}
87 
operator ()__anon3111238a0111::SetWipeCopyTo_188   void operator()(RLMachine& machine, int min, int numObjsToSet) {
89     int maxObj = min + numObjsToSet;
90     for (int i = min; i < maxObj; ++i) {
91       GetGraphicsObject(machine, this, i).SetWipeCopy(val_);
92     }
93   }
94 };
95 
96 struct objFreeAll : public RLOpcode<> {
operator ()__anon3111238a0111::objFreeAll97   virtual void operator()(RLMachine& machine) override {
98     LazyArray<GraphicsObject>& objects = GetGraphicsObjects(machine, this);
99     for (GraphicsObject& object : objects)
100       object.FreeObjectData();
101   }
102 };
103 
104 struct objInitAll : public RLOpcode<> {
operator ()__anon3111238a0111::objInitAll105   virtual void operator()(RLMachine& machine) override {
106     LazyArray<GraphicsObject>& objects = GetGraphicsObjects(machine, this);
107     for (GraphicsObject& object : objects)
108       object.InitializeParams();
109   }
110 };
111 
112 struct objFreeInitAll : public RLOpcode<> {
operator ()__anon3111238a0111::objFreeInitAll113   virtual void operator()(RLMachine& machine) override {
114     LazyArray<GraphicsObject>& objects = GetGraphicsObjects(machine, this);
115     for (GraphicsObject& object : objects) {
116       object.FreeObjectData();
117       object.InitializeParams();
118     }
119   }
120 };
121 
addObjManagementFunctions(RLModule & m,const std::string & base)122 void addObjManagementFunctions(RLModule& m, const std::string& base) {
123   m.AddOpcode(0, 0, base + "Free",
124               new Obj_CallFunction(&GraphicsObject::FreeObjectData));
125   m.AddOpcode(0, 1, base + "Free", RangeMappingFun(
126       new Obj_CallFunction(&GraphicsObject::FreeObjectData)));
127 
128   m.AddOpcode(4, 0, base + "WipeCopyOn", new SetWipeCopyTo_0(1));
129   m.AddOpcode(4, 1, base + "WipeCopyOn", new SetWipeCopyTo_1(1));
130   m.AddOpcode(5, 0, base + "WipeCopyOff", new SetWipeCopyTo_0(0));
131   m.AddOpcode(5, 1, base + "WipeCopyOff", new SetWipeCopyTo_1(0));
132 
133   m.AddOpcode(10, 0, base + "Init",
134               new Obj_CallFunction(&GraphicsObject::InitializeParams));
135   m.AddOpcode(10, 1, base + "Init", RangeMappingFun(
136       new Obj_CallFunction(&GraphicsObject::InitializeParams)));
137   m.AddOpcode(11, 0, base + "FreeInit", new Obj_CallFunction(
138       &GraphicsObject::FreeDataAndInitializeParams));
139   m.AddOpcode(11, 1, base + "FreeInit", RangeMappingFun(new Obj_CallFunction(
140       &GraphicsObject::FreeDataAndInitializeParams)));
141 
142   m.AddOpcode(100, 0, base + "FreeAll", new objFreeAll);
143   m.AddOpcode(110, 0, base + "InitAll", new objInitAll);
144   m.AddOpcode(111, 0, base + "FreeInitAll", new objFreeInitAll);
145 }
146 
147 struct objChildCopy : public RLOpcode<IntConstant_T, IntConstant_T> {
148   int fgbg_;
objChildCopy__anon3111238a0111::objChildCopy149   explicit objChildCopy(int fgbg) : fgbg_(fgbg) {}
150 
operator ()__anon3111238a0111::objChildCopy151   void operator()(RLMachine& machine, int sbuf, int dbuf) {
152     GraphicsSystem& sys = machine.system().graphics();
153 
154     // By the time we enter this method, our parameters have already been
155     // tampered with by the ChildObjAdaptor. So use P_PARENTOBJ as our toplevel
156     // object.
157     int parentobj;
158     if (GetProperty(P_PARENTOBJ, parentobj)) {
159       GraphicsObject& go = sys.GetObject(fgbg_, parentobj);
160       EnsureIsParentObject(go, sys.GetObjectLayerSize());
161 
162       // Pick out the object data.
163       ParentGraphicsObjectData& parent =
164           static_cast<ParentGraphicsObjectData&>(go.GetObjectData());
165 
166       GraphicsObject& src_obj = parent.GetObject(sbuf);
167       parent.SetObject(dbuf, src_obj);
168     }
169   }
170 };
171 
172 struct objFreeInit : public RLOpcode<IntConstant_T> {
operator ()__anon3111238a0111::objFreeInit173   virtual void operator()(RLMachine& machine, int buf) {
174     GraphicsSystem& sys = machine.system().graphics();
175     sys.FreeObjectData(buf);
176     sys.InitializeObjectParams(buf);
177   }
178 };
179 
180 struct objFgBgFreeInitAll : public RLOpcode<> {
operator ()__anon3111238a0111::objFgBgFreeInitAll181   virtual void operator()(RLMachine& machine) override {
182     GraphicsSystem& sys = machine.system().graphics();
183     sys.FreeAllObjectData();
184     sys.InitializeAllObjectParams();
185   }
186 };
187 
188 }  // namespace
189 
190 // -----------------------------------------------------------------------
191 
ObjManagement()192 ObjManagement::ObjManagement() : RLModule("ObjManagement", 1, 60) {
193   AddOpcode(0, 0, "objFree", CallFunction(&GraphicsSystem::FreeObjectData));
194   AddOpcode(0, 1, "objFree",
195             RangeMappingFun(CallFunction(&GraphicsSystem::FreeObjectData)));
196 
197   // TODO: This needs to be reverse engineered. It doesn't seem to be quite
198   // equivalent to objWipeCopyOff.
199   AddOpcode(1, 0, "objEraseWipeCopy", new SetWipeCopyTo_0(0));
200 
201   AddOpcode(2, 0, "objCopyFgToBg", new objCopyFgToBg_0);
202   AddOpcode(2, 1, "objCopyFgToBg", new objCopyFgToBg_1);
203 
204   AddOpcode(10, 0, "objInit",
205             CallFunction(&GraphicsSystem::InitializeObjectParams));
206   AddOpcode(10, 1, "objInit", RangeMappingFun(CallFunction(
207       &GraphicsSystem::InitializeObjectParams)));
208 
209   AddOpcode(11, 0, "objFreeInit", new objFreeInit);
210   AddOpcode(11, 1, "objFreeInit", RangeMappingFun(new objFreeInit));
211 
212   AddOpcode(
213       100, 0, "objFreeAll", CallFunction(&GraphicsSystem::FreeAllObjectData));
214   AddOpcode(
215       110, 0, "objInitAll",
216       CallFunction(&GraphicsSystem::InitializeAllObjectParams));
217   AddOpcode(111, 0, "objFreeInitAll", new objFgBgFreeInitAll);
218 }
219 
220 // -----------------------------------------------------------------------
221 
ObjFgManagement()222 ObjFgManagement::ObjFgManagement() : RLModule("ObjFgManagement", 1, 61) {
223   AddOpcode(2, 0, "objCopy", new objCopy(OBJ_FG, OBJ_FG));
224   AddOpcode(3, 0, "objCopyToBg", new objCopy(OBJ_FG, OBJ_BG));
225 
226   addObjManagementFunctions(*this, "objFg");
227   SetProperty(P_FGBG, OBJ_FG);
228 }
229 
230 // -----------------------------------------------------------------------
231 
ObjBgManagement()232 ObjBgManagement::ObjBgManagement() : RLModule("ObjBgManagement", 1, 62) {
233   AddOpcode(2, 0, "objBgCopyToFg", new objCopy(OBJ_BG, OBJ_FG));
234   AddOpcode(3, 0, "objBgCopy", new objCopy(OBJ_BG, OBJ_BG));
235 
236   addObjManagementFunctions(*this, "objBg");
237   SetProperty(P_FGBG, OBJ_BG);
238 }
239 
240 // -----------------------------------------------------------------------
241 
ChildObjFgManagement()242 ChildObjFgManagement::ChildObjFgManagement()
243     : MappedRLModule(ChildObjMappingFun, "ChildObjFgManagement", 2, 61) {
244   AddOpcode(2, 0, "objSetCopy", new objCopy(OBJ_FG, OBJ_FG));
245   AddOpcode(3, 0, "objSetCopyToBg", new objCopy(OBJ_FG, OBJ_BG));
246 
247   AddOpcode(14, 0, "objChildCopy", new objChildCopy(OBJ_FG));
248 
249   addObjManagementFunctions(*this, "objChildFg");
250   SetProperty(P_FGBG, OBJ_FG);
251 }
252 
253 // -----------------------------------------------------------------------
254 
ChildObjBgManagement()255 ChildObjBgManagement::ChildObjBgManagement()
256     : MappedRLModule(ChildObjMappingFun, "ChildObjFgManagement", 2, 62) {
257   AddOpcode(2, 0, "objSetBgCopyToFg", new objCopy(OBJ_BG, OBJ_FG));
258   AddOpcode(3, 0, "objSetBgCopy", new objCopy(OBJ_BG, OBJ_BG));
259 
260   AddOpcode(14, 0, "objChildCopy", new objChildCopy(OBJ_BG));
261 
262   addObjManagementFunctions(*this, "objChildBg");
263   SetProperty(P_FGBG, OBJ_BG);
264 }
265 
266 // -----------------------------------------------------------------------
267