summaryrefslogtreecommitdiff
path: root/src/koopatlas/camera.cpp
blob: f80ebcc99109c71bd7f747a143d3000e6225e665 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "koopatlas/camera.h"
#include "koopatlas/player.h"

dWorldCamera_c *dWorldCamera_c::instance = 0;

dWorldCamera_c *dWorldCamera_c::build() {

	void *buffer = AllocFromGameHeap1(sizeof(dWorldCamera_c));
	dWorldCamera_c *c = new(buffer) dWorldCamera_c;


	instance = c;
	return c;
}


dWorldCamera_c::dWorldCamera_c() {
	camera3d.camPos = (Vec){0.0, 10.0, 0.0};
	camera3d.target = (Vec){0.0, 0.0, 0.0};
	camera3d.camUp = (Vec){0.0, 1.0, 0.0};

	camera2d.camPos = (Vec){0.0, 10.0, 0.0};
	camera2d.target = (Vec){0.0, 0.0, 0.0};
	camera2d.camUp = (Vec){0.0, 1.0, 0.0};

	camPos = (Vec){0.0, 0.0, 1.0};
	camUp = (Vec){0.0, 1.0, 0.0};

	projection2d.near = -20000.0f;
	projection2d.far = 20000.0f;

	currentX = 416;
	currentY = -224;
	zoomLevel = STD_ZOOM;

	followPlayer = true;
}



int dWorldCamera_c::onCreate() {
	// Bad Code
	screen.projType = 0;
	screen.some_flag_bit |= 1;

	SSM *ssm = &ScreenSizesAndMultipliers[currentScreenSizeID];
	screen.height = ssm->height;
	screen.width = ssm->width;
	screen.near = 1.0;
	screen.far = 20000.0;

	if (screen.isCentered != 1) {
		screen.some_flag_bit |= 1;
		screen.isCentered = 1;
	}

	doStuff(10000.0);
	generateCameraMatrices();
	updateCameras();

	return true;
}


int dWorldCamera_c::onDelete() {
	return true;
}


int dWorldCamera_c::onExecute() {
	if (dScKoopatlas_c::instance->warpZoneHacks) {
		currentX = 2040.0f;
		currentY = -1460.0f;
		zoomLevel = 3.4f;

	} else if (panning) {
		// Calculate where we are
#define SMOOTHSTEP(x) ((x) * (x) * (3 - 2 * (x)))
		float stepRatio = panCurrentStep / panTotalSteps;
		stepRatio = 1.0f - SMOOTHSTEP(stepRatio);
		//OSReport("PAN: Step %f / %f ---- Ratio: %f", panCurrentStep, panTotalSteps, stepRatio);
		//OSReport("From %f, %f to %f, %f --- Zoom: %f to %f\n", panFromX, panFromY, panToX, panToY, panFromZoom, panToZoom);

		currentX = (panFromX * stepRatio) + (panToX * (1.0f - stepRatio));
		currentY = (panFromY * stepRatio) + (panToY * (1.0f - stepRatio));
		zoomLevel = (panFromZoom * stepRatio) + (panToZoom * (1.0f - stepRatio));
		//OSReport("Calculated: %f, %f with zoom %f\n", currentX, currentY, zoomLevel);

		panCurrentStep += 1.0f;

		if (panCurrentStep > panTotalSteps) {
			// YAY, we reached the end
			panning = false;
			currentX = panToX;
			currentY = panToY;
			zoomLevel = panToZoom;
		}

	} else if (followPlayer) {
		daWMPlayer_c *player = daWMPlayer_c::instance;
		currentX = player->pos.x;
		currentY = player->pos.y;
	}

	calculateScreenGeometry();
	doStuff(10000.0);
	generateCameraMatrices();
	updateCameras(); 
	return true;
}

void dWorldCamera_c::panToBounds(float left, float top, float right, float bottom) {
	// Pad it a bit
	left -= 64.0f;
	right += 64.0f;
	top -= 48.0f;
	bottom += 48.0f;

	//OSReport("Panning camera to bounds %f,%f to %f,%f\n", left, top, right, bottom);

	// Figure out the centre x/y we want
	float width = right - left;
	float height = bottom - top;

	float desiredCentreX = left + (width * 0.5f);
	float desiredCentreY = -(top + (height * 0.5f));

	//OSReport("Size: %f x %f ; Desired Centre: %f, %f\n", width, height, desiredCentreX, desiredCentreY);

	// Our default zoom is 2.8
	float minScreenWidth = GlobalScreenWidth * 1.2f;
	float minScreenHeight = GlobalScreenHeight * 1.2f;
	float maxScreenWidth = GlobalScreenWidth * 4.0f;
	float maxScreenHeight = GlobalScreenHeight * 4.0f;

	//OSReport("Screen Sizes: Minimum possible %f x %f ; Maximum possible %f x %f\n", minScreenWidth, minScreenHeight, maxScreenWidth, maxScreenHeight);

	// First off, gotta cap it to the ratio
	float screenRatio = GlobalScreenWidth / GlobalScreenHeight;
	float boundsRatio = width / height;
	float correctedWidth = width, correctedHeight = height;
	float desiredZoomLevel;
	//OSReport("Actual screen size is %f x %f --- Screen Ratio: %f, Bounds Ratio: %f\n", GlobalScreenWidth, GlobalScreenHeight, screenRatio, boundsRatio);

	float widthScale = width / GlobalScreenWidth;
	float heightScale = height / GlobalScreenHeight;

	if (heightScale > widthScale) {
		// Thing is constrained on the top/bottom
		desiredZoomLevel = heightScale;
	} else {
		// Thing is constrained on the left/right
		desiredZoomLevel = widthScale;
	}

	//OSReport("Desired zoom level is %f\n", desiredZoomLevel);

	// Cap the zoom
	if (desiredZoomLevel < 2.0f)
		desiredZoomLevel = 2.0f;
	if (desiredZoomLevel > 4.5f)
		desiredZoomLevel = 4.5f;
	//OSReport("After capping: %f\n", desiredZoomLevel);

	// And we're almost there YAY
	panToPosition(desiredCentreX, desiredCentreY, desiredZoomLevel);
}


void dWorldCamera_c::panToPosition(float x, float y, float zoom) {
	panFromX = currentX;
	panFromY = currentY;
	panFromZoom = zoomLevel;

	panToX = x;
	panToY = y;
	panToZoom = zoom;

	float xDiff = abs(panToX - panFromX);
	float yDiff = abs(panToY - panFromY);

	float panLength = sqrtf((xDiff*xDiff) + (yDiff*yDiff));
	float panSteps = panLength / 2.3f;
	float scaleSteps = abs(panToZoom - panFromZoom) / 0.1f;
	float stepCount = max(panSteps, scaleSteps);

	//OSReport("Pan length: %f over %f steps\n", panLength, panSteps);
	//OSReport("Scale steps: %f\n", scaleSteps);
	//OSReport("Step Count: %f\n", stepCount);

	panCurrentStep = 0.0f;
	panTotalSteps = stepCount;

	panning = true;
	followPlayer = false;
}


int dWorldCamera_c::onDraw() {
	GXRenderModeObj *rmode = nw4r::g3d::G3DState::GetRenderModeObj();

	nw4r::g3d::Camera cam(GetCameraByID(0));
	nw4r::g3d::Camera cam2d(GetCameraByID(1));

	if (rmode->field_rendering != 0) {
		cam.SetViewportJitter(VIGetNextField());
		cam2d.SetViewportJitter(VIGetNextField());
	}
	//cam2d.SetOrtho(rmode->efbHeight, 0.0f, 0.0f, rmode->fbWidth * (IsWideScreen() ? 1.3333334f : 1.0f), -100000.0f, 100000.0f);

	return true;
}


void dWorldCamera_c::calculateScreenGeometry() {
	zoomDivisor = 1.0 / zoomLevel;

	screenWidth = GlobalScreenWidth / zoomDivisor;
	screenHeight = GlobalScreenHeight / zoomDivisor;

	screenLeft = currentX - (screenWidth * 0.5);
	screenTop = currentY + (screenHeight * 0.5);
}


void dWorldCamera_c::doStuff(float something) {
	camTarget = (Vec){
		screenLeft + (screenWidth * 0.5),
	   (screenTop - screenHeight) + (screenHeight * 0.5),
	   0.0
	};

	camPos = (Vec){camTarget.x, camTarget.y, something};
}


void dWorldCamera_c::generateCameraMatrices() {
	float orthoTop = screenHeight * 0.5;
	float orthoLeft = -screenWidth * 0.5;
	float orthoBottom = -orthoTop;
	float orthoRight = -orthoLeft;

	camera3d.camPos = camPos;
	camera3d.target = camTarget;
	camera3d.camUp = camUp;

	camera3d.calculateMatrix();

	screen.setOrtho(orthoTop, orthoBottom, orthoLeft, orthoRight, -100000.0f, 100000.0f);

	VEC2 crap = GetSomeSizeRelatedBULLSHIT();
	Vec calcedAboutRatio = CalculateSomethingAboutRatio(orthoTop, orthoBottom, orthoLeft, orthoRight);
	screen._44 = CalculateSomethingElseAboutRatio();

	screen.width = crap.x;
	screen.some_flag_bit |= 1;
	screen.height = crap.y;
	screen.horizontalMultiplier = calcedAboutRatio.x;
	screen.verticalMultiplier = calcedAboutRatio.y;
	screen.unk3 = calcedAboutRatio.z;

	camera2d.camPos = (Vec){0.0, 0.0, 0.0};
	camera2d.target = (Vec){0.0, 0.0, -100.0};
	camera2d.camUp = camUp;

	camera2d.calculateMatrix();

	GXRenderModeObj *rmode = nw4r::g3d::G3DState::GetRenderModeObj();
	projection2d.setVolume(rmode->efbHeight, 0.0, 0.0, rmode->fbWidth * (IsWideScreen() ? 1.3333334f : 1.0f));
}


void dWorldCamera_c::updateCameras() {
	nw4r::g3d::Camera cam0(GetCameraByID(0));
	camera3d.assignToNW4RCamera(cam0);
	screen.loadIntoCamera(cam0);

	nw4r::g3d::Camera cam1(GetCameraByID(1));
	projection2d.setOrthoOntoCamera(cam1);
	camera2d.assignToNW4RCamera(cam1);
}