Why is my image not loading after pressing the left button?

63 views Asked by At

I am currently trying to display a window with a background image, but when the left button is clicked, a smaller image appears on the screen with the background image. The background image launches fine, but it's the second image that's the problem here.

Code:

windowcreate.h

#pragma once

#ifndef WINDOWCREATE_H
#define WINDOWCREATE_H

#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include <cstdio>
#include <Windows.h>

using namespace std;

void windowcr() {
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* window = SDL_CreateWindow("SDLwindow", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 650, 650, SDL_WINDOW_SHOWN);

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    SDL_RenderClear(renderer);

    SDL_RenderPresent(renderer);

    SDL_Surface* originalImage = IMG_LoadPNG_RW(SDL_RWFromFile("nac.png", "rb"));

    int newWidth = 550;
    int newHeight = 550;

    SDL_Surface* resizedImage = SDL_CreateRGBSurface(0, newWidth, newHeight, originalImage->format->BitsPerPixel, originalImage->format->Rmask, originalImage->format->Gmask, originalImage->format->Bmask, originalImage->format->Amask);

    SDL_BlitScaled(originalImage, nullptr, resizedImage, nullptr);

    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, resizedImage);

    int windowWidth, windowHeight;
    SDL_GetWindowSize(window, &windowWidth, &windowHeight);

    SDL_Rect dst_rect = {
        (windowWidth - newWidth) / 2,
        (windowHeight - newHeight) / 2,
        newWidth,
        newHeight
    };

    SDL_RenderCopy(renderer, texture, nullptr, &dst_rect);

        SDL_Surface* crossImage = IMG_LoadJPG_RW(SDL_RWFromFile("ep2.jpeg", "rb"));

        if (GetAsyncKeyState(0x01) & 0x8000) {

            if (!crossImage) {

                cerr << "Error loading ep2.jpeg: " << IMG_GetError() << endl;

            }
            else {
                int crossWidth = 200;
                int crossHeight = 200;

                SDL_Surface* crossResized = SDL_CreateRGBSurface(0, crossWidth, crossHeight, crossImage->format->BitsPerPixel, crossImage->format->Rmask, crossImage->format->Gmask, crossImage->format->Bmask, crossImage->format->Amask);

                SDL_BlitScaled(crossImage, nullptr, crossResized, nullptr);

                SDL_Texture* crossTexture = SDL_CreateTextureFromSurface(renderer, crossResized);

                int sWindowWidth, sWindowHeight;
                SDL_GetWindowSize(window, &sWindowWidth, &sWindowHeight);

                SDL_Rect cross_dst_rect = {
                    (sWindowWidth - crossWidth) / 2,
                    (sWindowHeight - crossHeight) / 2,
                    crossWidth,
                    crossHeight
                };


                SDL_RenderCopy(renderer, crossTexture, nullptr, &cross_dst_rect);

            };

        };
    SDL_RenderPresent(renderer);

    SDL_Event event;
    bool running = true;

    while (running) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
        }

        if (GetAsyncKeyState(0x45) & 0x8000) {
            SDL_DestroyRenderer(renderer);
            SDL_DestroyWindow(window);
            SDL_Quit();
            break;
        }
    }

    SDL_FreeSurface(originalImage);
    SDL_FreeSurface(resizedImage);
}

#endif

main.cpp

#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include <cstdio>
#include <Windows.h>
#include "windowcreate.h"

using namespace std;

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

    windowcr();

    return 0;
}

I have tried to change the key pressed in the if statement but that has not worked out either.

0

There are 0 answers