openframeworks / openframeworks/openFrameworks
ofGetHeight/ofGetWidth inconsistent between desktop and ios.
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 10.4k
- Forks
- 2.6k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 9
Description
On Desktop (e.g. OSX), calls to ofGetHeight/ofGetWidth can be called in initialization lists of default constructors of member classes.
e.g. a class like MyClass
#pragma once
#include "ofMain.h"
class MyClass
{
public:
MyClass():
_width(ofGetWidth()),
_height(ofGetHeight())
{
}
private:
int _width;
int _height;
};
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
MyClass myClass;
};
This initializes the width / height of MyClass correctly.
On iOS, there is an initialization order difference that causes invalid access when trying to do something similar like this:
#pragma once
#include "ofMain.h"
#include "ofxiOS.h"
#include "ofxiOSExtras.h"
class MyClass
{
public:
MyClass():
_width(ofGetWidth()),
_height(ofGetHeight())
{
}
private:
int _width;
int _height;
};
class ofApp : public ofxiOSApp {
public:
void setup();
void update();
void draw();
void exit();
void touchDown(ofTouchEventArgs & touch);
void touchMoved(ofTouchEventArgs & touch);
void touchUp(ofTouchEventArgs & touch);
void touchDoubleTap(ofTouchEventArgs & touch);
void touchCancelled(ofTouchEventArgs & touch);
void lostFocus();
void gotFocus();
void gotMemoryWarning();
void deviceOrientationChanged(int newOrientation);
MyClass theClass;
};
Basically it comes down to this section of ofAppIOSWindow.mm failing with bad access
ofPoint ofAppiOSWindow::getWindowSize() {
return *[[ofxiOSEAGLView getInstance] getWindowSize];
}
@julapy I tried out your PR just to see if it would fix it, but it didn't help (https://github.com/openframeworks/openFrameworks/pull/4327).
I understand that calling global state variables during the constructor can be tricky and perhaps even ill-advised in oF, but it seems that it should have consistent behavior across platforms whenever possible. From what I can tell, the ofAppIOSWindow should be created before ofRunApp is called.
e.g.
#include "ofMain.h"
#include "ofAppiOSWindow.h"
#include "ofApp.h"
int main() {
// here are the most commonly used iOS window settings.
//------------------------------------------------------
ofiOSWindowSettings settings;
settings.enableRetina = false; // enables retina resolution if the device supports it.
settings.enableDepth = false; // enables depth buffer for 3d drawing.
settings.enableAntiAliasing = false; // enables anti-aliasing which smooths out graphics on the screen.
settings.numOfAntiAliasingSamples = 0; // number of samples used for anti-aliasing.
settings.enableHardwareOrientation = false; // enables native view orientation.
settings.enableHardwareOrientationAnimation = false; // enables native orientation changes to be animated.
settings.glesVersion = OFXIOS_RENDERER_ES1; // type of renderer to use, ES1, ES2, ES3
settings.windowMode = OF_FULLSCREEN;
ofCreateWindow(settings);
return ofRunApp(new ofApp);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in ofAppIOSWindow.mm at ofAppiOSWindow::getWindowSize(), then trace the window creation path through ofCreateWindow() and ofRunApp(). Compare the iOS initialization order with the desktop behavior and consider PR #4327 as prior context. Done means calls to ofGetWidth() and ofGetHeight() from member constructors do not cause bad access on iOS.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, ios
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100