본문 바로가기
[ChatGPT] Sample Code 샘플 코드

[ChatGPT][SDL2][2D 게임 개발] C와 SDL2 라이브러리를 사용하여 360도 회전하는 로고 회전 애니메이션을 만드는 방법의 예

by byungwoo733 2023. 1. 13.
728x90
반응형
SMALL

C와 SDL2 라이브러리를 사용하여 360도 회전하는 로고 회전 애니메이션을 만드는 방법의 예

// Here's an example of how you can create a logo rotation animation that rotates 360 degrees using C and the SDL2 library
#include <SDL2/SDL.h>

int main(int argc, char* argv[])
{
    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }

    // Create the window
    SDL_Window* window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
    if (window == NULL)
    {
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }

    // Create the renderer
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL)
    {
        printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }

    // Load the image
    SDL_Surface* surface = SDL_LoadBMP("logo.bmp");
    if (surface == NULL)
    {
        printf("Unable to load image %s! SDL_Error: %s\n", "logo.bmp", SDL_GetError());
        return 1;
    }

    // Create the texture
    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
    if (texture == NULL)
    {
        printf("Unable to create texture from %s! SDL_Error: %s\n", "logo.bmp", SDL_GetError());
        return 1;
    }

    // Free the surface
    SDL_FreeSurface(surface);

    // Create a rectangle to represent the logo's position and size
    SDL_Rect logoRect = {200, 150, 400, 300};

    // Create a variable to store the angle of rotation
    double angle = 0.0;

    // Create a variable to store the time of the last frame
    Uint32 lastFrame = 0;

    // Start the game loop
    int quit = 0;
    while (!quit)
    {
        // Handle events
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                quit = 1;
            }
        }

        // Clear the screen
        SDL_RenderClear(renderer);

        // Draw the texture
        SDL_RenderCopyEx(renderer, texture, NULL, &logoRect, angle, NULL, SDL_FLIP_NONE);

이 코드는 SDL_RenderCopyEx() 함수를 사용하여 지정된 회전 각도로 렌더러에 텍스처를 그립니다. 첫 번째 매개변수는 렌더러, 두 번째 매개변수는 텍스처, 세 번째 매개변수는 소스 사각형(이 경우 NULL), 네 번째 매개변수는 대상 사각형(logoRect), 다섯 번째 매개변수는 회전 각도, 각도 변수인 여섯 번째 매개변수는 회전 중심으로 이 경우 NULL이고 마지막 매개변수는 플립 모드(이 경우 SDL_FLIP_NONE)입니다. 

SDL_RenderCopyEx() 함수를 사용하여 사각형의 중심을 여섯 번째 매개변수로 전달하여 텍스처를 사각형의 중심 주위로 회전할 수 있습니다. 그렇지 않으면 회전이 사각형의 왼쪽 상단 모서리 주위에서 수행됩니다.

728x90
LIST

댓글