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