Advertisement

SDL2 loading a bmp

Started by November 22, 2019 12:26 PM
6 comments, last by Neryss 5 years ago

Hello it's me again!

Still in pain with SDL2 I wanted to ask is someone could explain me how to display a .bmp ? I tried different things but none of them worked so I'm here again lol

 

I saw several ways but they're all different one from another and I don't know which one is the best :/ (If I even achieved to make them work)

Since all the tutorial I'm following is for SDL1.2 it's pretty hard to find equivalent for the SDL2 and if any of you have an idea of where I can learn how to use it it would be awesome !

 

Get rid of your SDL1 tutorial, there's plenty enough material on SDL2. 

This was my first hit on 'SDL2 tutorial':
https://www.willusher.io/sdl2 tutorials/2013/08/17/lesson-1-hello-world
 

Advertisement
1 hour ago, Prototype said:

Get rid of your SDL1 tutorial, there's plenty enough material on SDL2. 

This was my first hit on 'SDL2 tutorial':
https://www.willusher.io/sdl2 tutorials/2013/08/17/lesson-1-hello-world
 

Thank you, I was thinking about getting rid of it since it was the end of the chapter so an "updated" one would be way better ! 

I'll start this one tonight thank you so much, have a good day !

By the way is there a website you know on which I can keep learning ? I learnt C and I think I'll need to take a look at c++ :o

 

https://en.cppreference.com/w/

http://www.cplusplus.com/

https://www.learncpp.com/

... and others of course. And practice, practice, practice .... :-)

1 minute ago, Green_Baron said:

 

https://en.cppreference.com/w/

http://www.cplusplus.com/

https://www.learncpp.com/

... and others of course. And practice, practice, practice .... ?

Thank you I'll keep practicing ! :) I'm really motivated so I'll definitely keep going ?

Hey I have a "Invalid renderer" error whenever I try to run my program, I tried modifying several things but I can't make it work. It's different from the guide since I didn't learn the "std::string" thing so I tried to do it another way but I don't even know if this is because of that :/ Can I have a little hint about what's wrong because I can't figure it out ? It's supposed to load my .bmp but there's just no way :


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

void    pause();

int     main(int arc, char **arv)
{
    SDL_Window *win;
    SDL_Renderer *rend;
    SDL_Surface *bmp;
    SDL_Texture *tex;

    int i;

    if(SDL_Init(SDL_INIT_VIDEO) == -1)
    {
        printf("Error during SDL initialization : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    win = SDL_CreateWindow("Hello world!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
    bmp = SDL_LoadBMP("lac_en_montagne.bmp");
    if(bmp == NULL)
    {
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(win);
        printf("Error during BMP : %s\n", SDL_GetError());
    }
    tex = SDL_CreateTextureFromSurface(rend, bmp);
    SDL_FreeSurface(bmp);
    if(tex == NULL)
    {
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(win);
        printf("Error durin Tex to surf : %s\n", SDL_GetError());
    }
    while(i < 3)
    {
        SDL_RenderClear(rend);
        SDL_RenderCopy(rend, tex, NULL, NULL);
        SDL_RenderPresent(rend);
        SDL_Delay(1000);
        i++;
    }
    pause();
    SDL_Quit();
}

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

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

Thank you again for everything guys you're the best !

Advertisement
On 11/22/2019 at 6:06 PM, Neryss said:

Thank you I'll keep practicing ! :) I'm really motivated so I'll definitely keep going ?

Hey I have a "Invalid renderer" error whenever I try to run my program, I tried modifying several things but I can't make it work. It's different from the guide since I didn't learn the "std::string" thing so I tried to do it another way but I don't even know if this is because of that :/ Can I have a little hint about what's wrong because I can't figure it out ? It's supposed to load my .bmp but there's just no way :



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

void    pause();

int     main(int arc, char **arv)
{
    SDL_Window *win;
    SDL_Renderer *rend;
    SDL_Surface *bmp;
    SDL_Texture *tex;

    int i;

    if(SDL_Init(SDL_INIT_VIDEO) == -1)
    {
        printf("Error during SDL initialization : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    win = SDL_CreateWindow("Hello world!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
    bmp = SDL_LoadBMP("lac_en_montagne.bmp");
    if(bmp == NULL)
    {
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(win);
        printf("Error during BMP : %s\n", SDL_GetError());
    }
    tex = SDL_CreateTextureFromSurface(rend, bmp);
    SDL_FreeSurface(bmp);
    if(tex == NULL)
    {
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(win);
        printf("Error durin Tex to surf : %s\n", SDL_GetError());
    }
    while(i < 3)
    {
        SDL_RenderClear(rend);
        SDL_RenderCopy(rend, tex, NULL, NULL);
        SDL_RenderPresent(rend);
        SDL_Delay(1000);
        i++;
    }
    pause();
    SDL_Quit();
}

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

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

Thank you again for everything guys you're the best !

Never mind I forgot to use the Renderer so the result was kinda normal lol

This topic is closed to new replies.

Advertisement