Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Tool tip display

This thread is locked; no one can reply to it. rss feed Print
Tool tip display
Fire Wolf
Member #7,640
August 2006

At the moment I have this

if(mouse_x > 220 && mouse_x < 370 && mouse_y > 30 && mouse_y < 80)
{
  textprintf_ex(screen, font, mouse_x, mouse_y, makecol(222, 100, 111),
    -1, "This is where you can find a job");
  rest(100);
    CMenu Menu_refresh;
    Menu_refresh.Menu_refresh();
}

Then the screen refreshes so I don't leave a trail, but its all flashy. The rest(100) is there to stop the soaking of CPU... and looks totaly err shit, anyone got a fix or a better way for doing tool tips?

Derezo
Member #1,666
April 2001
avatar

Rest(100) is wayyyy too long.. Try just 1.

[edit] Oh wait, you're also drawing to the screen. Use a buffer (see examples). :)

"He who controls the stuffing controls the Universe"

Fire Wolf
Member #7,640
August 2006

ah buffers, ill try that brb

DanielH
Member #934
January 2001
avatar

If you are going to have more than one tool tip then I suggest having a structure of some sorts that will hold the coordinates to the tips. Just a suggestion. Here is something that I might do.

1typedef struct ToolTip
2{
3 int top;
4 int bottom;
5 int left;
6 int right;
7 char *text;
8} ToolTip;
9 
10ToolTip tips[] =
11{
12 { 30, 80, 220, 370, "This is where you can find a job" },
13 { 100, 200, 100, 200, "This is tooltip #2" },
14 { 0, 0, 0, 0, NULL }
15};
16 
17int tooltip = -1;
18 
19void logic_function()
20{
21 int i = 0;
22 
23 tooltip = -1;
24 
25 for ( i = 0; tips[ i ].text; i++ )
26 {
27 if( mouse_y >= tips[ i ].top &&
28 mouse_y < tips[ i ].bottom &&
29 mouse_x >= tips[ i ].left &&
30 mouse_x < tips[ i ].right )
31 {
32 tooltip = i;
33 }
34 }
35}
36 
37void draw_function()
38{
39 // do other drawing to buffer
40 
41 if ( tooltip >=0 )
42 {
43 textout_ex( buffer,
44 font,
45 tips[ i ].text,
46 tips[ i ].left,
47 tips[ i ].bottom,
48 makecol(222, 100, 111),
49 -1 );
50 // don't know what these are for
51 //rest(100);
52 //CMenu Menu_refresh;
53 //Menu_refresh.Menu_refresh();
54 }
55 
56 // do other drawing to buffer
57 
58 blit( buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H );
59}
60 
61void main_function()
62{
63 while( 1 )
64 {
65 if ( key[ KEY_ESC ] ) break;
66 
67 // I would normally put a timer around the logic_function
68 logic_function();
69 
70 draw_function();
71 rest( 1 );
72 }
73}

Fire Wolf
Member #7,640
August 2006

hmm, the program crashes when I added
draw_sprite( screen, buffer, 0, 0);

1void CMenu::Menu_refresh()
2{
3 clear_keybuf();
4 
5 acquire_screen();
6 //makes everything black
7 clear_to_color( screen, makecol( 0, 0, 0));
8 //Open bitmaps
9 draw_sprite( screen, Background, 0, 0);
10 draw_sprite( screen, FindNewJob, 220, 30);
11 draw_sprite( screen, buffer, 0, 0);
12 //Display Money top left x,y
13 textprintf_ex(screen, font, 10, 10, makecol(222, 100, 111),
14 -1, "Money: %d", money);
15 textprintf_ex(screen, font, 10, 20, makecol(222, 100, 111),
16 -1, "Current Job: %d", current_job);
17
18 release_screen();
19 rest(5);
20
21 CMenu Menu;
22 Menu.Menu();
23}

Any ideas?

DanielH
Member #934
January 2001
avatar

DId you create a buffer?

Also do all drawing to the buffer then draw the buffer to the screen.

1void CMenu::Menu_refresh()
2{
3 clear_keybuf();
4 
5 //makes everything black
6 clear_to_color( buffer, makecol( 0, 0, 0));
7 //Open bitmaps
8 draw_sprite( buffer, Background, 0, 0);
9 draw_sprite( buffer, FindNewJob, 220, 30);
10 //Display Money top left x,y
11 textprintf_ex(buffer, font, 10, 10, makecol(222, 100, 111),
12 -1, "Money: %d", money);
13 textprintf_ex(buffer, font, 10, 20, makecol(222, 100, 111),
14 -1, "Current Job: %d", current_job);
15
16 
17 acquire_screen();
18 draw_sprite( screen, buffer, 0, 0);
19 release_screen();
20 rest(5);
21
22 CMenu Menu;
23 Menu.Menu();
24}

What is

CMenu Menu;
Menu.Menu();

CMenu Menu_refresh;
Menu_refresh.Menu_refresh();

Why not may Menu() a static function?

class CMenu
{
...
...
    void static Menu();
    void static Menu_refresh();
...
...
};

// And call them like this
CMenu::Menu_refresh();
CMenu::Menu();

Fire Wolf
Member #7,640
August 2006

Ooo good one DanielH thats just what I'm looking for! :D

BAF
Member #2,981
December 2002
avatar

You don't need to acquire/release the screen for one go like that, plus you should use blit, not draw_sprite.

Fire Wolf
Member #7,640
August 2006

DanielH said:

DId you create a buffer?

Also do all drawing to the buffer then draw the buffer to the screen.

I did
BITMAP *buffer; //This will be our temporary bitmap for double buffering

at the top, and the program still crashes with your refined code...

DanielH said:

What is

CMenu Menu;
Menu.Menu();

CMenu Menu_refresh;
Menu_refresh.Menu_refresh();

Why not may Menu() a static function?

That calls other functions, but ill try your stuff :)

DanielH
Member #934
January 2001
avatar

What I'm saying is that you create a new variable to call one function when you could just call the function.

// creates a new variable everytime the refresh function is called
CMenu Menu;
Menu.Menu();

// this only calls the function Menu
CMenu::Menu();

About the buffer

//And you said you did this
BITMAP *buffer;

//But did you do this after setting graphics and 
//before calling any drawing functions
buffer = create_bitmap( SCREEN_W, SCREEN_H )


// and don't forget this when the program is finished
destroy_bitmap( buffer );

Fire Wolf
Member #7,640
August 2006

DanielH said:

If you are going to have more than one tool tip then I suggest having a structure of some sorts that will hold the coordinates to the tips. Just a suggestion. Here is something that I might do.

1typedef struct ToolTip
2{
3 int top;
4 int bottom;
5 int left;
6 int right;
7 char *text;
8} ToolTip;
9 
10ToolTip tips[] =
11{
12 { 30, 80, 220, 370, "This is where you can find a job" },
13 { 100, 200, 100, 200, "This is tooltip #2" },
14 { 0, 0, 0, 0, NULL }
15};
16 
17int tooltip = -1;
18 
19void logic_function()
20{
21 int i = 0;
22 
23 tooltip = -1;
24 
25 for ( i = 0; tips[ i ].text; i++ )
26 {
27 if( mouse_y >= tips[ i ].top &&
28 mouse_y < tips[ i ].bottom &&
29 mouse_x >= tips[ i ].left &&
30 mouse_x < tips[ i ].right )
31 {
32 tooltip = i;
33 }
34 }
35}
36 
37void draw_function()
38{
39 // do other drawing to buffer
40 
41 if ( tooltip >=0 )
42 {
43 textout_ex( buffer,
44 font,
45 tips[ i ].text,
46 tips[ i ].left,
47 tips[ i ].bottom,
48 makecol(222, 100, 111),
49 -1 );
50 // don't know what these are for
51 //rest(100);
52 //CMenu Menu_refresh;
53 //Menu_refresh.Menu_refresh();
54 }
55 
56 // do other drawing to buffer
57 
58 blit( buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H );
59}
60 
61void main_function()
62{
63 while( 1 )
64 {
65 if ( key[ KEY_ESC ] ) break;
66 
67 // I would normally put a timer around the logic_function
68 logic_function();
69 
70 draw_function();
71 rest( 1 );
72 }
73}

I tried that, when I hover the mouse over the area for the tool tip the program crashes...
BITMAP *buffer; is at the start

set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
buffer = create_bitmap( SCREEN_W, SCREEN_H );
is at the start of Main()

Any ideas?

Go to: