SDL2 C click on picture

-1

I know why this isn't working here but i just want to ask if there is a way to somehowe get a height width w or y position from void or int function. Or just how to recreate this to work it on mouse klik on exact picture. I know i can do this all with images in main but this is just a menu so i dont want to have a 900 lines main function. I got an idea to just define it in a different file without a void or int just write a code there but then there is a question how i will call it as a state. Here is my code:

#include <stdio.h>
#include <stdlib.h>

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

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

SDL_Window* window = NULL;
SDL_Renderer *renderer;

static SDL_Surface *screen;
static SDL_Surface *nazov;
static SDL_Surface *menu_start;
static SDL_Surface *menu_exit;
SDL_Texture *screen_texture;




 int draw_menu()
{
int width, height;
SDL_Rect src;
SDL_Rect dest;
SDL_Rect src_1;
SDL_Rect dest_1;
SDL_Rect src_2;
SDL_Rect dest_2;

src.x = 0;
src.y = 0;
src.w = nazov->w;
src.h = nazov->h;

dest.x = (screen->w / 2) - (src.w / 2);
dest.y = (screen->h / 2) - (src.h / 2);
dest.w = nazov->w;
dest.h = nazov->h;

src_1.x = 0;
src_1.y = 0;
src_1.w = menu_start->w;
src_1.h = menu_start->h;

dest_1.x = (screen->w / 2) - (src_1.w / 2);
dest_1.y = (screen->h / 2) + src.h;
dest_1.w = menu_start->w;
dest_1.h = menu_start->h;

src_2.x = 0;
src_2.y = 0;
src_2.w = menu_exit->w;
src_2.h = menu_exit->h;

dest_2.x = (screen->w / 2) - (src_2.w / 2);
dest_2.y = (screen->h / 2) + (src.h + src_1.h + (src_1.h * 2));
dest_2.w = menu_exit->w;
dest_2.h = menu_exit->h;

SDL_BlitSurface(nazov, &src, screen, &dest);
SDL_BlitSurface(menu_start, &src_1, screen, &dest_1);
SDL_BlitSurface(menu_exit, &src_2, screen, &dest_2);
}


 int main(int argc, char *args[])
{
if (initialize(SCREEN_WIDTH, SCREEN_HEIGHT, argc, args) == 1) {

    return 0;
}

SDL_GetWindowSize(window, &width, &height);
int quit = 0;
int state = 0;
SDL_Event event;



   while(quit == 0) {


    while (SDL_PollEvent(&event)){
    SDL_PumpEvents();
    const Uint8 *keystate = SDL_GetKeyboardState(NULL);

    if (keystate[SDL_SCANCODE_ESCAPE]) {

        quit = 1;
    }
    if (event.type == SDL_MOUSEBUTTONDOWN)
    {
        int x = event.button.x;
        int y = event.button.y;
        if(event.button.button == SDL_BUTTON_LEFT)
        {
            if ((x > src_1.x) && x < (src_1.x + src_1.w) && ( y > src_1.y ) && ( y < src_1.y + 
    src_1.h ))
            {
            printf("clik\n");
            }
        }
    }
    SDL_RenderClear(renderer);
    SDL_FillRect(screen, NULL, 0x000000ff);


    if (state == 0)
    {
        draw_menu();
    }
    SDL_UpdateTexture(screen_texture, NULL, screen->pixels, screen->w * sizeof (Uint32));
    SDL_RenderCopy(renderer, screen_texture, NULL, NULL);
    SDL_RenderPresent(renderer);

   }

   }

SDL_FreeSurface(screen);
SDL_FreeSurface(nazov);
SDL_FreeSurface(menu_start);
SDL_FreeSurface(menu_exit);
//free renderer and all textures used with it
SDL_DestroyRenderer(renderer);

//Destroy window
SDL_DestroyWindow(window);

//Quit SDL subsystems
SDL_Quit();

return 0;

}

 int initialize(int width, int height, int argc, char *args[]) {

//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {

    printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());

    return 1;
}


SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_FULLSCREEN_DESKTOP, &window, &renderer);


if (window == NULL) {

    printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());

    return 1;
}

screen = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_RGBA32);

if (screen == NULL) {

    printf("Could not create the screen surfce! SDL_Error: %s\n", SDL_GetError());

    return 1;
}

screen_texture = SDL_CreateTextureFromSurface(renderer, screen);

if (screen_texture == NULL) {

    printf("Could not create the screen_texture! SDL_Error: %s\n", SDL_GetError());

    return 1;
}

nazov = IMG_Load("nazov.png");
if (nazov == NULL) {

    printf("Could not Load nazov image! SDL_Error: %s\n", SDL_GetError());

    return 1;
}
menu_start = IMG_Load("menu_start.png");
if (menu_start == NULL) {

    printf("Could not Load menu_start image! SDL_Error: %s\n", SDL_GetError());

    return 1;
}
menu_exit = IMG_Load("menu_exit.png");
if (menu_exit == NULL) {

    printf("Could not Load nazov image! SDL_Error: %s\n", SDL_GetError());

    return 1;
}


Uint32 colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
SDL_SetColorKey(nazov, SDL_TRUE, colorkey);





return 0;
}
c
sdl-2
sdl-image
asked on Stack Overflow Mar 25, 2021 by matthews • edited Mar 25, 2021 by matthews

1 Answer

-1

I can't comment, but if your image has its own SDL_Rect (achieved by doing something like this):

SDL_Rect imageRect;
SDL_QueryTexture(imageTexture, nullptr, nullptr, &imageRect.w, &imageRect.h);

then you can call something like this:

if ( SDL_PointInRect( &mousePosition, &imageRect ) ){
    //Mouse in texture
}
answered on Stack Overflow Mar 26, 2021 by 404TeamNotFound

User contributions licensed under CC BY-SA 3.0