I've created a program using SDL in which a rectangle continuously collides within the walls of the program, but the collision checking is not working properly.
Here is the code:`
int main(int argc, char *argv[]){
//variable Initialization]
width = height = 45;
srcX = srcY = 0;
destY = destX = 0;
vlc = 1;
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Bouncing Balls","./ball.jpg");
backg = IMG_Load("./back.png");
ball = IMG_Load("./ball.jpg");
while (checkBounce){
//Increase velocity
destX += vlc;
destY += vlc;
//Collision Checking
if (destX < 0){
destX = 0;
vlc = -vlc;
destX += vlc;
}
if (destY < 0){
destY = 0;
vlc = -vlc;
destY += vlc;
}
if (destY + height > 480){
destY = 480 - height;
vlc = -vlc;
}
if (destX + width > 640){
destX = 640 - width;
vlc = -vlc;
}
if (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT)
checkBounce = false;
}
//Applying Surfaces
applySurface(0, 0, backg, screen);
applyBall(srcX, srcY, destX, destY, width, height, ball, screen);
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}
Here is the gif Image What is happening :Bouncing Rectangle.gif
I'm assuming that the expected result is for the rectangle to bounce off of the walls correctly?
You need to separate your velocity in to x and y components rather than using a single number. This is because velocity is two dimensional.
Your program is causing both the x and y components to become negative whenever a collision is detected. This causes the rectangle to bounce backwards along its path.
Here's an edit:
int main(int argc, char *argv[]){
//variable Initialization]
width = height = 45;
srcX = srcY = 0;
destY = destX = 0;
vlcX = 1;
vlcY = 1;
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Bouncing Balls","./ball.jpg");
backg = IMG_Load("./back.png");
ball = IMG_Load("./ball.jpg");
while (checkBounce){
//Increase velocity
destX += vlcX;
destY += vlcY;
//Collision Checking
if (destX < 0){
destX = 0;
vlcX = -vlcX;
destX += vlcX;
}
if (destY < 0){
destY = 0;
vlcY = -vlcY;
destY += vlcY;
}
if (destY + height > 480){
destY = 480 - height;
vlcY = -vlcY;
}
if (destX + width > 640){
destX = 640 - width;
vlcX = -vlcX;
}
if (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT)
checkBounce = false;
}
//Applying Surfaces
applySurface(0, 0, backg, screen);
applyBall(srcX, srcY, destX, destY, width, height, ball, screen);
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}
Related
I started Learning C++ yesterday And In that time i was rewriting my java "Falling Sand" Sandbox Game code in C++ using SFML (bit simplified since i don't know C++). but Performance in C++ was much worse in than java, what could be the reason for it, I Know this is very unfocused question, but my code is simple, i probably have a newbie mistakes which should be easy to correct.
#include <SFML/Graphics.hpp>
#include <iostream>
sf::Clock sclock;
const int WIDTH = 1440, HEIGHT = 960;
const char Blank = 0, Sand = 1, Water = 2;
const char* title = "Sandbox Simulation";
char map[WIDTH*HEIGHT];
sf::Vector2i mousePos;
int dist(int x1, int x2, int y1, int y2) {
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
int localBrushSize = 48;
short halfBrush = (short)floor(localBrushSize / 2);
char chosen = Sand;
void place() {
int randY = 0;
int randX = 0;
randX = randY = 1;
for (int y = mousePos.y - halfBrush; y <= mousePos.y + halfBrush; y += randY) {
for (int x = mousePos.x - halfBrush; x <= mousePos.x + halfBrush; x += randX) {
int I = x + y * WIDTH;
int distance = dist(mousePos.x, x, mousePos.y, y);
if (distance < halfBrush && I > 0) {
map[I] = chosen;
}
}
}
}
float Delta_Time() {
return sclock.restart().asSeconds();
}
int main() {
map[11111] = 2;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), title);
sf::Event evnt;
sf::RectangleShape pixel(sf::Vector2f(1.0f, 1.0f));
window.clear();
while (window.isOpen()) {
while (window.pollEvent(evnt)) {
switch (evnt.type) {
case sf::Event::Closed:
window.close();
break;
}
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
mousePos = sf::Mouse::getPosition(window);
place();
}
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int I = x + y * WIDTH;
switch (map[I]) {
case Sand:
pixel.setPosition(x, y);
pixel.setFillColor(sf::Color::Yellow);
window.draw(pixel);
break;
case Water:
pixel.setPosition(x, y);
pixel.setFillColor(sf::Color::Cyan);
window.draw(pixel);
break;
}
}
}
window.display();
}
return 0;
}
You might be able to make a cached / "framebuffer" like this
TOTALLY untested concept code. WIDTH/HEIGHT might be mixed up, endianess is not OK, etc.
sf::Image image;
sf:Image.create(WIDTH, HEIGHT, sf::Color(0, 0, 0));
sf::Sprite sprite;
std::array<sf::Uint8, WIDTH * HEIGHT * 4> pixels; // you can reuse this. The 4 is the size of RGBA
...
for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
int offset = (x + y * WIDTH) * 4;
pixels[offset] = sf::Color::Yellow.toInteger(); // in case BIG/Litte endian confusion you might have to do the below.
//pixels[offset + 0 ] = 255; // R?
//pixels[offset + 1 ] = 255; // G?
//pixels[offset + 2 ] = 255; // B?
//pixels[offset + 3 ] = 255; // A?
}
}
image.LoadFromPixels(WIDTH, HEIGHT, pixels);
sprite.SetImage(image);
window.Draw(sprite);
window.Display();
Hello I'm new to C++ SFML. I'm supposed to draw some rectangles and render their AABB while rotating and I want to detect if the dimensions set for them intersect another rotating AABB rectangle. Here is what I use to detect them.
Is it enough to check it that way if theyre rotating? would i need to use stuff like the separating axis theorem? or is there a way to not need to use that if its just an AABB than an OBB
#define RECT 5
sf::RectangleShape Rect[RECT];
Rect[0].setSize(sf::Vector2f(50.0f, 50.0f));
Rect[1].setSize(sf::Vector2f(50.0f, 100.0f));
Rect[2].setSize(sf::Vector2f(60.0f, 80.0f));
Rect[3].setSize(sf::Vector2f(100.0f, 60.0f));
Rect[4].setSize(sf::Vector2f(30.0f, 250.0f));
sf::Vector2f MinPoint[RECT];
sf::Vector2f MaxPoint[RECT];
for (int x = 0; x < RECT; x++)
{
//Starting Position
Rect[x].setOrigin(Rect[x].getSize().x / 2, Rect[x].getSize().y / 2);
xpos += 150;
Rect[x].setPosition(xpos, ypos);
colcount++;
if (colcount == 3)
{
xpos = 0;
ypos += 200;
colcount = 0;
}
Rect[x].setFillColor(sf::Color::Red);
}
while (window.isOpen())
{
window.clear(sf::Color::Black);
//Drawing Shapes
for (int x = 0; x < RECT; x++)
{
window.draw(Rect[x]);
}
Rect[0].rotate(90*3.14/180);
Rect[1].rotate(12 * 3.14 / 180);
Rect[2].rotate(10 * 3.14 / 180);
Rect[3].rotate(180 * 3.14 / 180);
Rect[4].rotate(360 * 3.14 / 180);
for (int i = 0; i < RECT; i++)
{
MinPoint[i].x = Rect[i].getPosition().x - (Rect[i].getSize().x / 2);
MaxPoint[i].x = Rect[i].getPosition().x + (Rect[i].getSize().x / 2);
MinPoint[i].y = Rect[i].getPosition().y - (Rect[i].getSize().y / 2);
MaxPoint[i].y = Rect[i].getPosition().y + (Rect[i].getSize().y / 2);
}
//Collision Detection
for (int i = 0; i < RECT; i++)
{
for (int j = i + 1; j < RECT; j++)
{
if (i != j)
{
if (MaxPoint[i].x >= MinPoint[j].x && MaxPoint[j].x >= MinPoint[i].x && MaxPoint[i].y >= MinPoint[j].y && MaxPoint[j].y >= MinPoint[i].y)
{
Rect[i].setFillColor(sf::Color::Green);
Rect[j].setFillColor(sf::Color::Green);
}
}
}
}
Apparently all I needed to do was make another set of transparent rectangles with outlines that were set at the same position as my rotating rectangle boxes then set their sizes to getGlobalBounds of my rotating rectangles. the collision check would then instead be put under these transparent bounding boxes instead of the rotating rectangle itself.
#define RECT 5
sf::RectangleShape Rect[RECT];
sf::RectangleShape AABB[RECT];
Rect[0].setSize(sf::Vector2f(50.0f, 50.0f));
Rect[1].setSize(sf::Vector2f(50.0f, 100.0f));
Rect[2].setSize(sf::Vector2f(60.0f, 80.0f));
Rect[3].setSize(sf::Vector2f(100.0f, 60.0f));
Rect[4].setSize(sf::Vector2f(30.0f, 250.0f));
sf::Vector2f MinPoint[RECT];
sf::Vector2f MaxPoint[RECT];
for (int x = 0; x < RECT; x++)
{
//Starting Position
Rect[x].setOrigin(Rect[x].getSize().x / 2, Rect[x].getSize().y / 2);
AABB[x].setOrigin(AABB[x].getSize().x / 2, AABB[x].getSize().y / 2);
xpos += 150;
Rect[x].setPosition(xpos, ypos);
AABB[x].setSize(sf::Vector2f(Rect[x].getGlobalBounds().width, Rect[x].getGlobalBounds().height));
AABB[x].setPosition(Rect[x].getPosition().x, Rect[x].getPosition().y);
colcount++;
if (colcount == 3)
{
xpos = 0;
ypos += 200;
colcount = 0;
}
Rect[x].setFillColor(sf::Color::Red);
AABB[x].setFillColor(sf::Color::Transparent);
AABB[x].setOutlineThickness(1);
AABB[x].setOutlineColor(sf::Color::White);
}
while (window.isOpen())
{
window.clear(sf::Color::Black);
//Drawing Shapes
for (int x = 0; x < RECT; x++)
{
window.draw(Rect[x]);
window.draw(AABB[x]);
}
//Rotation
Rect[0].rotate(1);
Rect[1].rotate(45);
Rect[2].rotate(11.25);
Rect[3].rotate(5.625);
Rect[4].rotate(22.5);
for (int i = 0; i < RECT; i++)
{
MinPoint[i].x = AABB[i].getPosition().x - (AABB[i].getSize().x / 2);
MaxPoint[i].x = AABB[i].getPosition().x + (AABB[i].getSize().x / 2);
MinPoint[i].y = AABB[i].getPosition().y - (AABB[i].getSize().y / 2);
MaxPoint[i].y = AABB[i].getPosition().y + (AABB[i].getSize().y / 2);
AABB[i].setOrigin(AABB[i].getSize().x / 2, AABB[i].getSize().y / 2);
AABB[i].setSize(sf::Vector2f(Rect[i].getGlobalBounds().width, Rect[i].getGlobalBounds().height));
AABB[i].setPosition(Rect[i].getPosition().x, Rect[i].getPosition().y);
}
//Collision Detection
for (int i = 0; i < RECT; i++)
{
for (int j = i + 1; j < RECT; j++)
{
if (i != j)
{
if (MaxPoint[i].x >= MinPoint[j].x && MaxPoint[j].x >= MinPoint[i].x && MaxPoint[i].y >= MinPoint[j].y && MaxPoint[j].y >= MinPoint[i].y)
{
Rect[i].setFillColor(sf::Color::Green);
Rect[j].setFillColor(sf::Color::Green);
AABB[i].setOutlineColor(sf::Color::Blue);
AABB[j].setOutlineColor(sf::Color::Blue);
}
}
}
}
#include<SFML/Window.hpp>
#include<SFML/Graphics.hpp>
#include<cstdint>
#include<iostream>
const int width = 500;
const int height = 500;
int main(){
sf::RenderWindow window(sf::VideoMode(width, height), "Window", sf::Style::Close);
window.setVerticalSyncEnabled(true);
sf::Event event;
uint8_t *pixels = new uint8_t[height *width *4];
for(int i = 0; i < height * width; i+= 4){
pixels[i] = 00; //r
pixels[i + 1] = 00;//g
pixels[i + 2] = 00;//b
pixels[i + 3] = 255;//a
}
sf::Texture texture;
texture.create(width, height);
sf::Sprite sprite;
while(window.isOpen()){
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed){
window.close();
}
}
window.clear(sf::Color::White);
texture.update(pixels);
sprite.setTexture(texture);
window.draw(sprite);
window.display();
}
delete pixels;
return 0;
}
The output I am getting for following program is :
I don't understand why only some part of the window is actually drawn. Since the program is very small I would normnally guess that the problem is created by switching the height and width variable but that is not the case here since both are equal.
The SFML documentation says that if I don't explicitly put the size of texture in sprite.setTexture() it will default to the size of the texture.
Why is this weird behaviour ? Am I missing something ?
for(int i = 0; i < height * width; i+= 4)
Your pixel buffer is of size height * width * 4, but you are only looping while i < height * width. Change the condition in your for loop to i < height * width * 4. Actually, it would make your code much clearer if you declared another variable to store that value. i.e.
int pixel_buffer_size = width * height * 4;
uint8_t *pixels = new uint8_t[pixel_buffer_size];
for(int i = 0; i < pixel_buffer_size; i+= 4) {
etc...
As the title suggests, I am trying to implement collision with scrolling.
The code here isn't the tidiest and it doesn't make use of object-oriented programming. But this code is a prototype for collision in a game I am making so I can reuse in that game.
Anyway, when I wrote this code and implemented no scrolling, the collision worked fine. However, when I implemented scrolling, suddenly, one pixel of the rectangle that represent the player gets stuck in the rectangles I've used to test the collision.
Here's the code.
Main.cpp:
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "SDL_ttf.h"
using namespace std;
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
Mix_Init(MIX_INIT_MP3);
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 640);
TTF_Init();
SDL_Window *Window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 600, SDL_WINDOW_SHOWN);
SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
bool quit = false;
SDL_Event Event;
SDL_Rect Box;
Box.w = 25;
Box.h = 25;
Box.x = 500 - int(Box.w / 2);
Box.y = 300 - int(Box.h / 2);
int CAMERA_X = 0;
int CAMERA_Y = 0;
int xVel = 0;
int yVel = 0;
SDL_Rect RECTS[3];
RECTS[0].x = 50;
RECTS[0].y = 50;
RECTS[0].w = 50;
RECTS[0].h = 100;
RECTS[1].x = 150;
RECTS[1].y = 50;
RECTS[1].w = 50;
RECTS[1].h = 100;
RECTS[2].x = 250;
RECTS[2].y = 50;
RECTS[2].w = 50;
RECTS[2].h = 100;
SDL_Rect CAM_RECTS[3];
CAM_RECTS[0].x = 50;
CAM_RECTS[0].y = 50;
CAM_RECTS[0].w = 50;
CAM_RECTS[0].h = 100;
CAM_RECTS[1].x = 150;
CAM_RECTS[1].y = 50;
CAM_RECTS[1].w = 50;
CAM_RECTS[1].h = 100;
CAM_RECTS[2].x = 250;
CAM_RECTS[2].y = 50;
CAM_RECTS[2].w = 50;
CAM_RECTS[2].h = 100;
const Uint8 *Input = NULL;
while (!quit) {
Input = SDL_GetKeyboardState(NULL);
while (SDL_PollEvent(&Event)) {
if (Event.type == SDL_QUIT) {
quit = true;
}
}
if (Input[SDL_SCANCODE_RIGHT]) {
xVel = -7;
} else if (Input[SDL_SCANCODE_LEFT]) {
xVel = 7;
} else {
xVel = 0;
}
if (Input[SDL_SCANCODE_UP]) {
yVel = 7;
} else if (Input[SDL_SCANCODE_DOWN]) {
yVel = -7;
} else {
yVel = 0;
}
for (unsigned int i = 0; i < 3; i++) {
CAM_RECTS[i].x = RECTS[i].x + CAMERA_X;
CAM_RECTS[i].y = RECTS[i].y + CAMERA_Y;
}
CAMERA_X += xVel;
for (unsigned int i = 0; i < 3; i++) {
if (Box.x <= CAM_RECTS[i].x + CAM_RECTS[i].w && Box.x + Box.w >= CAM_RECTS[i].x && Box.y <= CAM_RECTS[i].y + CAM_RECTS[i].h && Box.y + Box.h >= CAM_RECTS[i].y) {
if (Box.x <= CAM_RECTS[i].x + CAM_RECTS[i].w && Box.x + Box.w >= CAM_RECTS[i].x) {
CAMERA_X += -xVel;
}
}
}
CAMERA_Y += yVel;
for (unsigned int i = 0; i < 3; i++) {
if (Box.x <= CAM_RECTS[i].x + CAM_RECTS[i].w && Box.x + Box.w >= CAM_RECTS[i].x && Box.y <= CAM_RECTS[i].y + CAM_RECTS[i].h && Box.y + Box.h >= CAM_RECTS[i].y) {
if (Box.y <= CAM_RECTS[i].y + CAM_RECTS[i].h && Box.y + Box.h >= CAM_RECTS[i].y) {
CAMERA_Y += -yVel;
}
}
}
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
SDL_RenderClear(Renderer);
SDL_SetRenderDrawColor(Renderer, 255, 255, 255, 255);
SDL_RenderFillRect(Renderer, &Box);
SDL_SetRenderDrawColor(Renderer, 255, 255, 255, 255);
for (unsigned int i = 0; i < 3; i++) {
SDL_RenderFillRect(Renderer, &CAM_RECTS[i]);
}
SDL_RenderPresent(Renderer);
}
Input = NULL;
SDL_DestroyRenderer(Renderer);
SDL_DestroyWindow(Window);
TTF_Quit();
Mix_CloseAudio();
Mix_Quit();
IMG_Quit();
SDL_Quit();
return 0;
}
Please help.
Thank you very much :)
Ok, I changed answer, now it should work without jittering.
It's definitely not the best solution, but it would depends on wider concepion of your program.
int sign(int x)
{
return (x > 0) - (x < 0);
}
/* Movement values are updated by referenced arguments.*/
void check_collisions(SDL_Rect* obj_rects, unsigned num_of_obj,
SDL_Rect obstacle, int& horizontal_move, int& vertical_move)
{
for (unsigned int i = 0; i < num_of_obj; i++)
{
int obj_new_x = obj_rects[i].x + horizontal_move;
// Horizontal collision
if (obj_new_x < (obstacle.x + obstacle.w)
&& (obj_new_x + obj_rects[i].w) > obstacle.x
&& obj_rects[i].y < (obstacle.y + obstacle.h)
&& (obj_rects[i].h + obj_rects[i].y) > obstacle.y)
{
// Calculate maximal possible horizontal movement.
if (sign(horizontal_move) == 1)
horizontal_move = obstacle.x - (obj_rects[i].x + obj_rects[i].w);
else if (sign(horizontal_move) == -1)
horizontal_move = (obstacle.x + obstacle.w) - obj_rects[i].x;
}
int obj_new_y = obj_rects[i].y + vertical_move;
// Vertical collision
if (obj_rects[i].x < (obstacle.x + obstacle.w)
&& (obj_rects[i].x + obj_rects[i].w) > obstacle.x
&& obj_new_y < (obstacle.y + obstacle.h)
&& (obj_new_y + obj_rects[i].h) > obstacle.y)
{
if (sign(vertical_move) == 1)
vertical_move = obstacle.y - (obj_rects[i].y + obj_rects[i].h);
else if (sign(vertical_move) == -1)
vertical_move = (obstacle.y + obstacle.h) - obj_rects[i].y;
}
}
}
int cam_velocity = 5;
...
while (!quit) {
...
if (Input[SDL_SCANCODE_RIGHT])
{
x_movement = cam_velocity;
}
else if (Input[SDL_SCANCODE_LEFT])
{
x_movement = -cam_velocity;
}
if (Input[SDL_SCANCODE_UP])
{
y_movement = -cam_velocity;
}
else if (Input[SDL_SCANCODE_DOWN])
{
y_movement = cam_velocity;
}
check_collisions(CAM_RECTS, 3, Box, x_movement, y_movement);
CAMERA_X += x_movement;
CAMERA_Y += y_movement;
for (unsigned int i = 0; i < 3; i++) {
CAM_RECTS[i].x = RECTS[i].x + CAMERA_X;
CAM_RECTS[i].y = RECTS[i].y + CAMERA_Y;
}
...
I have written a programme to bounce around a rectangle. I use SDL_RenderFillRect to draw a rectangle and SDL_RenderPresent to present that rectangle after which SDL_RenderClear clears the renderer to do it all over again. I use SDL_GetTicks and SDL_Delay to get it to update 60 times a second but somehow the speed of the rectangle is irregular instead of smooth. I have tried different things the past few days but nothing helped. Is there a specific way I should use SDL_RenderPresent to get my window to update regularly?
#include <SDL.h>
#include <math.h>
#include <iostream>
#include <stdio.h>
const int width = 1200;
const int height = 800;
const double PI = 3.141592653589793;
void init();
void quit();
double x = width / 2;
double y = height / 2;
double r = 25;
double vx = 1;
double vy = 1;
double s = 4;
double tickStart = SDL_GetTicks();
double tickEnd = 0;
double tickDelta = 0;
double frameTime = 1001.0 / 60.0;
double delayTime = 0;
int c = 0;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event *event;
SDL_Rect rect;
int main(int argc, char* argv[]) {
init();
// main loop
bool running = true;
while (running) {
event = new SDL_Event;
while (SDL_PollEvent(event) != 0) {
if (event->type == SDL_QUIT) {
running = false;
break;
}
}
//renderer
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
// update position
x = x + vx*s;
y = y + vy*s;
// check wall collision
if (x - r <= 0) {
vx = -vx;
x = r;
}
else if (x + r >= width) {
vx = -vx;
x = width - r;
}
if (y - r <= 0) {
vy = -vy;
y = r;
}
else if (y + r >= height) {
vy = -vy;
y = height - r;
}
// rect
rect.h = rect.w = 2 * r;
rect.x = x - r;
rect.y = y - r;
SDL_RenderFillRect(renderer, &rect);
// present everything at the right time
tickEnd = SDL_GetTicks();
tickDelta = tickEnd - tickStart;
delayTime = frameTime - tickDelta;
std::cout << tickDelta << std::endl;
if (delayTime > 0)
SDL_Delay(delayTime);
tickStart = SDL_GetTicks();
SDL_RenderPresent(renderer);
c++;
}
quit();
return 0;
}
void init()
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("test", 200, 100, width, height, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
}
void quit()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_QUIT;
}
Calling SDL_Delay() seems to take about 15ms depending on circumstances. The renderer can be vertically synced by asigning the right flag: renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);