-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
187 lines (160 loc) · 4.02 KB
/
Main.cpp
File metadata and controls
187 lines (160 loc) · 4.02 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
using namespace std;
class Window
{
public:
Window(string s)
{
SDL_Window *main_window = SDL_CreateWindow("Paper Toss", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 480, 0);
renderer = SDL_CreateRenderer(main_window, -1, 0);
SDL_Texture *texture = IMG_LoadTexture(renderer, s.c_str());
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
//cout<<renderer<<endl;
}
SDL_Renderer *get_renderer()
{
return (renderer);
}
void update(string s)
{
SDL_Texture *texture = IMG_LoadTexture(renderer, s.c_str());
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
private:
SDL_Window *main_window;
SDL_Renderer *renderer;
SDL_Texture *texture;
};
class Mouse
{
public:
int get_mouse_x()
{
SDL_GetMouseState(&x_coordinate, &y_coordinate);
return (x_coordinate);
}
int get_mouse_y()
{
SDL_GetMouseState(&x_coordinate, &y_coordinate);
return (y_coordinate);
}
bool isclicked()
{
Uint32 cursors = SDL_GetMouseState(&x_coordinate, &y_coordinate);
if ((cursors & SDL_BUTTON_LMASK) != 0)
{
return true;
}
return false;
}
private:
int x_coordinate;
int y_coordinate;
};
class Button
{
public:
Button(int x, int y, int h, int w, string s)
{
x_cordinate = x;
y_cordinate = y;
width = w;
height = h;
content = s;
}
void place_button(Window *m)
{
SDL_Texture *t = IMG_LoadTexture(m->get_renderer(), content.c_str());
SDL_Rect *r = new SDL_Rect();
r->x = this->x_cordinate;
r->y = this->y_cordinate;
r->h = this->height;
r->w = this->width;
SDL_RenderCopy(m->get_renderer(), t, NULL, r);
SDL_RenderPresent(m->get_renderer());
}
bool cursor_on_button(Mouse *m)
{
int mouse_x = m->get_mouse_x();
int mouse_y = m->get_mouse_y();
if ((mouse_x > this->x_cordinate) && (mouse_x < this->x_cordinate + this->width))
{
if ((mouse_y > this->y_cordinate) && (mouse_y < this->y_cordinate + this->height))
{
return true;
}
return false;
}
return false;
}
bool isclicked(Mouse *m)
{
if (this->cursor_on_button(m) && m->isclicked())
{
return true;
}
return false;
}
int get_x_coordinate()
{
return (x_cordinate);
}
int get_y_coordinate()
{
return (y_cordinate);
}
private:
int x_cordinate;
int y_cordinate;
int width;
int height;
string content;
};
int main()
{
bool m_screen = true;
bool i_screen = false;
bool g_screen = false;
Window *main_screen = new Window("main.png");
Button *start_button;
start_button = new Button(25, 240, 50, 100, "start.png");
start_button->place_button(main_screen);
Button *exit_button = new Button(720, 5, 60, 60, "exit.png");
exit_button->place_button(main_screen);
Mouse *m = new Mouse();
bool f = false;
while (1)
{
SDL_PumpEvents();
if (exit_button->isclicked(m))
{
break;
}
if (m_screen)
{
if (start_button->isclicked(m))
{
cout << "It is working" << endl;
m_screen = false;
g_screen = true;
}
}
if (g_screen)
{
SDL_RenderClear(main_screen->get_renderer());
if (f == false)
{
Button *dustbin = new Button(600,320,100,150,"dustbin.png");
main_screen->update("room.png");
exit_button->place_button(main_screen);
dustbin->place_button(main_screen);
f = true;
}
}
}
return 0;
}