How can you resize an image with in SDL_image?

58 Views Asked by At

I have been trying to follow how to create an image in SDL recently. So far, I have been able to display an image on the screen, but the problem is that it takes up the entire window. Where I am stuck is how to resize an image so that it only takes up a portion of the screen.

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, 511, 683, SDL_WINDOW_SHOWN);

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

    SDL_SetRenderDrawColor(renderer, 46, 139, 87, 255);

    SDL_RenderClear(renderer);

    SDL_RenderPresent(renderer);

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

    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, image);

    SDL_Rect src_rect = { 0, 0 };
    SDL_Rect dst_rect = { 0, 0, 200, 200 };

    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);

    SDL_BlitScaled(image, &src_rect, SDL_GetWindowSurface(window), &dst_rect);

    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;
        }
    }

}

#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 alter the image's height and width in src_rect and dst_rect, however this has not worked.

0

There are 0 best solutions below