SDL2 C the image is not displayed

0

I want to create a quiz game. As a first state i want a menu to show up and i want to do it like if the image start is clicked than change a state. So the problem is that my picture with menu didn't even show up. And i have one more question. If for this thing is better option to use SDL_ttf where i click on text or i can use a picture?

here is a code:

#include <SDL.h>    
#include <stdlib.h>
#include <stdio.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 *menu;
SDL_Texture *screen_texture;

int width, height;

void draw_menu()
{
  SDL_Rect src;
  SDL_Rect dest;

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

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

  SDL_BlitSurface(menu, &src, screen, &dest);
}


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)
    {
        if(event.button.button == SDL_BUTTON_LEFT){

            printf("clik\n");
        }
    }
    SDL_RenderClear(renderer);
    SDL_FillRect(screen, NULL, 0x000000ff);


    if (state == 0)
    {
        draw_menu();
    }
    
    SDL_RenderCopy(renderer, screen_texture, NULL, NULL);
    SDL_RenderPresent(renderer);

  }

 }

SDL_FreeSurface(screen);
SDL_FreeSurface(menu);
SDL_DestroyRenderer(renderer);

SDL_DestroyWindow(window);

SDL_Quit();

return 0;

}

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


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_SHOWN, &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;
}

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

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

    return 1;
}

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



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

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0