Struggles with window creation

Hello, I am pretty new to programming and very new to SDL so please be patient. (Unfortunately I cannot upload text, as I have recently joined)

I have an issue where my instance of an SDL_Renderer* variable appears to be inhibiting the creation of my window.

I have initialized SDL_INIT_VIDEO, and have assigned my SDL_Window* variable to the return value of SDL_CreateWindow(parameters), none of these have failed (the variable is not equal to NULL).

I have also initialized my SDL_Renderer* variable to the return value of SDL_CreateRenderer(parameters) and there is no failures there either.

I have created an event loop that continuously polls for either a button or mouse click.

I know it is an issue with the renderer (more likely my usage of it) because my code works (a window is created) if I initialize a SDL_Surface* variable before initializing (or just never initializing) my renderer variable, and the event loop works as intended.

I am hesitant to use a surface as I heard they are archaic and not really used in modern programs, but it seems like my program wont work otherwise.

If this info helps: I am making a cpp project on linux in a wayland WM, and I am able to compile and use the example files in the SDL3 github directory just fine (and they do not have a SDL_Surface*) but I am not using the callbacks like the examples ( I am using int main(int argc, char* argv) ).

I have looked through a lot of stack overflow questions and searched through the documentation but I have not been able to find an answer.

Do you call SDL_RenderPresent (or equivalent)? According to this you need to do that before the window gets shown.

It’s easier to help if you post the code.

To post code just paste it and add ``` above and below it.

```
put your code here
```
1 Like
// MainGame.hpp 
#pragma once 

#include <GL/gl.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_video.h> 
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_hints.h>
#include <stddef.h> 
#include <iostream> 

class MainGame {
private:

  SDL_Window* m_window;
  SDL_Renderer* m_renderer; 
  SDL_Surface* m_surface;  
  SDL_Event m_event; 
  int m_screenWidth;
  int m_screenHeight; 
  bool m_quit; 

public: 
  
    
  MainGame(); 
  ~MainGame();
  void run(); 
  void initSystems(); 
  

};
// MainGame.cpp 
#include "MainGame.hpp"
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_video.h>
#include <cstddef>


inline MainGame::MainGame(void) {

  m_window = nullptr; 
  m_renderer = nullptr; 
  m_surface = nullptr; 
  m_screenWidth = 640; 
  m_screenHeight = 480; 
  m_quit = false;   
} 

inline MainGame::~MainGame(void) {
  SDL_Quit(); 
}

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

inline void MainGame::initSystems() {
  // Initialize SDL 
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    std::cout << "SDL video could not initialize: " << SDL_GetError();  
  } 
  
  m_window = SDL_CreateWindow("Window", m_screenWidth, 
     m_screenHeight, SDL_WINDOW_OPENGL);
 if (m_window == NULL){
    std::cout << "Unable to create Window: " << SDL_GetError(); 
  }  
//  m_surface = SDL_GetWindowSurface(m_window); 


  m_renderer = SDL_CreateRenderer(m_window, NULL);
  if (m_renderer == NULL) {
    std::cout << "Unable to create Renderer: " << SDL_GetError(); 
  }
  SDL_RenderPresent(m_renderer); 
/* 
  if (SDL_CreateWindowAndRenderer("Window", m_screenWidth, m_screenHeight, 
    SDL_WINDOW_OPENGL, &m_window, &m_renderer) < 0)
      std::cout << "Failed to create Window + Renderer" << SDL_GetError();
*/

  // SDL_UpdateWindowSurface(m_window);

  
  while (m_quit == false){
    while (SDL_PollEvent(&m_event)){
        if (m_event.type == SDL_EVENT_QUIT)
          m_quit = true;
        if (m_event.type == SDL_EVENT_KEY_DOWN) 
          m_quit = true; 
        if (m_event.type ==  SDL_EVENT_MOUSE_BUTTON_DOWN) 
          m_quit = true; 
    }
  }
  
}

// main.cpp 
#include "MainGame.cpp"
#include <SDL3/SDL_main.h>
#include <iostream> 

int main(int argc, char* argv[]) {
  {
  MainGame game; 
  game.run();
  }
  return 0; 
} 

It works! Thank you.

( This is what I have wrote so far if it interests anybody else who stumbles onto this post (After the edit was made). )