Problems with drawing using of ofGetWidth(), ofGetHeight()

Problems with drawing using of ofGetWidth(), ofGetHeight().
I made a code so that no blank spaces (the screen is filled with multiple squares) but it keeps coming out. There is no problem when using full screen mode. Do you know how to solve this problem when in Windows mode?

You need to share some code for us to help you with this.

#pragma once

#include “ofMain.h”

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);

vector<vectorglm::vec2> squarePos;

};

#include “ofApp.h”

//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(255);

squarePos.resize(8);

for(int i = 0; i < squarePos.size(); i++) {
    squarePos[i].resize(8);
}

for(int y = 0; y < squarePos.size(); y++) {
    for(int x = 0; x < squarePos[y].size(); x++) {
        squarePos[y][x] = glm::vec2(x * ofGetWidth() / 8, y * ofGetHeight() / 8); 
    }
}

}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){

ofSetColor(0, 0, 0, 100);

for(int y = 0; y < squarePos.size(); y++) {
    for(int x = 0; x < squarePos[y].size(); x++) {
        
        ofDrawRectangle(squarePos[y][x].x, squarePos[y][x].y, ofGetWidth()/ 8, ofGetHeight() / 8);
        
    }
}

}

I wrote the code like this, although it changed a little bit in the process of the transfer. What’s the problem? Thank you.

You need to cast it to the divisions explicitly to floats at first glance. Try doing ofGetWidth()/8.0 as int divided by an int would only return a int in C++. (Something I learn the hard way too!)

In all likelihood your full screen resolution becomes perfectly divisible by 8 which is why it’s not a problem but when it’s 768/8, your return values would be in decimals, which won’t be the case if you don’t explicitly cast the divisor as a float.

Here’s my own post on this from three years ago!

It’s solved! Thank you!!! :):heart_eyes: