1

I am trying to follow a tutorial that creates a blank window that can be closed using the Esc button, but the window never shows. This is on WSL running Ubuntu 20.05.6

The code compiles and runs without errors, but once it runs it doesn't really do anything. There's no icon on the taskbar or anything like that.

#include <SDL2/SDL.h>

#ifndef GAME_H
#define GAME_H

class Game
{
    public:
        Game();
        bool Initialize();
        void RunLoop();
        void Shutdown();
    private:
        void ProcessInput();
        void UpdateGame();
        void GenerateOutput();

        SDL_Window* mWindow;
        bool mIsRunning;
};

#endif

Game.h - This is the header file with the function declarations

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

Game::Game() {
    this->mWindow = nullptr;
    this->mIsRunning = true;
}

bool Game::Initialize() {
    int sdlResult = SDL_Init(SDL_INIT_VIDEO);
    if(sdlResult != 0) {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
        return false;
    }

    mWindow = SDL_CreateWindow(
        "Test",
        100,
        100,
        1024,
        768,
        SDL_WINDOW_SHOWN
    );
    if(!mWindow) {
        SDL_Log("Failed to create window: %s", SDL_GetError());
    }

    return true;
}

void Game::Shutdown() {
    SDL_DestroyWindow(mWindow);
    SDL_Quit();
}

void Game::RunLoop() {
    while(mIsRunning) {
        ProcessInput();
        UpdateGame();
        GenerateOutput();
    }
}

void Game::ProcessInput() {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        switch(event.type) {
            case SDL_QUIT:
            mIsRunning = false;
            break;
        }
    }
    const Uint8* state = SDL_GetKeyboardState(NULL);
    if (state[SDL_SCANCODE_ESCAPE]) {
        mIsRunning = false;
    }
}

void Game::UpdateGame() {}
void Game::GenerateOutput() {}

int main (int argc, char** argv) {
    Game game;
    bool success = game.Initialize();
    if(success) {
        std::cout << SDL_GetError() << std::endl;
        game.RunLoop();
    }
    game.Shutdown();
    return 0;
}

Game.cpp - This is the implementation of the Game class. UpdateGame() and GenerateOutput() are both empty because I haven't gotten that far in the tutorial yet.

The code doesn't output either of the error messages in the Initialize() method, so I'm assuming that the window is created, but not shown. Initially, I hadn't implemented the ProcessInput() method yet, but other questions on this site were resolved by adding an event loop, so I did as well, but it didn't change anything. There is an error that states "Unknown pixel format" but after running the debugger, I traced it to an SDL function that creates a dummy mouse cursor and calls a function with parameters that will always cause it to fail, so I don't think it's relevant.

EDIT: The window showed up once I compiled and ran it in Windows rather than in WSL. I did have to define SDL_MAIN_HANDLED in Game.h, I believe it has something to do with overriding WinMain()

9
  • OT: Wouldn't it be easier, or at least more in line with the event processing loop, to not use SDL_GetKeyboardState, and instead use keyboard events? Commented Jul 10 at 0:00
  • Which X server are you using?
    – genpfault
    Commented Jul 10 at 0:00
  • @genpfault I'm not actually running this on Ubuntu, it's on a Windows system with WSL so I'm not using X-server at all.
    – lnz43090
    Commented Jul 10 at 0:17
  • @Someprogrammerdude Maybe? I'm not very familiar with this library, so this is essentially just copy-pasted from a tutorial
    – lnz43090
    Commented Jul 10 at 0:19
  • 1
    I don't think it deserves a full answer. Though if you had to use WinMain, it means you didn't use SDL2 how it's supposed to be used. Commented Jul 10 at 17:41

0