### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` ``````// 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 #include 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 << ", " <

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!!

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` ``````#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
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.