Wednesday, July 22, 2020

SDL program - dispalying xy coordinates of mouse pointer in sdl window

 hi to all. i am giving here SDL2 program. In which we display images on mouse click, keystrokes and mouse motion. prerequisie : centos 7.5, codeblocks 17.12, SDL2 library and header files(install using rpm).

in C::B add following entries :
in project build options :-
tick on c++11 & targetx86_64 (64 bit) [-m64]

on linker settings :-

SDL2
SDL2_image
SDL2_ttf

in search directories:-
compiler:-
/usr/include/SDL2

in linker :-
/usr/lib64

now new project-> SDL2 project. Enter project name.-> next ->finish.

now open main.cpp and make it blank.
 then write following program:-

    #include <SDL.h>
    #include <SDL_image.h>
    #include <SDL_ttf.h>

    #include <iostream>
    #include <string>
    #include <cstring>

    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;

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

        SDL_Surface *image1 = nullptr;
        SDL_Surface *image2 = nullptr;
        SDL_Surface *image3 = nullptr;
        SDL_Surface * currentSurface  = nullptr;
        SDL_Renderer *renderer = nullptr;
        SDL_Renderer *textRenderer = nullptr;
        SDL_Texture * winTexture = nullptr;

        string str = "";
        char *x, *y;
        x = new char[sizeof(int) + 10];
        y = new char[sizeof(int) + 10];

        char x1[sizeof(int) + 1];
        char y1[sizeof(int) + 1];

        if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
        {
            cout << "Error in SDL_Init(SDL_INIT_EVERYTHING) ... " << SDL_GetError() << endl;
            exit(1);
        }

        SDL_Window *sdlwindow = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
        if(sdlwindow == nullptr)
        {
            cout << "Error in SDL_CreateWindow(\"Game Engine ... \" " << SDL_GetError() << endl;
            exit(2);
        }

        SDL_Surface * windowSurface = SDL_GetWindowSurface(sdlwindow);
        if( windowSurface == nullptr)
        {
            cout << "Error in  SDL_GetWindowSurface(sdlwindow); " << SDL_GetError() <<endl;
            exit(8);
        }

        if( TTF_Init() < 0)
        {
            cout << "Error in    if( TTF_Init() < 0) " << TTF_GetError() << endl;
            exit(11);
        }

        image1 = SDL_LoadBMP("/opt/projects/sdl/images/bmp_file.bmp");

        if(image1 == nullptr)
        {
            cout << " Error in SDL_LoadBMP(\"bmp_file.bmp\"); " <<SDL_GetError() << endl;
            exit(3);
        }
        if((image2 = SDL_LoadBMP("/opt/projects/sdl/images/file1.BMP")) == nullptr)
        {
            cout << "Error in SDL_LoadBMP(\"/opt/projects/sdl/images/file1.BMP\")) " << SDL_GetError() << endl;
            exit(4);
        }

        if((image3 = SDL_LoadBMP("/opt/projects/sdl/images/file2.BMP")) == nullptr)
        {
            cout << "Error in SDL_LoadBMP(\"/opt/projects/sdl/images/file2.BMP\") " << SDL_GetError() << endl;
            exit(5);
        }
        renderer = SDL_CreateRenderer(sdlwindow,-1, SDL_RENDERER_ACCELERATED);

        SDL_Surface *surface = new SDL_Surface;
        SDL_Texture *texture = nullptr;

        SDL_Rect *rect = new SDL_Rect;
        rect->x = rect->y = 0;

        SDL_Rect trect;
        trect.x = 500;
        trect.y = 250;

        TTF_Font *font = TTF_OpenFont("/opt/projects/sdl/fonts/roboto/Roboto-Medium.ttf", 20);
        SDL_Color color = { 0, 255, 0, 255};

        SDL_Texture *text = nullptr;
        SDL_Surface *textSurface = nullptr;

        int imgflags = IMG_INIT_PNG | IMG_INIT_JPG;
        if(!(IMG_Init(imgflags) & imgflags))
        {
            cout << "Error in if(!(IMG_Init(imgflags) & imgflags)) " << IMG_GetError() << endl;
            exit(9);
        }

        bool isRunning = true;

        SDL_Event * ev = new SDL_Event;
        while(isRunning)
        {
             while(SDL_PollEvent(ev) != 0)
            {

                if(ev->type == SDL_QUIT)
                {
                    isRunning = false;
                }
                else if(ev->type == SDL_MOUSEBUTTONDOWN)
                {
                    if(ev->button.button == SDL_BUTTON_LEFT)
                    {
                        surface = IMG_Load("/opt/projects/sdl/images/road.bmp");
                    }
                    else if(ev->button.button == SDL_BUTTON_RIGHT)
                    {
                        surface = IMG_Load("/opt/projects/sdl/images/rose.bmp");
                    }
                    texture = SDL_CreateTextureFromSurface(renderer,surface);
                }

                else if(ev->type == SDL_KEYDOWN)
                {

                    switch (ev->key.keysym.sym)
                    {
                    case SDLK_1:
                        surface = IMG_Load("/opt/projects/sdl/images/bridge.jpg");
                        break;
                    case SDLK_2:
                        surface = IMG_Load("/opt/projects/sdl/images/tree.jpg");
                        break;
                    case SDLK_3:
                        surface = IMG_Load("/opt/projects/sdl/images/filepng.png");
                        break;
                    }
                    texture = SDL_CreateTextureFromSurface(renderer,surface);
                }

                else if(ev->type == SDL_MOUSEMOTION)
                {
                    snprintf(x, sizeof(x), "%d", ev->button.x );
                    snprintf(y, sizeof(y), "%d", ev->button.y);

                    str.append(" x axis : " );
                    str.append(x);
                    str.append("  y axis : " );
                    str.append(y);

                    textSurface = TTF_RenderText_Solid(font, str.c_str(), color);
                    if(textSurface == nullptr)
                    {
                        cout << "Error in  textSurface = TTF_RenderText_Solid(font, str.c_str(), color); " << SDL_GetError() << endl;
                        exit(10);
                    }
                    winTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
                    if(winTexture == nullptr)
                    {
                        cout << "Error in  texture = SDL_CreateTextureFromSurface(renderer, textSurface); " << SDL_GetError() <<endl;
                        exit(12);
                    }
                    SDL_QueryTexture(winTexture, NULL, NULL, &trect.w, &trect.h);
                    str = "";
                }

            }

            SDL_RenderClear(renderer);
            SDL_RenderCopy(renderer, texture, NULL, NULL);//rect);
            SDL_RenderCopy(renderer, winTexture, NULL, &trect);
            SDL_RenderPresent(renderer);
        }

        SDL_FreeSurface(windowSurface);
        SDL_FreeSurface(currentSurface);
        SDL_DestroyWindow(sdlwindow);
        SDL_FreeSurface(image1);
        SDL_FreeSurface(image2);
        SDL_FreeSurface(image3);

        windowSurface = currentSurface = image1 = image2 = image3 = nullptr;
        sdlwindow = nullptr;

        IMG_Quit();
        TTF_Quit();
        SDL_Quit();

        return 0;
    }

now  download some image files used in this program. save these in /opt/projects/sdl/images







No comments:

Post a Comment