#SDL question

30 messages · Page 1 of 1 (latest)

patent pumice
#

Can somebody show me how to load a bit map

dusky palmBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

gleaming mountain
patent pumice
#

@gleaming mountain It didn't work

gleaming mountain
patent pumice
#

My screen rendered in white regardless

#

want to see or no

gleaming mountain
patent pumice
#

Nope i didn't get the failure

#

but surfaceblit returned -1

#

which is bad and im puzzled as to why

#
#undef main
#include <iostream>
#define SCREEN_WIDTH  1000
#define SCREEN_HEIGHT 1000
static bool success = false;
class WND
{
public:
    SDL_Window* window = NULL;
    SDL_Surface* wndsurface = NULL;
    SDL_Surface* imgsurface = NULL;

    void close()
    {
        //Deallocate surface
        SDL_FreeSurface(imgsurface);
        imgsurface = NULL;
        SDL_FreeSurface(wndsurface);

        //Destroy window
        SDL_DestroyWindow(window);
        window = NULL;

        //Quit SDL subsystems
        SDL_Quit();
    }
    bool init()
    {
        if (SDL_Init(SDL_INIT_VIDEO) < 0)
        {
            std::cout << "Couldn't initialize exiting" << '\n';
            success = false;
        }
        window = SDL_CreateWindow("Window created", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        SDL_Delay(2000);
        success = true;
        return success;
    }
    bool loadMedia()
    {
        
        bool success = true;

        //Load splash image
        imgsurface = SDL_LoadBMP("Bitmap.bmp");
        if (imgsurface == NULL)
        {
            printf("Unable to load image %s! SDL Error: %s\n", "Bitmap.bmp", SDL_GetError());
            success = false;
        }

        return success;
    }


};
int main()
{
    WND wc;
    SDL_Event e;
    bool quit = false;
    if (!wc.init())
    {
        std::cout << "Couldn't intialize" << '\n';
    }
    if (!wc.loadMedia())
    {
        std::cout << "Media failed" << '\n';
    }
    SDL_BlitSurface(wc.imgsurface, NULL, wc.wndsurface, NULL);
    SDL_UpdateWindowSurface(wc.window);
    SDL_Delay(2000);
    wc.close();
    
}

#

Honestly i'm confused what i'm doing wrong

gleaming mountain
#

I don't know, I haven't used the library

patent pumice
#

.

gleaming mountain
#

try asking in #graphics-gamedev

patent pumice
#

k

next halo
#

Why aren’t you using the renderer to draw on the window? And you could extract the texture from the bitmap surface, dump the surface right away and only store the texture pointer then, which you pass to the renderer.

next halo
#
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, imgsurface);
SDL_FreeSurface(imgsurfacee);```
and then

SDL_Rect srcRect = {0,0,64,64}; // change 64. to actual width and height of your source image
SDL_Rect destRect = {100,100,64,64}; // change x and y to position where you want to render
SDL_RenderCopy(renderer, texture, &srcRect, &destRect);

where `renderer` is a pointer to the `SDL_Renderer`which you create like this:

SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDER_ACCELERATED);

gleaming mountain
#

@patent pumice

patent pumice
#

o

#

i must be rlly bad at rendering

#

It is still rendering white

#

Maybe my bitmap is bad or something

next halo
#

@patent pumice

Maybe need to add for the actual rendering on screen, after you have created you window, your renderer and retrieved your texture, the full rendering process would look like this:

SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, &srcRect, &destRect);
SDL_RenderPresent(renderer);

if that still gives you a white screen, and you would like to see a different colour ;), add this before the render clear command:

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

Should be red then (RGBA values)

If that still does not work, try converting your BMP to PNG and see if that makes a difference.

if you are still stuck, please repost your modified code for review.

patent pumice
#

k

next halo
#

Any progress?

patent pumice
#

Yes i got it to render but now i am working on rendering sprites

next halo
#

👍

dusky palmBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.