I'm trying to render a triangle using SDL2 on my MAC (M1), however, the triangle i'm able to generate is too much pixellated and unnecessary pixels are being rendered.
Output:
My Code:
int main(int argc, char *argv[])
{
// returns zero on success else non-zero
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window* win = SDL_CreateWindow("GAME",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
900, 900, SDL_WINDOW_ALLOW_HIGHDPI);
if(!win)
{
std::cout << "Failed to create window\n";
return -1;
}
int *w_t = new int ();
int *h_t = new int ();
SDL_GetWindowSize(win,w_t,h_t);
std::cout<<"width:"<<*w_t<<"height:"<<*h_t;
SDL_Renderer* brush = SDL_CreateRenderer(win, -1, 0);
SDL_SetRenderDrawColor(brush, 255, 0, 0, 0);
SDL_Point a = {0,500};
SDL_Point b = {800,500};
SDL_Point c = {250,250};
bool quit = false;
SDL_Event e;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
SDL_RenderDrawLine(brush, a.x, a.y, b.x, b.y);
SDL_RenderDrawLine(brush, a.x, a.y, c.x, c.y);
SDL_RenderDrawLine(brush, b.x, b.y, c.x, c.y);
SDL_RenderPresent(brush);
}
return 0;
}
- OS: macOS Monterey
- IDE: Xcode
- Compiler: Clang
CodePudding user response:
Try clearing the renderer before drawing the lines:
SDL_SetRenderDrawColor(brush, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(brush);
SDL_SetRenderDrawColor(brush, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(brush, a.x, a.y, b.x, b.y);
SDL_RenderDrawLine(brush, a.x, a.y, c.x, c.y);
SDL_RenderDrawLine(brush, b.x, b.y, c.x, c.y);
SDL_RenderPresent(brush);