/
tut8.cpp
74 lines (59 loc) · 1.8 KB
/
tut8.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
#include<SDL.h>
#include<iostream>
int main(int argc, char *argv[])
{
// Initializing and loading variables
SDL_Window *window = nullptr;
SDL_Surface *windowSurface = nullptr;
SDL_Surface *image1 = nullptr;
SDL_Surface *image2 = nullptr;
SDL_Surface *image3 = nullptr;
SDL_Surface *currentImage = nullptr;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
SDL_Joystick *joystick = SDL_JoystickOpen(0);
std::cout << "Controller Name: " << SDL_JoystickName(joystick) << std::endl;
std::cout << "Num Axes: " << SDL_JoystickNumAxes(joystick) << std::endl;
std::cout << "Num Buttons: " << SDL_JoystickNumButtons(joystick) << std::endl;
window = SDL_CreateWindow("SDL CodingMadeEasy Series", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window);
image1 = SDL_LoadBMP("test.bmp");
image2 = SDL_LoadBMP("test1.bmp");
image3 = SDL_LoadBMP("test2.bmp");
currentImage = image1;
bool isRunning = true;
SDL_Event ev;
while(isRunning)
{
while(SDL_PollEvent(&ev) != 0)
{
// Getting the quit and the keyboard events
if(ev.type == SDL_QUIT)
isRunning = false;
else if(ev.type == SDL_JOYBUTTONDOWN)
{
if(ev.jbutton.which == 1)
{
if(ev.jbutton.button == 0)
currentImage = image2;
else if(ev.jbutton.button == 10)
currentImage = image3;
}
}
}
// Drawing the current image to the window
SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
}
// Freeing the memory
SDL_FreeSurface(image1);
SDL_FreeSurface(image2);
SDL_FreeSurface(image3);
SDL_DestroyWindow(window);
if(joystick != NULL)
SDL_JoystickClose(joystick);
joystick = nullptr;
image1 = image2 = image3 = windowSurface = nullptr;
window = nullptr;
SDL_Quit();
return 0;
}