[FIXED] Some functions don't work - SDL2.0

I am learning SDL, following a tutorial on youtube. But, some functions don’t work for me.
For example, SDL_GetWindowPosition returns just (0,0) even if I move the window.
And, these lines:

SDL_Surface *screen = SDL_GetWindowSurface(window);
  Uint32 white = SDL_MapRGB(screen->format,255,255,255);
  SDL_FillRect(screen, NULL, white);
  SDL_UpdateWindowSurface(window);

returns Segmentation fault (core dumped) and close the window immediately.

This is my whole code:

#include "SDL.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {

  SDL_Init(SDL_INIT_EVERYTHING); //Iniciar módulos do SDL, esse inicia tudo!

  //Criando janela
  SDL_Window *window;
  SDL_CreateWindow("Programa",
                  SDL_WINDOWPOS_UNDEFINED,
                  SDL_WINDOWPOS_UNDEFINED,
                  600,
                  400,
                  SDL_WINDOW_RESIZABLE);
  //Verificando erro
  if(window = NULL) {
    cout << "Erro:" << SDL_GetError() << endl;
  }

  SDL_Surface *screen = SDL_GetWindowSurface(window);
  Uint32 white = SDL_MapRGB(screen->format,255,255,255);
  SDL_FillRect(screen, NULL, white);
  SDL_UpdateWindowSurface(window);

  //Usar eventos
  SDL_Event event;
  bool running = true;

  int x,y,w,z;

  //Main Loop
  while(running) {
    //Loop para a janela manter aberta
    while(SDL_PollEvent(&event)) {
      if(event.type == SDL_QUIT) {
        running = false;
        break;
      }
    }
    /*SDL_GetWindowPosition(window, &x, &y);
    cout << x << "," << y << endl;*/

  }

  //SDL_Delay(5000);
  SDL_DestroyWindow(window); //Fecha a janela

  SDL_Quit(); //Finaliza SDL

  return 0;
}

Segfault is due “window” not getting set, and that error would
normally get caught, but the “if” statement doesn’t catch it
because of typo (should be double-equals, not equals). See below for
edits…

I am learning SDL, following a tutorial on youtube. But, some functions don’t work for me.
For example, SDL_GetWindowPosition returns just (0,0) even if I move the window.
And, these lines:

SDL_Surface *screen = SDL_GetWindowSurface(window);
Uint32 white = SDL_MapRGB(screen->format,255,255,255);
SDL_FillRect(screen, NULL, white);
SDL_UpdateWindowSurface(window);

returns Segmentation fault (core dumped) and close the window immediately.

This is my whole code:

#include “SDL.h”
#include
using namespace std;

int main(int argc, char *argv[]) {

SDL_Init(SDL_INIT_EVERYTHING); //Iniciar módulos do SDL, esse inicia tudo!

//Criando janela
SDL_Window *window;

window = SDL_CreateWindow(“Programa”,

              SDL_WINDOWPOS_UNDEFINED,
              SDL_WINDOWPOS_UNDEFINED,
              600,
              400,
              SDL_WINDOW_RESIZABLE);

//Verificando erro
if(window = NULL) {

if (window == NULL) {

cout << "Erro:" << SDL_GetError() << endl;

}

SDL_Surface *screen = SDL_GetWindowSurface(window);
Uint32 white = SDL_MapRGB(screen->format,255,255,255);
SDL_FillRect(screen, NULL, white);
SDL_UpdateWindowSurface(window);

//Usar eventos
SDL_Event event;
bool running = true;

int x,y,w,z;

//Main Loop
while(running) {
//Loop para a janela manter aberta
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
running = false;
break;
}
}
/SDL_GetWindowPosition(window, &x, &y);
cout << x << “,” << y << endl;
/

}

//SDL_Delay(5000);
SDL_DestroyWindow(window); //Fecha a janela

SDL_Quit(); //Finaliza SDL

return 0;
}


Visit Topic or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, click here.

Ed Phillips ed@udel.edu University of Delaware (302) 831-6082
Systems Programmer IV, Network and Systems Services

Thanks Ed, It worked! I made some mistakes.