Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
*.sln
*.vcx*
*.workspace*
addons.make
config.make
Makefile

*.vscode
*.xcodeproj
*.plist
*.xcconfig
Expand All @@ -27,6 +30,7 @@ project.xcworkspace

.svn/
obj/
bin/
bin/**
!bin/data
build/
!data/
!data/
5 changes: 5 additions & 0 deletions chp0-intro-01-randomwalk-traditional/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example I.1: Traditional Random Walk

![screenshot](./bin/data/screenshot-01.png)


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion chp0-intro-01-randomwalk-traditional/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions chp0-intro-01-randomwalk-traditional/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

//--------------------------------------------------------------
Expand Down
14 changes: 9 additions & 5 deletions chp0-intro-01-randomwalk-traditional/src/ofApp.h
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -19,6 +25,4 @@ class ofApp : public ofBaseApp{
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);

walker w;
};
49 changes: 24 additions & 25 deletions chp0-intro-01-randomwalk-traditional/src/walker.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
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());
}
20 changes: 12 additions & 8 deletions chp0-intro-01-randomwalk-traditional/src/walker.h
Original file line number Diff line number Diff line change
@@ -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; }
};