How to make a struct maker like CGRectMake (iphone) - iphone

i have a struct HLRange with two CGFloat's
struct HOLRange
{
CGFloat min;
CGFloat max;
};
typedef struct HOLRange HOLRange;
but how do i make a function like HLRangeMake(1,2); .. like CGRectMake?
--EDIT--
my header file
#import <Foundation/Foundation.h>
struct HOLRange
{
CGFloat min;
CGFloat max;
};
typedef struct HOLRange HOLRange;
HOLRange HOLRangeMake(CGFloat min, CGFloat max) {
HOLRange range;
range.min = min;
range.max = max;
return range;
}
#interface Structs : NSObject {
}
#end
error message: ld: duplicate symbol _HOLRangeMake in /Users/Documents/projects/iphone/test/catalog/base1/build/base1.build/Debug-iphoneos/base1.build/Objects-normal/armv6/base1AppDelegate.o and /Users/Documents/projects/iphone/test/catalog/base1/build/base1.build/Debug-iphoneos/base1.build/Objects-normal/armv6/main.o

HOLRange HLRangeMake(CGFloat min, CGFloat max) {
HOLRange range;
range.min = min;
range.max = max;
return range;
}

You can see CGRectMake source in CGGeometry.h so you can do the same:
CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x; rect.origin.y = y;
rect.size.width = width; rect.size.height = height;
return rect;
}
Edit: You must either define your function as inline or move its implementation to .m file. You're getting linker errors as you function becomes defined in every compile unit that imports HoleRange.h (?) header.

Old post. However, I'd like to share my method of resolving this problem for future viewers.
typdef struct _HOLRange {
CGFloat min;
CGFloat max;
} HOLRange;
static inline HOLRange(CGFloat min, CGFloat max) {
return (HOLRange) {min, max};
}
You can define your stuct and Make function like this. Short and quick.

I like the format of this better. It makes more sense visually and seems more "correct."
typedef struct {
CGFloat min;
CGFloat max;
} HOLRange;
static inline HOLRange HOLRangeMake(CGFloat min, CGFloat max) {
HOLRange range;
range.min = min;
range.max = max;
return range;
}

Related

Unity 2d Camera Scale to Screen Size

I am looking to scale a orthographic camera in unity 2d to screen size.
When you edit your game you orient the camera in a way you want it to be, view all your objects etc.
But then when you maximise it your camera zooms out showing maybe other things you don't want to show
Images for example
Edit Mode View
Maximised
Thank you if you can answer my question
Best Regards
Dawid
Attach the following component to your camera. Make sure to set the target aspect ratio in the inspector (16/9 for example).
using UnityEngine;
[ExecuteAlways]
[RequireComponent(typeof(Camera))]
public class ViewportScaler : MonoBehaviour
{
private Camera _camera;
[Tooltip("Set the target aspect ratio.")]
[SerializeField] private float _targetAspectRatio;
private void Awake()
{
_camera = GetComponent<Camera>();
if (Application.isPlaying)
ScaleViewport();
}
private void Update()
{
#if UNITY_EDITOR
if (_camera)
ScaleViewport();
#endif
}
private void ScaleViewport()
{
// determine the game window's current aspect ratio
var windowaspect = Screen.width / (float) Screen.height;
// current viewport height should be scaled by this amount
var scaleheight = windowaspect / _targetAspectRatio;
// if scaled height is less than current height, add letterbox
if (scaleheight < 1)
{
var rect = _camera.rect;
rect.width = 1;
rect.height = scaleheight;
rect.x = 0;
rect.y = (1 - scaleheight) / 2;
_camera.rect = rect;
}
else // add pillarbox
{
var scalewidth = 1 / scaleheight;
var rect = _camera.rect;
rect.width = scalewidth;
rect.height = 1;
rect.x = (1 - scalewidth) / 2;
rect.y = 0;
_camera.rect = rect;
}
}
}
I'd like to expand on #sean-carey 's very useful answer.
I modified his code in order to add a min and max aspect ratio, so the viewport will be full screen in between those two aspects, and will pillarbox when < min and letterbox when > max
using UnityEngine;
[ExecuteAlways]
[RequireComponent(typeof(Camera))]
public class ViewportController : MonoBehaviour
{
private Camera _camera;
[Tooltip("Set the target aspect ratio.")]
[SerializeField] private float _minAspectRatio;
[SerializeField] private float _maxAspectRatio;
private void Awake()
{
_camera = GetComponent<Camera>();
if (Application.isPlaying)
ScaleViewport();
}
private void Update()
{
#if UNITY_EDITOR
if (_camera)
ScaleViewport();
#endif
}
private void ScaleViewport()
{
// determine the game window's current aspect ratio
var windowaspect = Screen.width / (float) Screen.height;
// current viewport height should be scaled by this amount
var letterboxRatio = windowaspect / _minAspectRatio;
var pillarboxRatio = _maxAspectRatio / windowaspect;
// if max height is less than current height, add letterbox
if (letterboxRatio < 1)
{
SetRect(1, letterboxRatio, 0, (1-letterboxRatio)/2);
}
// if min height is more than current height, add pillarbox
else if (pillarboxRatio < 1)
{
SetRect(pillarboxRatio, 1, (1-pillarboxRatio)/2, 0);
}
// full screen
else
{
SetRect(1, 1, 0, 0);
}
}
private void SetRect(float w, float h, float x, float y)
{
var rect = _camera.rect;
rect.width = w;
rect.height = h;
rect.x = x;
rect.y = y;
_camera.rect = rect;
}
}

how to calculate the byte sum of struct in swift

this is the code in OC
It is not easy for me to convert it to swift, especially the calculate of check sum part
If anyone knows how to write, please let me know. Thank you.
OC Code:
MessageToOnboard {
unsigned char header[2];
ushort length;
uint8_t msgID;
uint missionID;
ushort wayPointID;
double lon;
double lat;
float height;
unsigned char wayPointAction;
// unsigned long reserve;
unsigned char checkSum;
}
MessageToOnboard message;
message.header[0] = 'Q';
message.header[1] = 'Z';
message.msgID = 0x21;
message.missionID = 12;
message.wayPointID = 10;
message.lon = 34.2323242;
message.lat = 68.2121221;
message.height = 30;
message.wayPointAction = '2';
message.length = sizeof(message) -5;
uint8_t *msgBIn = (uint8_t *)&message;
int iLength = sizeof(message);
uint8_t icheckSum = 0;
for (int i = 0 ; i < iLength - 3; i++)
{
icheckSum += *(msgBIn+2+i);
}
message.checkSum = icheckSum;
NSData *data = [[NSData alloc] initWithBytes:&message length:sizeof(message)];
Swift Code:
I tried this, but it is not right, I don't know how to use pointer to calculate the sum bytes of struct in swift
let msgBIn = withUnsafePointer(to: &message) {$0}
print(msgBIn.pointee)
let iLength: Int = MemoryLayout<MessageToOnboard>.size
let icheckSum: UInt8 = 0
for i in 0..<iLength - 1 {
let pointer = msgBIn.advanced(by: 2 + i)
icheckSum += UInt8(pointer)
}
finally, solved this problem
Message is a Struct
func sumByte(msg: inout Message) -> UInt8 {
let pointer = withUnsafePointer(to: msg) {UnsafeRawPointer($0)}
var sum: UInt8 = 0;
for i in 0..<(MemoryLayout<Message>.size) {
let nextP = pointer.advanced(by: i)
sum += nextP.load(as: UInt8.self)
}
return sum;
}

Initialisation of a CGPoint with { } notation

After CGPointMake let me down I found out that we can initalise a static const CGPoint like this instead:
static const CGPoint p = { 0.f, 0.f };
It works but what is the curly bracket notation actually doing?
CGPoint is a struct:
struct CGPoint {
CGFloat x;
CGFloat y;
};
It's a valid method to initialize a struct in C. See Struct initialization of the C/C++ programming language?.

iPhone/Objective-C struct question my own CGRectZero

I am designing a Padding struct as follows:
/* Padding. */
struct CGPadding {
CGFloat left;
CGFloat top;
CGFloat right;
CGFloat bottom;
};
typedef struct CGPadding CGPadding;
CG_INLINE CGPadding CGPaddingMake(CGFloat left, CGFloat top, CGFloat right, CGFloat bottom) { CGPadding p; p.left = left; p.top = top; p.right = right; p.bottom = bottom; return p; }
This all works perfectly well, the problem is how can I create a const CGPadding CGPaddingZero? If I do like this:
const CGPadding CGPaddingZero = (CGPadding){0.0, 0.0, 0.0, 0.0};
It doesnt work. So what am I doing wrong?
I find this very interesting because I do it all the time on the iPhone and it works fine. May it have something to do with the typedef afterward to the same name? What if you get rid of the typedef and define your struct like this?
typedef struct CGPadding {
CGFloat left;
CGFloat top;
CGFloat right;
CGFloat bottom;
} CGPadding;
Does it work then? Also, since all 0's is zero for a float as well and you're zero-ing out the whole thing, you could just say
const CGPadding CGPaddingZero = {0};
I do this stuff all the time on the iPhone so I'd be quite surprised if it doesn't work for you.

Enum or typedef with types

I'd like to create a simple object that I can access like this:
myobject.floatValue1 = 1.0;
myobject.floatValue2 = 2.0;
It shouldn't have anymore than the two properties. Is it possible to create an enum or typedef with such a simple structure. Or must I create a class?
Sure, just make a C structure:
struct myStruct
{
float floatValue1;
float floatValue2;
};
typedef struct myStruct myType;
Then use it like this:
myType myVariable = {0.0, 0.0}; // optional initialization
myVariable.floatValue1 = 1.0;
myVariable.floatValue2 = 2.0;
Take a look at using a struct, e.g.:
struct MyObjectType {
float floatValue1;
float floatValue2;
};
...
MyObjectType myobject;
myobject.floatValue1 = 1.0;
myobject.floatValue2 = 2.0;