File: test_graphics_system.cc

package info (click to toggle)
rlvm 0.14-5.2
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 17,104 kB
  • sloc: cpp: 91,574; ansic: 39,346; perl: 768; sh: 320; python: 181; makefile: 8
file content (152 lines) | stat: -rw-r--r-- 4,748 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi:tw=80:et:ts=2:sts=2
//
// -----------------------------------------------------------------------
//
// This file is part of RLVM, a RealLive virtual machine clone.
//
// -----------------------------------------------------------------------
//
// Copyright (C) 2006, 2007 Elliot Glaysher
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// -----------------------------------------------------------------------

#include "test_system/test_graphics_system.h"

#include <map>
#include <string>
#include <sstream>

#include "systems/base/colour.h"
#include "systems/base/graphics_object.h"
#include "systems/base/graphics_system.h"
#include "test_system/mock_colour_filter.h"
#include "test_system/mock_surface.h"
#include "utilities/exception.h"

using namespace std;

TestGraphicsSystem::TestGraphicsSystem(System& system, Gameexe& gexe)
    : GraphicsSystem(system, gexe) {
  SetScreenSize(Size(640, 480));

  for (int i = 0; i < 16; ++i) {
    ostringstream oss;
    oss << "DC #" << i;
    display_contexts_[i].reset(MockSurface::Create(oss.str()));
  }

  display_contexts_[0]->Allocate(screen_size());
  display_contexts_[1]->Allocate(screen_size());

  haikei_.reset(MockSurface::Create("Haikei"));
  haikei_->Allocate(screen_size());
}

TestGraphicsSystem::~TestGraphicsSystem() {}

void TestGraphicsSystem::AllocateDC(int dc, Size size) {
  if (dc >= 16)
    throw rlvm::Exception(
        "Invalid DC number in "
        "TestGraphicsSystem::allocate_dc");

  // We can't reallocate the screen!
  if (dc == 0)
    throw rlvm::Exception("Attempting to reallocate DC 0!");

  // DC 1 is a special case and must always be at least the size of
  // the screen.
  if (dc == 1) {
    std::shared_ptr<MockSurface> dc0 = display_contexts_[0];
    Size dc0_size = dc0->GetSize();
    if (size.width() < dc0_size.width())
      size.set_width(dc0_size.width());
    if (size.height() < dc0_size.height())
      size.set_height(dc0_size.height());
  }

  // Allocate a new obj.
  display_contexts_[dc]->Allocate(size);
}

void TestGraphicsSystem::SetMinimumSizeForDC(int, Size) {
  // noop for now.
}

void TestGraphicsSystem::FreeDC(int dc) {
  if (dc == 0) {
    throw rlvm::Exception("Attempt to deallocate DC[0]");
  } else if (dc == 1) {
    // DC[1] never gets freed; it only gets blanked
    display_contexts_[1]->Fill(RGBAColour::Black());
  } else {
    display_contexts_[dc]->Deallocate();
  }
}

void TestGraphicsSystem::InjectSurface(
    const std::string& short_filename,
    const std::shared_ptr<Surface>& surface) {
  named_surfaces_[short_filename] = surface;
}

std::shared_ptr<const Surface> TestGraphicsSystem::LoadSurfaceFromFile(
    const std::string& short_filename) {
  // If we have an injected surface, return it instead of a fresh surface.
  std::map<std::string, std::shared_ptr<const Surface>>::iterator it =
      named_surfaces_.find(short_filename);
  if (it != named_surfaces_.end()) {
    return it->second;
  }

  // We don't have an injected surface so make a surface.
  return std::shared_ptr<const Surface>(
      std::const_pointer_cast<const MockSurface>(
          std::shared_ptr<MockSurface>(
              MockSurface::Create(short_filename, Size(50, 50)))));
}

std::shared_ptr<Surface> TestGraphicsSystem::GetHaikei() { return haikei_; }

std::shared_ptr<Surface> TestGraphicsSystem::GetDC(int dc) {
  return display_contexts_[dc];
}

std::shared_ptr<Surface> TestGraphicsSystem::BuildSurface(const Size& s) {
  static int surface_num = 0;
  ostringstream oss;
  oss << "Built Surface #" << surface_num++;

  return std::shared_ptr<Surface>(MockSurface::Create(oss.str(), s));
}

ColourFilter* TestGraphicsSystem::BuildColourFiller() {
  return new MockColourFilter;
}

void TestGraphicsSystem::BeginFrame() {}

void TestGraphicsSystem::EndFrame() {}

std::shared_ptr<Surface> TestGraphicsSystem::EndFrameToSurface() {
  return std::shared_ptr<Surface>();
}

MockSurface& TestGraphicsSystem::GetMockDC(int dc) {
  return *display_contexts_[dc];
}