Home > Back-end >  Why doesn't the default case run here?
Why doesn't the default case run here?

Time:03-20

I wrote these following lines of codes:

#include <sdl.h>
#include <iostream>
#include <stdio.h>
#include <string>

bool running = true;
enum KeyPressSurfaces {

    KEY_PRESS_SURFACE_DEFAULT,
    KEY_PRESS_SURFACE_UP,
    KEY_PRESS_SURFACE_DOWN,
    KEY_PRESS_SURFACE_LEFT,
    KEY_PRESS_SURFACE_RIGHT,
    //de deallocate surface
    KEY_PRESS_SURFACE_TOTAL

};

int main(int argc, char** argv) {

    //standard stuffs
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("vibin' with smug pika", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 300, 300, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_Surface* loadSurface(std::string path);
    SDL_Surface* surface = SDL_GetWindowSurface(window);
    SDL_Surface* keypress [KEY_PRESS_SURFACE_TOTAL];
    //anh hien tai dang duoc load, de BlitSurface len surface
    //SDL_BlitSurface(current --> surface)
    SDL_Surface* current = NULL;
    //set windows icon
    SDL_Surface* icon = SDL_LoadBMP("../pikachu/keypress_bmp/icon.bmp");
    SDL_SetWindowIcon(window, icon);
    SDL_Event event;

    //load each image into the keypress array
    keypress[KEY_PRESS_SURFACE_DEFAULT] = SDL_LoadBMP("../pikachu/keypress_bmp/default.bmp");
    keypress[KEY_PRESS_SURFACE_UP] = SDL_LoadBMP("../pikachu/keypress_bmp/up.bmp");
    keypress[KEY_PRESS_SURFACE_DOWN] = SDL_LoadBMP("../pikachu/keypress_bmp/down.bmp");
    keypress[KEY_PRESS_SURFACE_LEFT] = SDL_LoadBMP("../pikachu/keypress_bmp/left.bmp");
    keypress[KEY_PRESS_SURFACE_RIGHT] = SDL_LoadBMP("../pikachu/keypress_bmp/right.bmp");


    while (running) {

        while (SDL_PollEvent(&event)){
            switch (event.type) {
            //user click x
            case SDL_QUIT:
                running = false;
                break;
            //where all the user inputs are handled
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym) {
                case SDLK_UP:
                    current = keypress[KEY_PRESS_SURFACE_UP];
                    break;
                case SDLK_DOWN:
                    current = keypress[KEY_PRESS_SURFACE_DOWN];
                    break;
                case SDLK_LEFT:
                    current = keypress[KEY_PRESS_SURFACE_LEFT];
                    break;
                case SDLK_RIGHT:
                    current = keypress[KEY_PRESS_SURFACE_RIGHT];
                    break;
                case SDLK_SPACE:
                    current = keypress[KEY_PRESS_SURFACE_DEFAULT];
                    break;
                default:
                    current = keypress[KEY_PRESS_SURFACE_DEFAULT];
                }
            }
        }
        SDL_BlitSurface(current, NULL, surface, NULL);
        SDL_UpdateWindowSurface(window);
    }

    //deallocating surfaces
    for(int i=0; i<KEY_PRESS_SURFACE_TOTAL;   i) {
        SDL_FreeSurface(keypress[i]);
        keypress[i] = NULL;
    }

    //destroy everythangggg
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
    return 0;
}

However, this bit:

                default:
                    current = keypress[KEY_PRESS_SURFACE_DEFAULT];

does not run at all, no matter where I put it. The program should have shown the default image at the beginning, but there's only black when I compiled. Everything else runs fine and I cannot determine what mistake I make.

I'm using CodeBlock version 20.03 and the latest version of SDL2.

CodePudding user response:

I created a solution for you, and I have some notes to check from your end,

  1. Check if the file path is correct ../pikachu/keypress_bmp/default.bmp.
  2. When you want to trigger the default, you should press another key e.i Enter, A, B, etc.
  3. I have downloaded an SDL library I am not sure if you are using the same, but mine is V2.
  4. I have commented on your code, which I didn't use here in the image path.

here is my code and result:

//#include <sdl.h>
#include <SDL.h>
#include <iostream>
#include <stdio.h>
#include <string>

bool running = true;
enum KeyPressSurfaces {

    KEY_PRESS_SURFACE_DEFAULT,
    KEY_PRESS_SURFACE_UP,
    KEY_PRESS_SURFACE_DOWN,
    KEY_PRESS_SURFACE_LEFT,
    KEY_PRESS_SURFACE_RIGHT,
    //de deallocate surface
    KEY_PRESS_SURFACE_TOTAL

};

int main(int argc, char** argv) {

    //standard stuffs
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("vibin' with smug pika", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 300, 300, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_Surface* loadSurface(std::string path);
    SDL_Surface* surface = SDL_GetWindowSurface(window);
    SDL_Surface* keypress[KEY_PRESS_SURFACE_TOTAL];
    //anh hien tai dang duoc load, de BlitSurface len surface
    //SDL_BlitSurface(current --> surface)
    SDL_Surface* current = NULL;
    //set windows icon
    //SDL_Surface* icon = SDL_LoadBMP("../pikachu/keypress_bmp/icon.bmp");
    SDL_Surface* icon = SDL_LoadBMP("C:/Users/Awat/Desktop/bmp/img1.bmp");
    SDL_SetWindowIcon(window, icon);
    SDL_Event event;

    //load each image into the keypress array
    /*
    keypress[KEY_PRESS_SURFACE_DEFAULT] = SDL_LoadBMP("../pikachu/keypress_bmp/default.bmp");
    keypress[KEY_PRESS_SURFACE_UP] = SDL_LoadBMP("../pikachu/keypress_bmp/up.bmp");
    keypress[KEY_PRESS_SURFACE_DOWN] = SDL_LoadBMP("../pikachu/keypress_bmp/down.bmp");
    keypress[KEY_PRESS_SURFACE_LEFT] = SDL_LoadBMP("../pikachu/keypress_bmp/left.bmp");
    keypress[KEY_PRESS_SURFACE_RIGHT] = SDL_LoadBMP("../pikachu/keypress_bmp/right.bmp");
    */

    keypress[KEY_PRESS_SURFACE_DEFAULT] = SDL_LoadBMP("C:/Users/Awat/Desktop/bmp/imgD.bmp");
    keypress[KEY_PRESS_SURFACE_UP] = SDL_LoadBMP("C:/Users/Awat/Desktop/bmp/img3.bmp");
    keypress[KEY_PRESS_SURFACE_DOWN] = SDL_LoadBMP("C:/Users/Awat/Desktop/bmp/img4.bmp");
    keypress[KEY_PRESS_SURFACE_LEFT] = SDL_LoadBMP("C:/Users/Awat/Desktop/bmp/img5.bmp");
    keypress[KEY_PRESS_SURFACE_RIGHT] = SDL_LoadBMP("C:/Users/Awat/Desktop/bmp/img6.bmp");


    while (running) {

        while (SDL_PollEvent(&event)) {
            switch (event.type) {
                //user click x
            case SDL_QUIT:
                running = false;
                break;
                //where all the user inputs are handled
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym) {
                case SDLK_UP:
                    current = keypress[KEY_PRESS_SURFACE_UP];
                    break;
                case SDLK_DOWN:
                    current = keypress[KEY_PRESS_SURFACE_DOWN];
                    break;
                case SDLK_LEFT:
                    current = keypress[KEY_PRESS_SURFACE_LEFT];
                    break;
                case SDLK_RIGHT:
                    current = keypress[KEY_PRESS_SURFACE_RIGHT];
                    break;
                case SDLK_SPACE:
                    current = keypress[KEY_PRESS_SURFACE_DEFAULT];
                    break;
                default:
                    current = keypress[KEY_PRESS_SURFACE_DEFAULT];
                }
            }
        }
        SDL_BlitSurface(current, NULL, surface, NULL);
        SDL_UpdateWindowSurface(window);
    }

    //deallocating surfaces
    for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL;   i) {
        SDL_FreeSurface(keypress[i]);
        keypress[i] = NULL;
    }

    //destroy everythangggg
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
    return 0;
}

Default Up Right Down Left Debug

  • Related