Advertisement

SDL window

Started by November 21, 2019 08:37 PM
2 comments, last by Neryss 5 years ago

Hello!

After a quick pain to install SDL and use it with VSCode I have another "issue" ;

I'm still following a class for beginners on openclassrooms and I can finally display a window (yay! Kinda huge achievement since for me, but the tutorial is for SDL1.2 and since I am using SDL2 I have some problems with the code used. 


int main(int argc, char *argv[])
{
    SDL_Surface *ecran = NULL; // Le pointeur qui va stocker la surface de l'écran

    SDL_Init(SDL_INIT_VIDEO);

    ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE); // On tente d'ouvrir une fenêtre
    if (ecran == NULL) // Si l'ouverture a échoué, on le note et on arrête
    {
        fprintf(stderr, "Impossible de charger le mode vidéo : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    
    SDL_WM_SetCaption("Ma super fenêtre SDL !", NULL);

    pause();

    SDL_Quit();

    return EXIT_SUCCESS;
}

But as I looked on the web, it doesn't work this way anymore and all the "surface" things also changed. So all the explanations in this class is kinda out of subject for me.

The guys who made it made some paragraphs about how surfaces work so you can create different layers and superpose them to display things. Purpose was simple here : displaying a green window.

Is there anyone who can guide me a bit so I can understand it ? Or if there's any guide (I looked for it but didn't find any "clear" one since I'm starting :/

Here's the link of my class (in french sorry guys, but if you want to take a look at the codes written here it is) : https://openclassrooms.com/fr/courses/19980-apprenez-a-programmer-en-c/17546-creation-dune-fenetre-et-de-surfaces

Thank you guys, have aa beautiful day !

green window

 


#include <iostream>

#include <SDL2/SDL.h>

const int WIDTH = 800, HEIGHT = 600;

int main( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Window *window = SDL_CreateWindow( "SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI );
    
    if ( NULL == window )
    {
        std::cout << "Could not create window: " << SDL_GetError( ) << std::endl;
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_SetRenderDrawColor(renderer, 50, 200, 100, 255);
    
    SDL_Event windowEvent;
    
    while ( true )
    {
        if ( SDL_PollEvent( &windowEvent ) )
        {
            if ( SDL_QUIT == windowEvent.type )
            {
                break;
            }
        }

        SDL_RenderClear(renderer);



        SDL_RenderPresent(renderer);
    }
    
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow( window );
    SDL_Quit( );
    
    return EXIT_SUCCESS;
}

 

Advertisement
10 hours ago, TabouretY said:

green window

 



#include <iostream>

#include <SDL2/SDL.h>

const int WIDTH = 800, HEIGHT = 600;

int main( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Window *window = SDL_CreateWindow( "SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI );
    
    if ( NULL == window )
    {
        std::cout << "Could not create window: " << SDL_GetError( ) << std::endl;
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_SetRenderDrawColor(renderer, 50, 200, 100, 255);
    
    SDL_Event windowEvent;
    
    while ( true )
    {
        if ( SDL_PollEvent( &windowEvent ) )
        {
            if ( SDL_QUIT == windowEvent.type )
            {
                break;
            }
        }

        SDL_RenderClear(renderer);



        SDL_RenderPresent(renderer);
    }
    
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow( window );
    SDL_Quit( );
    
    return EXIT_SUCCESS;
}

 

Hey thanks for the answer, I'm trying to make it work. Since the guy tell us that we don't know too much for now my code looks like this :


#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

void    pause();

int     main(int arc, char **arv)
{
    if(SDL_Init(SDL_INIT_VIDEO) == -1)
    {
        fprintf(stderr, "Error during SDL initialization : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_Window *sdlWindow = SDL_CreateWindow("Hellow I'm a cute window uwu", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL);

    pause();

    SDL_Quit();

    return(EXIT_FAILURE);
}

void    pause()
{
    int continuer = 1;
    SDL_Event event;

    while(continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
        }
    }
}

the pause function was made with events but he tells us that we just use it for now and it will be explained later so I don't really understand how yours work and if it differs that much from mine :/

 

Okey so quick update, I looked at yours and searched a bit on DSL wiki and came up with that :


#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

void    pause();

int     main(int arc, char **arv)
{
    SDL_Renderer *renderer;

    if(SDL_Init(SDL_INIT_VIDEO) == -1)
    {
        fprintf(stderr, "Error during SDL initialization : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_Window *sdlWindow = SDL_CreateWindow("Hellow I'm a cute window uwu", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL);
    renderer = SDL_CreateRenderer(sdlWindow, -1, 0);
    SDL_SetRenderDrawColor(renderer, 50, 200, 100, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    pause();

    SDL_Quit();

    return(EXIT_FAILURE);
}

void    pause()
{
    int continuer = 1;
    SDL_Event event;

    while(continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
        }
    }
}

Which works perfectly well :D Thank you again !

This topic is closed to new replies.

Advertisement