0
\$\begingroup\$

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.

SDLWrapper.cpp

#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_Init(SDL_INIT_VIDEO);
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)
        break;

    SDL_RenderClear(renderer);



    rasterPixels = (*generateRaster)();
    for (int i = 0; i < rasterPixels.size(); ++i)
    {
        for (int j = 0; j < rasterPixels.at(i).size(); ++j)
        {
            SDL_SetRenderDrawColor(renderer, 
            rasterPixels.at(i).at(j).R, 
            rasterPixels.at(i).at(j).G, 
            rasterPixels.at(i).at(j).B, 255);
            SDL_RenderDrawPoint(renderer, i, j);
        }
    }
    SDL_RenderPresent(renderer);


    SDL_Delay(time_left());
    next_time += TICK_INTERVAL;


}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}

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;
else
    return next_time - now;
}

RayCaster1.cpp

#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++){
              column.push_back(greenPixel);
          }

          rasterPixels.push_back(column);
      }

  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);
}
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

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.

\$\endgroup\$
1
  • 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\$ Jan 21, 2019 at 3:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .