git repos / blockattack-game

blame: source/code/sago/SagoLogicalResize.hpp

normal view · raw

62b87fe3 Poul Sander 2025-03-04 20:16 1
/*
62b87fe3 Poul Sander 2025-03-04 20:16 2
Copyright (c) 2025 Poul Sander
62b87fe3 Poul Sander 2025-03-04 20:16 3
62b87fe3 Poul Sander 2025-03-04 20:16 4
Permission is hereby granted, free of charge, to any person
62b87fe3 Poul Sander 2025-03-04 20:16 5
obtaining a copy of this software and associated documentation files
62b87fe3 Poul Sander 2025-03-04 20:16 6
(the "Software"), to deal in the Software without restriction,
62b87fe3 Poul Sander 2025-03-04 20:16 7
including without limitation the rights to use, copy, modify, merge,
62b87fe3 Poul Sander 2025-03-04 20:16 8
publish, distribute, sublicense, and/or sell copies of the Software,
62b87fe3 Poul Sander 2025-03-04 20:16 9
and to permit persons to whom the Software is furnished to do so,
62b87fe3 Poul Sander 2025-03-04 20:16 10
subject to the following conditions:
62b87fe3 Poul Sander 2025-03-04 20:16 11
62b87fe3 Poul Sander 2025-03-04 20:16 12
The above copyright notice and this permission notice shall be
62b87fe3 Poul Sander 2025-03-04 20:16 13
included in all copies or substantial portions of the Software.
62b87fe3 Poul Sander 2025-03-04 20:16 14
62b87fe3 Poul Sander 2025-03-04 20:16 15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
62b87fe3 Poul Sander 2025-03-04 20:16 16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62b87fe3 Poul Sander 2025-03-04 20:16 17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
62b87fe3 Poul Sander 2025-03-04 20:16 18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
62b87fe3 Poul Sander 2025-03-04 20:16 19
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
62b87fe3 Poul Sander 2025-03-04 20:16 20
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
62b87fe3 Poul Sander 2025-03-04 20:16 21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62b87fe3 Poul Sander 2025-03-04 20:16 22
SOFTWARE.
62b87fe3 Poul Sander 2025-03-04 20:16 23
*/
62b87fe3 Poul Sander 2025-03-04 20:16 24
62b87fe3 Poul Sander 2025-03-04 20:16 25
#pragma once
cb02a4a0 Poul Sander 2025-03-03 21:25 26
#include <algorithm>
cb02a4a0 Poul Sander 2025-03-03 21:25 27
#include "SDL.h"
cb02a4a0 Poul Sander 2025-03-03 21:25 28
cb02a4a0 Poul Sander 2025-03-03 21:25 29
cb02a4a0 Poul Sander 2025-03-03 21:25 30
namespace sago {
cb02a4a0 Poul Sander 2025-03-03 21:25 31
cb02a4a0 Poul Sander 2025-03-03 21:25 32
class SagoLogicalResize
cb02a4a0 Poul Sander 2025-03-03 21:25 33
{
cb02a4a0 Poul Sander 2025-03-03 21:25 34
public:
cb02a4a0 Poul Sander 2025-03-03 21:25 35
	SagoLogicalResize() : logical_width_(1), logical_height_(1), physical_width_(1), physical_height_(1) { SetScaleFactor(); }
cb02a4a0 Poul Sander 2025-03-03 21:25 36
	SagoLogicalResize(int logical_width, int logical_height)
cb02a4a0 Poul Sander 2025-03-03 21:25 37
		: logical_width_(std::max(1, logical_width)), logical_height_(std::max(1, logical_height)), physical_width_(1), physical_height_(1) { SetScaleFactor(); }
cb02a4a0 Poul Sander 2025-03-03 21:25 38
cb02a4a0 Poul Sander 2025-03-03 21:25 39
	void SetPhysicalSize(int physical_width, int physical_height)
cb02a4a0 Poul Sander 2025-03-03 21:25 40
	{
62b87fe3 Poul Sander 2025-03-04 20:16 41
		//Physical size must be at least 1. Less than 1 is not drawn anyway and it prevents division by zero.
cb02a4a0 Poul Sander 2025-03-03 21:25 42
		physical_width_ = std::max(1, physical_width);
cb02a4a0 Poul Sander 2025-03-03 21:25 43
		physical_height_ = std::max(1, physical_height);
cb02a4a0 Poul Sander 2025-03-03 21:25 44
		SetScaleFactor();
cb02a4a0 Poul Sander 2025-03-03 21:25 45
	}
cb02a4a0 Poul Sander 2025-03-03 21:25 46
cb02a4a0 Poul Sander 2025-03-03 21:25 47
	void LogicalToPhysical(int logical_x, int logical_y, int &physical_x, int &physical_y) const
cb02a4a0 Poul Sander 2025-03-03 21:25 48
	{
cb02a4a0 Poul Sander 2025-03-03 21:25 49
		physical_x = logical_x;
cb02a4a0 Poul Sander 2025-03-03 21:25 50
		physical_y = logical_y;
cb02a4a0 Poul Sander 2025-03-03 21:25 51
		LogicalToPhysical(&physical_x, &physical_y);
cb02a4a0 Poul Sander 2025-03-03 21:25 52
	}
cb02a4a0 Poul Sander 2025-03-03 21:25 53
cb02a4a0 Poul Sander 2025-03-03 21:25 54
	void LogicalToPhysical(int *x, int *y) const
cb02a4a0 Poul Sander 2025-03-03 21:25 55
	{
cb02a4a0 Poul Sander 2025-03-03 21:25 56
		if (x) {
cb02a4a0 Poul Sander 2025-03-03 21:25 57
			*x = *x * scale_factor_ + left_margin_;
cb02a4a0 Poul Sander 2025-03-03 21:25 58
		}
cb02a4a0 Poul Sander 2025-03-03 21:25 59
		if (y) {
cb02a4a0 Poul Sander 2025-03-03 21:25 60
			*y = *y * scale_factor_ + top_margin_;
cb02a4a0 Poul Sander 2025-03-03 21:25 61
		}
cb02a4a0 Poul Sander 2025-03-03 21:25 62
	}
cb02a4a0 Poul Sander 2025-03-03 21:25 63
cb02a4a0 Poul Sander 2025-03-03 21:25 64
	void LogicalToPhysical(SDL_Rect &inout) const
cb02a4a0 Poul Sander 2025-03-03 21:25 65
	{
cb02a4a0 Poul Sander 2025-03-03 21:25 66
		SDL_Rect input = inout;
cb02a4a0 Poul Sander 2025-03-03 21:25 67
		LogicalToPhysical(&inout.x, &inout.y);
cb02a4a0 Poul Sander 2025-03-03 21:25 68
		LogicalToPhysical(input.x + input.w + 1, input.y + input.h + 1, inout.w, inout.h);
cb02a4a0 Poul Sander 2025-03-03 21:25 69
		inout.w -= inout.x - 1;
cb02a4a0 Poul Sander 2025-03-03 21:25 70
		inout.h -= inout.y - 1;
cb02a4a0 Poul Sander 2025-03-03 21:25 71
	}
cb02a4a0 Poul Sander 2025-03-03 21:25 72
cb02a4a0 Poul Sander 2025-03-03 21:25 73
	void PhysicalToLogical(int physical_x, int physical_y, int &logical_x, int &logical_y) const
cb02a4a0 Poul Sander 2025-03-03 21:25 74
	{
cb02a4a0 Poul Sander 2025-03-03 21:25 75
		logical_x = physical_x * logical_width_ / physical_width_ + left_margin_;
cb02a4a0 Poul Sander 2025-03-03 21:25 76
		logical_y = physical_y * logical_height_ / physical_height_ + top_margin_;
cb02a4a0 Poul Sander 2025-03-03 21:25 77
	}
cb02a4a0 Poul Sander 2025-03-03 21:25 78
cb02a4a0 Poul Sander 2025-03-03 21:25 79
	int GetTopMargin() const
cb02a4a0 Poul Sander 2025-03-03 21:25 80
	{
cb02a4a0 Poul Sander 2025-03-03 21:25 81
		return top_margin_;
cb02a4a0 Poul Sander 2025-03-03 21:25 82
	}
cb02a4a0 Poul Sander 2025-03-03 21:25 83
cb02a4a0 Poul Sander 2025-03-03 21:25 84
	int GetLeftMargin() const
cb02a4a0 Poul Sander 2025-03-03 21:25 85
	{
cb02a4a0 Poul Sander 2025-03-03 21:25 86
		return left_margin_;
cb02a4a0 Poul Sander 2025-03-03 21:25 87
	}
cb02a4a0 Poul Sander 2025-03-03 21:25 88
cb02a4a0 Poul Sander 2025-03-03 21:25 89
private:
cb02a4a0 Poul Sander 2025-03-03 21:25 90
	void SetScaleFactor()
cb02a4a0 Poul Sander 2025-03-03 21:25 91
	{
cb02a4a0 Poul Sander 2025-03-03 21:25 92
		scale_factor_ = std::min(physical_width_ / logical_width_, physical_height_ / logical_height_);
cb02a4a0 Poul Sander 2025-03-03 21:25 93
		left_margin_ = (physical_width_ - logical_width_ * scale_factor_) / 2;
cb02a4a0 Poul Sander 2025-03-03 21:25 94
		top_margin_ = (physical_height_ - logical_height_ * scale_factor_) / 2;
cb02a4a0 Poul Sander 2025-03-03 21:25 95
	}
cb02a4a0 Poul Sander 2025-03-03 21:25 96
cb02a4a0 Poul Sander 2025-03-03 21:25 97
	double scale_factor_;
cb02a4a0 Poul Sander 2025-03-03 21:25 98
	int top_margin_;
cb02a4a0 Poul Sander 2025-03-03 21:25 99
	int left_margin_;
cb02a4a0 Poul Sander 2025-03-03 21:25 100
	double logical_width_;
cb02a4a0 Poul Sander 2025-03-03 21:25 101
	double logical_height_;
cb02a4a0 Poul Sander 2025-03-03 21:25 102
	double physical_width_;
cb02a4a0 Poul Sander 2025-03-03 21:25 103
	double physical_height_;
cb02a4a0 Poul Sander 2025-03-03 21:25 104
};
cb02a4a0 Poul Sander 2025-03-03 21:25 105
cb02a4a0 Poul Sander 2025-03-03 21:25 106
}  // namespace sago