Images won't load, and keyboard events aren't working, need help :)

  • bhogr001
    Likes 0

    Problem Description

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

    using namespace std;

    int main(int argc, char *argv[])
    {
        SDL_Window *window = nullptr;
        SDL_Surface *image1 = nullptr;
        SDL_Surface *image2 = nullptr;
        SDL_Surface *image3 = nullptr;
        SDL_Surface *currentImage = nullptr;
        SDL_Surface *windowSurface = nullptr;

        SDL_Init(SDL_INIT_VIDEO);

        window = SDL_CreateWindow("SDL Rajan's Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);    
        windowSurface = SDL_GetWindowSurface(window);

        //Loaded bitmap file to the window
        image1 = SDL_LoadBMP("bitmap example.bmp");
        image2 = SDL_LoadBMP("bitmap example 2.bmp");
        image3 = SDL_LoadBMP("bitmap example 3.bmp");
        currentImage = image1;

        bool IsRunning = true;
        SDL_Event ev;

        while (IsRunning)
        {
            //pollevent, puts events into a cue, and processes the events
            while (SDL_PollEvent(&ev) != 0)
            {
                //getting the quit and the mouse/keyboard events
                if (ev.type == SDL_QUIT)
                    IsRunning = false;
                else if (ev.type == SDL_MOUSEBUTTONDOWN)
                {
                    if (ev.button.button == SDL_BUTTON_LEFT)
                        currentImage = image1;
                    else if (ev.button.button == SDL_BUTTON_RIGHT)
                        currentImage = image2;
                    else if (ev.button.button == SDL_BUTTON_X1)
                        currentImage = image3;
                }
            }

            //Drawing the current image to the window
            SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
            SDL_UpdateWindowSurface(window);
        }

        //Freeing the memory
        SDL_FreeSurface(image1);
        SDL_FreeSurface(image2);
        SDL_FreeSurface(image3);

        currentImage = image1 = image2 = image3 = nullptr;
        window = nullptr;

        SDL_DestroyWindow(window);
        SDL_Quit();

        return 0;
    }

  • Sonar Systems admin
    Likes 0

    What happens, do you get any errors/warnings?

Login to reply