how to make my turtle draw

Hey everybody I have this simple Turtle code here. When I run the code in the terminal (on mac) I can move my turtle around by typing f (forward), l (left), r (right), b (backwards). I get the turtles current x & y position.
I would really like to draw the movement of the turtle to the screen. I have searched google for a multiple guides on how to do so, but I havent come close to anything yet.

My teacher told me to use onDraw(). I would appreciate some help, and please just give me some hints, dont do the code for me.

If anyone knows about a nice tutorial, guide or something to fix my problem I would like to get that too

here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// my stuff

// as the player of this game you have three choices, move the turtle forward and
// choose its direction - Either left or right

#include <iostream>
#include <math.h>
using namespace std;

// the turtle class

class Turtle {
public:

float x, y;

Turtle(){
 
 x = 0; // this is the starting point for the x
 y = 0; // this is the starting point for the y
 mUx = 1;
 mUy = 0;
 
 }

void move(float ds){
	x += mUx * ds;
	y += mUy * ds;

}

void turn(float ang){
	float ux = mUx;
	float uy = mUy;
	mUx = ux * cos(ang) - uy * sin(ang);
	mUy = uy * cos(ang) + ux * sin(ang);
	

}

private: 
	// we make the unit vector private because we provide
	// a sperate interface for changing them
	float mUx, mUy;
		
};


int main(){

Turtle t;

char myresponse; // I use here char because i want to write a command and not type an int

std::cout << "your turtle just woke up at " << t.x << "," << t.y << " what should it do?" << std::endl;

do
{

cin >> myresponse;

// Here I will use conditionals

if (myresponse == 'l')
{
t.turn(M_PI/2);
std::cout << "the turtle turns left and stays at " << t.x << ", " <<t.y << std::endl;
} 

else if (myresponse == 'r')
{
t.turn(M_PI/-2); // I divide with -2 because the calculation t.turn(M_PI/-2); turns
				 // the angle left by default. To turn the angle right I divide with -2 
std::cout << "the turtle turns right and stays at " << t.x << ", " <<t.y << std::endl;
}

else if (myresponse == 'f')
{
t.move(1); 
std::cout << "the turtle steps forward and stops at " << t.x << ", " <<t.y << std::endl;
}

else if (myresponse == 'b')
{
t.move(-1); //forward means to move along the x-axis, so to move back I need to put -1
std::cout << "the turtle moves backwards and stops at " << t.x << ", " <<t.y << std::endl;
}

else if (myresponse == 's')
{

	std::cout << "the turtle stops to draw a square" << std::endl; 
	for(int i=0;i<4;++i){
 	std::cout << t.x << ", " <<t.y << std::endl;
 	t.move(5); // we put 1 here "optional" this is ds
 	t.turn(M_PI/2); // why 4, because 360 divided with 4 is 90
 	}	
	std::cout << "done drawing" << t.x << ", " << t.y << std::endl;
} 

}while( myresponse != 'Q' );

}


mvh Bjørn Dam Larsen
Last edited on
What library do you use for the graphics?
Hey Im using gamma from Allosphere!

and my teacher told me to have a look at this audio scope code as an inspiration. I had to keep an eye out for onDraw and mMesh vertex.

"Look at the "Audio Scope" examples to see how to do the drawing. The first two arguments to mMesh.vertex should get the Turtle x,y position."

I understand what he is referring to, but im a bit clueless what to do next!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "allocore/io/al_App.hpp"
#include "Gamma/Oscillator.h"

using namespace al;

class MyApp : public App{
public:

	gam::Sine<> mSineWave1;
	gam::Sine<> mSineWave2;
	Mesh mMesh;

	float * mBuffer;	// pointer that is an array
	int mBufferSize;	// size of the buffer

	MyApp(){
		// initialize to point to nothing
		mBuffer = NULL;
		mBufferSize = 0;
		resizeBuffer(1024);
	
		nav().pos(0,0,4);
		initWindow();
		
		// Our audio block size must match our buffer size!
		// This is not the most flexible.
		initAudio(44100, 1024, 2,1);
	}
	
	
	void resizeBuffer(int n){
		// if array already allocated, deallocate it first
		if(NULL != mBuffer){
			delete[] mBuffer;
		}
	
		// allocate array of floats of size n
		mBuffer = new float[n];
		mBufferSize = n;
	}
	

	// Audio callback
	virtual void onSound(AudioIOData& io){
		gam::Sync::master().spu(io.fps());
	
		mSineWave1.freq(440);
		mSineWave2.freq(219);
	
		while(io()){
			float in = io.in(0);
			//float out1 = (mSineWave1() + mSineWave2()) * 0.2;
			float out2 = 0;
			
			float out1 = in;

			// get current frame number
			int frameNumber = io.frame();
			
			// treat indices into buffer as time
			mBuffer[frameNumber] = out1;

			io.out(0) = out1;
			io.out(1) = out2;
		}
	}

	// Graphics callbacks
	virtual void onAnimate(double dt){
	
		// reset empties all vertices in the mesh
		mMesh.reset();
		
		// set the drawing primitive
		mMesh.primitive(Graphics::LINE_STRIP);
	
		for(int i=0; i<mBufferSize; ++i){
			// add vertex position to mesh
			mMesh.vertex(i*0.01, mBuffer[i]);
		}
	}

	virtual void onDraw(Graphics& g, const Viewpoint& v){
		// have Graphics draw the mesh
		g.draw(mMesh);
	}
};


int main(){
	MyApp().start();
}
So far I have to use the init.window(), im sure about that at least
I guess you will have to create your own subclass of App in your Turtle program.

In the above program it uses a Mesh to draw. I think you can use a Mesh in your program too.

In the onAnimate function you can see how they do to add vertices (positions) to the Mesh. To add a vertex at position (x, y) you do mMesh.vertex(x, y);. The coordinates might need to be scaled.
mMesh.primitive(Graphics::LINE_STRIP); is used so that a line will be drawn between each vertex and the next when the Mesh is drawn, perfect for the turtle graphics.

The onDraw function is called to update the screen. All it does is drawing the Mesh g.draw(mMesh);.

You can update the Mesh in any function but onAnimate is probably a good place if you want the drawing to be animated. I think that onAnimate is called automatically from time to time. The time since onAnimate was last called is the dt parameter (probably in milliseconds).
Since you are on Mac you can have a look at
http://web.eecs.umich.edu/~sugih/courses/eecs487/glut-howto/sample.c
and
http://web.eecs.umich.edu/~sugih/courses/eecs487/glut-howto/#mac

OpenGL is already installed on Mac with Xcode so you can use that. It might be simpler to use it to draw lines than using a fill-fledged game development library
Hey Peter87, I appreciate your feedback and I understand why I have to put x and y into the mesh and so on.

My problem is just how to exactly create a sub class of App in my Turtle program, is it a very complicated thing to do?,

As I understand the my App is already a subclass in the audio scope, but how to join it together with the Turtle code?

Topic archived. No new replies allowed.