Hey guys, I never like my first post anywhere being a cry for help, but here goes! Google returned nothing, so figured I’d ask here.
I’m making an application using SDL2 that’s meant to display graphs in separate windows. To do that, I have a class called figure with the following header:
class figure
{
private:
short window_width;
short window_height;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Event fig_handler;
public:
figure(short fig_typ);
void render();
short figure_type;
};
And the following implementation:
figure::figure(short fig_typ)
{
window_width = 840;
window_height = 680;
window = SDL_CreateWindow( "Demo Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, SDL_WINDOW_SHOWN );
if (window==NULL)
{
std::cout << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
}
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if (renderer==NULL)
{
std::cout << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
}
// Set default colour to turquise
SDL_SetRenderDrawColor(renderer, 45, 164, 132, SDL_ALPHA_OPAQUE);
figure_type = fig_typ;
}
void figure::render()
{
while (SDL_PollEvent(&fig_handler))
{
if (fig_handler.type == SDL_QUIT)
{
// Do things here
}
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(0);
}
This all works rather well. I can just write while (true){fig1.render();} and a turquoise window will stay up until I close the program. But in my program, I want many of these figures to coexist, and to simplify the process, I decided I would make a new class model to keep track of the figures.
The header:
class model
{
public:
model();
void add_figure(short index, short fig_typ);
bool update();
private:
std::map<short, figure*> figure_map;
};
The implementation:
model::model()
{
// Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
std::cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
}
}
void model::add_figure(short index, short fig_typ)
{
figure tempFigure(fig_typ);
std::pair<short, figure*> tempPair(index, &tempFigure);
figure_map.insert(tempPair);
}
bool model::update()
{
for (std::map<short, figure*>::iterator it = figure_map.begin(); it != figure_map.end(); ++it)
{
it->second->render();
}
return true;
}
And mysteriously, when I create a model object and give it a figure, and subsequently call while (true) {model_object.update();}, the window turns black. When I delete the event handling from the figure object (like I originally intended), SDL doesn’t even bother to make a nice window: I only get a blurred white or black window, depending on which parts of the handling I delete.
It seems like SDL doesn’t even take the time to nicely render everything. Another StackOverflow-post suggested adding SDL_Delay(0) somewhere along the lines to make SDL catch its breath (as you can see, I put it into figure::render()) but so far it doesn’t make a difference.
What can I do to render my window like it’s supposed to, but still maintain this structure: a structure in which an object calls a function of another object to draw the window.
P.s. I’m working from a laptop with MacOS, and compiling using g++. Oh and thanks in advance ^^