add unfinished bmpImage implementation
[openc2e.git] / Camera.cpp
blobd1499cdecc53f2e571e7bb680a47da421158c6a6
1 /*
2 * Camera.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Tue May 25 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "Camera.h"
21 #include "CameraPart.h"
22 #include "World.h"
23 #include "Backend.h"
24 #include "MetaRoom.h"
26 Camera::Camera() {
27 metaroom = 0;
28 panning = false;
31 MetaRoom * const Camera::getMetaRoom() {
32 return world.map.getMetaRoom(metaroom);
35 void Camera::goToMetaRoom(unsigned int m) {
36 metaroom = m;
37 moveTo(getMetaRoom()->x(), getMetaRoom()->y());
40 void Camera::goToMetaRoom(unsigned int m, int _x, int _y, cameratransition transition) {
41 metaroom = m;
42 moveTo(_x, _y);
43 // TODO: transition
45 checkBounds();
48 void Camera::moveTo(int _x, int _y, panstyle pan) {
49 // TODO: we don't break tracking according to trackingstyle.. atm updateTracking() calls this, also
50 x = _x;
51 y = _y;
53 MetaRoom *m = world.map.metaRoomAt(x, y);
54 if (m)
55 metaroom = m->id;
57 // TODO: panning
59 checkBounds();
62 void MainCamera::moveTo(int _x, int _y, panstyle pan) {
63 int xoffset = _x - x;
64 int yoffset = _y - y;
65 Camera::moveTo(_x, _y, pan);
67 for (std::vector<AgentRef>::iterator i = floated.begin(); i != floated.end(); i++) {
68 assert(*i);
69 (*i)->moveTo((*i)->x + xoffset, (*i)->y + yoffset);
73 void MainCamera::addFloated(AgentRef a) {
74 assert(a);
75 floated.push_back(a);
78 void MainCamera::delFloated(AgentRef a) {
79 assert(a);
80 std::vector<AgentRef>::iterator i = std::find(floated.begin(), floated.end(), a);
81 if (i == floated.end()) return;
82 floated.erase(i);
85 void Camera::trackAgent(AgentRef a, int xp, int yp, trackstyle s, cameratransition transition) {
86 trackedagent = a;
87 trackingstyle = s;
88 updateTracking();
91 void Camera::checkBounds() {
92 MetaRoom *m = getMetaRoom();
93 if (!m) return;
95 if (m->wraparound()) {
96 // handle wrapping around, if necessary
97 if (x < (int)m->x()) {
98 moveTo(x + m->width(), y);
99 } else if (x > (int)m->x() + (int)m->width()) {
100 moveTo(x - m->width(), y);
102 } else {
103 // refuse to move beyond the boundaries
104 if (x < (int)m->x()) {
105 moveTo(m->x(), y);
106 } else if (x + getWidth() > m->x() + m->width()) {
107 moveTo(m->x() + m->width() - getWidth(), y);
111 // refuse to move beyond the boundaries
112 if (y < (int)m->y()) {
113 moveTo(x, m->y());
114 } else if (y + getHeight() > m->y() + m->height()) {
115 moveTo(x, m->y() + m->height() - getHeight());
119 void Camera::tick() {
120 updateTracking();
123 void Camera::updateTracking() {
124 if (!trackedagent) return;
126 // TODO: not very intelligent :) also, are int casts correct?
127 int trackx = (int)trackedagent->x + ((int)trackedagent->getWidth() / 2) - (int)(getWidth() / 2);
128 int tracky = (int)trackedagent->y + ((int)trackedagent->getHeight() / 2) - (int)(getHeight() / 2);
129 moveTo(trackx, tracky);
132 unsigned int const MainCamera::getWidth() {
133 if ((!getMetaRoom()) || (backend->getMainSurface()->getWidth() < getMetaRoom()->width()))
134 return backend->getMainSurface()->getWidth();
135 else
136 return getMetaRoom()->width();
139 unsigned int const MainCamera::getHeight() {
140 if ((!getMetaRoom()) || (backend->getMainSurface()->getHeight() < getMetaRoom()->height()))
141 return backend->getMainSurface()->getHeight();
142 else
143 return getMetaRoom()->height();
146 unsigned int const PartCamera::getWidth() {
147 // TODO: update from ZOOM values
148 return part->cameraWidth();
151 unsigned int const PartCamera::getHeight() {
152 // TODO: update from ZOOM values
153 return part->cameraHeight();
156 /* vim: set noet: */