Untitled
#define SDL_MAIN_USE_CALLBACKS 1 #include <SDL3/SDL.h> #include <SDL3/SDL_main.h> #include <stdio.h> #include <math.h> static SDL_Window* window = NULL; static SDL_Renderer* renderer = NULL; static SDL_Texture* playbuttonUP = NULL; static SDL_Texture* playbuttonDOWN = NULL; static SDL_Texture* playbuttonHOVER = NULL; static SDL_Texture* playmenuWALLPAPER = NULL; static int texture_width = 0; static int texture_height = 0; static float buttonX = 200; static float buttonW = 200; static float buttonY = 100; static float buttonH = 100; void loadtextures() { SDL_Surface* surface = NULL; char* bmp_path = NULL; /*Play button up*/ SDL_asprintf(&bmp_path, "%splaybuttonUPres.bmp", SDL_GetBasePath()); surface = SDL_LoadBMP(bmp_path); SDL_free(bmp_path); texture_width = surface->w; texture_height = surface->h; playbuttonUP = SDL_CreateTextureFromSurface(renderer, surface); SDL_DestroySurface(surface); /*Play button down*/ SDL_asprintf(&bmp_path, "%splaybuttonDOWNres.bmp", SDL_GetBasePath()); surface = SDL_LoadBMP(bmp_path); SDL_free(bmp_path); texture_width = surface->w; texture_height = surface->h; playbuttonDOWN = SDL_CreateTextureFromSurface(renderer, surface); SDL_DestroySurface(surface); /*Play button hover*/ SDL_asprintf(&bmp_path, "%splaybuttonHOVERres.bmp", SDL_GetBasePath()); surface = SDL_LoadBMP(bmp_path); SDL_free(bmp_path); texture_width = surface->w; texture_height = surface->h; playbuttonHOVER = SDL_CreateTextureFromSurface(renderer, surface); SDL_DestroySurface(surface); /*Play menu wallpaper*/ /*SDL_asprintf(&bmp_path, "%splaymenu.bmp", SDL_GetBasePath()); surface = SDL_LoadBMP(bmp_path); SDL_free(bmp_path); texture_width = surface->w; texture_height = surface->h; playmenuWALLPAPER = SDL_CreateTextureFromSurface(renderer, surface); SDL_DestroySurface(surface);*/ } /*void playmenuwallpapershow() { SDL_FRect wallpaper; wallpaper.x = 0; wallpaper.w = 600; wallpaper.y = 0; wallpaper.h = 600; SDL_RenderTexture(renderer, playmenuWALLPAPER, NULL, &wallpaper); }*/ void renderplaybutton(short option, float buttonX, float buttonW, float buttonY, float buttonH) { SDL_FRect PlayButton; PlayButton.x = buttonX; PlayButton.w = buttonW; PlayButton.y = buttonY; PlayButton.h = buttonH; switch (option) { case(1): /*Play button up*/ SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer); //playmenuwallpapershow(); SDL_RenderTexture(renderer, playbuttonUP, NULL, &PlayButton); SDL_RenderPresent(renderer); break; case(2): /*Play button down*/ SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer); //playmenuwallpapershow(); SDL_RenderTexture(renderer, playbuttonDOWN, NULL, &PlayButton); SDL_RenderPresent(renderer); break; case(3): /*Play button hover*/ SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer); //playmenuwallpapershow(); SDL_RenderTexture(renderer, playbuttonHOVER, NULL, &PlayButton); SDL_RenderPresent(renderer); break; } } bool buttondetection(float buttonX, float buttonW, float buttonY, float buttonH, SDL_Event* event) { float buttonhitboxX = buttonX; float buttonhitboxW = buttonX + buttonW; float buttonhitboxY = buttonY; float buttonhitboxH = buttonY + buttonH; static bool down; float cursorX = 0; float cursorY = 0; if (event->type == SDL_EVENT_MOUSE_MOTION) { SDL_GetMouseState(&cursorX, &cursorY); if (cursorX >= buttonhitboxX && cursorX <= buttonhitboxW && cursorY >= buttonhitboxY && cursorY <= buttonhitboxH && down == false) renderplaybutton(3, buttonX, buttonW, buttonY, buttonH); else if (down == false) renderplaybutton(1, buttonX, buttonW, buttonY, buttonH); } if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) if (event->button.button == SDL_BUTTON_LEFT) { SDL_GetMouseState(&cursorX, &cursorY); if (cursorX >= buttonhitboxX && cursorX <= buttonhitboxW && cursorY >= buttonhitboxY && cursorY <= buttonhitboxH) { renderplaybutton(2, buttonX, buttonW, buttonY, buttonH); down = true; } } if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) if (event->button.button == SDL_BUTTON_LEFT) if (down == true) { renderplaybutton(1, buttonX, buttonW, buttonY, buttonH); down = false; return true; } return false; } SDL_AppResult SDL_AppInit(void** appstate, int argc, char** argv) //runs once { static SDL_Window* window = NULL; bool done = false; SDL_Init(SDL_INIT_VIDEO); window = SDL_CreateWindowAndRenderer( "so true", 600, 600, SDL_WINDOW_RESIZABLE, &window, &renderer ); loadtextures(); renderplaybutton(1, buttonX, buttonW, buttonY, buttonH); printf("hello console"); return SDL_APP_CONTINUE; } SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) //runs when interacted { if (buttondetection(buttonX, buttonW, buttonY, buttonH, event) == true) printf("\nClick!"); if (event->type == SDL_EVENT_QUIT) { return SDL_APP_SUCCESS; } return SDL_APP_CONTINUE; } SDL_AppResult SDL_AppIterate(void* appstate) //runs every frame { return SDL_APP_CONTINUE; } void SDL_AppQuit(void* appstate, SDL_AppResult result) //runs before quit { }
Leave a Comment