1

I'm trying to get an SDL window to appear, but it doesn't seem to be working. The program will run, and the function to show the window will run with no errors, but nothing shows up on my screen. I only have an icon in the dock that says that the program is not responding. Here's my code:

int main(int argc, const char * argv[]) {

    MainComponent mainComponent;
    mainComponent.init();

    char myVar;

    cout << "Enter any key to quit...";
    cin >> myVar;

    return 0;
}

void MainComponent::init() {
    //Initialize SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    window = SDL_CreateWindow("My Game Window", 100, 100, 100, 100, SDL_WINDOW_SHOWN);

    cout << screenWidth << " " << screenHeight << endl;

    if(window == nullptr) {
        cout << "Error could not create window" << SDL_GetError() << endl;
    }

    SDL_Delay(5000);

}

Here's a screenshot of the icon on the dock https://www.dropbox.com/s/vc01iqp0z07zs25/Screenshot%202016-02-02%2017.26.44.png?dl=0 Let me know if there's something I'm doing wrong, thanks!

shadowarcher
  • 3,265
  • 5
  • 21
  • 33

1 Answers1

0

SDL_Renderer should be initialized to handle the rendering. This is explained in detailed here What is a SDL renderer?.

Here's a modified code above with an initialized renderer;

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

using namespace std;

class MainComponent
{
 public:
    void init();
    ~MainComponent();

 private:
    SDL_Window *window;
    SDL_Renderer* renderer;
};

MainComponent::~MainComponent()
{
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
}

void MainComponent::init() {
    //Initialize SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    int screenWidth = 400;
    int screenHeight = 300;

    window = SDL_CreateWindow("My Game Window", 100, 100, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, 0);

    cout << screenWidth << " " << screenHeight << endl;

    if(window == nullptr) {
        cout << "Error could not create window" << SDL_GetError() << endl;
    }

    //change the background color
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

    // Clear the entire screen to our selected color.
    SDL_RenderClear(renderer);

    // Up until now everything was drawn behind the scenes.
    // This will show the new, red contents of the window.
    SDL_RenderPresent(renderer);


    SDL_Delay(5000);

}


int main(int argc, const char * argv[]) {

    MainComponent mainComponent;
    mainComponent.init();

    char myVar;

    cout << "Enter any key to quit...";
    cin >> myVar;

    SDL_Quit();
    return 0;
}

This should compile and run properly. Hope that helps.

Community
  • 1
  • 1
share
  • 315
  • 2
  • 6