diff --git a/.gitignore b/.gitignore index 281c03b..a005807 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,11 @@ *.sln *.vcx* *.workspace* +addons.make config.make Makefile + +*.vscode *.xcodeproj *.plist *.xcconfig @@ -27,6 +30,7 @@ project.xcworkspace .svn/ obj/ -bin/ +bin/** +!bin/data build/ -!data/ \ No newline at end of file +!data/ diff --git a/chp0-intro-01-randomwalk-traditional/README.md b/chp0-intro-01-randomwalk-traditional/README.md new file mode 100644 index 0000000..28e51fd --- /dev/null +++ b/chp0-intro-01-randomwalk-traditional/README.md @@ -0,0 +1,5 @@ +# Example I.1: Traditional Random Walk + +![screenshot](./bin/data/screenshot-01.png) + + diff --git a/chp0-intro-01-randomwalk-traditional/bin/data/screenshot-01.png b/chp0-intro-01-randomwalk-traditional/bin/data/screenshot-01.png new file mode 100644 index 0000000..5491f45 Binary files /dev/null and b/chp0-intro-01-randomwalk-traditional/bin/data/screenshot-01.png differ diff --git a/chp0-intro-01-randomwalk-traditional/src/main.cpp b/chp0-intro-01-randomwalk-traditional/src/main.cpp index 6ca3557..3f4fdf0 100644 --- a/chp0-intro-01-randomwalk-traditional/src/main.cpp +++ b/chp0-intro-01-randomwalk-traditional/src/main.cpp @@ -3,7 +3,7 @@ //======================================================================== int main( ){ - ofSetupOpenGL(800,200,OF_WINDOW); // <-------- setup the GL context + ofSetupOpenGL(1024, 768, OF_WINDOW); // <-------- setup the GL context // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN diff --git a/chp0-intro-01-randomwalk-traditional/src/ofApp.cpp b/chp0-intro-01-randomwalk-traditional/src/ofApp.cpp index 2aced09..a67e55f 100644 --- a/chp0-intro-01-randomwalk-traditional/src/ofApp.cpp +++ b/chp0-intro-01-randomwalk-traditional/src/ofApp.cpp @@ -2,26 +2,26 @@ //-------------------------------------------------------------- void ofApp::setup(){ - - ofBackground(255); - ofSetBackgroundAuto(false); - - w = walker(); + ofSetBackgroundColor(m_backgroundColor); + ofSetBackgroundAuto(false); + ofSetCircleResolution(100); + + auto position = glm::vec2 { ofGetWidth() / 2, ofGetHeight() / 2 }; + + m_walker.setup(position, 2); } //-------------------------------------------------------------- void ofApp::update(){ - - ofSetFrameRate(mouseX); - w.update(); + m_walker.update(); } //-------------------------------------------------------------- void ofApp::draw(){ - - ofTranslate(ofGetWidth()/2, ofGetHeight()/2); - ofScale(w.scale, w.scale); - w.draw(); + ofFill(); + ofSetColor(m_walkerColor); + + m_walker.draw(); } //-------------------------------------------------------------- diff --git a/chp0-intro-01-randomwalk-traditional/src/ofApp.h b/chp0-intro-01-randomwalk-traditional/src/ofApp.h index 29f986f..09cf221 100644 --- a/chp0-intro-01-randomwalk-traditional/src/ofApp.h +++ b/chp0-intro-01-randomwalk-traditional/src/ofApp.h @@ -1,11 +1,17 @@ #pragma once #include "ofMain.h" -#include "walker.h" +#include "Walker.h" -class ofApp : public ofBaseApp{ +class ofApp : public ofBaseApp { + private: + ofColor m_backgroundColor { 40, 44, 52 }; + ofColor m_walkerColor { 140, 132, 247 }; + + Walker m_walker; + + public: -public: void setup(); void update(); void draw(); @@ -19,6 +25,4 @@ class ofApp : public ofBaseApp{ void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); - - walker w; }; diff --git a/chp0-intro-01-randomwalk-traditional/src/walker.cpp b/chp0-intro-01-randomwalk-traditional/src/walker.cpp index 5a61fc6..2599db2 100644 --- a/chp0-intro-01-randomwalk-traditional/src/walker.cpp +++ b/chp0-intro-01-randomwalk-traditional/src/walker.cpp @@ -1,36 +1,35 @@ +#include "Walker.h" -#include "walker.h" -walker::walker() { - x = 0; - y = 0; - scale = 5; +void Walker::setup(const glm::vec2& position, int radius) { + m_position = position; + m_radius = radius; } -void walker::update() { - int choice = int(ofRandom(4)); - cout << choice << endl; - - if (choice == 0) x++; - else if (choice == 1) x--; - else if (choice == 2) y++; - else if (choice == 3) y--; - - int rangeWidth = (ofGetWidth()/2) /scale; - int rangeHeight = (ofGetHeight()/2) /scale; - - x = constrain(x, -rangeWidth, rangeWidth); - y = constrain(y, -rangeHeight, rangeHeight); +void Walker::update() { + step(); + keepInBounds(); } -void walker::draw() { - ofSetColor(0,20); - ofDrawRectangle(x, y, 1, 1); +void Walker::draw() { + ofDrawCircle(m_position, m_radius); } -int walker::constrain(int input, int min, int max) { - return (input < min) ? min : ((input > max) ? max : input); -} \ No newline at end of file +void Walker::step() { + int choice = ofRandom(4); + cout << "New step choice: " << choice << "\n"; + + if (choice == 0) m_position.x += (m_radius * 2); + else if (choice == 1) m_position.x -= (m_radius * 2); + else if (choice == 2) m_position.y += (m_radius * 2); + else if (choice == 3) m_position.y -= (m_radius * 2); +} + + +void Walker::keepInBounds() { + m_position.x = ofClamp(m_position.x, 0.0, ofGetWidth()); + m_position.y = ofClamp(m_position.y, 0.0, ofGetHeight()); +} diff --git a/chp0-intro-01-randomwalk-traditional/src/walker.h b/chp0-intro-01-randomwalk-traditional/src/walker.h index 9973c21..22cf3bc 100644 --- a/chp0-intro-01-randomwalk-traditional/src/walker.h +++ b/chp0-intro-01-randomwalk-traditional/src/walker.h @@ -1,17 +1,21 @@ #pragma once + #include "ofMain.h" -class walker { -public: - walker(); +class Walker { + private: + glm::vec2 m_position; + int m_radius; + + void step(); + void keepInBounds(); + public: + void setup(const glm::vec2& position, int radius = 1); void update(); void draw(); - int constrain(int, int, int); - - int x; - int y; - int scale; + const glm::vec2& getPosition() { return m_position; } + void setPosition(const glm::vec2& position) { m_position = position; } };