Why can't this simple SDL app be closed without task manager or rapid clicking of x button?












1












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









share|improve this question











$endgroup$

















    1












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









    share|improve this question











    $endgroup$















      1












      1








      1





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









      share|improve this question











      $endgroup$




      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);
      }






      c++ sdl sdl2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 21:11









      Tyyppi_77

      5,19231835




      5,19231835










      asked Jan 20 at 20:52









      Bluebomber357Bluebomber357

      1083




      1083






















          1 Answer
          1






          active

          oldest

          votes


















          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.






          share|improve this answer









          $endgroup$









          • 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





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "53"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f167297%2fwhy-cant-this-simple-sdl-app-be-closed-without-task-manager-or-rapid-clicking-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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.






          share|improve this answer









          $endgroup$









          • 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
















          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.






          share|improve this answer









          $endgroup$









          • 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














          5












          5








          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.






          share|improve this answer









          $endgroup$



          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 20 at 21:10









          Tyyppi_77Tyyppi_77

          5,19231835




          5,19231835








          • 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














          • 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








          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$
          – Bluebomber357
          Jan 21 at 3:48




          $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


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Game Development Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f167297%2fwhy-cant-this-simple-sdl-app-be-closed-without-task-manager-or-rapid-clicking-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents