Reputation: 55
I am trying to render a point using SDL and I cannot seem to get the point to render. I am not getting any errors in the code and it is compiling however no point is appearing on the window.
Code:
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main() {
const int windowHeight = 600;
const int windowWidth = 800;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return 1;
cout << "Initialization failed" << endl;
}
SDL_Window *window = SDL_CreateWindow("Practice making sdl Window",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth,
windowHeight, SDL_WINDOW_SHOWN);
if (window == NULL) {
SDL_Quit();
return 2;
}
SDL_Renderer *s;
const int pointLocationx = windowWidth/2;
const int pointLocationy = windowHeight/2;
SDL_RenderDrawPoint(s, pointLocationx, pointLocationy);
bool quit = false;
SDL_Event event;
while (!quit) {
//drawing particles
//setting up objects
//repeated over and over again
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
}
Above is the code that I have. Any advice is appreciated and help is super appreciated.
Upvotes: 2
Views: 6336
Reputation: 1444
You're missing several things. The first and most important is that your renderer is not initialized, that is done with SDL_CreateRenderer. Now we're ready to draw onto the window. For this you'll need to set a color on your renderer (this color is used for functions like RenderDrawPoint and RenderDrawLine, etc).
After drawing your point we'll set the color to what was before. I chose black for the background and white for the point's color, you can choose whatever you need, the function for setting the color in the renderer is SDL_SetRenderDrawColor.
Now we can draw, but before every draw you have to clear your screen, make all the draw calls to the renderer and then show what you have drawn.
Here is a full example with the commented parts on what you were missing, I also moved your drawPoint inside your main loop, since at the end of the day is where you probably want it to be.
BUT (very rare use) if you want to draw once and never change whats on screen again, then you could take it outside the wall, call clear and present once and be done with it.
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main() {
const int windowHeight = 600;
const int windowWidth = 800;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return 1;
cout << "Initialization failed" << endl;
}
SDL_Window *window = SDL_CreateWindow("Practice making sdl Window",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth,
windowHeight, SDL_WINDOW_SHOWN);
if (window == NULL) {
SDL_Quit();
return 2;
}
// We create a renderer with hardware acceleration, we also present according with the vertical sync refresh.
SDL_Renderer *s = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) ;
const int pointLocationx = windowWidth/2;
const int pointLocationy = windowHeight/2;
bool quit = false;
SDL_Event event;
while (!quit) {
//drawing particles
//setting up objects
//repeated over and over again
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
// We clear what we draw before
SDL_RenderClear(s);
// Set our color for the draw functions
SDL_SetRenderDrawColor(s, 0xFF, 0xFF, 0xFF, 0xFF);
// Now we can draw our point
SDL_RenderDrawPoint(s, pointLocationx, pointLocationy);
// Set the color to what was before
SDL_SetRenderDrawColor(s, 0x00, 0x00, 0x00, 0xFF);
// .. you could do some other drawing here
// And now we present everything we draw after the clear.
SDL_RenderPresent(s);
}
SDL_DestroyWindow(window);
// We have to destroy the renderer, same as with the window.
SDL_DestroyRenderer(s);
SDL_Quit();
}
Upvotes: 7