Skip to content

Commit 310e3a4

Browse files
committed
mouse click and buttons
1 parent 01c2d06 commit 310e3a4

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

res/img/next_button.png

198 Bytes
Loading

res/img/pause_button.png

169 Bytes
Loading

res/img/play_button.png

190 Bytes
Loading

settings/settings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
{
1010
"fps_counter" : true,
1111
"gui_scale" : 1,
12-
"height" : 500,
13-
"max_framerate" : 120,
12+
"height" : 512,
13+
"max_framerate" : 60,
1414
"renderer_flags" :
1515
{
1616
"gpu_acceleration" : true,
@@ -19,7 +19,7 @@
1919
"vsync" : false
2020
},
2121
"scale" : 1,
22-
"width" : 1000
22+
"width" : 1024
2323
},
2424
"console" :
2525
{

src/main.cpp

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ int main(int argc, char* args[]) {
3131
Uint8 cursorState = 0; //0 -> normal, 1 -> hover, 2 -> normal clicked, 3 -> hover clicked
3232
Cursor cursor = JsonManager::getCursor();
3333
vector<HitBox2d> hitboxes = {HitBox2d(0, 0, 10, 10), HitBox2d(0, 100, 16, 16)}; //Vector of all hitboxes
34+
Uint8 inHitboxes = 0;
35+
bool clicked = false;
3436

3537
//Interpreter
3638
SystemBus SB;
@@ -67,14 +69,15 @@ int main(int argc, char* args[]) {
6769

6870
//Loading the textures
6971
SDL_Texture* cursorTexture = Window.loadTexture("res/img/cursor.png");
70-
SDL_Texture* grassBlockTexture = Window.loadTexture("res/img/grass_block.png");
7172
SDL_Texture* fontTexture = Window.loadTexture("res/img/font.png");
72-
vector<Entity> platforms = {Entity(Vector2f(0, 100), grassBlockTexture)/*,
73-
Entity(Vector2f(16, 100), grassBlockTexture),
74-
Entity(Vector2f(32, 100), grassBlockTexture)*/};
73+
SDL_Texture* playTexture = Window.loadTexture("res/img/play_button.png");
74+
SDL_Texture* nextTexture = Window.loadTexture("res/img/next_button.png");
75+
SDL_Texture* pauseTexture = Window.loadTexture("res/img/pause_button.png");
7576
Entity cursorEntity(Vector2f(0, 0), cursorTexture);
7677
TextEntity fpsCounterEntity(Vector2f(1, 1), fontTexture, &font);
77-
Button button1(Vector2f(100, 100), HitBox2d(100, 100, 8, 8), grassBlockTexture);
78+
Button playButton(Vector2f(229, 1), HitBox2d(229, 1, 7, 7), playTexture);
79+
Button nextButton(Vector2f(238, 1), HitBox2d(238, 1, 7, 7), nextTexture);
80+
Button pauseButton(Vector2f(247, 1), HitBox2d(247, 1, 7, 7), pauseTexture);
7881
fpsCounter = fpsText + fpsString;
7982
fpsCounterEntity = fpsCounter;
8083
//CPU
@@ -129,6 +132,7 @@ int main(int argc, char* args[]) {
129132
}
130133
}
131134
msNext = msNow + msStep;
135+
clicked = false;
132136
//Controls
133137
while(SDL_PollEvent(&event)) {
134138
if(event.type == SDL_QUIT) {
@@ -138,6 +142,7 @@ int main(int argc, char* args[]) {
138142
else if(event.type == SDL_MOUSEBUTTONDOWN) {
139143
if(cursorState == 0) cursorState = 2;
140144
else if(cursorState == 1) cursorState = 3;
145+
clicked = true;
141146
}
142147
else if(event.type == SDL_MOUSEBUTTONUP) {
143148
if(cursorState == 2) cursorState = 0;
@@ -148,17 +153,28 @@ int main(int argc, char* args[]) {
148153
SDL_GetMouseState(&cursorPosition.x, &cursorPosition.y);
149154
guiCursorPosition.x = (cursorPosition.x / settings.win.scale / 4);
150155
guiCursorPosition.y = (cursorPosition.y / settings.win.scale / 4);
151-
if(guiCursorPosition == *button1.getHitBox()) {
152-
cursorState = 1;
153-
button1.action(randFunc);
156+
inHitboxes = 0;
157+
if(guiCursorPosition == *playButton.getHitBox()) {
158+
inHitboxes++;
159+
if(clicked) playButton.action(randFunc);
160+
}
161+
if(guiCursorPosition == *nextButton.getHitBox()) {
162+
inHitboxes++;
163+
}
164+
if(guiCursorPosition == *pauseButton.getHitBox()) {
165+
inHitboxes++;
166+
}
167+
if(inHitboxes > 0) {
168+
if(cursorState == 0) cursorState = 1;
169+
else if(cursorState == 2) cursorState = 3;
170+
}
171+
else {
172+
if(cursorState == 1) cursorState = 0;
173+
else if(cursorState == 3) cursorState = 2;
154174
}
155-
else if(cursorState == 1) cursorState = 0;
156175
cursorEntity.setXY(cursorPosition.x, cursorPosition.y);
157176
cursorEntity.setCurrentFrame(cursor.pointers[cursorState]);
158177
Window.clear();
159-
for(Entity& p : platforms) {
160-
Window.render(p);
161-
}
162178
if(settings.win.fpsCounter) {
163179
Window.renderText(fpsCounterEntity);
164180
}
@@ -184,8 +200,10 @@ int main(int argc, char* args[]) {
184200
Window.renderText(arValue);
185201
Window.renderText(drValue);
186202
Window.renderText(spValue);
187-
188-
Window.renderButton(button1);
203+
//Buttons
204+
Window.renderButton(playButton);
205+
Window.renderButton(nextButton);
206+
Window.renderButton(pauseButton);
189207
Window.renderCursor(cursorEntity);
190208
Window.display();
191209
}

0 commit comments

Comments
 (0)