I’ve been trying to draw a circle and so far I have the following, that wont render for some reason that I can’t figure out. I’d appreciate any help.
Particle.cpp:
void Particle::DrawParticle(SDL_Renderer* renderer, int x0, int y0, int radius) {
Radius = radius;
float pi = 3.14159265358979323846264338327950288419716939937510;
float pih = pi / 2.0;
const int prec = 27;
float theta = 0;
int x = (float)Radius * cos(theta);
int y = (float)Radius * sin(theta);
int x1 = x;
int y1 = y;
float step = pih / (float)prec;
for (theta = step; theta <= pih; theta += step)
{
x1 = (float)Radius * cosf(theta) + 0.5;
y1 = (float)Radius * sinf(theta) + 0.5;
if ((x != x1) || (y != y1))
{
SDL_RenderDrawLine(renderer, x0 + x, y0 - y, x0 + x1, y0 - y1);
SDL_RenderDrawLine(renderer, x0 - x, y0 - y, x0 - x1, y0 - y1);
SDL_RenderDrawLine(renderer, x0 - x, y0 + y, x0 - x1, y0 + y1);
SDL_RenderDrawLine(renderer, x0 + x, y0 + y, x0 + x1, y0 + y1);
}
x = x1;
y = y1;
}
if (x != 0)
{
x = 0;
SDL_RenderDrawLine(renderer, x0 + x, y0 - y, x0 + x1, y0 - y1);
SDL_RenderDrawLine(renderer, x0 - x, y0 - y, x0 - x1, y0 - y1);
SDL_RenderDrawLine(renderer, x0 - x, y0 + y, x0 - x1, y0 + y1);
SDL_RenderDrawLine(renderer, x0 + x, y0 + y, x0 + x1, y0 + y1);
}
}
SDLmain.cpp:
#include "SDLmain.h"
SDLMain::SDLMain(){}
SDLMain::~SDLMain(){}
Particle* particle = nullptr;
void SDLMain::Init(const char* TITLE, int XPOS, int YPOS, int WIDTH, int HEIGHT, bool FULLSCREEN){
int FLAGS = 0;
if (FULLSCREEN) {
FLAGS = SDL_WINDOW_FULLSCREEN;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
window = SDL_CreateWindow(TITLE, XPOS, YPOS, WIDTH, HEIGHT, FLAGS);
if (window) {
std::cout << "SDL Window was successfully created!" << std::endl;
}
else {
std::cout << "SDL Window failed. SDL Error: " << SDL_GetError() << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
std::cout << "SDL Renderer was successfully created!" << std::endl;
}
else {
std::cout << "SDL Renderer failed. SDL Error: " << SDL_GetError() << std::endl;
}
isOnline = true;
}
else {
isOnline = false;
}
particle = new Particle();
particle->Init();
}
void SDLMain::handleEvents() {
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
isOnline = false;
break;
default:
break;
}
}
void SDLMain::Update(){
particle->Update();
}
void SDLMain::Render(){
SDL_RenderClear(renderer);
particle->DrawParticle(renderer, 150, 100, 50);
SDL_RenderPresent(renderer);
}
void SDLMain::Clean(){
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
particle->Clean();
SDL_Quit();
std::cout << "SDL System cleaned!" << std::endl;
}
EDIT:
Figured it out, the drawing & window rendering was using the same color and therefor making it impossible to see the drawing, just changed the colors and it worked.