Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Small problem.

This thread is locked; no one can reply to it. rss feed Print
Small problem.
julian_boolean
Member #8,201
January 2007

I'm using the code for the LS button (http://www.loomsoft.net/util_lsbutton.shtml) and was just trying to get rid of one of the functions that was there originally. So far it seems to work fine except for the fact that it doesn't play the sound effects.

1int button::is_clicked(void)
2{
3 if(is_mouse_over())
4 {
5 if(!(mouse_b & 1) && mouse_status == NOT_ON_FOCUS)
6 {
7 mouse_status = MOUSE_STATUS_UP;
8 draw_frame = DRAW_FRAME_ON;
9 sound_status = PLAY_SOUND_ON;
10 }
11 
12 else if(mouse_b & 1 && mouse_status != NOT_ON_FOCUS)
13 {
14 mouse_status = MOUSE_STATUS_DOWN;
15 draw_frame = DRAW_FRAME_DOWN;
16 sound_status = PLAY_SOUND_DOWN;
17 }
18 
19 else if(!(mouse_b & 1) && mouse_status == MOUSE_STATUS_DOWN)
20 {
21 mouse_status = MOUSE_STATUS_UP;
22 draw_frame = DRAW_FRAME_ON;
23 sound_status = PLAY_SOUND_ON;
24 
25 return TRUE;
26 }
27 }
28 
29 else if(!(mouse_b & 1))
30 {
31 mouse_status = NOT_ON_FOCUS;
32 }
33 
34 else
35 }
36 draw_frame = DRAW_FRAME_OFF
37 }
38 
39 return FALSE
40}
41 
42void button::draw(BITMAP *bitmap_buffer)
43{
44 if(draw_frame == DRAW_FRAME_DOWN)
45 {
46 if(down_bitmap != NULL)
47 {
48 acquire_screen();
49 draw_sprite(bitmap_buffer, down_bitmap, x_position, y_position);
50 release_screen();
51 }
52 }
53 
54 if(draw_frame == DRAW_FRAME_ON)
55 {
56 if(on_bitmap != NULL)
57 {
58 acquire_screen();
59 draw_sprite(bitmap_buffer, on_bitmap, x_position, y_position);
60 release_screen();
61 }
62 }
63 
64 if(draw_frame == DRAW_FRAME_OFF)
65 {
66 if(off_bitmap != NULL)
67 {
68 acquire_screen();
69 draw_sprite(bitmap_buffer, off_bitmap, x_position, y_position);
70 release_screen();
71 }
72 }
73}
74 
75void button::sounds(void)
76{
77 if(sound_status == PLAY_SOUND_DOWN)
78 {
79 if(down_sound != NULL)
80 {
81 if(off_sound != NULL)
82 stop_sample(off_sound);
83 if(on_sound != NULL)
84 stop_sample(on_sound);
85 if(down_sound != NULL)
86 play_sample(down_sound, 255, 127, 1000, FALSE);
87 }
88 }
89 
90 if(sound_status == PLAY_SOUND_ON)
91 {
92 if(down_on != NULL)
93 {
94 if(off_sound != NULL)
95 stop_sample(off_sound);
96 if(on_sound != NULL)
97 play_sample(on_sound, 255, 127, 1000, FALSE);
98 if(down_sound != NULL)
99 stop_sample(down_sound);
100 }
101 }
102 
103 if(sound_status == PLAY_SOUND_OFF)
104 {
105 if(off_sound != NULL)
106 {
107 if(off_sound != NULL)
108 play_sample(off_sound, 255, 127, 1000, FALSE);
109 if(on_sound != NULL)
110 stop_sample(on_sound);
111 if(down_sound != NULL)
112 stop_sample(down_sound);
113 }
114 }
115}

CGamesPlay
Member #2,559
July 2002
avatar

Shouldn't you call the sounds function somewhere?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Taiko Keiji
Member #8,307
February 2007
avatar

The problem I noticed is that when you send a value to sound_status it's either PLAY_SOUND_ON || PLAY_SOUND_DOWN

when you check the sound_status you should be checking for these instead of
SOUND_STATUS_ON || SOUND_STATUS_DOWN

int button::is_clicked(void)
{
if(is_mouse_over())
{
if(!(mouse_b & 1) && mouse_status == NOT_ON_FOCUS)
{
mouse_status = MOUSE_STATUS_UP;
draw_frame = DRAW_FRAME_ON;
sound_status = PLAY_SOUND_OFF;/*I'm assuming that this is what was supposed to be played here/

}

else if(mouse_b & 1 && mouse_status != NOT_ON_FOCUS)
{
mouse_status = MOUSE_STATUS_DOWN;
draw_frame = DRAW_FRAME_DOWN;
sound_status = PLAY_SOUND_DOWN;
}

else if(!(mouse_b & 1) && mouse_status == MOUSE_STATUS_DOWN)
{
mouse_status = MOUSE_STATUS_UP;
draw_frame = DRAW_FRAME_ON;
sound_status = PLAY_SOUND_ON;

return TRUE;
}
}

else if(!(mouse_b & 1))
{
mouse_status = NOT_ON_FOCUS;
}

else
}
draw_frame = DRAW_FRAME_OFF
}

return FALSE
}

void button::draw(BITMAP *bitmap_buffer)
{
if(draw_frame == DRAW_FRAME_DOWN)
{
if(down_bitmap != NULL)
{
acquire_screen();
draw_sprite(bitmap_buffer, down_bitmap, x_position, y_position);
release_screen();
}
}

if(draw_frame == DRAW_FRAME_ON)
{
if(on_bitmap != NULL)
{
acquire_screen();
draw_sprite(bitmap_buffer, on_bitmap, x_position, y_position);
release_screen();
}
}

if(draw_frame == DRAW_FRAME_OFF)
{
if(off_bitmap != NULL)
{
acquire_screen();
draw_sprite(bitmap_buffer, off_bitmap, x_position, y_position);
release_screen();
}
}
}

void button::sounds(void)
{
if(sound_status == PLAY_SOUND_DOWN)
{
if(down_sound != NULL)
{
if(off_sound != NULL)
stop_sample(off_sound);
if(on_sound != NULL)
stop_sample(on_sound);
if(down_sound != NULL)
play_sample(down_sound, 255, 127, 1000, FALSE);
}
}

if(sound_status == PLAY_SOUND_ON)
{
if(down_on != NULL)
{
if(off_sound != NULL)
stop_sample(off_sound);
if(on_sound != NULL)
play_sample(on_sound, 255, 127, 1000, FALSE);
if(down_sound != NULL)
stop_sample(down_sound);
}
}

if(sound_status == PLAY_SOUND_OFF)
{
if(off_sound != NULL)
{
if(off_sound != NULL)
play_sample(off_sound, 255, 127, 1000, FALSE);
if(on_sound != NULL)
stop_sample(on_sound);
if(down_sound != NULL)
stop_sample(down_sound);
}
}
}

Hope this helps, If i'm not even close i'm sorry.

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

CGamesPlay
Member #2,559
July 2002
avatar

All you did was change this line, correct?
sound_status = PLAY_SOUND_OFF;/*I'm assuming that this is *what was supposed to be played here*/

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Taiko Keiji
Member #8,307
February 2007
avatar

which line?

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

julian_boolean
Member #8,201
January 2007

Nada :/

And I am calling the function in the game state I'm using it in, it doesnt really make a difference and I figure I shouldn't have to anyway.

Taiko Keiji
Member #8,307
February 2007
avatar

Sorry, I tried.

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

julian_boolean
Member #8,201
January 2007

Thanks though ;) I can't really understand why the sound won't work while the drawing will, despite the fact that both functions are made the exact same way.

Edit:

It used to say:

    else if(!(mouse_b & 1))
    {
       mouse_status = NOT_ON_FOCUS;
       draw_frame = DRAW_FRAME_OFF;
       sound_status = PLAY_SOUND_OFF;
    }

Dunno if it made a difference though.

Edit again!

Oops typo, it's suppose to say PLAY_SOUND not SOUND_STATUS (still not working though.)

CGamesPlay
Member #2,559
July 2002
avatar

The sounds function needs to be called whenever the value of sound_status changes. That means that when you set sound_status = PLAY_SOUND_OFF, you need to call sounds(); right afterwards.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

Why is it any different though. Then logically I should have to call the draw function right after the value of draw_frame changes, but I don't..

Edit:

That doesn't seem to work either. :(

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Then logically I should have to call the draw function right after the value of draw_frame changes, but I don't..

If you look at the sounds function, you will see that it starts and stops sounds, whereas the draw function actually redraws frames. You don't need to "redraw" sounds. Clearly, the sounds function is meant to be called when the sound needs to change.

If it doesn't work, are you sure that the sounds are loaded?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

Hmm.. Well the sounds are being loaded just fine. Before I made this change it worked perfectly.. I tried taking out the stop_sample in each of the if statements and it worked, sorta.

CGamesPlay
Member #2,559
July 2002
avatar

SO, problem solved?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

Yup, except for the fact that it crashes when I press the button and just before I do it sounds as if it plays the sound file about 100 times. Don't I need to use stop_sample or else when I go to play a different sound (when the status changes) it'll play the previous one as well?

Steve Terry
Member #1,989
March 2002
avatar

You need something like this:

if(play_this_sound)
{
   play_sample(this_sound);
   play_this_sound = false;
}

That way it only calls play_sample once

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Go to: