@dicer2000 wrote:
I’ve got a pretty basic program going, but I can’t see why it keeps throwing this exception. It’s in Visual Studio 2019 (not sure if that’s the problem?) Sometimes it works fine, but most of the time, I get “HEAP CORRUPTION DETECTED”. Any ideas?
ofApp.h
#pragma once #include "ofMain.h" #define MAP_HEIGHT 20 #define MAP_WIDTH 20 class ofApp : public ofBaseApp { private: int nMapArray[MAP_HEIGHT * MAP_WIDTH] = { 0 }; bool bSetup = false; // private methods int getArrayVal(int x, int y); void setArrayVal(int x, int y, int value); void makeNewBoard(); 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); };
ofApp.cpp
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ ofBackground(0, 0, 0); // Do the initial draw makeNewBoard(); } void ofApp::makeNewBoard() { // Set the board to all zeros for (int i = 0; i < MAP_HEIGHT; i++) for (int j = 0; j < MAP_WIDTH; j++) setArrayVal(j, i, 0); // Setup arrays int nStepCount = 1000; int xPos = MAP_WIDTH / 2; int yPos = MAP_HEIGHT / 2; while (nStepCount > 0) { // Get a random next drunkard step int nNextStep = (int)ofRandom(0.0, 4.0); switch (nNextStep) { case 0: // North if (yPos > 0) yPos--; break; case 1: // East if (xPos < MAP_WIDTH) xPos++; break; case 2: // South if (yPos < MAP_HEIGHT) yPos++; break; default: // West if (xPos > 0) xPos--; break; } // Get the current count int nOrigValue = getArrayVal(xPos, yPos); // Set the count setArrayVal(xPos, yPos, ++nOrigValue); nStepCount--; } bSetup = true; } int ofApp::getArrayVal(int x, int y) { return nMapArray[y * MAP_WIDTH + x]; } void ofApp::setArrayVal(int x, int y, int value) { nMapArray[y * MAP_WIDTH + x] = value; } //-------------------------------------------------------------- void ofApp::update(){ } //-------------------------------------------------------------- void ofApp::draw() { // ofPushMatrix(); // Draw the gameboard if (bSetup) { ofTranslate(5.0, 5.0); for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { // Determine the color via map float val = getArrayVal(x, y); float colorVal = ofMap(val, 0.0, 30.0, 0.0, 255.0); ofSetColor(ofColor(colorVal, 10.0, colorVal)); // Draw the value ofDrawRectangle(x * 20.0, y * 20.0, 20.0, 20.0); } } } // ofPopMatrix(); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ if(key==' ') makeNewBoard(); } //-------------------------------------------------------------- void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseEntered(int x, int y){ } //-------------------------------------------------------------- void ofApp::mouseExited(int x, int y){ } //-------------------------------------------------------------- void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }
Posts: 1
Participants: 1