From 8f7de171aff90a5538ebef1f0091f4b02c0ae3ad Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 31 Mar 2021 02:29:25 -0500 Subject: [PATCH] [nostalgia/core] Make display size function return common::Size instead of common::Point --- src/nostalgia/common/CMakeLists.txt | 1 + src/nostalgia/common/size.hpp | 192 ++++++++++++++++++++++++++++ src/nostalgia/core/gba/gfx.cpp | 2 +- src/nostalgia/core/gfx.hpp | 4 +- src/nostalgia/core/sdl/gfx.cpp | 2 +- 5 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 src/nostalgia/common/size.hpp diff --git a/src/nostalgia/common/CMakeLists.txt b/src/nostalgia/common/CMakeLists.txt index 80e820d3..b579602b 100644 --- a/src/nostalgia/common/CMakeLists.txt +++ b/src/nostalgia/common/CMakeLists.txt @@ -10,6 +10,7 @@ install( bounds.hpp common.hpp point.hpp + size.hpp DESTINATION include/nostalgia/common ) diff --git a/src/nostalgia/common/size.hpp b/src/nostalgia/common/size.hpp new file mode 100644 index 00000000..09abc675 --- /dev/null +++ b/src/nostalgia/common/size.hpp @@ -0,0 +1,192 @@ +/* + * Copyright 2016 - 2021 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include + +namespace nostalgia::common { + +class Size { + public: + int width = 0; + int height = 0; + + constexpr Size() = default; + + constexpr Size(int width, int height); + + constexpr Size operator+(common::Size p) const; + + constexpr Size operator-(common::Size p) const; + + constexpr Size operator*(common::Size p) const; + + constexpr Size operator/(common::Size p) const; + + + constexpr Size operator+=(common::Size p); + + constexpr Size operator-=(common::Size p); + + constexpr Size operator*=(common::Size p); + + constexpr Size operator/=(common::Size p); + + + constexpr Size operator+(int i) const; + + constexpr Size operator-(int i) const; + + constexpr Size operator*(int i) const; + + constexpr Size operator/(int i) const; + + + constexpr Size operator+=(int i); + + constexpr Size operator-=(int i); + + constexpr Size operator*=(int i); + + constexpr Size operator/=(int i); + + + constexpr bool operator==(const Size&) const; + + constexpr bool operator!=(const Size&) const; + +}; + +constexpr Size::Size(int width, int height) { + this->width = width; + this->height = height; +} + +constexpr Size Size::operator+(common::Size p) const { + p.width += width; + p.height += height; + return p; +} + +constexpr Size Size::operator-(common::Size p) const { + auto out = *this; + out.width -= p.width; + out.height -= p.height; + return out; +} + +constexpr Size Size::operator*(common::Size p) const { + p.width *= width; + p.height *= height; + return p; +} + +constexpr Size Size::operator/(common::Size p) const { + auto out = *this; + out.width /= p.width; + out.height /= p.height; + return out; +} + +constexpr Size Size::operator+=(common::Size p) { + width += p.width; + height += p.height; + return *this; +} + +constexpr Size Size::operator-=(common::Size p) { + width -= p.width; + height -= p.height; + return *this; +} + +constexpr Size Size::operator*=(common::Size p) { + width *= p.width; + height *= p.height; + return *this; +} + +constexpr Size Size::operator/=(common::Size p) { + width /= p.width; + height /= p.height; + return *this; +} + + +constexpr Size Size::operator+(int i) const { + auto out = *this; + out.width += i; + out.height += i; + return out; +} + +constexpr Size Size::operator-(int i) const { + auto out = *this; + out.width -= i; + out.height -= i; + return out; +} + +constexpr Size Size::operator*(int i) const { + auto out = *this; + out.width *= i; + out.height *= i; + return out; +} + +constexpr Size Size::operator/(int i) const { + auto out = *this; + out.width /= i; + out.height /= i; + return out; +} + +constexpr Size Size::operator+=(int i) { + width += i; + height += i; + return *this; +} + +constexpr Size Size::operator-=(int i) { + width -= i; + height -= i; + return *this; +} + +constexpr Size Size::operator*=(int i) { + width *= i; + height *= i; + return *this; +} + +constexpr Size Size::operator/=(int i) { + width /= i; + height /= i; + return *this; +} + +constexpr bool Size::operator==(const Size &p) const { + return width == p.width && height == p.height; +} + + +constexpr bool Size::operator!=(const Size &p) const { + return width != p.width || height != p.height; +} + + +template +ox::Error model(T *io, Size *obj) { + io->setTypeInfo("nostalgia::common::Bounds", 2); + oxReturnError(io->field("width", &obj->width)); + oxReturnError(io->field("height", &obj->height)); + return OxError(0); +} + +} diff --git a/src/nostalgia/core/gba/gfx.cpp b/src/nostalgia/core/gba/gfx.cpp index 14975ea7..5c68464b 100644 --- a/src/nostalgia/core/gba/gfx.cpp +++ b/src/nostalgia/core/gba/gfx.cpp @@ -108,7 +108,7 @@ int getScreenHeight(Context*) { return 160; } -common::Point getScreenSize(Context*) { +common::Size getScreenSize(Context*) { return {240, 160}; } diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index 17869b19..ded871c8 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -9,7 +9,7 @@ #pragma once #include -#include +#include #include "color.hpp" #include "context.hpp" @@ -80,7 +80,7 @@ int getScreenWidth(Context *ctx); int getScreenHeight(Context *ctx); -common::Point getScreenSize(Context *ctx); +common::Size getScreenSize(Context *ctx); [[nodiscard]] uint8_t bgStatus(Context *ctx); diff --git a/src/nostalgia/core/sdl/gfx.cpp b/src/nostalgia/core/sdl/gfx.cpp index f76a63c2..c5dcc45a 100644 --- a/src/nostalgia/core/sdl/gfx.cpp +++ b/src/nostalgia/core/sdl/gfx.cpp @@ -66,7 +66,7 @@ int getScreenHeight(Context *ctx) { return y; } -common::Point getScreenSize(Context *ctx) { +common::Size getScreenSize(Context *ctx) { auto id = ctx->windowerData(); int x = 0, y = 0; SDL_GetWindowSize(id->window, &x, &y);