SDL2 C 点击图片

问题描述

我知道为什么这在这里不起作用,但我只想问问是否有办法从 void 或 int 函数以某种方式获得高度宽度 w 或 y 位置。或者只是如何重新创建它以在精确图片上的鼠标点击上工作。我知道我可以用 main 中的图像来完成这一切,但这只是一个菜单,所以我不想有一个 900 行的 main 函数我有一个想法,只需在没有 void 或 int 的不同文件中定义它,只需在那里编写代码,但是有一个问题,我将如何将其称为状态。 这是我的代码

#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,&dest_1);
SDL_BlitSurface(menu_exit,&src_2,&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,screen->pixels,screen->w * sizeof (Uint32));
    SDL_Rendercopy(renderer,screen_texture,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,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,255);
SDL_SetColorKey(nazov,SDL_TRUE,colorkey);





return 0;
}

解决方法

我无法发表评论,但如果您的图片有自己的 SDL_Rect(通过执行类似操作实现):

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

然后你可以这样调用:

if ( SDL_PointInRect( &mousePosition,&imageRect ) ){
    //Mouse in texture
}