forked from Ayu-hack/GeeksforGeeks-POTD-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDL2project.cpp
114 lines (103 loc) · 3.83 KB
/
SDL2project.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// # SDL2 Project
// ## Prerequisites
// - Install SDL2 library.
// ### On Windows:
// 1. Download SDL2 from SDL2 Downloads.
// 2. Extract the files and copy the `include` and `lib` folders to your project directory.
// 3. Add the SDL2 `lib` folder to your compiler's library path and the `include` folder to your compiler's include path.
// ### On macOS:
// 1. Install SDL2 using Homebrew:
// ```bash
// brew install sdl2
#define SDL_MAIN_HANDLED
#include <iostream>
#include <SDL.h>
using namespace std;
int main()
{
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Event e;
SDL_Init(SDL_INIT_EVERYTHING);
bool running = true;
SDL_Rect r{50, 50, 250, 250};
SDL_CreateWindowAndRenderer(640, 480, 0, &window, &renderer);
while (running)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
running = false;
else if (e.type == SDL_KEYDOWN)
{
const Uint8 *state = SDL_GetKeyboardState(NULL);
if ((state[SDL_SCANCODE_LCTRL] && state[SDL_SCANCODE_UP]) || (state[SDL_SCANCODE_RCTRL] && state[SDL_SCANCODE_UP]))
{
cout << "CTRL + UP Key is Pressed" << endl;
r.x -= 50;
r.y -= 50;
cout << r.x << '\t' << r.y << endl
<< endl;
}
else if ((state[SDL_SCANCODE_LCTRL] && state[SDL_SCANCODE_DOWN]) || (state[SDL_SCANCODE_RCTRL] && state[SDL_SCANCODE_DOWN]))
{
cout << "CTRL + DOWN Key is Pressed" << endl;
r.x += 50;
r.y += 50;
cout << r.x << '\t' << r.y << endl
<< endl;
}
else
{
switch (e.key.keysym.sym) // accessing only one key at one time
{
case SDLK_RIGHT: // Right key
cout << "Right Key is pressed" << endl;
cout << r.x << '\t' << r.y << endl
<< endl;
r.x += 10; // x direction ke coordinate change ho rahi hh
break;
case SDLK_LEFT:
cout << "Left Key is Pressed" << endl;
cout << r.x << '\t' << r.y << endl
<< endl;
r.x -= 10;
break;
case SDLK_UP:
r.y -= 10;
cout << "UP Key is Pressed" << endl;
cout << r.x << '\t' << r.y << endl
<< endl;
break;
case SDLK_DOWN:
r.y += 10;
cout << "Down Key is Pressed" << endl;
cout << r.x << '\t' << r.y << endl
<< endl;
break;
}
}
}
else if (e.type == SDL_MOUSEMOTION)
{
SDL_GetMouseState(&r.x, &r.y);
}
else if (e.type == SDL_MOUSEBUTTONDOWN)
{
r.h -= 10;
r.w -= 10;
cout << "The mouse key is preesed " << endl
<< "height and width are " << endl
<< r.h << " " << r.w << endl
<< endl;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // White Color ke liye
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Balck Color ke liye
SDL_RenderFillRect(renderer, &r);
SDL_RenderPresent(renderer);
SDL_Delay(10);
}
return 0;
}