-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
60 lines (46 loc) · 1.82 KB
/
main.cpp
File metadata and controls
60 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <SDL2/SDL.h>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int FRAME_WIDTH = 64; // Width of each frame in the sprite sheet
const int FRAME_HEIGHT = 64; // Height of each frame in the sprite sheet
const int NUM_FRAMES = 4; // Total number of frames in the sprite sheet
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SDL Animation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Surface* spriteSheetSurface = SDL_LoadBMP("spritesheet.bmp");
SDL_Texture* spriteSheetTexture = SDL_CreateTextureFromSurface(renderer, spriteSheetSurface);
SDL_FreeSurface(spriteSheetSurface);
SDL_Rect frameRects[NUM_FRAMES];
for (int i = 0; i < NUM_FRAMES; ++i) {
frameRects[i].x = i * FRAME_WIDTH;
frameRects[i].y = 0;
frameRects[i].w = FRAME_WIDTH;
frameRects[i].h = FRAME_HEIGHT;
}
int frameIndex = 0;
int animationTimer = 0;
const int animationDelay = 100; // Delay between frame updates (in milliseconds)
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
animationTimer += SDL_GetTicks();
if (animationTimer >= animationDelay) {
animationTimer = 0;
frameIndex = (frameIndex + 1) % NUM_FRAMES;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, spriteSheetTexture, &frameRects[frameIndex], nullptr);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(spriteSheetTexture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}