[C++] SDL2 Rendering for Turtle Graphics program via /r/learnprogramming


[C++] SDL2 Rendering for Turtle Graphics program

I am trying to make a turtle graphics program as a sense of practice for SDL2, I have pieced together this code here:

turtle.h:

#include "../Util.h" // Struct to hold the location of the turtle struct character { int x = 10; int y = 10; // Variables for the previous coords int px = 10; int py = 10; }; class Turtle { public: Turtle(); void run(); private: void handle_input(SDL_Event e); void move(character &c, int x, int y); void update_character(character &c); private: Util m_util; // Object for window and useful tools character m_turt; // Turtle character object }; 

turtle.cpp:

#include "Turtle.h" #include <iostream> Turtle::Turtle(){ m_util.set_title("Turtle Graphics"); m_util.set_resoultion(500, 500); m_util.set_scale(20); this->run(); } void Turtle::run() { while (1) { this->handle_input(m_util.m_event); // Clear and set the renderer to white SDL_SetRenderDrawColor(m_util.m_renderer, 240, 240, 240, 255); SDL_RenderClear(m_util.m_renderer); //SDL_SetRenderDrawColor(m_util.m_renderer, 0, 0, 0, 255); //SDL_RenderDrawPoint(m_util.m_renderer, 10, 10); this->update_character(this->m_turt); SDL_RenderPresent(m_util.m_renderer); } } void Turtle::handle_input(SDL_Event e) { if (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) SDL_Quit(); else if (e.type == SDL_KEYDOWN && e.key.repeat == 0) { if (e.key.keysym.sym == SDLK_RIGHT) this->move(m_turt, 1, 0); else if (e.key.keysym.sym == SDLK_LEFT) this->move(m_turt, -1, 0); else if (e.key.keysym.sym == SDLK_UP) this->move(m_turt, 0, -1); else if (e.key.keysym.sym == SDLK_DOWN) this->move(m_turt, 0, 1); } } } void Turtle::move(character &c, int x, int y) { if (x > 0) { c.px = c.x; c.x++; } if (x < 0) { c.px = c.x; c.x--; } if (y > 0) { c.py = c.y; c.y++; } if (y < 0) { c.py = c.y; c.y--; } // Log previous and current points std::cout << "===============\n"; std::cout << "P " << c.px << ", " << c.py << std::endl; std::cout << "C " << c.x << ", " << c.y << std::endl; } void Turtle::update_character(character &c) { SDL_SetRenderDrawColor(m_util.m_renderer, 255, 0, 255, 255); SDL_RenderDrawLine(m_util.m_renderer, c.px, c.py, c.x, c.y); } 

I can't get the previous lines to stay that I draw, and I cant draw completely horizontal or vertical lines, they always are diagonal which I presume is from how they are rendered. How would I store the previous lines, and then have them in the direction the way they should be?

Submitted July 15, 2017 at 08:26AM by BlakeBarnes00
via reddit http://ift.tt/2tUsFg3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s