Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6501

SDL2 Window won't show

$
0
0

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_Hclass 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.


Viewing all articles
Browse latest Browse all 6501


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>