When I move the mouse to the area for tool tip 1 the program crashes. Anyone know how to fix it?
1 | typedef struct ToolTip |
2 | { |
3 | int top; |
4 | int bottom; |
5 | int left; |
6 | int right; |
7 | char *text; |
8 | } ToolTip; |
9 | |
10 | ToolTip tips[] = |
11 | { |
12 | { 100, 150, 100, 250, "Start new game" },//tool tip 1 |
13 | { 0, 0, 0, 0, NULL } |
14 | }; |
15 | |
16 | int tooltip = -1; |
17 | int i = 0; |
18 | |
19 | void CMenu::logic_function() |
20 | { |
21 | |
22 | tooltip = -1; |
23 | |
24 | for ( i = 0; tips[ i ].text; i++ ) |
25 | { |
26 | if( mouse_y >= tips[ i ].top && |
27 | mouse_y < tips[ i ].bottom && |
28 | mouse_x >= tips[ i ].left && |
29 | mouse_x < tips[ i ].right ) |
30 | { |
31 | tooltip = i; |
32 | } |
33 | } |
34 | } |
35 | |
36 | void CMenu::draw_function() |
37 | { |
38 | |
39 | if ( tooltip >=0 ) |
40 | { |
41 | textout_ex( buffer, |
42 | font, |
43 | tips[ i ].text, |
44 | tips[ i ].left, |
45 | tips[ i ].bottom, |
46 | makecol(222, 100, 111), |
47 | -1 ); |
48 | |
49 | } |
50 | // do drawing to buffer |
51 | blit( buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H ); |
52 | } |
i is going to be something wrong here, resulting in going out-of-bounds on the tooltip array:
tips[ i ].text,
tips[ i ].left,
tips[ i ].bottom,
For that matter, why is i a global and not just a local thing inside the for loop?
Im not the best programmer in the world so bare ith me.
'i' : undeclared identifier in the draw_function() if I only declare it in the logic_function()
As far as I can see, i is never changed from it's initial value of 0, at least in that code snippet.
I'm assuming logic_function is called now and then, which would screw with things eventually, I should think. Anyway, why are you even using i in that function when what you want to use is tooltip?
Nevermind, I completely overlooked that for loop.
But yeah, why is i global? And why is the variable to tell you what tooltip to draw global, instead of a class member?
dont know how to do it any other way, I suck at programming I'm dumb!. Does it matter?
Go back and learn the basics before trying to tackle this task. It's way above your head. Do something simpler first, like Hello World, and go from there. Preferably in a slightly more safe language like Python.
Sorry X-G I'm doing c++ for my degree not python. Doing this is helping me alot.
I'm being serious. You don't seem to grasp the basics of imperative programming. Without even that, dealing with the additional intricacies of a language like C++ is going to be outside of your league. You need to start at the bottom and work your way up, and Python is a good language for a beginner. I recommend you try that out.
Sorry X-G I'm doing c++ for my degree not python. Doing this is helping me alot.
How is this helping you?
I don't think it was mentioned explicitly but all I think you need to do is change draw_function to this
if ( tooltip >=0 ) { textout_ex( buffer, font, tips[ tooltip ].text, tips[ tooltip ].left, tips[ tooltip ].bottom, makecol(222, 100, 111), -1 );
Geez, kazzmir, that way he won't learn anything. And if he can't figure out that bit for himself, he really needs to start out with something simpler, as I said.
Im doing it for fun as well X-G, dont be so serious
Edit: Awesome kazzmir, thanks for the help. I can continue programming my game in C++ now.
Fire, for the ToolTip struct, you declare char *text, and then later create two objects: the first contains the text, "Start new game" and the other contains NULL.
If you change "Start new game" to NULL or even '\0', does it still crash?
Just briefly looking at it would make me think that *text hasn't been given a specific size, and therefore, would SEGFAULT when you try to cram "Start new game" into it.
Try changing it to NULL and see if it still crashes. If not, my theory is wrong, but nothing's really wasted but a few minutes of testing time