I wrote a simple app that generates a window, a raster, and redraws the raster once every 16ms. Right now the calculations are empty, it just draws a solid color. I don't know how, but I have created a very resource hungry app that cannot be easily closed. I generally have to close it via the task manager.

Here is my SDL "wrapper" class. Below this class is what will eventually calculate a ray caster. I initially thought my call to SDL_RenderDrawPoint for every pixel in the 600x600 window was causing the issue, but I still cannot close the window easily with that code commented out.


#include <iostream>
#include <SDL2/SDL.h>
#include <vector>
#include "include/SDLWrapper.h"

using namespace std;

#define TICK_INTERVAL 16

static int next_time;

SDLWrapper::SDLWrapper() {

int SDLWrapper::setupSDLRenderer(int WINDOW_WIDTH, vector<vector<RGB>> (*generateRaster)()){
    SDL_Event event;
    SDL_Renderer *renderer;
    SDL_Window *window;

SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);    
next_time = SDL_GetTicks() + TICK_INTERVAL;
while (1) {
    if (SDL_PollEvent(&event) && event.type == SDL_QUIT)


    rasterPixels = (*generateRaster)();
    for (int i = 0; i < rasterPixels.size(); ++i)
        for (int j = 0; j < rasterPixels.at(i).size(); ++j)
            rasterPixels.at(i).at(j).B, 255);
            SDL_RenderDrawPoint(renderer, i, j);

    next_time += TICK_INTERVAL;


RGB SDLWrapper::peek(int x, int y){
    return rasterPixels.at(x).at(y);

void SDLWrapper::poke(int x, int y, RGB color) {
   rasterPixels[x][y] = ((RGB){color.R, color.G, color.B});

int SDLWrapper::time_left(void)
Uint32 now;

now = SDL_GetTicks();
if(next_time <= now)
    return 0;
    return next_time - now;


#include "include/SDLWrapper.h"
#include <iostream>
#include <vector>

#define WINDOW_WIDTH 600

#define mapWidth 24
#define mapHeight 24

vector<vector<RGB>> rasterPixels;

vector<vector<RGB>> generateRaster(){
      RGB greenPixel;
      greenPixel.R = 255;
      greenPixel.G = 255;
      greenPixel.B = 255;

      for(int i = 0; i < WINDOW_WIDTH; i++) {
          vector<RGB> column;

          for(int j = 0; j < WINDOW_WIDTH; j++){


  return rasterPixels;

int main(int /*argc*/, char */*argv*/[])
    double posX = 22, posY = 12;  //x and y start position
    double dirX = -1, dirY = 0; //initial direction vector
    double planeX = 0, planeY = 0.66; //the 2d raycaster version of camera plane

    SDLWrapper sdlWrapper;
    sdlWrapper.setupSDLRenderer(WINDOW_WIDTH, generateRaster);

Your event loop looks ill-formed, as it will only process one event per frame. Here's what it probably should look like:

SDL_Event e;
while (SDL_PollEvent(&e)) {
    if (e.type == SDL_QUIT) run = false;

And then change your mainloop to depend on run.

  • 1
    \$\begingroup\$ Your advice is correct. I was scratching my head when you said "change your mainloop" as I only had the one loop. I believe you meant "then add a proper mainloop"? I googled for a proper one, found Lazy Foo: lazyfoo.net/articles/article04/index.php It works now! Thanks for getting me on the right path :) \$\endgroup\$ – Bluebomber357 Jan 21 at 3:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.