Get middle of screen


#1

Hello,
I have already looked around to find a solution, but with no success. I want to get the coordinate to the middle of the window/screen, whether I am on OF_FULLSCREEN or on OF_WINDOW. I have tried using ofGetWindowMode() in the setup() to determine which screen mode I am on but it always comes out as OF_WINDOW, so it is not working. The only thing I can think of is that ofGetWindowMode() should not be used in the setup() but I don’t know where else it would go. Any help would be much appreciated!


#2

you can do the maths with ofGetWindowPositionX(), ofGetWindowPositionY(), ofGetWindowWidth(), ofGetWindowHeight(), ofGetScreenWidth(), ofGetScreenHeight()


#3

but is it possible to know whether my program is on OF_WINDOW or OF_FULLSCREEN?


#4

Your middle of the screen is always ofGetWidth()/2 and ofGetHeight()/2. If you’re storing them in some variables, update the variable is ofApp::windowResized(int x, int y) or just get the coordinates in your ofApp::update() for each frame.

Or did you mean the centre of the computer screen, and not the centre of the oF app window?


#5

if ofGetWindowMode is not working you can try comparing the window size and the screen size. if these are the same then it is fullscreen.


#6

I cannot seem to find the command to get screen size, I am able to use ofGetWindowSize() but not ofGetScreenSize(). Do you know how I could get the screen size?


#7

I want my program to be able to detect when it should get the centre of the screen (when it is on fullscreen) or when it should get the centre of the window (when it is on a window).


#8

Centre of the window is always ofGetWidth()/2 and ofGetHeight()/2 and when it’s full screen, it’s the same because then width == screen width and height == screen height.


#9

For some reason this isn’t working because my ofGetWidth() and ofGetHeight() remain the same even when I switch to a different screen mode. I’m not sure how/why this is happening.


#10

Make sure that you call ofGetWidth() and/or ofGetHeight() in either update() or draw() since these functions are being called every frame.

maybe this example is helpful. if not, please share your code.

void ofApp::draw(){
    float xCenter = ofGetWidth() / 2;
    float yCenter = ofGetHeight() / 2;
    ofDrawCircle(xCenter, yCenter, 50);
}

com-video-to-gif


#11

The only problem with this is that I want the ball to only be in the middle when I initially run the program, so I am trying to get the ball to be in the middle inside the setup() function, and this is working for when I run my program with OF_WINDOW, but not with OF_FULLSCREEN for some reason


#12

i guess the resolution specified in main.cpp does not match your screen resolution ( == fullscreen resolution). but you can ignore this if you move you code from setup() into the update() or draw() area.


#13

Let’s say your screen is 1920*1080

//main.cpp
ofSetupOpenGL( 1024,768, OF_WINDOW);

//ofApp
void ofApp::setup(){
    sw = ofGetWidth();
    sh = ofGetHeight();
}

void ofApp::draw(){
    ofBackground(0,0,0);
    ofDrawBitmapString( "sw: " + ofToString(sw) + " sh: " + ofToString(sh) + " w: " + ofToString(ofGetWidth()) + " h: " + ofToString(ofGetHeight()), 150,150);
}

Will print :
sw: 1024 sh: 768 w: 1024 h: 768
(w and h will change if you resize the window with your mouse)

//main.cpp
ofSetupOpenGL( 1024,768, OF_FULLSCREEN);

//ofApp
void ofApp::setup(){
    sw = ofGetWidth();
    sh = ofGetHeight();
}

void ofApp::draw(){
    ofBackground(0,0,0);
    ofDrawBitmapString( "sw: " + ofToString(sw) + " sh: " + ofToString(sh) + " w: " + ofToString(ofGetWidth()) + " h: " + ofToString(ofGetHeight()), 150,150);

}

Will print :
sw: 1024 sh: 768 w: 1920 h: 1080

This means that OF_FULLSCREEN parameter of ofSetupOpenGL if affected AFTER the setup() function.
I don’t know if this s a bug or not, but it is this way. (I think it is ?)


#14

Try putting ofToggleFullScreen() as the first thing in the setup function, and leave main to OF_WINDOW? What does that do?


#15

Hi,
The function is ofToggleFullscreen() with a minor ‘s’, and yeah, it does work like this.
I opened an issue on github though, because I think it should be automatic when OF_FULLSCREEN, shouldn’t it ?


#16

This actually makes my program work on full screen! However, it always goes to fullscreen whether i use OF_FULLSCREEN or OF_WINDOW


#17

Set up a quick little toggle in the Key Released function.

if (key == ' '){ //or any other key.
  ofToggleFullscreen();
}

Use that to toggle between full screen and windowed mode.


#18

Well, if you are looking for a real workaround:

// in setup function : 

    float appWidth;
    float appHeight;

    if(ofGetWindowMode() == OF_FULLSCREEN){
        appWidth = ofGetScreenWidth();
        appHeight = ofGetScreenHeight();
    }else {
        appWidth = ofGetWidth();
        appHeight = ofGetHeight();
    }
    ofLog() << "App size: " << appWidth << "x" << appHeight;

You can use appWidth and appHeight, they will be correct in the setup function.


#19

I just tried this, but it is not working when I use OF_FULLSCREEN


#20

I would prefer if my program detected whether it was initially set to OF_FULLSCREEN or OF_WINDOW