/
tut14.cpp
95 lines (76 loc) · 2.25 KB
/
tut14.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<SDL.h>
#include<SDL_image.h>
#include<iostream>
SDL_Texture *LoadTexture(std::string filePath, SDL_Renderer *renderTarget)
{
SDL_Texture *texture = nullptr;
SDL_Surface *surface = IMG_Load(filePath.c_str());
if(surface == NULL)
std::cout << "Error" << std::endl;
else
{
texture = SDL_CreateTextureFromSurface(renderTarget, surface);
if(texture == NULL)
std::cout << "Error" << std::endl;
}
SDL_FreeSurface(surface);
return texture;
}
int main(int argc, char *argv[])
{
// Initializing and loading variables
SDL_Window *window = nullptr;
SDL_Texture *currentImage = nullptr;
SDL_Renderer *renderTarget = nullptr;
SDL_Rect playerRect;
SDL_Rect playerPosition;
playerPosition.x = playerPosition.y = 0;
playerPosition.w = playerPosition.h = 32;
int frameWidth, frameHeight;
int textureWidth, textureHeight;
int frameTime = 0;
SDL_Init(SDL_INIT_VIDEO);
int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG;
if(IMG_Init(imgFlags) != imgFlags)
std::cout << "Error: " << IMG_GetError() << std::endl;
window = SDL_CreateWindow("SDL CodingMadeEasy Series", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
renderTarget = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
currentImage = LoadTexture("image.png", renderTarget);
SDL_QueryTexture(currentImage, NULL, NULL, &textureWidth, &textureHeight);
frameWidth = textureWidth / 3;
frameHeight = textureHeight / 4;
playerRect.x = playerRect.y = 0;
playerRect.w = frameWidth;
playerRect.h = frameHeight;
SDL_SetRenderDrawColor(renderTarget, 0xFF, 0, 0, 0xFF);
bool isRunning = true;
SDL_Event ev;
while(isRunning)
{
while(SDL_PollEvent(&ev) != 0)
{
// Getting the events
if(ev.type == SDL_QUIT)
isRunning = false;
}
frameTime++;
if(frameTime >= 15)
{
frameTime = 0;
playerRect.x += frameWidth;
if(playerRect.x >= textureWidth)
playerRect.x = 0;
}
SDL_RenderClear(renderTarget);
SDL_RenderCopy(renderTarget, currentImage, &playerRect, &playerPosition);
SDL_RenderPresent(renderTarget);
}
SDL_DestroyWindow(window);
SDL_DestroyTexture(currentImage);
SDL_DestroyRenderer(renderTarget);
window = nullptr;
currentImage = nullptr;
renderTarget = nullptr;
SDL_Quit();
return 0;
}