본문 바로가기
Game Development Study 게임 개발 공부

[SDL2][Code::Blocks 코드블럭] SDL2 Color Bar Sample Code

by byungwoo733 2022. 10. 15.
728x90
반응형
SMALL

Code::Blocks 코드블럭에서 실행해본 SDL2 Color Bar 샘플 코드인데 색상코드를 조금 수정해보고 있다.

수정된 부분은 이 부분인데

// Added RGB Colors
                    143, 143, 143,
                    255,  16, 253,
                  253,   3,   2,
                   18,  14, 252,
                    0,   0,   0,
                    143, 143, 143,

RGB 색상 추가했는데 기본 최대 색상 RGB (255, 255, 255) 중에서 색상 코드 숫자를 바꿔보면서

테스트 해보았다.

int rgb[] = { 203, 203, 203, // Gray
                  254, 254,  31, // Yellow
                    0, 255, 255, // Cyan
                    0, 254,  30, // Green
                  255,  16, 253, // Magenta
                  253,   3,   2, // Red
                   18,  14, 252, // Blue
                    0,   0,   0,  // Black
                    // Added RGB Colors
                    143, 143, 143,
                    255,  16, 253,
                  253,   3,   2,
                   18,  14, 252,
                    0,   0,   0,
                    143, 143, 143,
                };

Code::Blocks 코드블럭 20.03 IDE 환경

 

실행된 화면

========================

SDL2 RGB Color Bar 전체 샘플코드

#include <exception>
#include <string>
#include <iostream>
#include <SDL2/SDL.h>


class InitError : public std::exception
{
    std::string msg;
public:
    InitError();
    InitError( const std::string & );
    virtual ~InitError() throw();
    virtual const char * what() const throw();
};

InitError::InitError() :
    exception(),
    msg( SDL_GetError() )
{
}

InitError::InitError( const std::string & m ) :
    exception(),
    msg( m )
{
}

InitError::~InitError() throw()
{
}

const char * InitError::what() const throw()
{
    return msg.c_str();
}

class SDL
{
    SDL_Window * m_window;
    SDL_Renderer * m_renderer;
public:
    SDL( Uint32 flags = 0 );
    virtual ~SDL();
    void draw();
};

SDL::SDL( Uint32 flags )
{
    if ( SDL_Init( flags ) != 0 )
        throw InitError();

    if ( SDL_CreateWindowAndRenderer( 1280, 720, SDL_WINDOW_SHOWN,
                                      &m_window, &m_renderer ) != 0 )
        throw InitError();
}

SDL::~SDL()
{
    SDL_DestroyWindow( m_window );
    SDL_DestroyRenderer( m_renderer );
    SDL_Quit();
}

void SDL::draw()
{
    // Clear the window with a black background
    SDL_SetRenderDrawColor( m_renderer, 0, 0, 0, 255 );
    SDL_RenderClear( m_renderer );

    // Show the window
    SDL_RenderPresent( m_renderer );

    int rgb[] = { 203, 203, 203, // Gray
                  254, 254,  31, // Yellow
                    0, 255, 255, // Cyan
                    0, 254,  30, // Green
                  255,  16, 253, // Magenta
                  253,   3,   2, // Red
                   18,  14, 252, // Blue
                    0,   0,   0,  // Black
                    // Added RGB Colors
                    143, 143, 143,
                    255,  16, 253,
                  253,   3,   2,
                   18,  14, 252,
                    0,   0,   0,
                    143, 143, 143,
                };

    SDL_Rect colorBar;
    colorBar.x = 0; colorBar.y = 0; colorBar.w = 90; colorBar.h = 480;

    // Render a new color bar every 0.5 seconds
    for ( int i = 0; i != sizeof rgb / sizeof *rgb; i += 3, colorBar.x += 90 )
    {
        SDL_SetRenderDrawColor( m_renderer, rgb[i], rgb[i + 1], rgb[i + 2], 255 );
        SDL_RenderFillRect( m_renderer, &colorBar );
        SDL_RenderPresent( m_renderer );
        SDL_Delay( 500 );
    }
}

int main( int argc, char * argv[] )
{
    try
    {
        SDL sdl( SDL_INIT_VIDEO | SDL_INIT_TIMER );
        sdl.draw();

        return 0;
    }
    catch ( const InitError & err )
    {
        std::cerr << "Error while initializing SDL:  "
                  << err.what()
                  << std::endl;
    }

    return 0;
}

 

* (주의) 여기 예제나 글들은 게임개발에 도움이 되는 정보 보다는 개인 공부을 위한 것이니 옳은 방법이 아닐 수 있으니 참고바랍니다. 

728x90
LIST

댓글