Why can't this simple SDL app be closed without task manager or rapid clicking of x button?
$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);
}
c++ sdl sdl2
New contributor
$endgroup$
add a comment |
$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);
}
c++ sdl sdl2
New contributor
$endgroup$
add a comment |
$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);
}
c++ sdl sdl2
New contributor
$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
c++ sdl sdl2
New contributor
New contributor
edited 3 hours ago
Tyyppi_77
5,13931835
5,13931835
New contributor
asked 4 hours ago
Bluebomber357Bluebomber357
1062
1062
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$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$
add a comment |
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
});
}
});
Bluebomber357 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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$
add a comment |
$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$
add a comment |
$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$
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
.
answered 3 hours ago
Tyyppi_77Tyyppi_77
5,13931835
5,13931835
add a comment |
add a comment |
Bluebomber357 is a new contributor. Be nice, and check out our Code of Conduct.
Bluebomber357 is a new contributor. Be nice, and check out our Code of Conduct.
Bluebomber357 is a new contributor. Be nice, and check out our Code of Conduct.
Bluebomber357 is a new contributor. Be nice, and check out our Code of Conduct.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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