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, 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 "test_system/test_graphics_system.h"
29
30 #include <map>
31 #include <string>
32 #include <sstream>
33
34 #include "systems/base/colour.h"
35 #include "systems/base/graphics_object.h"
36 #include "systems/base/graphics_system.h"
37 #include "test_system/mock_colour_filter.h"
38 #include "test_system/mock_surface.h"
39 #include "utilities/exception.h"
40
41 using namespace std;
42
TestGraphicsSystem(System & system,Gameexe & gexe)43 TestGraphicsSystem::TestGraphicsSystem(System& system, Gameexe& gexe)
44 : GraphicsSystem(system, gexe) {
45 SetScreenSize(Size(640, 480));
46
47 for (int i = 0; i < 16; ++i) {
48 ostringstream oss;
49 oss << "DC #" << i;
50 display_contexts_[i].reset(MockSurface::Create(oss.str()));
51 }
52
53 display_contexts_[0]->Allocate(screen_size());
54 display_contexts_[1]->Allocate(screen_size());
55
56 haikei_.reset(MockSurface::Create("Haikei"));
57 haikei_->Allocate(screen_size());
58 }
59
~TestGraphicsSystem()60 TestGraphicsSystem::~TestGraphicsSystem() {}
61
AllocateDC(int dc,Size size)62 void TestGraphicsSystem::AllocateDC(int dc, Size size) {
63 if (dc >= 16)
64 throw rlvm::Exception(
65 "Invalid DC number in "
66 "TestGraphicsSystem::allocate_dc");
67
68 // We can't reallocate the screen!
69 if (dc == 0)
70 throw rlvm::Exception("Attempting to reallocate DC 0!");
71
72 // DC 1 is a special case and must always be at least the size of
73 // the screen.
74 if (dc == 1) {
75 std::shared_ptr<MockSurface> dc0 = display_contexts_[0];
76 Size dc0_size = dc0->GetSize();
77 if (size.width() < dc0_size.width())
78 size.set_width(dc0_size.width());
79 if (size.height() < dc0_size.height())
80 size.set_height(dc0_size.height());
81 }
82
83 // Allocate a new obj.
84 display_contexts_[dc]->Allocate(size);
85 }
86
SetMinimumSizeForDC(int,Size)87 void TestGraphicsSystem::SetMinimumSizeForDC(int, Size) {
88 // noop for now.
89 }
90
FreeDC(int dc)91 void TestGraphicsSystem::FreeDC(int dc) {
92 if (dc == 0) {
93 throw rlvm::Exception("Attempt to deallocate DC[0]");
94 } else if (dc == 1) {
95 // DC[1] never gets freed; it only gets blanked
96 display_contexts_[1]->Fill(RGBAColour::Black());
97 } else {
98 display_contexts_[dc]->Deallocate();
99 }
100 }
101
InjectSurface(const std::string & short_filename,const std::shared_ptr<Surface> & surface)102 void TestGraphicsSystem::InjectSurface(
103 const std::string& short_filename,
104 const std::shared_ptr<Surface>& surface) {
105 named_surfaces_[short_filename] = surface;
106 }
107
LoadSurfaceFromFile(const std::string & short_filename)108 std::shared_ptr<const Surface> TestGraphicsSystem::LoadSurfaceFromFile(
109 const std::string& short_filename) {
110 // If we have an injected surface, return it instead of a fresh surface.
111 std::map<std::string, std::shared_ptr<const Surface>>::iterator it =
112 named_surfaces_.find(short_filename);
113 if (it != named_surfaces_.end()) {
114 return it->second;
115 }
116
117 // We don't have an injected surface so make a surface.
118 return std::shared_ptr<const Surface>(
119 std::const_pointer_cast<const MockSurface>(
120 std::shared_ptr<MockSurface>(
121 MockSurface::Create(short_filename, Size(50, 50)))));
122 }
123
GetHaikei()124 std::shared_ptr<Surface> TestGraphicsSystem::GetHaikei() { return haikei_; }
125
GetDC(int dc)126 std::shared_ptr<Surface> TestGraphicsSystem::GetDC(int dc) {
127 return display_contexts_[dc];
128 }
129
BuildSurface(const Size & s)130 std::shared_ptr<Surface> TestGraphicsSystem::BuildSurface(const Size& s) {
131 static int surface_num = 0;
132 ostringstream oss;
133 oss << "Built Surface #" << surface_num++;
134
135 return std::shared_ptr<Surface>(MockSurface::Create(oss.str(), s));
136 }
137
BuildColourFiller()138 ColourFilter* TestGraphicsSystem::BuildColourFiller() {
139 return new MockColourFilter;
140 }
141
BeginFrame()142 void TestGraphicsSystem::BeginFrame() {}
143
EndFrame()144 void TestGraphicsSystem::EndFrame() {}
145
EndFrameToSurface()146 std::shared_ptr<Surface> TestGraphicsSystem::EndFrameToSurface() {
147 return std::shared_ptr<Surface>();
148 }
149
GetMockDC(int dc)150 MockSurface& TestGraphicsSystem::GetMockDC(int dc) {
151 return *display_contexts_[dc];
152 }
153