|
Show and Remove Text with the same key |
Lasaqus7
Member #15,950
May 2015
|
I cannot seem to fix it whereby spacebar allows the text to show on screen until spacebar is pressed again to remove it. Currently it shows then gets removed straight away due to SPACE being unpressed. The only way I can seem to do it is by putting another event_queue in the ShowQuest function, but this seems to cause problems as sometimes I need to press spacebar multiple times for it to be removed. I'll post my whole code here for ease, so you can see it all. (Sorry if it's terrible) 1#pragma once
2
3// Search for required *.lib libraries while linking
4#ifdef _DEBUG
5#pragma comment(lib, "allegro-5.0.9-monolith-md-debug.lib")
6#else
7#pragma comment(lib, "allegro-5.0.9-monolith-md.lib")
8#endif // _DEBUG
9
10#include <allegro5\allegro.h>
11#include <allegro5\allegro_native_dialog.h>
12#include <allegro5\allegro_primitives.h>
13#include <allegro5\allegro_font.h>
14#include <allegro5\allegro_ttf.h>
15#include <allegro5\allegro_image.h>
16#include "objects.h"
17#include <iostream>
18
19
20//GLOBALS
21const int WIDTH = 1280;
22const int HEIGHT = 720;
23
24enum STATE{MENU, PLAYING, COMBATSCREEN, COMBAT, GAMEOVER};
25enum KEYS{UP, DOWN, LEFT, RIGHT, ESCAPE, W, A, S, D, SPACE};
26bool keys[] = { false, false, false, false, false, false, false, false, false, false };
27bool dialog_open = false;
28
29Player Human;
30Enemy Orc;
31QuestGiver NPCQuestOne;
32Quests Qs;
33
34//Prototypes
35void ChangeState(int &state, int newState);
36
37void InitHuman(Player &Human, ALLEGRO_BITMAP *image);
38void DrawHuman(Player &Human);
39void MoveHumanUp(Player &Human);
40void MoveHumanDown(Player &Human);
41void MoveHumanLeft(Player &Human);
42void MoveHumanRight(Player &Human);
43void HumanAttack(Player &Human);
44
45void InitNPCQuestOne(QuestGiver &NPCQuestOne, ALLEGRO_BITMAP *image);
46void DrawNPCQuestOne(QuestGiver &NPCQuestOne);
47
48void InitQuests(Quests &Qs, ALLEGRO_BITMAP *image);
49void DrawQuestItems(Quests &Qs);
50void QuestDialog(Quests &Qs);
51void CompleteQuests(Quests &Qs);
52void ShowQuest(Quests &Qs, int quest_num, ALLEGRO_FONT *font18, ALLEGRO_EVENT_QUEUE *event_queue, ALLEGRO_EVENT &ev);
53
54int main(void)
55{
56
57 //VARIABLES
58 bool done = false;
59 bool render = true;
60 const int FPS = 60;
61 int pos_x;
62 int pos_y;
63 int level;
64 int frames = 0;
65 int gameFPS = 0;
66 int quest_num = 1;
67
68 //Menu Options Bounds
69 int Menu_x1 = 550;
70 int Menu_x2 = 735;
71 int NewGame_y1 = 410;
72 int NewGame_y2 = 460;
73 int Exit_y1 = 530;
74 int Exit_y2 = 580;
75
76 //PROJECT VARIABLES
77 int state = -1;
78
79 //ALLEGRO VARIABLES
80 ALLEGRO_DISPLAY *display = NULL;
81 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
82 ALLEGRO_TIMER *timer = NULL;
83 ALLEGRO_FONT *font18 = NULL;
84 ALLEGRO_FONT *font36 = NULL;
85 ALLEGRO_BITMAP *MainMenu;
86 ALLEGRO_BITMAP *Level1;
87 ALLEGRO_BITMAP *HumanImage;
88 ALLEGRO_BITMAP *NPC1;
89 ALLEGRO_BITMAP *Quest1Item;
90
91 //ALLEGRO INIT FUNCTIONS
92 if (!al_init()) //Initialize Allegro
93 return -1;
94
95 display = al_create_display(WIDTH, HEIGHT); //Create display object
96
97 if (!display) //Test display object
98 return -1;
99
100 //ADDON INSTALL
101 al_init_primitives_addon();
102 al_install_keyboard();
103 al_install_mouse();
104 al_init_image_addon();
105 al_init_font_addon();
106 al_init_ttf_addon();
107
108 //PROJECT INIT
109 event_queue = al_create_event_queue();
110 timer = al_create_timer(1.0 / FPS);
111
112 font18 = al_load_font("arial.ttf", 18, 0);
113 font36 = al_load_font("colonna.ttf", 36, 0);
114
115 //LOAD IMAGES AND SPRITESHEETS
116 //Deleted due to post size (Works fine)
117
118 srand(time(NULL));
119 //INIT CHARACTERS
120 ChangeState(state, MENU);
121
122 InitHuman(Human, HumanImage);
123 InitNPCQuestOne(NPCQuestOne, NPC1);
124 InitQuests(Qs, Quest1Item);
125
126 //TIMER INIT AND STARTUP
127
128 al_register_event_source(event_queue, al_get_keyboard_event_source());
129 al_register_event_source(event_queue, al_get_mouse_event_source());
130 al_register_event_source(event_queue, al_get_timer_event_source(timer));
131 al_register_event_source(event_queue, al_get_display_event_source(display));
132
133 al_start_timer(timer);
134 while (!done)
135 {
136 ALLEGRO_EVENT ev;
137 al_wait_for_event(event_queue, &ev);
138
139 if (ev.type == ALLEGRO_EVENT_TIMER)
140 {
141 render = true;
142
143 //UPDATE===========================================
144 if (keys[UP] || keys[W])
145 MoveHumanUp(Human);
146 else if (keys[DOWN] || keys[S])
147 MoveHumanDown(Human);
148 else if (keys[LEFT] || keys[A])
149 MoveHumanLeft(Human);
150 else if (keys[RIGHT] || keys[D])
151 MoveHumanRight(Human);
152 else if (keys[SPACE])
153 //=====================================================
154 if (state == MENU)
155 {
156
157 }
158 else if (state == PLAYING)
159 {
160 //CompleteQuests(Qs);
161 }
162 else if (state == GAMEOVER)
163 {
164
165 }
166 }
167 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
168 {
169 done = true;
170 }
171
172 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
173 {
174 switch (ev.keyboard.keycode)
175 {
176 case ALLEGRO_KEY_ESCAPE:
177 keys[ESCAPE] = true;
178 break;
179 case ALLEGRO_KEY_SPACE:
180 keys[SPACE] = true;
181 break;
182 case ALLEGRO_KEY_UP:
183 keys[UP] = true;
184 break;
185 case ALLEGRO_KEY_DOWN:
186 keys[DOWN] = true;
187 break;
188 case ALLEGRO_KEY_LEFT:
189 keys[LEFT] = true;
190 break;
191 case ALLEGRO_KEY_RIGHT:
192 keys[RIGHT] = true;
193 break;
194 case ALLEGRO_KEY_W:
195 keys[W] = true;
196 break;
197 case ALLEGRO_KEY_S:
198 keys[S] = true;
199 break;
200 case ALLEGRO_KEY_A:
201 keys[A] = true;
202 break;
203 case ALLEGRO_KEY_D:
204 keys[D] = true;
205 break;
206 }
207 }
208 else if (ev.type == ALLEGRO_EVENT_KEY_UP)
209 {
210 switch (ev.keyboard.keycode)
211 {
212 case ALLEGRO_KEY_ESCAPE:
213 done = true;
214 break;
215 case ALLEGRO_KEY_SPACE:
216 keys[SPACE] = false;
217 break;
218 case ALLEGRO_KEY_UP:
219 keys[UP] = false;
220 break;
221 case ALLEGRO_KEY_DOWN:
222 keys[DOWN] = false;
223 break;
224 case ALLEGRO_KEY_LEFT:
225 keys[LEFT] = false;
226 break;
227 case ALLEGRO_KEY_RIGHT:
228 keys[RIGHT] = false;
229 break;
230 case ALLEGRO_KEY_W:
231 keys[W] = false;
232 break;
233 case ALLEGRO_KEY_S:
234 keys[S] = false;
235 break;
236 case ALLEGRO_KEY_A:
237 keys[A] = false;
238 break;
239 case ALLEGRO_KEY_D:
240 keys[D] = false;
241 break;
242 }
243 }
244 else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)
245 {
246 pos_x = ev.mouse.x;
247 pos_y = ev.mouse.y;
248 }
249 else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
250 {
251 if (state == MENU)
252 {
253 //Check for mouse location and pressed button to start game.
254 if (ev.mouse.button & 1 && ev.mouse.x >= Menu_x1 && ev.mouse.x <= Menu_x2 && ev.mouse.y >= NewGame_y1 && ev.mouse.y <= NewGame_y2)
255 {
256 level = 1;
257 state = PLAYING;
258 }
259 //Check for mouse location and pressed button to exit.
260 else if (ev.mouse.button & 1 && ev.mouse.x >= Menu_x1 && ev.mouse.x <= Menu_x2 && ev.mouse.y >= Exit_y1 && ev.mouse.y <= Exit_y2)
261 {
262 done = true;
263 }
264 }
265 else if (state == PLAYING)
266 {
267 }
268 else if (state == GAMEOVER)
269 {
270
271 }
272
273 else if (ev.mouse.button & 2)
274 {
275
276 }
277 }
278
279 //==========================================================
280 //RENDER
281 //==========================================================
282 if (render && al_is_event_queue_empty(event_queue))
283 {
284 render = false;
285
286 if (state == MENU)
287 {
288 //Draw Main Menu
289 al_draw_bitmap(MainMenu, 0, 0, 0);
290 //Deleted due to post size (Works fine)
291
292 }
293 else if (state == PLAYING)
294 {
295 al_draw_bitmap(Level1, 0, 0, 0);
296 DrawNPCQuestOne(NPCQuestOne);
297 DrawQuestItems(Qs);
298 DrawHuman(Human);
299 //Check to see if player is at the NPC location and if SPACE is pressed show quest
300 if (Human.x >= NPCQuestOne.x - 40 && Human.x <= NPCQuestOne.x + 40 && Human.y >= NPCQuestOne.y - 40 && Human.y <= NPCQuestOne.y + 40 && keys[SPACE])
301 {
302 dialog_open = true;
303 QuestDialog(Qs);
304 ShowQuest(Qs, quest_num, font18, event_queue, ev);
305 }
306 }
307 else if (state == GAMEOVER)
308 {
309 //Not needed yet
310 al_draw_text(font18, al_map_rgb(255, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Press Space to Exit");
311 }
312
313 //FLIP BUFFERS =========================================
314 al_flip_display();
315 al_clear_to_color(al_map_rgb(0, 0, 0));
316 }
317 }
318
319 //==========================================================
320 //DESTROY PROJECT OBJECTS
321 //==========================================================
322 al_destroy_bitmap(MainMenu);
323 al_destroy_bitmap(Level1);
324 al_destroy_bitmap(HumanImage);
325 al_destroy_bitmap(NPC1);
326 al_destroy_bitmap(Quest1Item);
327 al_destroy_event_queue(event_queue);
328 al_destroy_timer(timer);
329 al_destroy_font(font18);
330 al_destroy_display(display);
331
332 return 0;
333}
334
335void InitNPCQuestOne(QuestGiver &NPCQuestOne, ALLEGRO_BITMAP *image = NULL)
336{
337 //Deleted due to post size (Works fine)
338}
339void DrawNPCQuestOne(QuestGiver &NPCQuestOne)
340{
341 //Deleted due to post size (Works fine)
342}
343
344void InitHuman(Player &Human, ALLEGRO_BITMAP *image = NULL)
345{
346 //Deleted due to post size (Works fine)
347}
348void DrawHuman(Player &Human)
349{
350 //Deleted due to post size (Works fine)
351}
352void MoveHumanUp(Player &Human)
353{
354 //Deleted due to post size (Works fine)
355}
356void MoveHumanDown(Player &Human)
357{
358 //Deleted due to post size (Works fine)
359}
360void MoveHumanLeft(Player &Human)
361{
362 //Deleted due to post size (Works fine)
363}
364void MoveHumanRight(Player &Human)
365{
366 //Deleted due to post size (Works fine)
367}
368
369void ChangeState(int &state, int newState)
370 //ALLEGRO_EVENT_QUEUE *event_queue, ALLEGRO_EVENT &ev
371{
372 if (state == MENU)
373 {
374 }
375 else if (state == PLAYING)
376 {
377 }
378 else if (state == GAMEOVER)
379 {
380 }
381
382 state = newState;
383
384 if (state == MENU)
385 {
386 }
387 else if (state == PLAYING)
388 {
389 InitHuman(Human);
390 }
391 else if (state == GAMEOVER)
392 {
393 }
394}
395
396void InitQuests(Quests &Qs, ALLEGRO_BITMAP *image = NULL)
397{
398 Qs.IsOnQuest = false;
399 Qs.QuestCompleted = false;
400 Qs.quest_num = 1;
401
402
403 if (image != NULL)
404 Qs.image = image;
405}
406void DrawQuestItems(Quests &Qs)
407{
408 Qs.Q1x = 520;
409 Qs.Q1y = 210;
410 int Q1frameWidth = 32;
411 int Q1frameHeight = 32;
412
413 //If player is on quest but has not collected the quest item
414 if (Qs.HasQuestItem == false)
415 {
416 al_draw_bitmap(Qs.image, Qs.Q1x - Q1frameWidth / 2, Qs.Q1y - Q1frameHeight / 2, 0);
417 //if player has moved to the quest item, pick it up
418 if (Human.x > Qs.Q1x - 40 && Human.x < Qs.Q1x + 40 && Human.y > Qs.Q1y - 40 && Human.y < Qs.Q1y + 40)
419 {
420 Qs.HasQuestItem = true;
421 }
422 }
423}
424
425void QuestDialog(Quests &Qs)
426{
427 //Draw box for the quest text to go in
428 al_draw_rounded_rectangle(WIDTH / 2 - 220, 550, WIDTH / 2 + 220, 660, 2, 2, al_map_rgb(0, 0, 0), 6);
429 al_draw_filled_rectangle(WIDTH / 2 - 220, 550, WIDTH / 2 + 220, 660, al_map_rgb(0, 255, 255));
430
431}
432void ShowQuest(Quests &Qs, int quest_num, ALLEGRO_FONT *font18, ALLEGRO_EVENT_QUEUE *event_queue, ALLEGRO_EVENT &ev)
433{
434 //THIS WORKS
435
436 //If player is not on a quest and the quest is number 1 then display quest text.
437 if (quest_num == 1 && Qs.IsOnQuest == false)
438 {
439 al_draw_text(font18, al_map_rgb(0, 0, 0), WIDTH / 2, 560, ALLEGRO_ALIGN_CENTRE, "TEXT HERE.");
440
441 al_wait_for_event(event_queue, &ev);
442 {
443 //***********************************************************
444 //THIS IS WHERE I AM NOW HAVING PROBLEMS
445 //
446
447 //When player presses space close quest text
448 if (keys[SPACE])
449 dialog_open = false;
450 }
451
452 }
453 if (quest_num == 1 && Qs.HasQuestItem == true)
454 {
455 //THIS WORKS
456
457 //If Player is on quest 1 and collected the quest item show new text
458 al_draw_text(font18, al_map_rgb(0, 0, 0), WIDTH / 2, 560, ALLEGRO_ALIGN_CENTRE, "TEXT HERE.");
459 quest_num++;
460 }
461}
|
Dizzy Egg
Member #10,824
March 2009
|
Instead of using the spacebar 'directly', setup a bool called "TextActive". Then do something like: If press spacebar, and TextActive = true, make TextActive = false, Then only draw text when 'TextActive' = true!
---------------------------------------------------- |
Lasaqus7
Member #15,950
May 2015
|
Thanks for the reply Dizzy I thought's that's what I had done with the dialog_open part within the code If the player is at the npc and space is pressed then dialog_open = true and then in the ShowQuest() i have if space pressed again then dialog_open = false or am I doing it the wrong way. I tried a few ways with this and edited the following section of code: 1if (Human.x >= NPCQuestOne.x - 40 && Human.x <= NPCQuestOne.x + 40 && Human.y >= NPCQuestOne.y - 40 && Human.y <= NPCQuestOne.y + 40)
2 {
3 if (keys[SPACE])
4 {
5 dialog_open = true;
6 if (dialog_open == true)
7 {
8 QuestDialog(Qs);
9 //ShowQuest(Qs, quest_num, font18, event_queue, ev);
10 }
11 }
12 }
This way performs the same way, text show then removed instantly, if I hold space then it stays on screen. And this way 1if (Human.x >= NPCQuestOne.x - 40 && Human.x <= NPCQuestOne.x + 40 && Human.y >= NPCQuestOne.y - 40 && Human.y <= NPCQuestOne.y + 40)
2 {
3 if (keys[SPACE] && dialog_open == false)
4 {
5 QuestDialog(Qs);
6 //ShowQuest(Qs, quest_num, font18, event_queue, ev);
7 dialog_open = true;
8 }
9 }
This performs the same however it will not allow me to press space again to show anything (which in my newbie eyes kinda shows the dialog_open is working, since dialog_open is now true and hence should not display anything). Edit = I did comment out the ShowQuest() as QuestDialog() is only shown at the same time and is less to edit. (thats only drawing a rectangle on screen). |
Chris Katko
Member #1,881
January 2002
|
Try: 1bool space_is_pressed = false; //initial value outside of loop
2
3while(looping)
4{
5if (keys[SPACE] && space_is_pressed == false)
6 {
7 QuestDialog(Qs);
8 space_is_pressed = true;
9 } //can only fire once until the second condition below (releasing space) is satisfied.
10
11if(keys[SPACE] == false)
12 {
13 space_is_pressed = false; //release
14 }
15}
-----sig: |
Lasaqus7
Member #15,950
May 2015
|
Thanks for the reply Chris. I did try following your code however, I only have 1 while loop which is the while(!done) game loop. I have tried to add the code Under my DrawHuman(In the Render section and state playing section of my code) the text appears and disappears instantly, if I put a while loop here the game freezes when I get to the NPC (The while loop was the Human.x >= NPCQuestOne.x - 40) 1//I need the if statement to make sure the player is at the right location
2if (Human.x >= NPCQuestOne.x - 40 && Human.x <= NPCQuestOne.x + 40 && Human.y >= NPCQuestOne.y - 40 && Human.y <= NPCQuestOne.y + 40) //Copy of your code follows
3 {
4 if (keys[SPACE] && dialog_open == false)
5 {
6 QuestDialog(Qs);
7 dialog_open = true;
8 }
9 if (keys[SPACE] == false)
10 {
11 dialog_open = false;
12 }
13 }
My dialog_open (your's is space_is_pressed) is a global variable so that is outside the loop. Is this caused by my ALLEGRO_EVENT_KEY_DOWN and _UP's for SPACE as I'm sure I need them? Am I just placing the code in the wrong section ?. The worst part is I understand the code you put and what is doing I just can't seem to implement it correctly and get it working :-( |
Chris Katko
Member #1,881
January 2002
|
Someone else chime in regarding using Allegro events and whatnot, but this is straight from a working piece of A5 game code for a Joust game I'm toying with: al_get_keyboard_state(&kbd_state); //... if(al_key_down(&kbd_state, ALLEGRO_KEY_UP)) { if(KEY_UP_PRESSED == false) { temp_man->move_vel(0, -FLAP_VELOCITY); } KEY_UP_PRESSED = true; }else{ KEY_UP_PRESSED = false; } I think I had a problem very similar to yours before with Allegro 5, where the logic looked fine, but testing for the keys being false didn't work. -----sig: |
Lasaqus7
Member #15,950
May 2015
|
Thanks again Chris. After looking into the events you posted (I haven't used them before or seem them in the tutorials) I think al_get_keyboard_state(&kbd_state); Goes into my while(!done) game loop. al_key_down requires a bool which I set as global:- I added ALLEGRO_KEYBOARD_STATE kstate; to my list of allegro variables (As seen in first post code) And then I added this code to my (state = PLAYING) 1if (al_key_down(&kstate, ALLEGRO_KEY_SPACE))
2 {
3 //if (Human.x >= NPCQuestOne.x - 40 && Human.x <= NPCQuestOne.x + 40 && Human.y >= NPCQuestOne.y - 40 && Human.y <= NPCQuestOne.y + 40)
4 //{
5 if (KEY_SPACE_PRESSED == false)
6 {
7 QuestDialog(Qs);
8 }
9 KEY_SPACE_PRESSED = true;
10 }
11 else
12 {
13 KEY_SPACE_PRESSED = false;
14 }
If this is done correct from what I read about al_get_keyboard_state and al_key_down then the same thing is happening. I press space dialog appears and instantly goes off screen :-( I have now managed to take working code, implement it and break it. EDIT - I think this may have something to do with the way I coded my keys. |
Chris Katko
Member #1,881
January 2002
|
Oh, there's your problem. When you load a dialog, you've got two options. {"name":"465137-diablo-macintosh-screenshot-inventory-holding-carryings.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/b\/7b1b34c338a910dc0bdd16b60a194dbd.png","w":640,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/b\/7b1b34c338a910dc0bdd16b60a194dbd"} 1) A dialog box where the game keeps playing: Set a flag by pressing the "dialog button" for example, and whenever that flag is set, draw the dialog box. {"name":"Train-Conductor-2-Pause-menu.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/3\/032b6359deebccca2118de8a7c74caf6.png","w":800,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/3\/032b6359deebccca2118de8a7c74caf6"} 2) A dialog box where the game is interrupted until the dialog exits. (Like pressing escape in most games.) In that case, the easiest way is to have a second game loop in the dialog itself. That is, a call to say main_menu_dialog() goes to that function, and stays there until that dialog box's exit condition is satisfied, which calls return, and sends execution back to the main loop. In case #1: You have to be careful to pass input to the dialog box while it's open. In case #2: You have to make sure you events all get handled. You don't want to go to a new function only checking keyboard events, and then end up having all of the joystick/mouse/whatever events fill up like crazy until you return. -----sig: |
Lasaqus7
Member #15,950
May 2015
|
Cheers again Chris It's 5am here but I'm still trying to get it right. Although I kinda guess I made some progress now knowing it doesn't seem to be the code you gave me, but now rather than my actually coding. I'm going to bed now but i'll be back at it when I wake up and hopefully make some progress |
Dizzy Egg
Member #10,824
March 2009
|
Did you manage to figure this out??
---------------------------------------------------- |
Lasaqus7
Member #15,950
May 2015
|
I'll looking at it now Dizzy, make some changes and I'll see where I'm at from them. EDIT:- Still no further forward. I tried to add in the while loop Chris posted although my game froze up (if i debug at the loop it looks like it works, although I can't press the required key to cancel the loop, hence the freeze) So back to editing/testing/reading etc. Something somewhere is gonna slap me in the face and it will work, just need to figure it out. EDIT:- What I did want to ask is this taking my code(but cut right down) 1 while (!done)
2 {
3 ALLEGRO_EVENT ev;
4 al_wait_for_event(event_queue, &ev);
5
6 if (ev.type == ALLEGRO_EVENT_TIMER)
7 {
8 render = true;
9 }
10
11 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
12 {
13 switch (ev.keyboard.keycode)
14 {
15 case ALLEGRO_KEY_F:
16 keys[F] = true;
17 break;
18 }
19 }
20 else if (ev.type == ALLEGRO_EVENT_KEY_UP)
21 {
22 switch (ev.keyboard.keycode)
23 {
24 case ALLEGRO_KEY_F:
25 keys[F] = false;
26 break;
27 }
28 if (render && al_is_event_queue_empty(event_queue))
29 {
30 render = false;
31 if (state == PLAYING)
32 {
33 //Check to see if player is at the NPC location and if F is pressed show quest
34 if (Human.x >= NPCQuestOne.x - 40 && Human.x <= NPCQuestOne.x + 40 && Human.y >= NPCQuestOne.y - 40 && Human.y <= NPCQuestOne.y + 40)
35 {
36 if (keys[F]) //This should go true if F is pressed
37 {
38 KEY_F_PRESSED = true; //This is true as F was pressed
39 if (KEY_F_PRESSED)
40 {
41 QuestDialog(Qs); //This shows as F was pressed
42 }
43 }
44 }
45 }
46 //FLIP BUFFERS =========================================
47 al_flip_display();
48 al_clear_to_color(al_map_rgb(0, 0, 0));
49 }
50 }
51
52void QuestDialog(Quests &Qs)
53{
54 //Draw box for the quest text to go in
55
56 al_draw_rounded_rectangle(WIDTH / 2 - 220, 550, WIDTH / 2 + 220, 660, 2, 2, al_map_rgb(0, 0, 0), 6);
57 al_draw_filled_rectangle(WIDTH / 2 - 220, 550, WIDTH / 2 + 220, 660, al_map_rgb(0, 255, 255));
58 if (keys[SPACE])
59 {
60 KEY_F_PRESSED = false;
61 }
62}
On the 2nd loop, is it correct to say that the al_wait_for_event(event_queue, &ev); has now registered the F was released and now F = false, so when the code gets back to the state == playing, that since F is now false it will not go onto CheckDialog(Qs) and as such no longer display the rectangle ? If so then this is what I cannot work out and this could be why even changing the key to a different key still doesn't work |
Dizzy Egg
Member #10,824
March 2009
|
Well...yes, I think so... Using your code above, the dialog will ONLY be displayed if you are standing in the right place AND holding down the F key!? Is that what you want?
---------------------------------------------------- |
Lasaqus7
Member #15,950
May 2015
|
No, I would prefer it if he stands in front of the NPC, presses a key to show Dialog and then presses a key (preferably the same key, as it would help to know how this works) for the dialog to be removed . I'm glad I actually understand what the code is doing, that's always a start :-) |
Dizzy Egg
Member #10,824
March 2009
|
Something like this might help: 1 bool canPressKey = true;
2 bool keyPressed = false;
3
4 while (!done)
5 {
6 ALLEGRO_EVENT ev;
7 al_wait_for_event(event_queue, &ev);
8
9 if (ev.type == ALLEGRO_EVENT_TIMER)
10 {
11 render = true;
12 }
13
14 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
15 {
16 switch (ev.keyboard.keycode)
17 {
18 case ALLEGRO_KEY_F:
19 if(canPressKey == true){
20 keys[F] = true;
21 canPressKey = false;
22
23 keyPressed = !keyPressed;
24 }
25 break;
26 }
27 }
28 else if (ev.type == ALLEGRO_EVENT_KEY_UP)
29 {
30 switch (ev.keyboard.keycode)
31 {
32 case ALLEGRO_KEY_F:
33 canPressKey = true;
34 keys[F] = false;
35 break;
36 }
37 if (render && al_is_event_queue_empty(event_queue))
38 {
39 render = false;
40 if (state == PLAYING)
41 {
42 //Check to see if player is at the NPC location and if F is pressed show quest
43 if (Human.x >= NPCQuestOne.x - 40 && Human.x <= NPCQuestOne.x + 40 && Human.y >= NPCQuestOne.y - 40 && Human.y <= NPCQuestOne.y + 40)
44 {
45 if (keyPressed)
46 {
47 QuestDialog(Qs); //This shows as F was pressed
48 }
49 }
50 }
51 //FLIP BUFFERS =========================================
52 al_flip_display();
53 al_clear_to_color(al_map_rgb(0, 0, 0));
54 }
55 }
56
57void QuestDialog(Quests &Qs)
58{
59 //Draw box for the quest text to go in
60
61 al_draw_rounded_rectangle(WIDTH / 2 - 220, 550, WIDTH / 2 + 220, 660, 2, 2, al_map_rgb(0, 0, 0), 6);
62 al_draw_filled_rectangle(WIDTH / 2 - 220, 550, WIDTH / 2 + 220, 660, al_map_rgb(0, 255, 255));
63 if (keys[SPACE])
64 {
65 KEY_F_PRESSED = false;
66 }
67}
---------------------------------------------------- |
Lasaqus7
Member #15,950
May 2015
|
Well done, I was looking at adding code to my event_key_down and up but it wouldn't have looked like that. Thank you so much for that Dizzy it works. Well for now it works, I just hope it stays that way. Thanks so much for that and thank you too Chris for helping out |
Dizzy Egg
Member #10,824
March 2009
|
Cool! Hope it all goes well!
---------------------------------------------------- |
Lasaqus7
Member #15,950
May 2015
|
Well knowing how that works, can help in my other additions I want to try and do :-) I cannot thank you enough for that :-) |
beoran
Member #12,636
March 2011
|
if(canPressKey = true){ I think you mean if(canPressKey){
|
Dizzy Egg
Member #10,824
March 2009
|
Or even, canPressKey == true
---------------------------------------------------- |
beoran
Member #12,636
March 2011
|
Exactly because of the risk of the error above, in C, I prefer to use booleans and pointers as such without the unneeded comparison to true, false or NULL. But there's no point in arguing about programming style, so let's not. |
Lasaqus7
Member #15,950
May 2015
|
I'm just happy is working :-) Whichever way it was decided to be done is all good |
Chris Katko
Member #1,881
January 2002
|
Are these always identical? int x = 1501; if(x){} if(x == true){} That is to say, are there any cases where explicitly testing to true is not the same as implicit true? And likewise: if(!x){} //Maybe? if(x == false){} That logical negation is the one I'm really wondering about. -----sig: |
Edgar Reynaldo
Major Reynaldo
May 2007
|
The proper way to prevent accidental assignment instead of equality comparison is to write the constant on the left hand side of the expression. if (3 == sscanf(input.c_str() , "COORDS %lf %lf %lf" , &x , &y , &z)) { // success } else { // error, didn't read all three arguments } That way, if you accidentally write if (3 = sscanf(/*...*/)) { // compile error, you can't assign 3 a value } It will error out on you. In this case you could have written sscanf on the left hand side too, and if you tried to assign to it it would error. With gcc it says "lvalue required as left operand of assignment". As far as checking non-bool types against true or false, don't do it. true and false are usually defined as 1 and 0. However, implicit bool casts are okay :
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
beoran
Member #12,636
March 2011
|
It also depends on whether it's C89 or C99 or C++. Here's an article on the subject: http://www.jacquesf.com/2011/04/in-defense-of-the-c99-boolean-type/ |
|