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
// 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>
usingnamespace 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;
}
elseif (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;
}
elseif (myresponse == 'f')
{
t.move(1);
std::cout << "the turtle steps forward and stops at " << t.x << ", " <<t.y << std::endl;
}
elseif (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;
}
elseif (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' );
}
#include "allocore/io/al_App.hpp"
#include "Gamma/Oscillator.h"
usingnamespace 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 = newfloat[n];
mBufferSize = n;
}
// Audio callback
virtualvoid 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
virtualvoid 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]);
}
}
virtualvoid onDraw(Graphics& g, const Viewpoint& v){
// have Graphics draw the mesh
g.draw(mMesh);
}
};
int main(){
MyApp().start();
}
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).
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