/
tut6.cpp
62 lines (51 loc) · 1.41 KB
/
tut6.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
#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);
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_MOUSEMOTION)
{
if(ev.button.x < 200)
currentImage = image1;
else if(ev.button.x < 400)
currentImage = image2;
else
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);
image1 = image2 = image3 = windowSurface = nullptr;
window = nullptr;
SDL_Quit();
return 0;
}