You are not logged in.

#1 2014-10-15 21:24:46

InspiredOne
Member
Registered: 2014-04-20
Posts: 7

SDL2 Window is transparent and SDL_PollEvent() always returns false.

I'm learning to use SDL to make simple GUI Applications and I've run into a couple problems. With the first issue, I'm not actually sure it's a problem, but I thought I would bring it up since all the tutorials I've seen don't display the behavior. When I make a window with SDL_CreateWindow(), all I get is a frame with a snapshot of whatever it spawned ontop of. I can drag this frame around and it will pull the background image with it. It's very odd behavior, and I haven't seen it in any of the tutorials I've watched. Here is a picture showing my Game Engine window after executing the code I've attached:

PQB6gLo.png

Next, SDL_PollEvent() is never returning true, only false, no matter how many buttons I push or mouse motions I make over the window. There are no compiling errors or runtime errors that I can see from the console. (Perhaps there is somewhere I can look to see an error log for SDL2?).

I've tried:

  • First, System Upgrade

  • Turning off my composite window manager

  • Adding delays before and after SDL_CreateWindow

  • Running my program on both Awesome WM and LXDE

Below you will see the very simple framework that I've managed to build. It's split into three files for scalability. All it's supposed to do at this point is print "Event has been called" whenever an OS event triggers SDL_PollEvent() and if the event happens to be a mouse move event, print the x coordinate of the mouse. I've confirmed that the gameLoop() function does work, (when the commented line is uncommented, it spams "In process input").

This is all the information I can think of. Do tell me if you need more in order to help me debug this. I do not believe it is an SDL problem, but possibly something wrong with my installation or with Arch. Of course, it could just be something I'm doing wrong.  Also, if this is in the wrong section, please let me know. I still consider myself an Arch Newbie. Thank you for your help.

main.cpp

#include <iostream>
#include <SDL2/SDL.h>
#include <MainGame.h>


int main(int argc, char** argv){
    MainGame mainGame;
    mainGame.run();

    return 0;
}

MainGame.h

#ifndef MAINGAME_H
#define MAINGAME_H

#include <SDL2/SDL.h>
#include <GL/gl.h>

enum class GameState{PLAY, EXIT};

class MainGame
{
    public:
        MainGame();
        virtual ~MainGame();
        void run();

    protected:
    private:
        void initSystems();
        void processInput();
        void gameLoop();

        SDL_Window* _window;
        int _screenWidth;
        int _screenHeight;
        GameState _gameState;

};

#endif // MAINGAME_H

MainGame.cpp

#include "MainGame.h"
#include <iostream>



MainGame::MainGame()
{
    _window = NULL;
    _screenWidth = 1024;
    _screenHeight = 768;
    _gameState = GameState::PLAY;
}

MainGame::~MainGame()
{
    //dtor
}

void MainGame::run() {
    initSystems();
    gameLoop();
}


void MainGame::initSystems() {
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Delay(500);
    _window = SDL_CreateWindow("Game Engine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,_screenWidth,_screenHeight,SDL_WINDOW_OPENGL);
    SDL_Delay(500);
}

void MainGame::processInput() {
    SDL_Event event;
    while(SDL_PollEvent(&event)) {
        std::cout<<"Event has been called\n";
        switch(event.window.type) {
            case SDL_MOUSEMOTION:
                std::cout<<event.motion.x<<"\n";
                _gameState = GameState::EXIT;
                break;
        }
    }
}

void MainGame::gameLoop() {
    while(_gameState != GameState::EXIT) {
            //std::cout<<"In process input";
        processInput();
    }
}

Last edited by InspiredOne (2014-10-15 21:31:48)

Offline

#2 2014-10-16 12:51:56

silverhammermba
Wiki Maintainer
Registered: 2011-04-14
Posts: 156

Re: SDL2 Window is transparent and SDL_PollEvent() always returns false.

First, know that a lot of SDL errors must be detected at runtime and can be displayed using SDL_GetError(). For example, SDL_CreateWindow returns nullptr on failure. You should definitely be checking for errors whenever you call an SDL function that could fail.

Secondly, I think you're seeing a weird transparent window because you aren't drawing anything to it - so I'm guessing the contents of the window are undefined. Here's what I had to do to get a proper window:

// in MainGame.h
#include <SDL2/SDL_opengl.h>
// add a member to MainGame class
SDL_GLContext _context;

// in MainGame.cpp after creating the window
_context = SDL_GL_CreateContext(_window);
glClearColor(1.f, 0.f, 0.f, 1.f);

// and in gameLoop
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(_window);

Offline

Board footer

Powered by FluxBB