728x90
반응형
SMALL
2D 게임 캐릭터 스프라이트 SDL2 샘플 코드
#include <SDL2/SDL.h>
int main() {
// Initialize SDL
SDL_Init(SDL_INIT_VIDEO);
// Create a window
SDL_Window* window = SDL_CreateWindow("Sprite Sheet Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
// Create a renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
// Load the sprite sheet
SDL_Surface* spriteSheet = SDL_LoadBMP("character.bmp");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, spriteSheet);
SDL_FreeSurface(spriteSheet);
// Set the starting animation frame
int frame = 0;
// Set the animation to the walking right animation
SDL_Rect srcRect = {frame * 64, 0, 64, 64}; // 64 is the width and height of each frame
// Main game loop
while (1) {
// Handle events
SDL_Event event;
if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
break;
}
}
// Update the animation frame
frame = (frame + 1) % 8; // 8 is the number of frames in the sprite sheet
// Clear the window
SDL_RenderClear(renderer);
// Draw the sprite
SDL_RenderCopy(renderer, texture, &srcRect, NULL);
// Display the window
SDL_RenderPresent(renderer);
}
// Clean up
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
/* here is an example of how you could handle input controls for a character in a 2D game using C and SDL library
// Update the character's position based on input
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_LEFT]) {
srcRect.x -= 5;
// Set the animation to the walking left animation
srcRect.y = 64;
}
if (state[SDL_SCANCODE_RIGHT]) {
srcRect.x += 5;
// Set the animation to the walking right animation
srcRect.y = 0;
}
if (state[SDL_SCANCODE_UP]) {
srcRect.y -= 5;
// Set the animation to the jumping animation
srcRect.y = 128;
}
if (state[SDL_SCANCODE_DOWN]) {
srcRect.y += 5;
// Set the animation to the crouching animation
srcRect.y = 192;
}
*/
728x90
LIST
'[ChatGPT] Sample Code 샘플 코드' 카테고리의 다른 글
[ChatGPT][SFML][2D 게임 개발] C++ 및 SFML을 사용하여 2D 게임에서 스테이지 간 이동을 위한 시스템을 구현 SFML 샘플 (0) | 2023.01.12 |
---|---|
[ChatGPT][SDL2][2D 게임 개발] C 및 SDL 라이브러리를 사용하여 2D 게임에서 스테이지 전환을 구현 SDL2 샘플 (0) | 2023.01.11 |
[ChatGPT][SFML][2D 게임 개발] 2D 게임 캐릭터 스프라이트 SFML 샘플 코드 (0) | 2023.01.11 |
[ChatGPT][SFML][2D 게임 개발] 2D 스프라이트 SFML 샘플 코드 (0) | 2023.01.11 |
[ChatGPT][SDL2 개발] 2D 스프라이트 SDL2 샘플 코드 (0) | 2023.01.11 |
댓글