개발/게임

SDL2(4) - RUST 버전

슈케르 2025. 5. 30. 11:40
728x90

C++로 만들었던 테트리스를 RUST로 바꿔볼 차례다.

C/C++ 로 만들때는 sdl3-ttf library를 가져다 쓰기가 귀찮아서 픽셀로 표현했었는데,

RUST 는 crate 를 가져다가 쓰기 편하니까 ttf로 바꿔서 구현하려고 한다.

https://docs.rs/releases/search?query=sdl3

 

그런데... 음.. 아직 sdl3 버전에서 ttf library 사용 지원이 안되는걸까?..  요거 건너 뛰어야겠다. 

공식적으로 binding 지원이 안되는것 같다. 

그냥 c++과 같은 방식으로. 흠흠

 

 

SDL2로 재작업 시작...

1달동안 다른거 (자격증 공부)하느라 잊고 있었던 작업을 다시 해보자.

 

위에 언급한것처럼 sdl3 binding 지원이 완료되지 않은것 같아서 그냥 sdl2로 작성해보겠다.

RUST는 짬짬이 공부하는 언어라서 소스 분석도 한번 곁들여서 note를 해보려고 한다.

 

 

먼저 cargo.toml 부터 살펴보자.

[package]
name = "tetris_rust"
version = "0.1.0"
edition = "2021"

[dependencies]
sdl2 = {version = "0.37.0", features = ["image", "ttf"]}
rand = "0.9.1"

features에 image와 ttf를 추가했다.

 

 

windows 에서 build시 아래처럼 link error가 발생할거다. window용 lib을 설치하고, config.toml 또는 환경 변수 설정을 해줘야 한다. 편집 순서가 이상해보일 수 있는데, 사실 코드는 얼추 만들어놓은 상태에서 글을 쓰기 시작한것이기 때문이다.

console에서 cargo build시 발생하는 error message다. 

error: linking with `link.exe` failed: exit code: 1181
  |
  = note: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.42.34433\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "C:\\Users\\suker\\AppData\\Local\\Temp\\rustc2LcPps\\symbols.o" "C:\\Users\\suker\\MYSRC\\RUST\\tetris_rust\\target\\debug\\deps\\tetris_rust.

... 생략 ...
= note: LINK : fatal error LNK1181: cannot open input file 'SDL2.lib'␍

 

1. SDL2 다운로드

https://github.com/libsdl-org/SDL/releases/tag/release-2.32.6

 

Releases · libsdl-org/SDL

Simple Directmedia Layer. Contribute to libsdl-org/SDL development by creating an account on GitHub.

github.com

  ==> SDL2-devel-2.32.6-VC.zip  download

         다른 파일에는 .dll만 있다거나 소스만 존재한다.

         따라서 .lib와 .dll을 같이 받기위해서 vc 버전을 선택하여 download한다.

 

2. SDL2_image 다운로드

https://github.com/libsdl-org/SDL_image/releases/tag/release-2.8.8

 

Release 2.8.8 · libsdl-org/SDL_image

This is a stable bugfix release, with the following changes: Fixed alpha in less than 32-bit ICO and CUR images

github.com

 

 

3. SDL2_ttf 다운로드

https://github.com/libsdl-org/SDL_ttf/releases/tag/release-2.24.0

 

Release 2.24.0 · libsdl-org/SDL_ttf

Here are the major changes in this release: Added TTF_SetFontLineSkip()

github.com

 

 

적당한 위치에 압축을 풀어놓고, path를 설정한다.

나는 시스템환경변수 설정을 하지 않고, .cargo/config.toml을 이용했다.

 

4. .cargo/config.toml

# .cargo/config.toml
[target.x86_64-pc-windows-msvc]
rustflags = [
  "-L", "C:/Users/suker/MYSRC/tools/SDL2-2.32.6/lib/x64",
  "-L", "C:/Users/suker/MYSRC/tools/SDL2_image-2.8.8/lib/x64",
  "-L", "C:/Users/suker/MYSRC/tools/SDL2_ttf-2.24.0/lib/x64"
]

 

 

5. 소스코드 분석

첫번째 작성한 소스는 아래 git을 참고

https://github.com/kchhero/suker_rust_project

commit : 5a381623b02b3d144633af785b0a143b1ac6a36d

 

GitHub - kchhero/suker_rust_project

Contribute to kchhero/suker_rust_project development by creating an account on GitHub.

github.com

 

사실 해당 시리즈 시작에서 밝혔듯이 C로 만든것을 ChatGpt를 이용해서 C++, RUST로 converting 하고 있다. 

따라서 cpp버전의 소스를 rust로 바꿔달라고 chatgpt에게 요청하였고 뭐, 금새 만들어 냈다.

https://github.com/kchhero/suker_vs_project/tree/master/tetris_cpp  

 

6. 하지만.....

쫌 길어져서.. 다음 글에서 계속...

728x90